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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines