ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.16
Committed: Tue Mar 6 18:41:34 1990 UTC (35 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.15: +18 -17 lines
Log Message:
final fixes for menu quit

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