ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.14
Committed: Tue Mar 6 17:44:40 1990 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +27 -11 lines
Log Message:
made sundev work with resizing

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