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 |
#define GAMMA 2.2 /* exponent for color correction */ |
33 |
|
34 |
#define COMLH 3 /* number of command lines */ |
35 |
|
36 |
#define FIRSTCOLOR 2 /* first usable entry */ |
37 |
#define NCOLORS 251 /* number of usable colors */ |
38 |
|
39 |
#define CWIDTH 1100 /* starting canvas width */ |
40 |
#define CHEIGHT 800 /* starting canvas height */ |
41 |
|
42 |
int sun_close(), sun_clear(), sun_paintr(), sun_getcur(), |
43 |
sun_comout(), sun_comin(); |
44 |
|
45 |
static struct driver sun_driver = { |
46 |
sun_close, sun_clear, sun_paintr, |
47 |
sun_getcur, sun_comout, sun_comin, |
48 |
NULL, 1.0, CWIDTH, CHEIGHT |
49 |
}; |
50 |
|
51 |
static FILE *ttyin; |
52 |
|
53 |
static Frame frame = 0; |
54 |
static Tty tty = 0; |
55 |
static Canvas canvas = 0; |
56 |
|
57 |
static int xres = 0, yres = 0; |
58 |
|
59 |
extern char *progname; |
60 |
|
61 |
|
62 |
struct driver * |
63 |
sun_init(name, id) /* initialize SunView */ |
64 |
char *name, *id; |
65 |
{ |
66 |
extern Notify_value newinput(), my_destroy_func(); |
67 |
extern int canvas_resize(); |
68 |
char *ttyargv[3], arg1[16]; |
69 |
int pd[2]; |
70 |
int com; |
71 |
|
72 |
frame = window_create(0, FRAME, |
73 |
FRAME_LABEL, id==NULL ? name : id, |
74 |
FRAME_ICON, &sun_icon, |
75 |
WIN_X, 0, WIN_Y, 0, |
76 |
0); |
77 |
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 |
sprintf(arg1, "%d", pd[1]); |
86 |
ttyargv[0] = TTYPROG; |
87 |
ttyargv[1] = arg1; |
88 |
ttyargv[2] = NULL; |
89 |
#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 |
TTY_QUIT_ON_CHILD_DEATH, TRUE, |
96 |
0); |
97 |
if (tty == 0) { |
98 |
stderr_v("cannot create tty subwindow\n"); |
99 |
return(NULL); |
100 |
} |
101 |
close(pd[1]); |
102 |
if ((ttyin = fdopen(pd[0], "r")) == NULL) { |
103 |
stderr_v("cannot open tty\n"); |
104 |
return(NULL); |
105 |
} |
106 |
notify_set_input_func(sun_init, newinput, pd[0]); |
107 |
canvas = window_create(frame, CANVAS, |
108 |
CANVAS_RETAINED, FALSE, |
109 |
CANVAS_RESIZE_PROC, canvas_resize, |
110 |
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 |
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 |
make_gmap(GAMMA); |
123 |
window_set(canvas, CANVAS_RETAINED, TRUE, 0); |
124 |
|
125 |
notify_interpose_destroy_func(frame, my_destroy_func); |
126 |
sun_driver.inpready = 0; |
127 |
return(&sun_driver); |
128 |
} |
129 |
|
130 |
|
131 |
static |
132 |
sun_close() /* all done */ |
133 |
{ |
134 |
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 |
} |
142 |
} |
143 |
|
144 |
|
145 |
static Notify_value |
146 |
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 |
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 |
} |
166 |
|
167 |
|
168 |
static |
169 |
canvas_resize(canvas, width, height) /* canvas being resized */ |
170 |
Canvas canvas; |
171 |
int width, height; |
172 |
{ |
173 |
if (width == xres && height == yres) |
174 |
return; |
175 |
sun_driver.xsiz = width; |
176 |
sun_driver.ysiz = height; |
177 |
strcpy(getcombuf(&sun_driver), "new\n"); |
178 |
} |
179 |
|
180 |
|
181 |
static |
182 |
sun_clear(nwidth, nheight) /* clear our canvas */ |
183 |
int nwidth, nheight; |
184 |
{ |
185 |
register Pixwin *pw; |
186 |
|
187 |
pw = canvas_pixwin(canvas); |
188 |
if (nwidth != xres || nheight != yres) { |
189 |
xres = nwidth; |
190 |
yres = nheight; |
191 |
window_set(frame, WIN_SHOW, FALSE, 0); |
192 |
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 |
new_ctab(NCOLORS); |
199 |
} |
200 |
|
201 |
|
202 |
static |
203 |
sun_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
204 |
COLOR col; |
205 |
int xmin, ymin, xmax, ymax; |
206 |
{ |
207 |
extern int newcolr(); |
208 |
int pval; |
209 |
register Pixwin *pw; |
210 |
|
211 |
pval = get_pixel(col, newcolr) + FIRSTCOLOR; |
212 |
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 |
static |
219 |
sun_comin(buf, prompt) /* input a string from the command line */ |
220 |
char *buf, *prompt; |
221 |
{ |
222 |
extern Notify_value newinput(); |
223 |
/* check command buffer */ |
224 |
if (prompt != NULL) |
225 |
if (fromcombuf(buf, &sun_driver)) |
226 |
return; |
227 |
else |
228 |
sun_comout(prompt); |
229 |
/* await signal */ |
230 |
if (sun_driver.inpready <= 0) |
231 |
newinput(sun_init, fileno(ttyin)); |
232 |
/* echo characters */ |
233 |
do { |
234 |
mygets(buf, ttyin); |
235 |
sun_comout(buf); |
236 |
} while (buf[0]); |
237 |
/* get result */ |
238 |
mygets(buf, ttyin); |
239 |
|
240 |
if (sun_driver.inpready > 0) |
241 |
sun_driver.inpready--; |
242 |
/* reinstall handler */ |
243 |
notify_set_input_func(sun_init, newinput, fileno(ttyin)); |
244 |
} |
245 |
|
246 |
|
247 |
static int |
248 |
sun_getcur(xpp, ypp) /* get cursor position */ |
249 |
int *xpp, *ypp; |
250 |
{ |
251 |
Event ev; |
252 |
int c; |
253 |
|
254 |
notify_no_dispatch(); /* allow read to block */ |
255 |
window_set(canvas, WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
256 |
again: |
257 |
if (window_read_event(canvas, &ev) == -1) { |
258 |
notify_perror(); |
259 |
stderr_v("window event read error\n"); |
260 |
quit(1); |
261 |
} |
262 |
c = event_id(&ev); |
263 |
switch (c) { |
264 |
case MS_LEFT: |
265 |
c = MB1; |
266 |
break; |
267 |
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 |
} |
278 |
*xpp = event_x(&ev); |
279 |
*ypp = yres-1 - event_y(&ev); |
280 |
|
281 |
window_set(canvas, WIN_IGNORE_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
282 |
notify_do_dispatch(); |
283 |
return(c); |
284 |
} |
285 |
|
286 |
|
287 |
static |
288 |
sun_comout(s) /* put string out to tty subwindow */ |
289 |
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 |
/* must be a single call */ |
300 |
ttysw_output(tty, buf, cp-buf); |
301 |
} |
302 |
|
303 |
|
304 |
static |
305 |
getmap() /* allocate color map segments */ |
306 |
{ |
307 |
char cmsname[20]; |
308 |
unsigned char red[256], green[256], blue[256]; |
309 |
register Pixwin *pw; |
310 |
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 |
/* name shared segment */ |
320 |
sprintf(cmsname, "rv%d", getpid()); |
321 |
/* set canvas */ |
322 |
pw = canvas_pixwin(canvas); |
323 |
if (pw->pw_pixrect->pr_depth < 8) |
324 |
return(-1); |
325 |
pw_setcmsname(pw, cmsname); |
326 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
327 |
/* set tty subwindow */ |
328 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
329 |
pw_setcmsname(pw, cmsname); |
330 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
331 |
/* set frame */ |
332 |
pw = (Pixwin *)window_get(frame, WIN_PIXWIN); |
333 |
pw_setcmsname(pw, cmsname); |
334 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
335 |
|
336 |
return(0); |
337 |
} |
338 |
|
339 |
|
340 |
static |
341 |
newcolr(ndx, r, g, b) /* enter a color into hardware table */ |
342 |
int ndx; |
343 |
unsigned char r, g, b; |
344 |
{ |
345 |
register Pixwin *pw; |
346 |
|
347 |
pw = canvas_pixwin(canvas); |
348 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
349 |
&r, &g, &b); |
350 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
351 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
352 |
&r, &g, &b); |
353 |
} |