ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.15
Committed: Tue Mar 6 18:22:25 1990 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +21 -6 lines
Log Message:
fixed menu quit and resize handling

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.8 if (frame != 0) {
135     window_set(frame, FRAME_NO_CONFIRM, TRUE, 0);
136     window_destroy(frame);
137 greg 1.1 }
138 greg 1.8 frame = 0;
139 greg 1.1 }
140    
141    
142 greg 1.12 static Notify_value
143     newinput(cid, fd) /* register new input */
144     int (*cid)();
145     int fd;
146     {
147     notify_set_input_func(sun_init, NOTIFY_FUNC_NULL, fd);
148     getc(ttyin);
149     sun_driver.inpready++;
150     return(NOTIFY_DONE);
151     }
152    
153    
154 greg 1.15 static Notify_value
155     my_destroy_func(dest_frame, status) /* say bye-bye */
156     Frame dest_frame;
157     Destroy_status status;
158     {
159     if (status != DESTROY_CHECKING) {
160     frame = 0; /* coordinate with sun_close() */
161     quit(1);
162     }
163     return(notify_next_destroy_func(dest_frame, status));
164     }
165    
166    
167 greg 1.12 static
168 greg 1.14 canvas_resize(canvas, width, height) /* canvas being resized */
169     Canvas canvas;
170     int width, height;
171     {
172 greg 1.15 if (width == xres && height == yres)
173 greg 1.14 return;
174 greg 1.15 sun_driver.xsiz = width;
175     sun_driver.ysiz = height;
176 greg 1.14 strcpy(getcombuf(&sun_driver), "new\n");
177     }
178    
179    
180     static
181 greg 1.8 sun_clear(nwidth, nheight) /* clear our canvas */
182     int nwidth, nheight;
183 greg 1.1 {
184     register Pixwin *pw;
185    
186     pw = canvas_pixwin(canvas);
187     if (nwidth != xres || nheight != yres) {
188 greg 1.15 xres = nwidth;
189     yres = nheight;
190     window_set(frame, WIN_SHOW, FALSE, 0);
191 greg 1.1 window_set(canvas, WIN_WIDTH, nwidth, WIN_HEIGHT, nheight, 0);
192     window_fit(frame);
193     window_set(frame, WIN_SHOW, TRUE, 0);
194     notify_do_dispatch();
195     }
196     pw_writebackground(pw, 0, 0, xres, xres, PIX_SRC);
197 greg 1.6 new_ctab(NCOLORS);
198 greg 1.1 }
199    
200    
201 greg 1.12 static
202 greg 1.8 sun_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
203     COLOR col;
204     int xmin, ymin, xmax, ymax;
205 greg 1.1 {
206 greg 1.6 extern int newcolr();
207 greg 1.1 int pval;
208     register Pixwin *pw;
209    
210 greg 1.6 pval = get_pixel(col, newcolr) + FIRSTCOLOR;
211 greg 1.1 pw = canvas_pixwin(canvas);
212     pw_rop(pw, xmin, yres-ymax, xmax-xmin, ymax-ymin,
213     PIX_SRC|PIX_COLOR(pval), NULL, 0, 0);
214     }
215    
216    
217 greg 1.12 static
218 greg 1.10 sun_comin(buf, prompt) /* input a string from the command line */
219     char *buf, *prompt;
220 greg 1.1 {
221 greg 1.14 extern Notify_value newinput();
222     /* check command buffer */
223 greg 1.10 if (prompt != NULL)
224 greg 1.14 if (fromcombuf(buf, &sun_driver))
225     return;
226     else
227     sun_comout(prompt);
228 greg 1.13 /* await signal */
229     if (sun_driver.inpready <= 0)
230     newinput(sun_init, fileno(ttyin));
231 greg 1.1 /* echo characters */
232     do {
233     mygets(buf, ttyin);
234 greg 1.8 sun_comout(buf);
235 greg 1.1 } while (buf[0]);
236     /* get result */
237     mygets(buf, ttyin);
238 greg 1.12
239     if (sun_driver.inpready > 0)
240     sun_driver.inpready--;
241 greg 1.13 /* reinstall handler */
242 greg 1.12 notify_set_input_func(sun_init, newinput, fileno(ttyin));
243 greg 1.1 }
244    
245    
246 greg 1.12 static int
247 greg 1.8 sun_getcur(xpp, ypp) /* get cursor position */
248     int *xpp, *ypp;
249 greg 1.1 {
250 greg 1.3 Event ev;
251 greg 1.1 int c;
252    
253     notify_no_dispatch(); /* allow read to block */
254     window_set(canvas, WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS, 0);
255 greg 1.3 again:
256     if (window_read_event(canvas, &ev) == -1) {
257     notify_perror();
258 greg 1.8 stderr_v("window event read error\n");
259     quit(1);
260 greg 1.3 }
261     c = event_id(&ev);
262     switch (c) {
263     case MS_LEFT:
264     c = MB1;
265 greg 1.1 break;
266 greg 1.3 case MS_MIDDLE:
267     c = MB2;
268     break;
269     case MS_RIGHT:
270     c = MB3;
271     break;
272     default:
273     if (c < ASCII_FIRST || c > ASCII_LAST)
274     goto again;
275     break;
276 greg 1.1 }
277 greg 1.8 *xpp = event_x(&ev);
278     *ypp = yres-1 - event_y(&ev);
279 greg 1.3
280 greg 1.1 window_set(canvas, WIN_IGNORE_KBD_EVENT, WIN_ASCII_EVENTS, 0);
281     notify_do_dispatch();
282 greg 1.8 return(c);
283 greg 1.1 }
284    
285    
286 greg 1.12 static
287 greg 1.8 sun_comout(s) /* put string out to tty subwindow */
288 greg 1.1 register char *s;
289     {
290     char buf[256];
291     register char *cp;
292    
293     for (cp = buf; *s; s++) {
294     if (*s == '\n')
295     *cp++ = '\r';
296     *cp++ = *s;
297     }
298 greg 1.8 /* must be a single call */
299 greg 1.1 ttysw_output(tty, buf, cp-buf);
300     }
301    
302    
303 greg 1.12 static
304 greg 1.4 getmap() /* allocate color map segments */
305 greg 1.1 {
306 greg 1.4 char cmsname[20];
307 greg 1.5 unsigned char red[256], green[256], blue[256];
308 greg 1.1 register Pixwin *pw;
309 greg 1.5 register int i;
310    
311     for (i = 0; i < 256; i++)
312     red[i] = green[i] = blue[i] = 128;
313     red[0] = green[0] = blue[0] = 255;
314     red[1] = green[1] = blue[1] = 0;
315     red[255] = green[255] = blue[255] = 0;
316     red[254] = green[254] = blue[254] = 255;
317     red[253] = green[253] = blue[253] = 0;
318 greg 1.4 /* name shared segment */
319     sprintf(cmsname, "rv%d", getpid());
320     /* set canvas */
321 greg 1.1 pw = canvas_pixwin(canvas);
322 greg 1.4 if (pw->pw_pixrect->pr_depth < 8)
323     return(-1);
324     pw_setcmsname(pw, cmsname);
325 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
326 greg 1.4 /* set tty subwindow */
327 greg 1.1 pw = (Pixwin *)window_get(tty, WIN_PIXWIN);
328 greg 1.4 pw_setcmsname(pw, cmsname);
329 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
330 greg 1.4 /* set frame */
331     pw = (Pixwin *)window_get(frame, WIN_PIXWIN);
332     pw_setcmsname(pw, cmsname);
333 greg 1.5 pw_putcolormap(pw, 0, 256, red, green, blue);
334 greg 1.1
335 greg 1.4 return(0);
336 greg 1.1 }
337    
338    
339 greg 1.12 static
340 greg 1.5 newcolr(ndx, r, g, b) /* enter a color into hardware table */
341     int ndx;
342     unsigned char r, g, b;
343 greg 1.1 {
344     register Pixwin *pw;
345 greg 1.5
346 greg 1.1 pw = canvas_pixwin(canvas);
347 greg 1.5 pw_putcolormap(pw, ndx+FIRSTCOLOR, 1,
348     &r, &g, &b);
349 greg 1.1 pw = (Pixwin *)window_get(tty, WIN_PIXWIN);
350 greg 1.5 pw_putcolormap(pw, ndx+FIRSTCOLOR, 1,
351     &r, &g, &b);
352 greg 1.1 }