ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.4
Committed: Mon Oct 2 17:12:36 1989 UTC (35 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +27 -144 lines
Log Message:
New adaptive color allocation scheme for rview

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