ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devmain.c
Revision: 1.7
Committed: Tue Jan 30 11:37:52 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +8 -2 lines
Log Message:
fixed bug where drivers would send commands inappropriately

File Contents

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