ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/sundev.c
Revision: 1.12
Committed: Thu Feb 22 12:39:27 1990 UTC (35 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +33 -8 lines
Log Message:
Changed devcomm so that flush() is used to check for input

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