ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devmain.c
Revision: 2.4
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R5, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1
Changes since 2.3: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.3 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4     /*
5     * devmain.c - main for independent drivers.
6     *
7     * Redefine your initialization routine to dinit.
8 greg 2.3 */
9    
10 greg 2.4 #include "copyright.h"
11 greg 1.1
12 gregl 2.2 #include "standard.h"
13 greg 1.1
14     #include "color.h"
15    
16     #include "driver.h"
17    
18     struct driver *dev = NULL; /* output device */
19    
20     FILE *devin, *devout; /* communications channels */
21    
22     char *progname; /* driver name */
23    
24 greg 1.8 int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_flush();
25 greg 1.1
26     int (*dev_func[NREQUESTS])() = { /* request handlers */
27     r_clear, r_paintr,
28 greg 1.8 r_getcur, r_comout,
29     r_comin, r_flush
30 greg 1.1 };
31    
32    
33     main(argc, argv) /* set up communications and main loop */
34     int argc;
35     char *argv[];
36     {
37     extern struct driver *dinit();
38     int com;
39     /* set up I/O */
40     progname = argv[0];
41     if (argc < 3) {
42 gregl 2.2 eputs("arg count\n");
43 greg 1.1 quit(1);
44     }
45     devin = fdopen(atoi(argv[1]), "r");
46     devout = fdopen(atoi(argv[2]), "w");
47     if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) {
48 gregl 2.2 eputs("connection failure\n");
49 greg 1.1 quit(1);
50     }
51     /* open device */
52 greg 1.2 if ((dev = dinit(argv[0], argv[3])) == NULL)
53 greg 1.1 quit(1);
54     putw(COM_RECVM, devout); /* verify initialization */
55 greg 1.11 sendstate();
56 greg 1.1 fflush(devout);
57     /* loop on requests */
58     while ((com = getc(devin)) != EOF) {
59     if (com >= NREQUESTS || dev_func[com] == NULL) {
60 gregl 2.2 eputs("invalid request\n");
61 greg 1.1 quit(1);
62     }
63     (*dev_func[com])(); /* process request */
64     }
65     quit(0); /* all done, clean up and exit */
66     }
67    
68    
69 greg 2.3 void
70 greg 1.1 quit(code) /* close device and exit */
71     int code;
72     {
73     if (dev != NULL)
74     (*dev->close)();
75     exit(code);
76     }
77    
78    
79     r_clear() /* clear screen */
80     {
81     int xres, yres;
82    
83     xres = getw(devin);
84     yres = getw(devin);
85     (*dev->clear)(xres, yres);
86     }
87    
88    
89     r_paintr() /* paint a rectangle */
90     {
91     COLOR col;
92     int xmin, ymin, xmax, ymax;
93    
94 greg 1.6 fread((char *)col, sizeof(COLOR), 1, devin);
95 greg 1.1 xmin = getw(devin); ymin = getw(devin);
96     xmax = getw(devin); ymax = getw(devin);
97     (*dev->paintr)(col, xmin, ymin, xmax, ymax);
98 greg 1.8 }
99    
100    
101     r_flush() /* flush output */
102     {
103     if (dev->flush != NULL)
104     (*dev->flush)();
105 greg 1.9 putc(COM_FLUSH, devout);
106 greg 1.11 sendstate();
107 greg 1.9 fflush(devout);
108 greg 1.1 }
109    
110    
111     r_getcur() /* get and return cursor position */
112     {
113     int c;
114     int x, y;
115     /* get it if we can */
116     if (dev->getcur == NULL) {
117     c = ABORT;
118     x = y = 0;
119     } else
120     c = (*dev->getcur)(&x, &y);
121     /* reply */
122     putc(COM_GETCUR, devout);
123     putc(c, devout);
124     putw(x, devout);
125     putw(y, devout);
126     fflush(devout);
127     }
128    
129    
130     r_comout() /* print string to command line */
131     {
132     char str[256];
133    
134     mygets(str, devin);
135     (*dev->comout)(str);
136     }
137    
138    
139     r_comin() /* read string from command line */
140     {
141 greg 1.7 char buf[256], *prompt;
142     /* get prompt */
143     if (getc(devin)) {
144     mygets(buf, devin);
145     prompt = buf;
146     } else
147     prompt = NULL;
148 greg 1.1 /* get string */
149 greg 1.7 (*dev->comin)(buf, prompt);
150 greg 1.1 /* reply */
151     putc(COM_COMIN, devout);
152     myputs(buf, devout);
153 greg 1.11 sendstate();
154 greg 1.1 fflush(devout);
155     }
156    
157    
158     mygets(s, fp) /* get string from file (with nul) */
159     register char *s;
160     register FILE *fp;
161     {
162     register int c;
163    
164     while ((c = getc(fp)) != EOF)
165     if ((*s++ = c) == '\0')
166     return;
167     *s = '\0';
168     }
169    
170    
171     myputs(s, fp) /* put string to file (with nul) */
172     register char *s;
173     register FILE *fp;
174     {
175     do
176     putc(*s, fp);
177     while (*s++);
178     }
179    
180    
181 greg 2.3 void
182 gregl 2.2 eputs(s) /* put string to stderr */
183 greg 1.1 register char *s;
184     {
185 greg 1.12 static int midline = 0;
186 greg 1.1
187 greg 1.13 if (!*s)
188     return;
189 greg 1.12 if (!midline++) {
190 greg 1.1 fputs(progname, stderr);
191     fputs(": ", stderr);
192     }
193     fputs(s, stderr);
194 greg 1.13 if (s[strlen(s)-1] == '\n') {
195 greg 1.1 fflush(stderr);
196 greg 1.12 midline = 0;
197 greg 1.1 }
198 greg 1.11 }
199    
200    
201     sendstate() /* send driver state variables */
202     {
203     fwrite((char *)&dev->pixaspect, sizeof(dev->pixaspect), 1, devout);
204     putw(dev->xsiz, devout);
205     putw(dev->ysiz, devout);
206     putw(dev->inpready, devout);
207 greg 1.1 }