ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.44
Committed: Wed Apr 23 00:52:33 2003 UTC (20 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.43: +2 -1 lines
Log Message:
Added (void *) cast to realloc calls

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Holodeck display process.
6 */
7
8 #include "rholo.h"
9 #include "rhdisp.h"
10 #include "rhdriver.h"
11 #include "selcall.h"
12 #include <ctype.h>
13
14 #ifndef VIEWHISTLEN
15 #define VIEWHISTLEN 4 /* number of remembered views */
16 #endif
17
18 #ifndef FSIZDEF
19 #define FSIZDEF 0.125 /* default focus frame size */
20 #endif
21
22 #if defined(freebsd)
23 #define fbufcnt(f) ((f)->_r)
24 #elif defined(__GNUC__)
25 #define fbufcnt(f) ((f)->_IO_read_end - (f)->_IO_read_ptr)
26 #else
27 #define fbufcnt(f) ((f)->_cnt)
28 #endif
29
30 HOLO *hdlist[HDMAX+1]; /* global holodeck list */
31
32 char *hdgfn[HDMAX]; /* holodeck section geometry list */
33 char *hdpfn[HDMAX]; /* section portal list */
34
35 char cmdlist[DC_NCMDS][8] = DC_INIT;
36
37 int imm_mode = 0; /* bundles are being delivered immediately */
38
39 int do_outside = 0; /* render from outside sections */
40
41 double eyesepdist = 1; /* eye separation distance */
42
43 char *progname; /* global argv[0] */
44
45 FILE *sstdin, *sstdout; /* server's standard input and output */
46
47 #ifdef DEBUG
48 extern time_t time();
49 static time_t tmodesw;
50 static time_t timm, tadd;
51 static long nimmrays, naddrays;
52 #endif
53
54 #define RDY_SRV 01
55 #define RDY_DEV 02
56 #define RDY_SIN 04
57
58
59 main(argc, argv)
60 int argc;
61 char *argv[];
62 {
63 int rdy, inp, res = 0, pause = 0;
64
65 progname = argv[0];
66 if (argc < 3)
67 error(USER, "bad command line arguments");
68 /* open our device */
69 dev_open(argv[1]);
70 /* open server process i/o */
71 sstdout = fdopen(atoi(argv[2]), "w");
72 if (argc < 4 || (inp = atoi(argv[3])) < 0)
73 sstdin = NULL;
74 else
75 sstdin = fdopen(inp, "r");
76 /* set command error vector */
77 erract[COMMAND].pf = eputs;
78 #ifdef DEBUG
79 tmodesw = time(NULL);
80 #endif
81 /* enter main loop */
82 do {
83 rdy = disp_wait();
84 if (rdy & RDY_SRV) { /* process server result */
85 res = serv_result();
86 if (pause && res != DS_SHUTDOWN) {
87 serv_request(DR_ATTEN, 0, NULL);
88 while ((res = serv_result()) != DS_ACKNOW &&
89 res != DS_SHUTDOWN)
90 ;
91 }
92 }
93 if (rdy & RDY_DEV) { /* user input from driver */
94 inp = dev_input();
95 if (inp & DFL(DC_SETVIEW))
96 new_view(&odev.v);
97 else if (inp & DFL(DC_LASTVIEW))
98 new_view(NULL);
99 if (inp & DFL(DC_REDRAW))
100 imm_mode = beam_sync(1) > 0;
101 if (inp & DFL(DC_GETVIEW))
102 printview();
103 if (inp & DFL(DC_FOCUS))
104 set_focus(odev_args);
105 if (inp & DFL(DC_KILL)) {
106 serv_request(DR_KILL, 0, NULL);
107 pause = 0;
108 }
109 if (inp & DFL(DC_CLOBBER))
110 serv_request(DR_CLOBBER, 0, NULL);
111 if (inp & DFL(DC_RESTART)) {
112 serv_request(DR_RESTART, 0, NULL);
113 pause = 0;
114 }
115 if (inp & DFL(DC_RESUME)) {
116 serv_request(DR_NOOP, 0, NULL);
117 pause = 0;
118 }
119 if (inp & DFL(DC_PAUSE))
120 pause = 1;
121 if (inp & DFL(DC_QUIT))
122 serv_request(DR_SHUTDOWN, 0, NULL);
123 }
124 if (rdy & RDY_SIN && !imm_mode) /* user input from sstdin */
125 switch (usr_input()) {
126 case DC_PAUSE:
127 pause = 1;
128 break;
129 case DC_RESUME:
130 serv_request(DR_NOOP, 0, NULL);
131 /* fall through */
132 case DC_KILL:
133 case DC_RESTART:
134 pause = 0;
135 break;
136 }
137 } while (res != DS_SHUTDOWN);
138 #ifdef DEBUG
139 if (timm && nimmrays)
140 fprintf(stderr,
141 "%s: %.1f rays recalled/second (%ld rays total)\n",
142 progname, (double)nimmrays/timm, nimmrays);
143 if (tadd && naddrays)
144 fprintf(stderr,
145 "%s: %.1f rays calculated/second (%ld rays total)\n",
146 progname, (double)naddrays/tadd, naddrays);
147 #endif
148 /* all done */
149 quit(0);
150 }
151
152
153 int
154 disp_wait() /* wait for more input */
155 {
156 fd_set readset, errset;
157 int flgs;
158 int n;
159 register int i;
160 /* see if we can avoid select call */
161 if (hdlist[0] == NULL)
162 return(RDY_SRV); /* initialize first */
163 flgs = 0; /* flag what's ready already */
164 if (imm_mode || fbufcnt(stdin) > 0)
165 flgs |= RDY_SRV;
166 if (sstdin != NULL && fbufcnt(sstdin) > 0)
167 flgs |= RDY_SIN;
168 if (odev.inpready)
169 flgs |= RDY_DEV;
170 if (flgs) /* got something? */
171 return(flgs);
172 if (dev_flush()) /* else flush output & check keyboard+mouse */
173 return(RDY_DEV);
174 /* if nothing, we need to call select */
175 FD_ZERO(&readset); FD_ZERO(&errset);
176 FD_SET(0, &readset);
177 FD_SET(0, &errset);
178 FD_SET(odev.ifd, &readset);
179 FD_SET(odev.ifd, &errset);
180 n = odev.ifd+1;
181 if (sstdin != NULL) {
182 FD_SET(fileno(sstdin), &readset);
183 FD_SET(fileno(sstdin), &errset);
184 if (fileno(sstdin) >= n)
185 n = fileno(sstdin) + 1;
186 }
187 n = select(n, &readset, NULL, &errset, NULL);
188 if (n < 0) {
189 if (errno == EINTR)
190 return(0);
191 error(SYSTEM, "select call failure in disp_wait");
192 }
193 if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset))
194 flgs |= RDY_SRV;
195 if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset))
196 flgs |= RDY_DEV;
197 if (sstdin != NULL && (FD_ISSET(fileno(sstdin), &readset) ||
198 FD_ISSET(fileno(sstdin), &errset)))
199 flgs |= RDY_SIN;
200 return(flgs);
201 }
202
203
204 add_holo(hdg, gfn, pfn) /* register a new holodeck section */
205 HDGRID *hdg;
206 char *gfn, *pfn;
207 {
208 VIEW nv;
209 double d;
210 register int hd;
211
212 for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++)
213 ;
214 if (hd >= HDMAX)
215 error(INTERNAL, "too many holodeck sections in add_holo");
216 hdlist[hd] = (HOLO *)malloc(sizeof(HOLO));
217 if (hdlist[hd] == NULL)
218 error(SYSTEM, "out of memory in add_holo");
219 bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID));
220 hdcompgrid(hdlist[hd]);
221 hdgfn[hd] = savestr(gfn);
222 hdpfn[hd] = pfn && *pfn ? savestr(pfn) : (char *)NULL;
223 if (hd)
224 return;
225 /* set initial viewpoint */
226 copystruct(&nv, &odev.v);
227 VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5);
228 VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5);
229 VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5);
230 fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]);
231 VCOPY(nv.vup, hdlist[0]->xv[2]);
232 if (do_outside) {
233 normalize(nv.vdir);
234 d = VLEN(hdlist[0]->xv[1]);
235 d += VLEN(hdlist[0]->xv[2]);
236 VSUM(nv.vp, nv.vp, nv.vdir, -d);
237 }
238 new_view(&nv);
239 }
240
241
242 disp_bundle(p) /* display a ray bundle */
243 register PACKHEAD *p;
244 {
245 GCOORD gc[2];
246 FVECT ro, rd, wp;
247 double d;
248 register int i;
249 /* get beam coordinates */
250 if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL)
251 error(INTERNAL, "bad holodeck number in disp_bundle");
252 if (!hdbcoord(gc, hdlist[p->hd], p->bi))
253 error(INTERNAL, "bad beam index in disp_bundle");
254 /* display each ray */
255 for (i = p->nr; i--; ) {
256 hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r);
257 d = hddepth(hdlist[p->hd], packra(p)[i].d);
258 if (d < .99*FHUGE) {
259 VSUM(wp, ro, rd, d); /* might be behind viewpoint */
260 dev_value(packra(p)[i].v, rd, wp);
261 } else
262 dev_value(packra(p)[i].v, rd, NULL);
263 }
264 #ifdef DEBUG
265 if (imm_mode) nimmrays += p->nr;
266 else naddrays += p->nr;
267 #endif
268 }
269
270
271 new_view(v) /* change view parameters */
272 register VIEW *v;
273 {
274 static VIEW viewhist[VIEWHISTLEN];
275 static unsigned nhist;
276 VIEW *dv;
277 int i, res[2];
278 int2 *slist;
279 char *err;
280 /* restore previous view? */
281 if (v == NULL) {
282 if (nhist > 1) /* get one before last setting */
283 nhist--;
284 else /* else go to end of list */
285 while (nhist < VIEWHISTLEN && viewhist[nhist].type)
286 nhist++;
287 v = viewhist + ((nhist-1)%VIEWHISTLEN);
288 } else
289 again:
290 if ((err = setview(v)) != NULL) {
291 error(COMMAND, err);
292 return;
293 }
294 if (!dev_view(v)) /* notify display driver */
295 goto again;
296 if (v->type == VT_PAR) {
297 error(COMMAND, "cannot handle parallel views");
298 return;
299 }
300 beam_init(odev.firstuse); /* compute new beam set */
301 for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) {
302 if ((slist = beam_view(dv, res[0], res[1])) == NULL) {
303 if (!nhist) {
304 error(COMMAND, "invalid starting view");
305 return;
306 }
307 copystruct(v, viewhist + ((nhist-1)%VIEWHISTLEN));
308 goto again; /* poss. overloading dev_section()? */
309 }
310 DCHECK(*slist < 0, WARNING, "no visible sections in new_view");
311 for ( ; *slist >= 0; slist++)
312 dev_section(hdgfn[*slist], hdpfn[*slist]);
313 }
314 dev_section(NULL,NULL); /* end section list */
315 dev_flush(); /* update display */
316 /* update server */
317 imm_mode = beam_sync(odev.firstuse) > 0;
318 /* record new view */
319 if (v < viewhist || v >= viewhist+VIEWHISTLEN) {
320 copystruct(viewhist + (nhist%VIEWHISTLEN), v);
321 nhist++;
322 }
323 }
324
325
326 set_focus(args) /* set focus frame */
327 char *args;
328 {
329 double hcent, vcent, hsiz, vsiz;
330 VIEW *dv, vwfocus;
331 int i, res[2];
332
333 i = sscanf(args, "%lf %lf %lf %lf", &hcent, &vcent, &hsiz, &vsiz);
334 if (i < 2 || hcent < 0 || hcent > 1 || vcent < 0 || vcent > 1) {
335 beam_init(0); /* restore view */
336 for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++)
337 beam_view(dv, res[0], res[1]);
338 beam_sync(0); /* update server */
339 return;
340 }
341 if (i < 4 || hsiz <= hcent || hsiz > 1 || vsiz <= vcent || vsiz > 1)
342 hsiz = vsiz = FSIZDEF; /* gave center only */
343 else {
344 hsiz -= hcent; hcent += 0.5*hsiz; /* gave min and max */
345 vsiz -= vcent; vcent += 0.5*vsiz;
346 }
347 beam_init(0); /* add basic views */
348 for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++)
349 beam_view(dv, res[0]>>4, res[1]>>4);
350 copystruct(&vwfocus, &odev.v); /* add focus view */
351 switch (odev.v.type) {
352 case VT_PER:
353 vwfocus.horiz = 2.*180./PI*atan(
354 hsiz * tan(PI/180./2.*odev.v.horiz) );
355 vwfocus.vert = 2.*180./PI*atan(
356 vsiz * tan(PI/180./2.*odev.v.vert) );
357 break;
358 case VT_PAR:
359 case VT_ANG:
360 vwfocus.horiz = hsiz * odev.v.horiz;
361 vwfocus.vert = vsiz * odev.v.vert;
362 break;
363 case VT_HEM:
364 vwfocus.horiz = 2.*180./PI*asin(
365 hsiz * sin(PI/180./2.*odev.v.horiz) );
366 vwfocus.vert = 2.*180./PI*asin(
367 vsiz * sin(PI/180./2.*odev.v.vert) );
368 break;
369 case VT_CYL:
370 vwfocus.horiz = hsiz * odev.v.horiz;
371 vwfocus.vert = 2.*180./PI*atan(
372 vsiz * tan(PI/180./2.*odev.v.vert) );
373 break;
374 default:
375 error(INTERNAL, "bad view type in set_focus");
376 }
377 vwfocus.hoff = (odev.v.hoff + hcent - 0.5)/hsiz;
378 vwfocus.voff = (odev.v.voff + vcent - 0.5)/vsiz;
379 setview(&vwfocus);
380 beam_view(&vwfocus, (int)(3*odev.hres*hsiz)+100,
381 (int)(3*odev.vres*vsiz)+100);
382 beam_sync(0); /* update server */
383 }
384
385
386 int
387 usr_input() /* get user input and process it */
388 {
389 VIEW vparams;
390 char cmd[256];
391 register char *args;
392 register int i;
393
394 if (fgets(cmd, sizeof(cmd), sstdin) == NULL) {
395 fclose(sstdin);
396 sstdin = NULL;
397 return(-1);
398 }
399 if (*cmd == '\n')
400 return(DC_RESUME);
401 for (args = cmd; *args && !isspace(*args); args++)
402 ;
403 while (isspace(*args))
404 *args++ = '\0';
405 if (*args && args[i=strlen(args)-1] == '\n')
406 args[i] = '\0';
407 for (i = 0; i < DC_NCMDS; i++)
408 if (!strcmp(cmd, cmdlist[i]))
409 break;
410 if (i >= DC_NCMDS) {
411 dev_auxcom(cmd, args);
412 return(-1);
413 }
414 switch (i) {
415 case DC_SETVIEW: /* set the view */
416 copystruct(&vparams, &odev.v);
417 if (!sscanview(&vparams, args))
418 error(COMMAND, "missing view options");
419 else
420 new_view(&vparams);
421 break;
422 case DC_GETVIEW: /* print the current view */
423 printview();
424 break;
425 case DC_LASTVIEW: /* restore previous view */
426 new_view(NULL);
427 break;
428 case DC_FOCUS: /* set focus frame */
429 set_focus(args);
430 break;
431 case DC_PAUSE: /* pause the current calculation */
432 case DC_RESUME: /* resume the calculation */
433 /* handled in main() */
434 break;
435 case DC_REDRAW: /* redraw from server */
436 imm_mode = beam_sync(1) > 0;
437 dev_clear();
438 break;
439 case DC_KILL: /* kill rtrace process(es) */
440 serv_request(DR_KILL, 0, NULL);
441 break;
442 case DC_CLOBBER: /* clobber holodeck */
443 serv_request(DR_CLOBBER, 0, NULL);
444 break;
445 case DC_RESTART: /* restart rtrace */
446 serv_request(DR_RESTART, 0, NULL);
447 break;
448 case DC_QUIT: /* quit request */
449 serv_request(DR_SHUTDOWN, 0, NULL);
450 break;
451 default:
452 error(CONSISTENCY, "bad command id in usr_input");
453 }
454 return(i);
455 }
456
457
458 printview() /* print our current view to server stdout */
459 {
460 fputs(VIEWSTR, sstdout);
461 fprintview(&odev.v, sstdout);
462 fputc('\n', sstdout);
463 fflush(sstdout);
464 }
465
466
467 int
468 serv_result() /* get next server result and process it */
469 {
470 static char *buf = NULL;
471 static int bufsiz = 0;
472 MSGHEAD msg;
473 int n;
474 /* read message header */
475 if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1)
476 goto readerr;
477 if (msg.nbytes > 0) { /* get the message body */
478 if (msg.nbytes > bufsiz) {
479 if (buf == NULL)
480 buf = (char *)malloc(bufsiz=msg.nbytes);
481 else
482 buf = (char *)realloc((void *)buf,
483 bufsiz=msg.nbytes);
484 if (buf == NULL)
485 error(SYSTEM, "out of memory in serv_result");
486 }
487 if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes)
488 goto readerr;
489 }
490 switch (msg.type) { /* process results */
491 case DS_BUNDLE:
492 if (msg.nbytes < sizeof(PACKHEAD) ||
493 msg.nbytes != packsiz(((PACKHEAD *)buf)->nr))
494 error(INTERNAL, "bad display packet from server");
495 disp_bundle((PACKHEAD *)buf);
496 break;
497 case DS_ADDHOLO:
498 if (msg.nbytes < sizeof(HDGRID)+2)
499 error(INTERNAL, "bad holodeck record from server");
500 add_holo((HDGRID *)buf, buf+sizeof(HDGRID),
501 buf+sizeof(HDGRID)+strlen(buf+sizeof(HDGRID))+1);
502 break;
503 case DS_OUTSECT:
504 do_outside = 1;
505 goto noargs;
506 case DS_EYESEP:
507 if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY)
508 error(INTERNAL, "bad eye separation from server");
509 break;
510 case DS_STARTIMM:
511 case DS_ENDIMM:
512 if (!(imm_mode = msg.type==DS_STARTIMM))
513 dev_flush();
514 #ifdef DEBUG
515 {
516 time_t tnow = time(NULL);
517 if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw;
518 else timm += tnow - tmodesw;
519 tmodesw = tnow;
520 }
521 #endif
522 goto noargs;
523 case DS_ACKNOW:
524 case DS_SHUTDOWN:
525 goto noargs;
526 default:
527 error(INTERNAL, "unrecognized result from server process");
528 }
529 return(msg.type); /* return message type */
530 noargs:
531 if (msg.nbytes) {
532 sprintf(errmsg, "unexpected body with server message %d",
533 msg.type);
534 error(INTERNAL, errmsg);
535 }
536 return(msg.type);
537 readerr:
538 if (feof(stdin))
539 error(SYSTEM, "server process died");
540 error(SYSTEM, "error reading from server process");
541 }
542
543
544 serv_request(type, nbytes, p) /* send a request to the server process */
545 int type, nbytes;
546 char *p;
547 {
548 MSGHEAD msg;
549 int m;
550 /* consistency checks */
551 DCHECK(nbytes < 0 || nbytes > 0 & p == NULL,
552 CONSISTENCY, "bad buffer handed to serv_request");
553 /* get server's attention for big request */
554 if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) {
555 serv_request(DR_ATTEN, 0, NULL);
556 while ((m = serv_result()) != DS_ACKNOW)
557 if (m == DS_SHUTDOWN) /* the bugger quit on us */
558 quit(0);
559 }
560 msg.type = type; /* write and flush the message */
561 msg.nbytes = nbytes;
562 fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout);
563 if (nbytes > 0)
564 fwrite(p, 1, nbytes, stdout);
565 if (fflush(stdout) < 0)
566 error(SYSTEM, "write error in serv_request");
567 }
568
569
570 void
571 eputs(s) /* put error message to stderr */
572 register char *s;
573 {
574 static int midline = 0;
575
576 if (!*s)
577 return;
578 if (!midline++) { /* prepend line with program name */
579 fputs(progname, stderr);
580 fputs(": ", stderr);
581 }
582 fputs(s, stderr);
583 if (s[strlen(s)-1] == '\n') {
584 fflush(stderr);
585 midline = 0;
586 }
587 }
588
589
590 void
591 quit(code) /* clean up and exit */
592 int code;
593 {
594 if (code)
595 exit(code);
596 if (odev.v.type)
597 dev_close();
598 exit(0);
599 }