ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo4.c
Revision: 3.28
Committed: Sat Feb 22 02:07:25 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.27: +1 -4 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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