15 |
|
#include "view.h" |
16 |
|
#include "resolu.h" |
17 |
|
|
18 |
+ |
/* set the following to 0 to forgo forking */ |
19 |
+ |
#ifndef MAXFORK |
20 |
+ |
#define MAXFORK 3 /* allotment of duped processes */ |
21 |
+ |
#endif |
22 |
+ |
|
23 |
|
/* rpict command */ |
24 |
|
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
25 |
|
int rpargc = 9; |
35 |
|
int outfd; |
36 |
|
long scanorig; |
37 |
|
int syncfd = -1; /* lock file descriptor */ |
38 |
+ |
int nforked = 0; |
39 |
|
|
40 |
|
char *progname; |
41 |
|
int verbose = 0; |
123 |
|
} |
124 |
|
init(argc, argv); |
125 |
|
rpiece(); |
126 |
< |
rval = cleanup(); |
126 |
> |
rval = cleanup(0); |
127 |
|
exit(rval); |
128 |
|
} |
129 |
|
|
244 |
|
if (sscanf(buf, "%d %d", xp, yp) == 2) |
245 |
|
return(1); |
246 |
|
fprintf(stderr, "%s: input format error\n", progname); |
247 |
< |
exit(1); |
247 |
> |
exit(cleanup(1)); |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
int |
252 |
< |
cleanup() /* close rpict process and clean up */ |
252 |
> |
cleanup(rstat) /* close rpict process and clean up */ |
253 |
> |
int rstat; |
254 |
|
{ |
255 |
< |
register int rpstat; |
255 |
> |
int status; |
256 |
|
|
257 |
|
free((char *)pbuf); |
258 |
|
fclose(torp); |
259 |
|
fclose(fromrp); |
260 |
< |
rpstat = close_process(rpd); |
261 |
< |
if (rpstat == -1) |
262 |
< |
rpstat = 0; |
263 |
< |
return(rpstat); |
260 |
> |
while (wait(&status) != -1) |
261 |
> |
if (rstat == 0) |
262 |
> |
rstat = status>>8 & 0xff; |
263 |
> |
return(rstat); |
264 |
|
} |
265 |
|
|
266 |
|
|
268 |
|
{ |
269 |
|
VIEW pview; |
270 |
|
int xorg, yorg; |
271 |
< |
|
271 |
> |
/* compute view parameters */ |
272 |
> |
copystruct(&pview, &ourview); |
273 |
> |
switch (ourview.type) { |
274 |
> |
case VT_PER: |
275 |
> |
pview.horiz = 2.*180./PI*atan( |
276 |
> |
tan(PI/180./2.*ourview.horiz)/hmult ); |
277 |
> |
pview.vert = 2.*180./PI*atan( |
278 |
> |
tan(PI/180./2.*ourview.vert)/vmult ); |
279 |
> |
break; |
280 |
> |
case VT_PAR: |
281 |
> |
case VT_ANG: |
282 |
> |
pview.horiz = ourview.horiz / hmult; |
283 |
> |
pview.vert = ourview.vert / vmult; |
284 |
> |
break; |
285 |
> |
case VT_HEM: |
286 |
> |
pview.horiz = 2.*180./PI*asin( |
287 |
> |
sin(PI/180./2.*ourview.horiz)/hmult ); |
288 |
> |
pview.vert = 2.*180./PI*asin( |
289 |
> |
sin(PI/180./2.*ourview.vert)/vmult ); |
290 |
> |
break; |
291 |
> |
default: |
292 |
> |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
293 |
> |
progname, ourview.type); |
294 |
> |
exit(cleanup(1)); |
295 |
> |
} |
296 |
> |
/* render each piece */ |
297 |
|
while (nextpiece(&xorg, &yorg)) { |
298 |
< |
copystruct(&pview, &ourview); /* compute view for piece */ |
299 |
< |
switch (ourview.type) { |
268 |
< |
case VT_PER: |
269 |
< |
pview.horiz = 2.*180./PI*atan( |
270 |
< |
tan(PI/180./2.*ourview.horiz)/hmult ); |
271 |
< |
pview.vert = 2.*180./PI*atan( |
272 |
< |
tan(PI/180./2.*ourview.vert)/vmult ); |
273 |
< |
break; |
274 |
< |
case VT_PAR: |
275 |
< |
case VT_ANG: |
276 |
< |
pview.horiz = ourview.horiz / hmult; |
277 |
< |
pview.vert = ourview.vert / vmult; |
278 |
< |
break; |
279 |
< |
case VT_HEM: |
280 |
< |
pview.horiz = 2.*180./PI*asin( |
281 |
< |
sin(PI/180./2.*ourview.horiz)/hmult ); |
282 |
< |
pview.vert = 2.*180./PI*asin( |
283 |
< |
sin(PI/180./2.*ourview.vert)/vmult ); |
284 |
< |
break; |
285 |
< |
default: |
286 |
< |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
287 |
< |
progname, ourview.type); |
288 |
< |
exit(1); |
289 |
< |
} |
290 |
< |
pview.hoff += xorg - 0.5*(hmult-1); |
291 |
< |
pview.voff += yorg - 0.5*(vmult-1); |
298 |
> |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
299 |
> |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
300 |
|
fputs(VIEWSTR, torp); |
301 |
|
fprintview(&pview, torp); |
302 |
|
putc('\n', torp); |
303 |
< |
fflush(torp); /* assign piece to rpict */ |
303 |
> |
fflush(torp); /* assigns piece to rpict */ |
304 |
|
putpiece(xorg, yorg); /* place piece in output */ |
305 |
|
if (verbose) { /* notify caller */ |
306 |
|
printf("%d %d done\n", xorg, yorg); |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
+ |
int |
314 |
|
putpiece(xpos, ypos) /* get next piece from rpict */ |
315 |
|
int xpos, ypos; |
316 |
|
{ |
317 |
|
struct flock fls; |
318 |
+ |
int pid, status; |
319 |
|
int hr, vr; |
320 |
|
register int y; |
321 |
|
/* check bounds */ |
322 |
|
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
323 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
324 |
|
progname, xpos, ypos); |
325 |
< |
exit(1); |
325 |
> |
exit(cleanup(1)); |
326 |
|
} |
327 |
|
/* check header from rpict */ |
328 |
|
getheader(fromrp, NULL); |
329 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) { |
329 |
> |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) { |
330 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
331 |
|
progname, rpargv[0]); |
332 |
< |
exit(1); |
332 |
> |
exit(cleanup(1)); |
333 |
|
} |
334 |
|
/* load new piece into buffer */ |
335 |
|
for (y = 0; y < vr; y++) |
336 |
|
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
337 |
|
fprintf(stderr, "%s: read error from %s\n", |
338 |
|
progname, rpargv[0]); |
339 |
< |
exit(1); |
339 |
> |
exit(cleanup(1)); |
340 |
|
} |
341 |
+ |
#if MAXFORK |
342 |
+ |
/* fork so we don't slow rpict down */ |
343 |
+ |
if ((pid = fork()) > 0) { |
344 |
+ |
if (++nforked >= MAXFORK) { |
345 |
+ |
wait(&status); /* reap a child */ |
346 |
+ |
if (status) |
347 |
+ |
exit(cleanup(status>>8 & 0xff)); |
348 |
+ |
nforked--; |
349 |
+ |
} |
350 |
+ |
return(pid); |
351 |
+ |
} |
352 |
+ |
#else |
353 |
+ |
pid = -1; /* no forking */ |
354 |
+ |
#endif |
355 |
|
/* lock file section so NFS doesn't mess up */ |
356 |
|
fls.l_whence = 0; |
357 |
|
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
361 |
|
/* write new piece to file */ |
362 |
|
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
363 |
|
goto seekerr; |
364 |
< |
for (y = 0; y < vr; y++) { |
365 |
< |
if (writebuf(outfd, (char *)(pbuf+y*hr), hr*sizeof(COLR)) != |
366 |
< |
hr*sizeof(COLR)) { |
367 |
< |
fprintf(stderr, "%s: write error on file \"%s\"\n", |
368 |
< |
progname, outfile); |
369 |
< |
exit(1); |
364 |
> |
if (hmult == 1) { |
365 |
> |
if (writebuf(outfd, (char *)pbuf, |
366 |
> |
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
367 |
> |
goto writerr; |
368 |
> |
} else |
369 |
> |
for (y = 0; y < vr; y++) { |
370 |
> |
if (writebuf(outfd, (char *)(pbuf+y*hr), |
371 |
> |
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
372 |
> |
goto writerr; |
373 |
> |
if (y < vr-1 && lseek(outfd, |
374 |
> |
(long)(hmult-1)*hr*sizeof(COLR), |
375 |
> |
1) == -1) |
376 |
> |
goto seekerr; |
377 |
|
} |
378 |
< |
if (hmult != 1 && y < vr-1 && lseek(outfd, |
379 |
< |
(long)(hmult-1)*hr*sizeof(COLR), 1) == -1) |
380 |
< |
goto seekerr; |
378 |
> |
if (pid == -1) { /* fork failed */ |
379 |
> |
fls.l_type = F_UNLCK; /* release lock */ |
380 |
> |
fcntl(outfd, F_SETLKW, &fls); |
381 |
> |
return(0); |
382 |
|
} |
383 |
< |
/* unlock file section */ |
352 |
< |
fls.l_type = F_UNLCK; |
353 |
< |
fcntl(outfd, F_SETLKW, &fls); |
354 |
< |
return; |
383 |
> |
_exit(0); /* else exit child process (releasing lock) */ |
384 |
|
seekerr: |
385 |
|
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
386 |
< |
exit(1); |
386 |
> |
_exit(1); |
387 |
> |
writerr: |
388 |
> |
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
389 |
> |
_exit(1); |
390 |
|
} |