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