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