ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.5
Committed: Tue Oct 3 11:10:04 1989 UTC (35 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +26 -26 lines
Log Message:
Improved color table allocation, second cut.

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1988 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * sundev.c - standalone driver for Sunwindows.
9     *
10     * 10/3/88
11     */
12    
13     #include <stdio.h>
14     #include <fcntl.h>
15     #include <suntool/sunview.h>
16     #include <suntool/canvas.h>
17     #include <suntool/tty.h>
18    
19     #include "color.h"
20    
21     #include "driver.h"
22    
23     short icon_image[] = {
24     #include "suntools.icon"
25     };
26     DEFINE_ICON_FROM_IMAGE(sun_icon, icon_image);
27    
28     #ifndef TTYPROG
29     #define TTYPROG "sun.com" /* tty program */
30     #endif
31    
32 greg 1.4 #define GAMMA 2.2 /* exponent for color correction */
33 greg 1.1
34     #define COMLH 3 /* number of command lines */
35    
36     #define FIRSTCOLOR 2 /* first usable entry */
37 greg 1.5 #define NCOLORS 251 /* number of usable colors */
38 greg 1.1
39 greg 1.5 int newcolr();
40    
41 greg 1.1 int sun_clear(), sun_paintr(), sun_getcur(),
42     sun_comout(), sun_comin();
43    
44     int (*dev_func[NREQUESTS])() = { /* request handlers */
45     sun_clear, sun_paintr, sun_getcur,
46     sun_comout, sun_comin
47     };
48    
49     FILE *ttyin;
50    
51     Frame frame = 0;
52     Tty tty = 0;
53     Canvas canvas = 0;
54    
55     int xres = 0, yres = 0;
56    
57     char *progname;
58    
59    
60     main(argc, argv)
61     int argc;
62     char *argv[];
63     {
64     extern Notify_value my_notice_destroy();
65     char *ttyargv[4], arg1[10], arg2[10];
66     int pd[2];
67     int com;
68    
69     progname = argv[0];
70    
71     frame = window_create(0, FRAME,
72     FRAME_LABEL, argc > 1 ? argv[1] : argv[0],
73     FRAME_ICON, &sun_icon,
74     WIN_X, 0, WIN_Y, 0,
75     0);
76     if (frame == 0)
77     quit("cannot create frame");
78     if (pipe(pd) == -1)
79     quit("cannot create pipe");
80     sprintf(arg1, "%d", getppid());
81     sprintf(arg2, "%d", pd[1]);
82     ttyargv[0] = TTYPROG;
83     ttyargv[1] = arg1;
84     ttyargv[2] = arg2;
85     ttyargv[3] = NULL;
86     #ifdef BSD
87     fcntl(pd[0], F_SETFD, 1);
88     #endif
89     tty = window_create(frame, TTY,
90     TTY_ARGV, ttyargv,
91     WIN_ROWS, COMLH,
92     0);
93     if (tty == 0)
94     quit("cannot create tty subwindow");
95     close(pd[1]);
96     if ((ttyin = fdopen(pd[0], "r")) == NULL)
97     quit("cannot open tty");
98     canvas = window_create(frame, CANVAS,
99     CANVAS_RETAINED, FALSE,
100     WIN_INPUT_DESIGNEE, window_get(tty,WIN_DEVICE_NUMBER),
101     WIN_CONSUME_PICK_EVENTS, WIN_NO_EVENTS,
102     MS_LEFT, MS_MIDDLE, MS_RIGHT, 0,
103     0);
104     if (canvas == 0)
105     quit("cannot create canvas");
106     if (getmap() < 0)
107     quit("not a color screen");
108 greg 1.5 make_cmap(GAMMA);
109 greg 1.1 window_set(canvas, CANVAS_RETAINED, TRUE, 0);
110     notify_interpose_destroy_func(frame, my_notice_destroy);
111    
112     while ((com = getc(stdin)) != EOF) { /* loop on requests */
113     if (com >= NREQUESTS || dev_func[com] == NULL)
114     quit("invalid request");
115     (*dev_func[com])();
116     }
117     quit(NULL);
118     }
119    
120    
121     quit(msg) /* exit */
122     char *msg;
123     {
124     if (msg != NULL) {
125     fprintf(stderr, "%s: %s\n", progname, msg);
126 greg 1.2 kill(getppid(), SIGPIPE);
127 greg 1.1 exit(1);
128     }
129     exit(0);
130     }
131    
132    
133     Notify_value
134     my_notice_destroy(fr, st) /* we're on our way out */
135     Frame fr;
136     Destroy_status st;
137     {
138     if (st != DESTROY_CHECKING) {
139 greg 1.3 kill((int)window_get(tty, TTY_PID), SIGPIPE);
140     kill(getppid(), SIGPIPE);
141 greg 1.1 }
142     return(notify_next_destroy_func(fr, st));
143     }
144    
145    
146     sun_clear() /* clear our canvas */
147     {
148     register Pixwin *pw;
149     int nwidth, nheight;
150    
151     fread(&nwidth, sizeof(nwidth), 1, stdin);
152     fread(&nheight, sizeof(nheight), 1, stdin);
153     pw = canvas_pixwin(canvas);
154     if (nwidth != xres || nheight != yres) {
155     window_set(frame, WIN_SHOW, FALSE, 0);
156     window_set(canvas, CANVAS_AUTO_SHRINK, TRUE,
157     CANVAS_AUTO_EXPAND, TRUE, 0);
158     window_set(canvas, WIN_WIDTH, nwidth, WIN_HEIGHT, nheight, 0);
159     window_set(canvas, CANVAS_AUTO_SHRINK, FALSE,
160     CANVAS_AUTO_EXPAND, FALSE, 0);
161     window_fit(frame);
162     window_set(frame, WIN_SHOW, TRUE, 0);
163     notify_do_dispatch();
164     xres = nwidth;
165     yres = nheight;
166     }
167     pw_writebackground(pw, 0, 0, xres, xres, PIX_SRC);
168 greg 1.5 new_ctab(NCOLORS, newcolr);
169 greg 1.1 }
170    
171    
172     sun_paintr() /* fill a rectangle */
173     {
174     COLOR col;
175     int pval;
176     int xmin, ymin, xmax, ymax;
177     register Pixwin *pw;
178    
179     fread(col, sizeof(COLOR), 1, stdin);
180     fread(&xmin, sizeof(xmin), 1, stdin);
181     fread(&ymin, sizeof(ymin), 1, stdin);
182     fread(&xmax, sizeof(xmax), 1, stdin);
183     fread(&ymax, sizeof(ymax), 1, stdin);
184 greg 1.4 pval = get_pixel(col) + FIRSTCOLOR;
185 greg 1.1 pw = canvas_pixwin(canvas);
186     pw_rop(pw, xmin, yres-ymax, xmax-xmin, ymax-ymin,
187     PIX_SRC|PIX_COLOR(pval), NULL, 0, 0);
188     }
189    
190    
191     sun_comout() /* output a string to command line */
192     {
193     char out[256];
194    
195     mygets(out, stdin);
196     ttyputs(out);
197     }
198    
199    
200     sun_comin() /* input a string from the command line */
201     {
202     char buf[256];
203     /* echo characters */
204     do {
205     mygets(buf, ttyin);
206     ttyputs(buf);
207     } while (buf[0]);
208     /* get result */
209     mygets(buf, ttyin);
210     /* send reply */
211     putc(COM_COMIN, stdout);
212     myputs(buf, stdout);
213     fflush(stdout);
214     }
215    
216    
217     sun_getcur() /* get cursor position */
218     {
219 greg 1.3 Event ev;
220 greg 1.1 int xpos, ypos;
221     int c;
222    
223     notify_no_dispatch(); /* allow read to block */
224     window_set(canvas, WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS, 0);
225 greg 1.3 again:
226     if (window_read_event(canvas, &ev) == -1) {
227     notify_perror();
228     quit("window event read error");
229     }
230     c = event_id(&ev);
231     switch (c) {
232     case MS_LEFT:
233     c = MB1;
234 greg 1.1 break;
235 greg 1.3 case MS_MIDDLE:
236     c = MB2;
237     break;
238     case MS_RIGHT:
239     c = MB3;
240     break;
241     default:
242     if (c < ASCII_FIRST || c > ASCII_LAST)
243     goto again;
244     break;
245 greg 1.1 }
246 greg 1.3 xpos = event_x(&ev);
247     ypos = yres-1 - event_y(&ev);
248    
249 greg 1.1 window_set(canvas, WIN_IGNORE_KBD_EVENT, WIN_ASCII_EVENTS, 0);
250     notify_do_dispatch();
251     putc(COM_GETCUR, stdout);
252     putc(c, stdout);
253     fwrite(&xpos, sizeof(xpos), 1, stdout);
254     fwrite(&ypos, sizeof(ypos), 1, stdout);
255     fflush(stdout);
256     }
257    
258    
259     mygets(s, fp) /* get string from file (with nul) */
260     register char *s;
261     register FILE *fp;
262     {
263     register int c;
264    
265     while ((c = getc(fp)) != EOF)
266     if ((*s++ = c) == '\0')
267     return;
268     *s = '\0';
269     }
270    
271    
272     myputs(s, fp) /* put string to file (with nul) */
273     register char *s;
274     register FILE *fp;
275     {
276     do
277     putc(*s, fp);
278     while (*s++);
279     }
280    
281    
282     ttyputs(s) /* put string out to tty subwindow */
283     register char *s;
284     {
285     char buf[256];
286     register char *cp;
287    
288     for (cp = buf; *s; s++) {
289     if (*s == '\n')
290     *cp++ = '\r';
291     *cp++ = *s;
292     }
293     ttysw_output(tty, buf, cp-buf);
294     }
295    
296    
297 greg 1.4 getmap() /* allocate color map segments */
298 greg 1.1 {
299 greg 1.4 char cmsname[20];
300 greg 1.5 unsigned char red[256], green[256], blue[256];
301 greg 1.1 register Pixwin *pw;
302 greg 1.5 register int i;
303    
304     for (i = 0; i < 256; i++)
305     red[i] = green[i] = blue[i] = 128;
306     red[0] = green[0] = blue[0] = 255;
307     red[1] = green[1] = blue[1] = 0;
308     red[255] = green[255] = blue[255] = 0;
309     red[254] = green[254] = blue[254] = 255;
310     red[253] = green[253] = blue[253] = 0;
311 greg 1.4 /* name shared segment */
312     sprintf(cmsname, "rv%d", getpid());
313     /* set canvas */
314 greg 1.1 pw = canvas_pixwin(canvas);
315 greg 1.4 if (pw->pw_pixrect->pr_depth < 8)
316     return(-1);
317     pw_setcmsname(pw, cmsname);
318 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
319 greg 1.4 /* set tty subwindow */
320 greg 1.1 pw = (Pixwin *)window_get(tty, WIN_PIXWIN);
321 greg 1.4 pw_setcmsname(pw, cmsname);
322 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
323 greg 1.4 /* set frame */
324     pw = (Pixwin *)window_get(frame, WIN_PIXWIN);
325     pw_setcmsname(pw, cmsname);
326 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
327 greg 1.1
328 greg 1.4 return(0);
329 greg 1.1 }
330    
331    
332 greg 1.5 newcolr(ndx, r, g, b) /* enter a color into hardware table */
333     int ndx;
334     unsigned char r, g, b;
335 greg 1.1 {
336     register Pixwin *pw;
337 greg 1.5
338 greg 1.1 pw = canvas_pixwin(canvas);
339 greg 1.5 pw_putcolormap(pw, ndx+FIRSTCOLOR, 1,
340     &r, &g, &b);
341 greg 1.1 pw = (Pixwin *)window_get(tty, WIN_PIXWIN);
342 greg 1.5 pw_putcolormap(pw, ndx+FIRSTCOLOR, 1,
343     &r, &g, &b);
344 greg 1.1 }