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 |
|
16 |
HOLO *hdlist[HDMAX+1]; /* global holodeck list */ |
17 |
|
18 |
int imm_mode = 0; /* bundles are being delivered immediately */ |
19 |
|
20 |
char *progname; /* global argv[0] */ |
21 |
|
22 |
#define RDY_SRV 01 |
23 |
#define RDY_DEV 02 |
24 |
|
25 |
|
26 |
main(argc, argv) |
27 |
int argc; |
28 |
char *argv[]; |
29 |
{ |
30 |
int rdy, inp, res = 0, pause = 0; |
31 |
|
32 |
progname = argv[0]; |
33 |
if (argc != 2) |
34 |
error(USER, "bad command line arguments"); |
35 |
/* open our device */ |
36 |
dev_open(argv[1]); |
37 |
/* enter main loop */ |
38 |
do { |
39 |
rdy = disp_wait(); |
40 |
if (rdy & RDY_DEV) { /* get user input */ |
41 |
inp = dev_input(); |
42 |
if (inp & DEV_NEWVIEW) |
43 |
new_view(&odev.v); |
44 |
if (inp & DEV_SHUTDOWN) |
45 |
serv_request(DR_SHUTDOWN, 0, NULL); |
46 |
if (inp & DEV_REDRAW) { |
47 |
imm_mode = 1; /* preempt updates */ |
48 |
beam_sync(); |
49 |
} |
50 |
if (inp & DEV_WAIT) |
51 |
pause = 1; |
52 |
if (inp & DEV_RESUME) { |
53 |
serv_request(DR_NOOP, 0, NULL); |
54 |
pause = 0; |
55 |
} |
56 |
} |
57 |
if (rdy & RDY_SRV) { /* get server result */ |
58 |
res = serv_result(); |
59 |
if (pause && res != DS_SHUTDOWN) { |
60 |
serv_request(DR_ATTEN, 0, NULL); |
61 |
while ((res = serv_result()) != DS_ACKNOW && |
62 |
res != DS_SHUTDOWN) |
63 |
; |
64 |
} |
65 |
} |
66 |
} while (res != DS_SHUTDOWN); |
67 |
/* all done */ |
68 |
quit(0); |
69 |
} |
70 |
|
71 |
|
72 |
int |
73 |
disp_wait() /* wait for more input */ |
74 |
{ |
75 |
fd_set readset, errset; |
76 |
int flgs; |
77 |
int n; |
78 |
register int i; |
79 |
/* see if we can avoid select call */ |
80 |
if (imm_mode || stdin->_cnt > 0) |
81 |
return(RDY_SRV); |
82 |
if (dev_flush()) |
83 |
return(RDY_DEV); |
84 |
/* make the call */ |
85 |
FD_ZERO(&readset); FD_ZERO(&errset); |
86 |
FD_SET(0, &readset); |
87 |
FD_SET(0, &errset); |
88 |
FD_SET(odev.ifd, &readset); |
89 |
FD_SET(odev.ifd, &errset); |
90 |
n = odev.ifd + 1; |
91 |
n = select(n, &readset, NULL, &errset, NULL); |
92 |
if (n < 0) { |
93 |
if (errno == EINTR) |
94 |
return(0); |
95 |
error(SYSTEM, "select call failure in disp_wait"); |
96 |
} |
97 |
flgs = 0; /* flag what's ready */ |
98 |
if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset)) |
99 |
flgs |= RDY_SRV; |
100 |
if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset)) |
101 |
flgs |= RDY_DEV; |
102 |
return(flgs); |
103 |
} |
104 |
|
105 |
|
106 |
add_holo(hdg) /* register a new holodeck section */ |
107 |
HDGRID *hdg; |
108 |
{ |
109 |
VIEW nv; |
110 |
register int hd; |
111 |
|
112 |
for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++) |
113 |
; |
114 |
if (hd >= HDMAX) |
115 |
error(INTERNAL, "too many holodeck sections in add_holo"); |
116 |
hdlist[hd] = (HOLO *)malloc(sizeof(HOLO)); |
117 |
if (hdlist[hd] == NULL) |
118 |
error(SYSTEM, "out of memory in add_holo"); |
119 |
bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID)); |
120 |
hdcompgrid(hdlist[hd]); |
121 |
if (hd) |
122 |
return; |
123 |
/* set initial viewpoint */ |
124 |
copystruct(&nv, &odev.v); |
125 |
VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5); |
126 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5); |
127 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5); |
128 |
fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]); |
129 |
VCOPY(nv.vup, hdlist[0]->xv[2]); |
130 |
new_view(&nv); |
131 |
} |
132 |
|
133 |
|
134 |
disp_bundle(p) /* display a ray bundle */ |
135 |
register PACKHEAD *p; |
136 |
{ |
137 |
GCOORD gc[2]; |
138 |
FVECT ro, rd, wp; |
139 |
double d; |
140 |
register int i; |
141 |
/* get beam coordinates */ |
142 |
if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL) |
143 |
error(INTERNAL, "bad holodeck number in disp_bundle"); |
144 |
if (!hdbcoord(gc, hdlist[p->hd], p->bi)) |
145 |
error(INTERNAL, "bad beam index in disp_bundle"); |
146 |
/* display each ray */ |
147 |
for (i = p->nr; i--; ) { |
148 |
hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r); |
149 |
d = hddepth(hdlist[p->hd], packra(p)[i].d); |
150 |
VSUM(wp, ro, rd, d); /* might be behind viewpoint */ |
151 |
dev_value(packra(p)[i].v, wp); |
152 |
} |
153 |
} |
154 |
|
155 |
|
156 |
new_view(v) /* change view parameters */ |
157 |
VIEW *v; |
158 |
{ |
159 |
char *err; |
160 |
|
161 |
if ((err = setview(v)) != NULL) |
162 |
error(INTERNAL, err); |
163 |
dev_view(v); /* update display driver */ |
164 |
dev_flush(); /* update screen */ |
165 |
beam_view(v); /* update beam list */ |
166 |
} |
167 |
|
168 |
|
169 |
int |
170 |
serv_result() /* get next server result and process it */ |
171 |
{ |
172 |
static char *buf = NULL; |
173 |
static int bufsiz = 0; |
174 |
MSGHEAD msg; |
175 |
int n; |
176 |
/* read message header */ |
177 |
if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1) |
178 |
goto readerr; |
179 |
if (msg.nbytes > 0) { /* get the message body */ |
180 |
if (msg.nbytes > bufsiz) { |
181 |
if (buf == NULL) |
182 |
buf = (char *)malloc(bufsiz=msg.nbytes); |
183 |
else |
184 |
buf = (char *)realloc(buf, bufsiz=msg.nbytes); |
185 |
if (buf == NULL) |
186 |
error(SYSTEM, "out of memory in serv_result"); |
187 |
} |
188 |
if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes) |
189 |
goto readerr; |
190 |
} |
191 |
switch (msg.type) { /* process results */ |
192 |
case DS_BUNDLE: |
193 |
if (msg.nbytes < sizeof(PACKHEAD) || |
194 |
msg.nbytes != packsiz(((PACKHEAD *)buf)->nr)) |
195 |
error(INTERNAL, "bad display packet from server"); |
196 |
disp_bundle((PACKHEAD *)buf); |
197 |
break; |
198 |
case DS_ADDHOLO: |
199 |
if (msg.nbytes != sizeof(HDGRID)) |
200 |
error(INTERNAL, "bad holodeck record from server"); |
201 |
add_holo((HDGRID *)buf); |
202 |
break; |
203 |
case DS_STARTIMM: |
204 |
case DS_ENDIMM: |
205 |
imm_mode = msg.type==DS_STARTIMM; |
206 |
if (msg.type == DS_ENDIMM) |
207 |
dev_flush(); /* update display NOW */ |
208 |
/* fall through */ |
209 |
case DS_ACKNOW: |
210 |
case DS_SHUTDOWN: |
211 |
if (msg.nbytes) { |
212 |
sprintf(errmsg, |
213 |
"unexpected body with server message %d", |
214 |
msg.type); |
215 |
error(INTERNAL, errmsg); |
216 |
} |
217 |
break; |
218 |
default: |
219 |
error(INTERNAL, "unrecognized result from server process"); |
220 |
} |
221 |
return(msg.type); /* return message type */ |
222 |
readerr: |
223 |
if (feof(stdin)) |
224 |
error(SYSTEM, "server process died"); |
225 |
error(SYSTEM, "error reading from server process"); |
226 |
} |
227 |
|
228 |
|
229 |
serv_request(type, nbytes, p) /* send a request to the server process */ |
230 |
int type, nbytes; |
231 |
char *p; |
232 |
{ |
233 |
MSGHEAD msg; |
234 |
int m; |
235 |
/* get server's attention for big request */ |
236 |
if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) { |
237 |
serv_request(DR_ATTEN, 0, NULL); |
238 |
while ((m = serv_result()) != DS_ACKNOW) |
239 |
if (m == DS_SHUTDOWN) /* the bugger quit on us */ |
240 |
quit(0); |
241 |
} |
242 |
msg.type = type; /* write and flush the message */ |
243 |
msg.nbytes = nbytes; |
244 |
fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout); |
245 |
if (nbytes > 0) |
246 |
fwrite(p, 1, nbytes, stdout); |
247 |
if (fflush(stdout) < 0) |
248 |
error(SYSTEM, "write error in serv_request"); |
249 |
} |
250 |
|
251 |
|
252 |
eputs(s) /* put error message to stderr */ |
253 |
register char *s; |
254 |
{ |
255 |
static int midline = 0; |
256 |
|
257 |
if (!*s) |
258 |
return; |
259 |
if (!midline++) { /* prepend line with program name */ |
260 |
fputs(progname, stderr); |
261 |
fputs(": ", stderr); |
262 |
} |
263 |
fputs(s, stderr); |
264 |
if (s[strlen(s)-1] == '\n') { |
265 |
fflush(stderr); |
266 |
midline = 0; |
267 |
} |
268 |
} |
269 |
|
270 |
|
271 |
quit(code) /* clean up and exit */ |
272 |
int code; |
273 |
{ |
274 |
if (odev.v.type) |
275 |
dev_close(); |
276 |
exit(code); |
277 |
} |