ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.6
Committed: Tue Oct 3 14:06:53 1989 UTC (35 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +4 -5 lines
Log Message:
Changed calling conventions slightly and added custom color map.

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