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 |
VIEW dvw; /* our current display view */ |
21 |
|
22 |
char *progname; /* global argv[0] */ |
23 |
|
24 |
#define RDY_SRV 01 |
25 |
#define RDY_DEV 02 |
26 |
|
27 |
|
28 |
main(argc, argv) |
29 |
int argc; |
30 |
char *argv[]; |
31 |
{ |
32 |
int rdy, inp, res = 0, pause = 0; |
33 |
|
34 |
progname = argv[0]; |
35 |
if (argc != 2) |
36 |
error(USER, "bad command line arguments"); |
37 |
/* open our device */ |
38 |
dev_open(argv[1]); |
39 |
/* enter main loop */ |
40 |
do { |
41 |
rdy = disp_wait(); |
42 |
if (rdy & RDY_DEV) { /* get user input */ |
43 |
inp = dev_input(); |
44 |
if (inp & DEV_NEWVIEW) |
45 |
new_view(&odev.v); |
46 |
if (inp & DEV_SHUTDOWN) |
47 |
serv_request(DR_SHUTDOWN, 0, NULL); |
48 |
if (inp & DEV_REDRAW) { |
49 |
beam_sync(); |
50 |
imm_mode = 1; /* preempt updates */ |
51 |
} |
52 |
if (inp & DEV_WAIT) |
53 |
pause = 1; |
54 |
if (inp & DEV_RESUME) { |
55 |
serv_request(DR_NOOP, 0, NULL); |
56 |
pause = 0; |
57 |
} |
58 |
} |
59 |
if (rdy & RDY_SRV) { /* get 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 |
} while (res != DS_SHUTDOWN); |
69 |
/* all done */ |
70 |
quit(0); |
71 |
} |
72 |
|
73 |
|
74 |
int |
75 |
disp_wait() /* wait for more input */ |
76 |
{ |
77 |
fd_set readset, errset; |
78 |
int flgs; |
79 |
int n; |
80 |
register int i; |
81 |
/* see if we can avoid select call */ |
82 |
if (imm_mode || stdin->_cnt > 0) |
83 |
return(RDY_SRV); |
84 |
if (dev_flush()) |
85 |
return(RDY_DEV); |
86 |
/* make the call */ |
87 |
FD_ZERO(&readset); FD_ZERO(&errset); |
88 |
FD_SET(0, &readset); |
89 |
FD_SET(0, &errset); |
90 |
FD_SET(odev.ifd, &readset); |
91 |
FD_SET(odev.ifd, &errset); |
92 |
n = odev.ifd + 1; |
93 |
n = select(n, &readset, NULL, &errset, NULL); |
94 |
if (n < 0) { |
95 |
if (errno == EINTR) |
96 |
return(0); |
97 |
error(SYSTEM, "select call failure in disp_wait"); |
98 |
} |
99 |
flgs = 0; /* flag what's ready */ |
100 |
if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset)) |
101 |
flgs |= RDY_SRV; |
102 |
if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset)) |
103 |
flgs |= RDY_DEV; |
104 |
return(flgs); |
105 |
} |
106 |
|
107 |
|
108 |
add_holo(hdg) /* register a new holodeck section */ |
109 |
HDGRID *hdg; |
110 |
{ |
111 |
VIEW nv; |
112 |
register int hd; |
113 |
|
114 |
for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++) |
115 |
; |
116 |
if (hd >= HDMAX) |
117 |
error(INTERNAL, "too many holodeck sections in add_holo"); |
118 |
hdlist[hd] = (HOLO *)malloc(sizeof(HOLO)); |
119 |
if (hdlist[hd] == NULL) |
120 |
error(SYSTEM, "out of memory in add_holo"); |
121 |
bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID)); |
122 |
hdcompgrid(hdlist[hd]); |
123 |
if (hd) |
124 |
return; |
125 |
/* set initial viewpoint */ |
126 |
copystruct(&nv, &odev.v); |
127 |
VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5); |
128 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5); |
129 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5); |
130 |
fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]); |
131 |
VCOPY(nv.vup, hdlist[0]->xv[2]); |
132 |
new_view(&nv); |
133 |
} |
134 |
|
135 |
|
136 |
disp_bundle(p) /* display a ray bundle */ |
137 |
register PACKHEAD *p; |
138 |
{ |
139 |
GCOORD gc[2]; |
140 |
FVECT ro, rd, wp; |
141 |
double d; |
142 |
register int i; |
143 |
/* get beam coordinates */ |
144 |
if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL) |
145 |
error(INTERNAL, "bad holodeck number in disp_bundle"); |
146 |
if (!hdbcoord(gc, hdlist[p->hd], p->bi)) |
147 |
error(INTERNAL, "bad beam index in disp_bundle"); |
148 |
/* display each ray */ |
149 |
for (i = p->nr; i--; ) { |
150 |
hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r); |
151 |
d = hddepth(hdlist[p->hd], packra(p)[i].d); |
152 |
VSUM(wp, ro, rd, d); /* might be behind viewpoint */ |
153 |
dev_value(packra(p)[i].v, wp); |
154 |
} |
155 |
} |
156 |
|
157 |
|
158 |
new_view(v) /* change view parameters */ |
159 |
VIEW *v; |
160 |
{ |
161 |
char *err; |
162 |
|
163 |
if ((err = setview(v)) != NULL) |
164 |
error(INTERNAL, err); |
165 |
dev_view(v); /* update display driver */ |
166 |
beam_view(&dvw, v); /* update beam list */ |
167 |
copystruct(&dvw, v); /* record new view */ |
168 |
} |
169 |
|
170 |
|
171 |
int |
172 |
serv_result() /* get next server result and process it */ |
173 |
{ |
174 |
static char *buf = NULL; |
175 |
static int bufsiz = 0; |
176 |
MSGHEAD msg; |
177 |
int n; |
178 |
/* read message header */ |
179 |
if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1) |
180 |
goto readerr; |
181 |
if (msg.nbytes > 0) { /* get the message body */ |
182 |
if (msg.nbytes > bufsiz) { |
183 |
if (buf == NULL) |
184 |
buf = (char *)malloc(bufsiz=msg.nbytes); |
185 |
else |
186 |
buf = (char *)realloc(buf, bufsiz=msg.nbytes); |
187 |
if (buf == NULL) |
188 |
error(SYSTEM, "out of memory in serv_result"); |
189 |
} |
190 |
if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes) |
191 |
goto readerr; |
192 |
} |
193 |
switch (msg.type) { /* process results */ |
194 |
case DS_BUNDLE: |
195 |
if (msg.nbytes < sizeof(PACKHEAD) || |
196 |
msg.nbytes != packsiz(((PACKHEAD *)buf)->nr)) |
197 |
error(INTERNAL, "bad display packet from server"); |
198 |
disp_bundle((PACKHEAD *)buf); |
199 |
break; |
200 |
case DS_ADDHOLO: |
201 |
if (msg.nbytes != sizeof(HDGRID)) |
202 |
error(INTERNAL, "bad holodeck record from server"); |
203 |
add_holo((HDGRID *)buf); |
204 |
break; |
205 |
case DS_STARTIMM: |
206 |
case DS_ENDIMM: |
207 |
imm_mode = msg.type==DS_STARTIMM; |
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 |
} |