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