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