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