ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp.c
Revision: 3.19
Committed: Sun Jan 4 12:28:09 1998 UTC (26 years, 2 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.18: +9 -9 lines
Log Message:
changed order of read after select to avoid stalls waiting for server

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Holodeck display process.
9     */
10    
11     #include "rholo.h"
12     #include "rhdisp.h"
13     #include "rhdriver.h"
14     #include "selcall.h"
15 gregl 3.10 #include <ctype.h>
16 gregl 3.1
17 gregl 3.12 #ifndef VIEWHISTLEN
18 gregl 3.16 #define VIEWHISTLEN 4 /* number of remembered views */
19 gregl 3.12 #endif
20    
21 gregl 3.1 HOLO *hdlist[HDMAX+1]; /* global holodeck list */
22    
23 gregl 3.10 char cmdlist[DC_NCMDS][8] = DC_INIT;
24    
25 gregl 3.1 int imm_mode = 0; /* bundles are being delivered immediately */
26    
27     char *progname; /* global argv[0] */
28    
29 gregl 3.10 FILE *sstdin, *sstdout; /* server's standard input and output */
30 gregl 3.1
31 gregl 3.10 #define RDY_SRV 01
32     #define RDY_DEV 02
33     #define RDY_SIN 04
34 gregl 3.1
35 gregl 3.10
36 gregl 3.1 main(argc, argv)
37     int argc;
38     char *argv[];
39     {
40 gregl 3.10 extern int eputs();
41 gregl 3.3 int rdy, inp, res = 0, pause = 0;
42 gregl 3.1
43     progname = argv[0];
44 gregl 3.10 if (argc < 3)
45 gregl 3.1 error(USER, "bad command line arguments");
46     /* open our device */
47     dev_open(argv[1]);
48 gregl 3.10 /* open server process i/o */
49     sstdout = fdopen(atoi(argv[2]), "w");
50     if (argc < 4 || (inp = atoi(argv[3])) < 0)
51     sstdin = NULL;
52     else
53     sstdin = fdopen(inp, "r");
54     /* set command error vector */
55     erract[COMMAND].pf = eputs;
56 gregl 3.1 /* enter main loop */
57     do {
58     rdy = disp_wait();
59 gregl 3.19 if (rdy & RDY_SRV) { /* process server result */
60     res = serv_result();
61     if (pause && res != DS_SHUTDOWN) {
62     serv_request(DR_ATTEN, 0, NULL);
63     while ((res = serv_result()) != DS_ACKNOW &&
64     res != DS_SHUTDOWN)
65     ;
66     }
67     }
68 gregl 3.10 if (rdy & RDY_DEV) { /* user input from driver */
69 gregl 3.3 inp = dev_input();
70 gregl 3.13 if (inp & DFL(DC_SETVIEW))
71     new_view(&odev.v);
72     if (inp & DFL(DC_GETVIEW))
73 gregl 3.10 printview();
74 gregl 3.13 if (inp & DFL(DC_LASTVIEW))
75 gregl 3.12 new_view(NULL);
76 gregl 3.13 if (inp & DFL(DC_RESUME)) {
77 gregl 3.3 serv_request(DR_NOOP, 0, NULL);
78     pause = 0;
79     }
80 gregl 3.15 if (inp & DFL(DC_PAUSE))
81     pause = 1;
82 gregl 3.13 if (inp & DFL(DC_REDRAW))
83     imm_mode = beam_sync() > 0;
84 gregl 3.15 if (inp & DFL(DC_KILL)) {
85 gregl 3.14 serv_request(DR_KILL, 0, NULL);
86 gregl 3.15 pause = 0;
87     }
88 gregl 3.14 if (inp & DFL(DC_CLOBBER))
89     serv_request(DR_CLOBBER, 0, NULL);
90 gregl 3.15 if (inp & DFL(DC_RESTART)) {
91 gregl 3.14 serv_request(DR_RESTART, 0, NULL);
92 gregl 3.15 pause = 0;
93     }
94 gregl 3.13 if (inp & DFL(DC_QUIT))
95     serv_request(DR_SHUTDOWN, 0, NULL);
96 gregl 3.3 }
97 gregl 3.10 if (rdy & RDY_SIN) /* user input from sstdin */
98     switch (usr_input()) {
99     case DC_PAUSE:
100     pause = 1;
101     break;
102     case DC_RESUME:
103     serv_request(DR_NOOP, 0, NULL);
104 gregl 3.15 /* fall through */
105     case DC_KILL:
106     case DC_RESTART:
107 gregl 3.10 pause = 0;
108     break;
109     }
110 gregl 3.1 } while (res != DS_SHUTDOWN);
111     /* all done */
112     quit(0);
113     }
114    
115    
116     int
117     disp_wait() /* wait for more input */
118     {
119     fd_set readset, errset;
120     int flgs;
121     int n;
122     register int i;
123     /* see if we can avoid select call */
124     if (imm_mode || stdin->_cnt > 0)
125 gregl 3.4 return(RDY_SRV);
126 gregl 3.10 if (sstdin != NULL && sstdin->_cnt > 0)
127     return(RDY_SIN);
128 gregl 3.1 if (dev_flush())
129 gregl 3.4 return(RDY_DEV);
130 gregl 3.1 /* make the call */
131     FD_ZERO(&readset); FD_ZERO(&errset);
132     FD_SET(0, &readset);
133     FD_SET(0, &errset);
134     FD_SET(odev.ifd, &readset);
135     FD_SET(odev.ifd, &errset);
136 gregl 3.10 n = odev.ifd+1;
137     if (sstdin != NULL) {
138     FD_SET(fileno(sstdin), &readset);
139     if (fileno(sstdin) >= n)
140     n = fileno(sstdin) + 1;
141     }
142 gregl 3.1 n = select(n, &readset, NULL, &errset, NULL);
143     if (n < 0) {
144     if (errno == EINTR)
145     return(0);
146     error(SYSTEM, "select call failure in disp_wait");
147     }
148     flgs = 0; /* flag what's ready */
149     if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset))
150 gregl 3.4 flgs |= RDY_SRV;
151 gregl 3.1 if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset))
152 gregl 3.4 flgs |= RDY_DEV;
153 gregl 3.10 if (sstdin != NULL && FD_ISSET(fileno(sstdin), &readset))
154     flgs |= RDY_SIN;
155 gregl 3.1 return(flgs);
156     }
157    
158    
159     add_holo(hdg) /* register a new holodeck section */
160     HDGRID *hdg;
161     {
162     VIEW nv;
163     register int hd;
164    
165     for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++)
166     ;
167     if (hd >= HDMAX)
168     error(INTERNAL, "too many holodeck sections in add_holo");
169     hdlist[hd] = (HOLO *)malloc(sizeof(HOLO));
170     if (hdlist[hd] == NULL)
171     error(SYSTEM, "out of memory in add_holo");
172     bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID));
173     hdcompgrid(hdlist[hd]);
174     if (hd)
175     return;
176     /* set initial viewpoint */
177     copystruct(&nv, &odev.v);
178     VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5);
179     VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5);
180     VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5);
181     fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]);
182     VCOPY(nv.vup, hdlist[0]->xv[2]);
183     new_view(&nv);
184     }
185    
186    
187     disp_bundle(p) /* display a ray bundle */
188     register PACKHEAD *p;
189     {
190     GCOORD gc[2];
191     FVECT ro, rd, wp;
192     double d;
193     register int i;
194     /* get beam coordinates */
195     if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL)
196     error(INTERNAL, "bad holodeck number in disp_bundle");
197     if (!hdbcoord(gc, hdlist[p->hd], p->bi))
198     error(INTERNAL, "bad beam index in disp_bundle");
199     /* display each ray */
200     for (i = p->nr; i--; ) {
201     hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r);
202     d = hddepth(hdlist[p->hd], packra(p)[i].d);
203     VSUM(wp, ro, rd, d); /* might be behind viewpoint */
204 gregl 3.9 dev_value(packra(p)[i].v, wp, rd);
205 gregl 3.1 }
206     }
207    
208    
209     new_view(v) /* change view parameters */
210 gregl 3.12 register VIEW *v;
211 gregl 3.1 {
212 gregl 3.12 static VIEW viewhist[VIEWHISTLEN];
213     static unsigned nhist;
214 gregl 3.1 char *err;
215 gregl 3.12 /* restore previous view? */
216     if (v == NULL) {
217 gregl 3.16 if (nhist > 1) /* get one before last setting */
218     nhist--;
219     else /* else go to end of list */
220     while (nhist < VIEWHISTLEN && viewhist[nhist].type)
221     nhist++;
222 gregl 3.12 v = viewhist + ((nhist-1)%VIEWHISTLEN);
223 gregl 3.17 } else
224     again:
225     if ((err = setview(v)) != NULL) {
226 gregl 3.12 error(COMMAND, err);
227     return;
228     }
229     if (v->type == VT_PAR) {
230     error(COMMAND, "cannot handle parallel views");
231     return;
232     }
233     if (!dev_view(v)) /* update display driver */
234     goto again;
235     dev_flush(); /* update screen */
236     if (!beam_view(v)) /* update beam list */
237     goto again;
238     /* record new view */
239     if (v < viewhist || v >= viewhist+VIEWHISTLEN) {
240     copystruct(viewhist + (nhist%VIEWHISTLEN), v);
241     nhist++;
242     }
243 gregl 3.10 }
244    
245    
246     int
247     usr_input() /* get user input and process it */
248     {
249     VIEW vparams;
250     char cmd[128];
251     register char *args;
252 gregl 3.11 register int i;
253 gregl 3.10
254     if (fgets(cmd, sizeof(cmd), sstdin) == NULL)
255     return(DC_QUIT);
256     for (args = cmd; *args && !isspace(*args); args++)
257     ;
258     while (isspace(*args))
259     *args++ = '\0';
260     if (!*cmd)
261     return(DC_RESUME);
262 gregl 3.11 if (*args && args[i=strlen(args)-1] == '\n')
263     args[i] = '\0';
264     for (i = 0; i < DC_NCMDS; i++)
265     if (!strcmp(cmd, cmdlist[i]))
266 gregl 3.10 break;
267 gregl 3.11 if (i >= DC_NCMDS) {
268 gregl 3.10 sprintf(errmsg, "unknown command: %s", cmd);
269     error(COMMAND, errmsg);
270     return(-1);
271     }
272 gregl 3.11 switch (i) {
273 gregl 3.10 case DC_SETVIEW: /* set the view */
274     copystruct(&vparams, &odev.v);
275     if (!sscanview(&vparams, args))
276     error(COMMAND, "missing view options");
277     else
278     new_view(&vparams);
279     break;
280     case DC_GETVIEW: /* print the current view */
281     printview();
282 gregl 3.12 break;
283     case DC_LASTVIEW: /* restore previous view */
284     new_view(NULL);
285 gregl 3.10 break;
286     case DC_PAUSE: /* pause the current calculation */
287     case DC_RESUME: /* resume the calculation */
288     /* handled in main() */
289 gregl 3.13 break;
290     case DC_REDRAW: /* redraw from server */
291     imm_mode = beam_sync() > 0;
292 gregl 3.14 break;
293     case DC_KILL: /* kill rtrace process(es) */
294     serv_request(DR_KILL, 0, NULL);
295     break;
296     case DC_CLOBBER: /* clobber holodeck */
297     serv_request(DR_CLOBBER, 0, NULL);
298     break;
299     case DC_RESTART: /* restart rtrace */
300     serv_request(DR_RESTART, 0, NULL);
301 gregl 3.10 break;
302     case DC_QUIT: /* quit request */
303     serv_request(DR_SHUTDOWN, 0, NULL);
304     break;
305     default:
306     error(CONSISTENCY, "bad command id in usr_input");
307     }
308 gregl 3.11 return(i);
309 gregl 3.10 }
310    
311    
312     printview() /* print our current view to server stdout */
313     {
314     fputs(VIEWSTR, sstdout);
315     fprintview(&odev.v, sstdout);
316     fputc('\n', sstdout);
317     fflush(sstdout);
318 gregl 3.1 }
319    
320    
321     int
322     serv_result() /* get next server result and process it */
323     {
324     static char *buf = NULL;
325     static int bufsiz = 0;
326     MSGHEAD msg;
327     int n;
328     /* read message header */
329     if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1)
330     goto readerr;
331     if (msg.nbytes > 0) { /* get the message body */
332     if (msg.nbytes > bufsiz) {
333     if (buf == NULL)
334     buf = (char *)malloc(bufsiz=msg.nbytes);
335     else
336     buf = (char *)realloc(buf, bufsiz=msg.nbytes);
337     if (buf == NULL)
338     error(SYSTEM, "out of memory in serv_result");
339     }
340     if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes)
341     goto readerr;
342     }
343     switch (msg.type) { /* process results */
344     case DS_BUNDLE:
345     if (msg.nbytes < sizeof(PACKHEAD) ||
346     msg.nbytes != packsiz(((PACKHEAD *)buf)->nr))
347     error(INTERNAL, "bad display packet from server");
348     disp_bundle((PACKHEAD *)buf);
349     break;
350     case DS_ADDHOLO:
351     if (msg.nbytes != sizeof(HDGRID))
352     error(INTERNAL, "bad holodeck record from server");
353     add_holo((HDGRID *)buf);
354     break;
355     case DS_STARTIMM:
356     case DS_ENDIMM:
357     imm_mode = msg.type==DS_STARTIMM;
358     /* fall through */
359     case DS_ACKNOW:
360     case DS_SHUTDOWN:
361     if (msg.nbytes) {
362     sprintf(errmsg,
363     "unexpected body with server message %d",
364     msg.type);
365     error(INTERNAL, errmsg);
366     }
367     break;
368     default:
369     error(INTERNAL, "unrecognized result from server process");
370     }
371     return(msg.type); /* return message type */
372     readerr:
373     if (feof(stdin))
374     error(SYSTEM, "server process died");
375     error(SYSTEM, "error reading from server process");
376     }
377    
378    
379     serv_request(type, nbytes, p) /* send a request to the server process */
380     int type, nbytes;
381     char *p;
382     {
383     MSGHEAD msg;
384     int m;
385     /* get server's attention for big request */
386     if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) {
387     serv_request(DR_ATTEN, 0, NULL);
388     while ((m = serv_result()) != DS_ACKNOW)
389     if (m == DS_SHUTDOWN) /* the bugger quit on us */
390     quit(0);
391     }
392     msg.type = type; /* write and flush the message */
393     msg.nbytes = nbytes;
394     fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout);
395     if (nbytes > 0)
396     fwrite(p, 1, nbytes, stdout);
397     if (fflush(stdout) < 0)
398     error(SYSTEM, "write error in serv_request");
399 gregl 3.2 }
400    
401    
402     eputs(s) /* put error message to stderr */
403     register char *s;
404     {
405     static int midline = 0;
406    
407     if (!*s)
408     return;
409     if (!midline++) { /* prepend line with program name */
410     fputs(progname, stderr);
411     fputs(": ", stderr);
412     }
413     fputs(s, stderr);
414     if (s[strlen(s)-1] == '\n') {
415     fflush(stderr);
416     midline = 0;
417     }
418 gregl 3.1 }
419    
420    
421     quit(code) /* clean up and exit */
422     int code;
423     {
424     if (odev.v.type)
425     dev_close();
426     exit(code);
427     }