ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo4.c
(Generate patch)

Comparing ray/src/hd/rholo4.c (file contents):
Revision 3.1 by gregl, Fri Oct 31 10:23:29 1997 UTC vs.
Revision 3.20 by gwlarson, Tue Oct 6 18:09:49 1998 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines