ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.7
Committed: Tue Oct 3 16:50:59 1989 UTC (35 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +3 -0 lines
Log Message:
Fixed tty subwindow exiting

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 greg 1.7 window_destroy(tty);
116 greg 1.1 quit(NULL);
117     }
118    
119    
120     quit(msg) /* exit */
121     char *msg;
122     {
123     if (msg != NULL) {
124     fprintf(stderr, "%s: %s\n", progname, msg);
125 greg 1.2 kill(getppid(), SIGPIPE);
126 greg 1.1 exit(1);
127     }
128     exit(0);
129     }
130    
131    
132     Notify_value
133     my_notice_destroy(fr, st) /* we're on our way out */
134     Frame fr;
135     Destroy_status st;
136     {
137     if (st != DESTROY_CHECKING) {
138 greg 1.7 /*
139 greg 1.3 kill((int)window_get(tty, TTY_PID), SIGPIPE);
140 greg 1.7 */
141 greg 1.3 kill(getppid(), SIGPIPE);
142 greg 1.1 }
143     return(notify_next_destroy_func(fr, st));
144     }
145    
146    
147     sun_clear() /* clear our canvas */
148     {
149     register Pixwin *pw;
150     int nwidth, nheight;
151    
152     fread(&nwidth, sizeof(nwidth), 1, stdin);
153     fread(&nheight, sizeof(nheight), 1, stdin);
154     pw = canvas_pixwin(canvas);
155     if (nwidth != xres || nheight != yres) {
156     window_set(frame, WIN_SHOW, FALSE, 0);
157     window_set(canvas, CANVAS_AUTO_SHRINK, TRUE,
158     CANVAS_AUTO_EXPAND, TRUE, 0);
159     window_set(canvas, WIN_WIDTH, nwidth, WIN_HEIGHT, nheight, 0);
160     window_set(canvas, CANVAS_AUTO_SHRINK, FALSE,
161     CANVAS_AUTO_EXPAND, FALSE, 0);
162     window_fit(frame);
163     window_set(frame, WIN_SHOW, TRUE, 0);
164     notify_do_dispatch();
165     xres = nwidth;
166     yres = nheight;
167     }
168     pw_writebackground(pw, 0, 0, xres, xres, PIX_SRC);
169 greg 1.6 new_ctab(NCOLORS);
170 greg 1.1 }
171    
172    
173     sun_paintr() /* fill a rectangle */
174     {
175 greg 1.6 extern int newcolr();
176 greg 1.1 COLOR col;
177     int pval;
178     int xmin, ymin, xmax, ymax;
179     register Pixwin *pw;
180    
181     fread(col, sizeof(COLOR), 1, stdin);
182     fread(&xmin, sizeof(xmin), 1, stdin);
183     fread(&ymin, sizeof(ymin), 1, stdin);
184     fread(&xmax, sizeof(xmax), 1, stdin);
185     fread(&ymax, sizeof(ymax), 1, stdin);
186 greg 1.6 pval = get_pixel(col, newcolr) + FIRSTCOLOR;
187 greg 1.1 pw = canvas_pixwin(canvas);
188     pw_rop(pw, xmin, yres-ymax, xmax-xmin, ymax-ymin,
189     PIX_SRC|PIX_COLOR(pval), NULL, 0, 0);
190     }
191    
192    
193     sun_comout() /* output a string to command line */
194     {
195     char out[256];
196    
197     mygets(out, stdin);
198     ttyputs(out);
199     }
200    
201    
202     sun_comin() /* input a string from the command line */
203     {
204     char buf[256];
205     /* echo characters */
206     do {
207     mygets(buf, ttyin);
208     ttyputs(buf);
209     } while (buf[0]);
210     /* get result */
211     mygets(buf, ttyin);
212     /* send reply */
213     putc(COM_COMIN, stdout);
214     myputs(buf, stdout);
215     fflush(stdout);
216     }
217    
218    
219     sun_getcur() /* get cursor position */
220     {
221 greg 1.3 Event ev;
222 greg 1.1 int xpos, ypos;
223     int c;
224    
225     notify_no_dispatch(); /* allow read to block */
226     window_set(canvas, WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS, 0);
227 greg 1.3 again:
228     if (window_read_event(canvas, &ev) == -1) {
229     notify_perror();
230     quit("window event read error");
231     }
232     c = event_id(&ev);
233     switch (c) {
234     case MS_LEFT:
235     c = MB1;
236 greg 1.1 break;
237 greg 1.3 case MS_MIDDLE:
238     c = MB2;
239     break;
240     case MS_RIGHT:
241     c = MB3;
242     break;
243     default:
244     if (c < ASCII_FIRST || c > ASCII_LAST)
245     goto again;
246     break;
247 greg 1.1 }
248 greg 1.3 xpos = event_x(&ev);
249     ypos = yres-1 - event_y(&ev);
250    
251 greg 1.1 window_set(canvas, WIN_IGNORE_KBD_EVENT, WIN_ASCII_EVENTS, 0);
252     notify_do_dispatch();
253     putc(COM_GETCUR, stdout);
254     putc(c, stdout);
255     fwrite(&xpos, sizeof(xpos), 1, stdout);
256     fwrite(&ypos, sizeof(ypos), 1, stdout);
257     fflush(stdout);
258     }
259    
260    
261     mygets(s, fp) /* get string from file (with nul) */
262     register char *s;
263     register FILE *fp;
264     {
265     register int c;
266    
267     while ((c = getc(fp)) != EOF)
268     if ((*s++ = c) == '\0')
269     return;
270     *s = '\0';
271     }
272    
273    
274     myputs(s, fp) /* put string to file (with nul) */
275     register char *s;
276     register FILE *fp;
277     {
278     do
279     putc(*s, fp);
280     while (*s++);
281     }
282    
283    
284     ttyputs(s) /* put string out to tty subwindow */
285     register char *s;
286     {
287     char buf[256];
288     register char *cp;
289    
290     for (cp = buf; *s; s++) {
291     if (*s == '\n')
292     *cp++ = '\r';
293     *cp++ = *s;
294     }
295     ttysw_output(tty, buf, cp-buf);
296     }
297    
298    
299 greg 1.4 getmap() /* allocate color map segments */
300 greg 1.1 {
301 greg 1.4 char cmsname[20];
302 greg 1.5 unsigned char red[256], green[256], blue[256];
303 greg 1.1 register Pixwin *pw;
304 greg 1.5 register int i;
305    
306     for (i = 0; i < 256; i++)
307     red[i] = green[i] = blue[i] = 128;
308     red[0] = green[0] = blue[0] = 255;
309     red[1] = green[1] = blue[1] = 0;
310     red[255] = green[255] = blue[255] = 0;
311     red[254] = green[254] = blue[254] = 255;
312     red[253] = green[253] = blue[253] = 0;
313 greg 1.4 /* name shared segment */
314     sprintf(cmsname, "rv%d", getpid());
315     /* set canvas */
316 greg 1.1 pw = canvas_pixwin(canvas);
317 greg 1.4 if (pw->pw_pixrect->pr_depth < 8)
318     return(-1);
319     pw_setcmsname(pw, cmsname);
320 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
321 greg 1.4 /* set tty subwindow */
322 greg 1.1 pw = (Pixwin *)window_get(tty, WIN_PIXWIN);
323 greg 1.4 pw_setcmsname(pw, cmsname);
324 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
325 greg 1.4 /* set frame */
326     pw = (Pixwin *)window_get(frame, WIN_PIXWIN);
327     pw_setcmsname(pw, cmsname);
328 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
329 greg 1.1
330 greg 1.4 return(0);
331 greg 1.1 }
332    
333    
334 greg 1.5 newcolr(ndx, r, g, b) /* enter a color into hardware table */
335     int ndx;
336     unsigned char r, g, b;
337 greg 1.1 {
338     register Pixwin *pw;
339 greg 1.5
340 greg 1.1 pw = canvas_pixwin(canvas);
341 greg 1.5 pw_putcolormap(pw, ndx+FIRSTCOLOR, 1,
342     &r, &g, &b);
343 greg 1.1 pw = (Pixwin *)window_get(tty, WIN_PIXWIN);
344 greg 1.5 pw_putcolormap(pw, ndx+FIRSTCOLOR, 1,
345     &r, &g, &b);
346 greg 1.1 }