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

Comparing ray/src/rt/devcomm.c (file contents):
Revision 1.5 by greg, Wed Jun 7 13:22:54 1989 UTC vs.
Revision 2.6 by greg, Sat Feb 22 02:07:28 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1988 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  devcomm.c - communication routines for separate drivers.
6   *
7 < *      10/5/88
7 > *  External symbols declared in driver.h
8   */
9  
10 < #include <stdio.h>
10 > /* ====================================================================
11 > * The Radiance Software License, Version 1.0
12 > *
13 > * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 > * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 > *
16 > * Redistribution and use in source and binary forms, with or without
17 > * modification, are permitted provided that the following conditions
18 > * are met:
19 > *
20 > * 1. Redistributions of source code must retain the above copyright
21 > *         notice, this list of conditions and the following disclaimer.
22 > *
23 > * 2. Redistributions in binary form must reproduce the above copyright
24 > *       notice, this list of conditions and the following disclaimer in
25 > *       the documentation and/or other materials provided with the
26 > *       distribution.
27 > *
28 > * 3. The end-user documentation included with the redistribution,
29 > *           if any, must include the following acknowledgment:
30 > *             "This product includes Radiance software
31 > *                 (http://radsite.lbl.gov/)
32 > *                 developed by the Lawrence Berkeley National Laboratory
33 > *               (http://www.lbl.gov/)."
34 > *       Alternately, this acknowledgment may appear in the software itself,
35 > *       if and wherever such third-party acknowledgments normally appear.
36 > *
37 > * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 > *       and "The Regents of the University of California" must
39 > *       not be used to endorse or promote products derived from this
40 > *       software without prior written permission. For written
41 > *       permission, please contact [email protected].
42 > *
43 > * 5. Products derived from this software may not be called "Radiance",
44 > *       nor may "Radiance" appear in their name, without prior written
45 > *       permission of Lawrence Berkeley National Laboratory.
46 > *
47 > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 > * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 > * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 > * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 > * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 > * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 > * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 > * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 > * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 > * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 > * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 > * SUCH DAMAGE.
59 > * ====================================================================
60 > *
61 > * This software consists of voluntary contributions made by many
62 > * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 > * information on Lawrence Berkeley National Laboratory, please see
64 > * <http://www.lbl.gov/>.
65 > */
66  
67 < #include <signal.h>
67 > #include "standard.h"
68  
69   #include "color.h"
70  
71   #include "driver.h"
72  
73 + #include "vfork.h"
74 +
75   #ifndef DEVPATH
76   #define DEVPATH         getenv("PATH")  /* device search path */
77   #endif
78  
79 < #ifndef BSD
80 < #define vfork           fork
81 < #endif
79 > static int      comm_getcur();
80 > static void     comm_close(), comm_clear(), comm_paintr(),
81 >                comm_comin(), comm_comout(), comm_flush();
82  
83 < #ifndef WFLUSH
30 < #define WFLUSH          30              /* flush after this many rays */
31 < #endif
32 <
33 < extern char     *getpath(), *getenv();
34 <
35 < int     onsigio();
36 <
37 < int     comm_close(), comm_clear(), comm_paintr(), comm_errout(),
38 <                comm_getcur(), comm_comout(), comm_comin();
39 <
40 < struct driver   comm_driver, comm_default = {
83 > struct driver   comm_driver = {
84          comm_close, comm_clear, comm_paintr, comm_getcur,
85 <        comm_comout, comm_comin,
43 <        MAXRES, MAXRES, 0
85 >        comm_comout, comm_comin, comm_flush
86   };
87  
88 + static void     mygets(), myputs(), reply_error(), getstate();
89 +
90   FILE    *devin, *devout;
91  
92   int     devchild;
93  
94  
95 + static struct driver *
96 + final_connect()                         /* verify and initialize connection */
97 + {
98 +        putw(COM_SENDM, devout);
99 +        fflush(devout);
100 +        if (getw(devin) != COM_RECVM)
101 +                return(NULL);
102 +                                                /* get driver parameters */
103 +        getstate();
104 +                                                /* set error vectors */
105 +        erract[COMMAND].pf = comm_comout;
106 +        if (erract[WARNING].pf != NULL)
107 +                erract[WARNING].pf = comm_comout;
108 +        return(&comm_driver);
109 + }
110 +
111 +
112   struct driver *
113 < comm_init(argv)                 /* set up and execute driver */
114 < char    *argv[];
113 > slave_init(dname, id)                   /* run rview in slave mode */
114 > char    *dname, *id;
115   {
116 <        char    *devname;
117 <        int     p1[2], p2[2];
116 >        devchild = -1;                          /* we're the slave here */
117 >        devout = stdout;                        /* use standard input */
118 >        devin = stdin;                          /* and standard output */
119 >        return(final_connect());                /* verify initialization */
120 > }
121  
122 <        if ((devname = getpath(argv[0], DEVPATH)) == NULL) {
123 <                stderr_v(argv[0]);
124 <                stderr_v(": not found\n");
122 >
123 > struct driver *
124 > comm_init(dname, id)                    /* set up and execute driver */
125 > char    *dname, *id;
126 > {
127 >        char    *dvcname;
128 >        int     p1[2], p2[2];
129 >        char    pin[16], pout[16];
130 >                                                /* find driver program */
131 >        if ((dvcname = getpath(dname, DEVPATH, X_OK)) == NULL) {
132 >                eputs(dname);
133 >                eputs(": not found\n");
134                  return(NULL);
135          }
136 +                                                /* open communication pipes */
137          if (pipe(p1) == -1 || pipe(p2) == -1)
138                  goto syserr;
139 <        if ((devchild = vfork()) == 0) {
139 >        if ((devchild = vfork()) == 0) {        /* fork driver process */
140                  close(p1[1]);
141                  close(p2[0]);
142 <                if (p1[0] != 0) {
143 <                        dup2(p1[0], 0);
144 <                        close(p1[0]);
145 <                }
72 <                if (p2[1] != 1) {
73 <                        dup2(p2[1], 1);
74 <                        close(p2[1]);
75 <                }
76 <                execv(devname, argv);
77 <                stderr_v(devname);
78 <                stderr_v(": cannot execute\n");
142 >                sprintf(pin, "%d", p1[0]);
143 >                sprintf(pout, "%d", p2[1]);
144 >                execl(dvcname, dname, pin, pout, id, 0);
145 >                perror(dvcname);
146                  _exit(127);
147          }
148          if (devchild == -1)
# Line 86 | Line 153 | char   *argv[];
153                  goto syserr;
154          if ((devin = fdopen(p2[0], "r")) == NULL)
155                  goto syserr;
156 <        bcopy(&comm_default, &comm_driver, sizeof(comm_driver));
90 <        signal(SIGIO, onsigio);
91 <        cmdvec = comm_comout;                   /* set error vectors */
92 <        if (wrnvec != NULL)
93 <                wrnvec = comm_comout;
94 <        return(&comm_driver);
156 >        return(final_connect());                /* verify initialization */
157   syserr:
158 <        perror(argv[0]);
158 >        perror(dname);
159          return(NULL);
160   }
161  
162  
163 < static
163 > static void
164   comm_close()                    /* done with driver */
165   {
166          int     pid;
167  
168 <        cmdvec = NULL;                          /* reset error vectors */
169 <        if (wrnvec != NULL)
170 <                wrnvec = stderr_v;
109 <        signal(SIGIO, SIG_DFL);
168 >        erract[COMMAND].pf = NULL;              /* reset error vectors */
169 >        if (erract[WARNING].pf != NULL)
170 >                erract[WARNING].pf = wputs;
171          fclose(devout);
172          fclose(devin);
173 +        if (devchild < 0)
174 +                return;
175          while ((pid = wait(0)) != -1 && pid != devchild)
176                  ;
177   }
178  
179  
180 < static
180 > static void
181   comm_clear(xres, yres)                          /* clear screen */
182   int     xres, yres;
183   {
184          putc(COM_CLEAR, devout);
185 <        fwrite(&xres, sizeof(xres), 1, devout);
186 <        fwrite(&yres, sizeof(yres), 1, devout);
185 >        putw(xres, devout);
186 >        putw(yres, devout);
187          fflush(devout);
188   }
189  
190  
191 < static
191 > static void
192   comm_paintr(col, xmin, ymin, xmax, ymax)        /* paint a rectangle */
193   COLOR   col;
194   int     xmin, ymin, xmax, ymax;
195   {
133        extern long     nrays;          /* number of rays traced */
134        static long     lastflush = 0;  /* ray count at last flush */
135
196          putc(COM_PAINTR, devout);
197 <        fwrite(col, sizeof(COLOR), 1, devout);
198 <        fwrite(&xmin, sizeof(xmin), 1, devout);
199 <        fwrite(&ymin, sizeof(ymin), 1, devout);
200 <        fwrite(&xmax, sizeof(xmax), 1, devout);
201 <        fwrite(&ymax, sizeof(ymax), 1, devout);
142 <        if (nrays - lastflush >= WFLUSH) {
143 <                fflush(devout);
144 <                lastflush = nrays;
145 <        }
197 >        fwrite((char *)col, sizeof(COLOR), 1, devout);
198 >        putw(xmin, devout);
199 >        putw(ymin, devout);
200 >        putw(xmax, devout);
201 >        putw(ymax, devout);
202   }
203  
204  
205 + static void
206 + comm_flush()                            /* flush output to driver */
207 + {
208 +        putc(COM_FLUSH, devout);
209 +        fflush(devout);
210 +        if (getc(devin) != COM_FLUSH)
211 +                reply_error("flush");
212 +        getstate();
213 + }
214 +
215 +
216   static int
217   comm_getcur(xp, yp)                     /* get and return cursor position */
218   int     *xp, *yp;
# Line 157 | Line 224 | int    *xp, *yp;
224          if (getc(devin) != COM_GETCUR)
225                  reply_error("getcur");
226          c = getc(devin);
227 <        fread(xp, sizeof(*xp), 1, devin);
228 <        fread(yp, sizeof(*yp), 1, devin);
227 >        *xp = getw(devin);
228 >        *yp = getw(devin);
229          return(c);
230   }
231  
232  
233 < static
233 > static void
234   comm_comout(str)                        /* print string to command line */
235   char    *str;
236   {
237          putc(COM_COMOUT, devout);
238          myputs(str, devout);
239 <        fflush(devout);
239 >        if (str[strlen(str)-1] == '\n')
240 >                fflush(devout);
241   }
242  
243  
244 < static
245 < comm_comin(buf)                         /* read string from command line */
244 > static void
245 > comm_comin(buf, prompt)                 /* read string from command line */
246   char    *buf;
247 + char    *prompt;
248   {
249          putc(COM_COMIN, devout);
250 +        if (prompt == NULL)
251 +                putc(0, devout);
252 +        else {
253 +                putc(1, devout);
254 +                myputs(prompt, devout);
255 +        }
256          fflush(devout);
257          if (getc(devin) != COM_COMIN)
258                  reply_error("comin");
259          mygets(buf, devin);
260 <        comm_driver.inpready = 0;
260 >        getstate();
261   }
262  
263  
264 < static
190 < comm_errout(str)                        /* display an error message */
191 < char    *str;
192 < {
193 <        comm_comout(str);
194 <        stderr_v(str);                  /* send to standard error also */
195 < }
196 <
197 <
198 < static
264 > static void
265   mygets(s, fp)                           /* get string from file (with nul) */
266   register char   *s;
267   register FILE   *fp;
# Line 209 | Line 275 | register FILE  *fp;
275   }
276  
277  
278 < static
278 > static void
279   myputs(s, fp)                           /* put string to file (with nul) */
280   register char   *s;
281   register FILE   *fp;
# Line 220 | Line 286 | register FILE  *fp;
286   }
287  
288  
289 < static
289 > static void
290   reply_error(routine)                    /* what should we do here? */
291   char    *routine;
292   {
293 <        stderr_v(routine);
294 <        stderr_v(": driver reply error\n");
293 >        eputs(routine);
294 >        eputs(": driver reply error\n");
295          quit(1);
296   }
297  
298  
299 < static
300 < onsigio()                               /* input ready */
299 > static void
300 > getstate()                              /* get driver state variables */
301   {
302 <        comm_driver.inpready++;
302 >        fread((char *)&comm_driver.pixaspect,
303 >                        sizeof(comm_driver.pixaspect), 1, devin);
304 >        comm_driver.xsiz = getw(devin);
305 >        comm_driver.ysiz = getw(devin);
306 >        comm_driver.inpready = getw(devin);
307   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines