1 |
gwlarson |
3.25 |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
2 |
gregl |
3.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Holodeck display process communication |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include "rholo.h" |
12 |
|
|
#include "rhdisp.h" |
13 |
gregl |
3.2 |
#include <sys/uio.h> |
14 |
gregl |
3.1 |
|
15 |
gregl |
3.2 |
#ifndef HDSUF |
16 |
gwlarson |
3.26 |
#define HDSUF ".hdi" |
17 |
gregl |
3.2 |
#endif |
18 |
gwlarson |
3.26 |
#ifndef SLAVENAME |
19 |
|
|
#define SLAVENAME "slave" |
20 |
|
|
#endif |
21 |
gregl |
3.1 |
|
22 |
gwlarson |
3.21 |
#ifndef FNONBLK |
23 |
|
|
#define FNONBLK O_NONBLOCK |
24 |
|
|
#endif |
25 |
|
|
|
26 |
gregl |
3.2 |
static int inp_flags; |
27 |
|
|
static int dpd[3]; |
28 |
gregl |
3.5 |
static FILE *dpout; |
29 |
gregl |
3.2 |
|
30 |
|
|
|
31 |
|
|
disp_open(dname) /* open the named display driver */ |
32 |
gregl |
3.1 |
char *dname; |
33 |
|
|
{ |
34 |
gwlarson |
3.25 |
char buf[sizeof(HDGRID)+512], fd0[8], fd1[8], *cmd[5], *sfn; |
35 |
gwlarson |
3.24 |
int i, n, len; |
36 |
gwlarson |
3.26 |
|
37 |
|
|
if (!strcmp(dname, SLAVENAME)) { |
38 |
|
|
dpd[0] = 0; /* read from stdin */ |
39 |
|
|
dpout = stdout; /* write to stdout */ |
40 |
|
|
dpd[2] = -1; /* we're the slave process */ |
41 |
|
|
} else { |
42 |
|
|
/* get full display program name */ |
43 |
gregl |
3.2 |
#ifdef DEVPATH |
44 |
gwlarson |
3.26 |
sprintf(buf, "%s/%s%s", DEVPATH, dname, HDSUF); |
45 |
gregl |
3.2 |
#else |
46 |
gwlarson |
3.26 |
sprintf(buf, "dev/%s%s", dname, HDSUF); |
47 |
gregl |
3.2 |
#endif |
48 |
gwlarson |
3.26 |
/* dup stdin and stdout */ |
49 |
|
|
if (readinp) |
50 |
|
|
sprintf(fd0, "%d", dup(0)); |
51 |
|
|
else |
52 |
|
|
strcpy(fd0, "-1"); |
53 |
|
|
sprintf(fd1, "%d", dup(1)); |
54 |
|
|
/* start the display process */ |
55 |
|
|
cmd[0] = buf; |
56 |
|
|
cmd[1] = froot; cmd[2] = fd1; cmd[3] = fd0; |
57 |
|
|
cmd[4] = NULL; |
58 |
|
|
i = open_process(dpd, cmd); |
59 |
|
|
if (i <= 0) |
60 |
|
|
error(USER, "cannot start display process"); |
61 |
|
|
if ((dpout = fdopen(dpd[1], "w")) == NULL) |
62 |
|
|
error(SYSTEM, "problem opening display pipe"); |
63 |
|
|
/* close dup'ed stdin and stdout */ |
64 |
|
|
if (readinp) |
65 |
|
|
close(atoi(fd0)); |
66 |
|
|
close(atoi(fd1)); |
67 |
|
|
} |
68 |
gregl |
3.11 |
dpd[1] = -1; /* causes ignored error in close_process() */ |
69 |
gregl |
3.2 |
inp_flags = 0; |
70 |
gregl |
3.17 |
/* check if outside */ |
71 |
|
|
if (vdef(OBSTRUCTIONS) && vbool(OBSTRUCTIONS)) |
72 |
|
|
disp_result(DS_OUTSECT, 0, NULL); |
73 |
gwlarson |
3.18 |
/* send eye separation if specified */ |
74 |
|
|
if (vdef(EYESEP)) { |
75 |
gwlarson |
3.23 |
sprintf(buf, "%.9e", vflt(EYESEP)); |
76 |
|
|
disp_result(DS_EYESEP, strlen(buf)+1, buf); |
77 |
gwlarson |
3.18 |
} |
78 |
gwlarson |
3.23 |
/* write out hologram grids & octrees */ |
79 |
|
|
for (i = 0; hdlist[i] != NULL; i++) { |
80 |
|
|
bcopy((char *)hdlist[i], buf, sizeof(HDGRID)); |
81 |
gwlarson |
3.24 |
len = sizeof(HDGRID); |
82 |
|
|
n = vdef(GEOMETRY); |
83 |
|
|
sfn = i<n ? nvalue(GEOMETRY,i) : |
84 |
|
|
n ? nvalue(GEOMETRY,n-1) : vval(OCTREE); |
85 |
|
|
strcpy(buf+len, sfn); |
86 |
|
|
len += strlen(sfn) + 1; |
87 |
|
|
n = vdef(PORTS); |
88 |
|
|
sfn = i<n ? nvalue(PORTS,i) : n ? nvalue(PORTS,n-1) : ""; |
89 |
|
|
strcpy(buf+len, sfn); |
90 |
|
|
len += strlen(sfn) + 1; |
91 |
|
|
disp_result(DS_ADDHOLO, len, buf); |
92 |
gwlarson |
3.23 |
} |
93 |
gregl |
3.5 |
disp_flush(); |
94 |
gregl |
3.1 |
} |
95 |
|
|
|
96 |
|
|
|
97 |
gregl |
3.2 |
disp_packet(p) /* display a packet */ |
98 |
gregl |
3.4 |
register PACKHEAD *p; |
99 |
gregl |
3.2 |
{ |
100 |
gregl |
3.4 |
disp_result(DS_BUNDLE, packsiz(p->nr), (char *)p); |
101 |
gregl |
3.2 |
} |
102 |
|
|
|
103 |
|
|
|
104 |
gregl |
3.1 |
disp_check(block) /* check display process */ |
105 |
|
|
int block; |
106 |
|
|
{ |
107 |
gregl |
3.2 |
MSGHEAD msg; |
108 |
|
|
int n; |
109 |
|
|
char *buf = NULL; |
110 |
|
|
|
111 |
gregl |
3.6 |
if (dpout == NULL) |
112 |
gregl |
3.2 |
return(-1); |
113 |
gregl |
3.5 |
/* flush display output */ |
114 |
|
|
disp_flush(); |
115 |
gregl |
3.3 |
/* check read blocking */ |
116 |
gregl |
3.2 |
if (block != (inp_flags == 0)) { |
117 |
|
|
inp_flags = block ? 0 : FNONBLK; |
118 |
|
|
if (fcntl(dpd[0], F_SETFL, inp_flags) < 0) |
119 |
|
|
goto fcntlerr; |
120 |
|
|
} |
121 |
|
|
/* read message header */ |
122 |
|
|
n = read(dpd[0], (char *)&msg, sizeof(MSGHEAD)); |
123 |
|
|
if (n != sizeof(MSGHEAD)) { |
124 |
gregl |
3.10 |
if (n >= 0) { |
125 |
|
|
dpout = NULL; |
126 |
gregl |
3.2 |
error(USER, "display process died"); |
127 |
gregl |
3.10 |
} |
128 |
gregl |
3.2 |
if (errno != EAGAIN & errno != EINTR) |
129 |
|
|
goto readerr; |
130 |
gregl |
3.3 |
return(2); /* acceptable failure */ |
131 |
gregl |
3.2 |
} |
132 |
|
|
if (msg.nbytes) { /* get the message body */ |
133 |
gwlarson |
3.20 |
if (msg.nbytes < 0) |
134 |
|
|
error(INTERNAL, "anti-message from display process"); |
135 |
gregl |
3.2 |
buf = (char *)malloc(msg.nbytes); |
136 |
|
|
if (buf == NULL) |
137 |
|
|
error(SYSTEM, "out of memory in disp_check"); |
138 |
gwlarson |
3.19 |
if (inp_flags != 0 && fcntl(dpd[0], F_SETFL, inp_flags=0) < 0) |
139 |
gregl |
3.2 |
goto fcntlerr; |
140 |
|
|
if (readbuf(dpd[0], buf, msg.nbytes) != msg.nbytes) |
141 |
|
|
goto readerr; |
142 |
|
|
} |
143 |
|
|
switch (msg.type) { /* take appropriate action */ |
144 |
gregl |
3.12 |
case DR_BUNDLE: /* new bundle to calculate */ |
145 |
gregl |
3.2 |
if (msg.nbytes != sizeof(PACKHEAD)) |
146 |
|
|
error(INTERNAL, "bad DR_BUNDLE from display process"); |
147 |
|
|
bundle_set(BS_ADD, (PACKHEAD *)buf, 1); |
148 |
|
|
break; |
149 |
gregl |
3.12 |
case DR_NEWSET: /* new calculation set */ |
150 |
gregl |
3.2 |
if (msg.nbytes % sizeof(PACKHEAD)) |
151 |
|
|
error(INTERNAL, "bad DR_NEWSET from display process"); |
152 |
gwlarson |
3.19 |
if (msg.nbytes) |
153 |
|
|
disp_result(DS_STARTIMM, 0, NULL); |
154 |
gregl |
3.2 |
bundle_set(BS_NEW, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
155 |
gwlarson |
3.19 |
if (msg.nbytes) { |
156 |
|
|
disp_result(DS_ENDIMM, 0, NULL); |
157 |
|
|
disp_flush(); |
158 |
|
|
} |
159 |
gregl |
3.2 |
break; |
160 |
gregl |
3.12 |
case DR_ADDSET: /* add to calculation set */ |
161 |
gwlarson |
3.19 |
if (!msg.nbytes) |
162 |
|
|
break; |
163 |
gregl |
3.2 |
if (msg.nbytes % sizeof(PACKHEAD)) |
164 |
|
|
error(INTERNAL, "bad DR_ADDSET from display process"); |
165 |
|
|
disp_result(DS_STARTIMM, 0, NULL); |
166 |
|
|
bundle_set(BS_ADD, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
167 |
|
|
disp_result(DS_ENDIMM, 0, NULL); |
168 |
gregl |
3.15 |
disp_flush(); |
169 |
gregl |
3.2 |
break; |
170 |
gregl |
3.12 |
case DR_ADJSET: /* adjust calculation set members */ |
171 |
gwlarson |
3.19 |
if (!msg.nbytes) |
172 |
|
|
break; |
173 |
gregl |
3.9 |
if (msg.nbytes % sizeof(PACKHEAD)) |
174 |
|
|
error(INTERNAL, "bad DR_ADJSET from display process"); |
175 |
|
|
disp_result(DS_STARTIMM, 0, NULL); |
176 |
|
|
bundle_set(BS_ADJ, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
177 |
|
|
disp_result(DS_ENDIMM, 0, NULL); |
178 |
|
|
disp_flush(); |
179 |
|
|
break; |
180 |
gregl |
3.12 |
case DR_DELSET: /* delete from calculation set */ |
181 |
gwlarson |
3.19 |
if (!msg.nbytes) |
182 |
|
|
break; |
183 |
gregl |
3.2 |
if (msg.nbytes % sizeof(PACKHEAD)) |
184 |
|
|
error(INTERNAL, "bad DR_DELSET from display process"); |
185 |
|
|
bundle_set(BS_DEL, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
186 |
|
|
break; |
187 |
gwlarson |
3.22 |
case DR_VIEWPOINT: /* set target eye position */ |
188 |
|
|
if (msg.nbytes != sizeof(VIEWPOINT)) |
189 |
|
|
error(INTERNAL, "bad DR_VIEWPOINT from display process"); |
190 |
|
|
copystruct(&myeye, (VIEWPOINT *)buf); |
191 |
|
|
break; |
192 |
gregl |
3.12 |
case DR_ATTEN: /* block for priority request */ |
193 |
gregl |
3.2 |
if (msg.nbytes) |
194 |
|
|
error(INTERNAL, "bad DR_ATTEN from display process"); |
195 |
|
|
/* send acknowledgement */ |
196 |
|
|
disp_result(DS_ACKNOW, 0, NULL); |
197 |
gregl |
3.3 |
return(disp_check(1)); /* block on following request */ |
198 |
gregl |
3.13 |
case DR_KILL: /* kill computation process(es) */ |
199 |
|
|
if (msg.nbytes) |
200 |
|
|
error(INTERNAL, "bad DR_KILL from display process"); |
201 |
gregl |
3.14 |
if (nprocs > 0) |
202 |
|
|
done_rtrace(); |
203 |
|
|
else |
204 |
gregl |
3.13 |
error(WARNING, "no rtrace process to kill"); |
205 |
|
|
break; |
206 |
|
|
case DR_RESTART: /* restart computation process(es) */ |
207 |
|
|
if (msg.nbytes) |
208 |
|
|
error(INTERNAL, "bad DR_RESTART from display process"); |
209 |
gregl |
3.14 |
if (ncprocs > nprocs) |
210 |
|
|
new_rtrace(); |
211 |
|
|
else if (nprocs > 0) |
212 |
gregl |
3.13 |
error(WARNING, "rtrace already runnning"); |
213 |
|
|
else |
214 |
|
|
error(WARNING, "holodeck not open for writing"); |
215 |
|
|
break; |
216 |
|
|
case DR_CLOBBER: /* clobber holodeck */ |
217 |
|
|
if (msg.nbytes) |
218 |
|
|
error(INTERNAL, "bad DR_CLOBBER from display process"); |
219 |
|
|
if (!force || !ncprocs) |
220 |
|
|
error(WARNING, "request to clobber holodeck denied"); |
221 |
|
|
else { |
222 |
|
|
error(WARNING, "clobbering holodeck contents"); |
223 |
|
|
hdclobber(NULL); |
224 |
|
|
} |
225 |
|
|
break; |
226 |
gregl |
3.12 |
case DR_SHUTDOWN: /* shut down program */ |
227 |
gregl |
3.2 |
if (msg.nbytes) |
228 |
|
|
error(INTERNAL, "bad DR_SHUTDOWN from display process"); |
229 |
|
|
return(0); /* zero return signals shutdown */ |
230 |
gregl |
3.12 |
case DR_NOOP: /* do nothing */ |
231 |
gregl |
3.7 |
break; |
232 |
gregl |
3.2 |
default: |
233 |
|
|
error(INTERNAL, "unrecognized request from display process"); |
234 |
|
|
} |
235 |
gregl |
3.3 |
if (msg.nbytes) /* clean up */ |
236 |
gregl |
3.2 |
free(buf); |
237 |
|
|
return(1); /* normal return value */ |
238 |
|
|
fcntlerr: |
239 |
|
|
error(SYSTEM, "cannot change display blocking mode"); |
240 |
|
|
readerr: |
241 |
|
|
error(SYSTEM, "error reading from display process"); |
242 |
gregl |
3.1 |
} |
243 |
|
|
|
244 |
|
|
|
245 |
gregl |
3.2 |
int |
246 |
|
|
disp_close() /* close our display process */ |
247 |
gregl |
3.1 |
{ |
248 |
gregl |
3.2 |
int rval; |
249 |
|
|
|
250 |
gregl |
3.6 |
if (dpout == NULL) |
251 |
gregl |
3.2 |
return(-1); |
252 |
gwlarson |
3.23 |
myeye.rng = 0; |
253 |
gregl |
3.2 |
disp_result(DS_SHUTDOWN, 0, NULL); |
254 |
gregl |
3.5 |
fclose(dpout); |
255 |
|
|
dpout = NULL; |
256 |
gwlarson |
3.26 |
return(dpd[2]<0 ? 0 : close_process(dpd)); |
257 |
gregl |
3.2 |
} |
258 |
|
|
|
259 |
|
|
|
260 |
gregl |
3.5 |
disp_result(type, nbytes, p) /* queue result message to display process */ |
261 |
gregl |
3.2 |
int type, nbytes; |
262 |
|
|
char *p; |
263 |
|
|
{ |
264 |
|
|
MSGHEAD msg; |
265 |
gwlarson |
3.20 |
/* consistency checks */ |
266 |
|
|
#ifdef DEBUG |
267 |
|
|
if (nbytes < 0 || nbytes > 0 & p == NULL) |
268 |
|
|
error(CONSISTENCY, "bad buffer handed to disp_result"); |
269 |
|
|
#endif |
270 |
gregl |
3.6 |
if (dpout == NULL) |
271 |
|
|
return; |
272 |
gregl |
3.2 |
msg.type = type; |
273 |
|
|
msg.nbytes = nbytes; |
274 |
gregl |
3.5 |
fwrite((char *)&msg, sizeof(MSGHEAD), 1, dpout); |
275 |
|
|
if (nbytes > 0) |
276 |
|
|
fwrite(p, 1, nbytes, dpout); |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
|
280 |
|
|
disp_flush() /* flush output to display */ |
281 |
|
|
{ |
282 |
|
|
if (fflush(dpout) < 0) |
283 |
|
|
error(SYSTEM, "error writing to the display process"); |
284 |
gregl |
3.1 |
} |