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