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