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 |
gregl |
2.6 |
#include "standard.h" |
14 |
greg |
1.1 |
#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 |
greg |
1.4 |
#define GAMMA 2.2 /* exponent for color correction */ |
32 |
greg |
1.1 |
|
33 |
|
|
#define COMLH 3 /* number of command lines */ |
34 |
|
|
|
35 |
|
|
#define FIRSTCOLOR 2 /* first usable entry */ |
36 |
greg |
1.5 |
#define NCOLORS 251 /* number of usable colors */ |
37 |
greg |
1.1 |
|
38 |
greg |
1.14 |
#define CWIDTH 1100 /* starting canvas width */ |
39 |
|
|
#define CHEIGHT 800 /* starting canvas height */ |
40 |
|
|
|
41 |
greg |
1.17 |
static int sun_close(), sun_clear(), sun_paintr(), sun_getcur(), |
42 |
greg |
1.1 |
sun_comout(), sun_comin(); |
43 |
|
|
|
44 |
greg |
1.8 |
static struct driver sun_driver = { |
45 |
|
|
sun_close, sun_clear, sun_paintr, |
46 |
|
|
sun_getcur, sun_comout, sun_comin, |
47 |
greg |
1.14 |
NULL, 1.0, CWIDTH, CHEIGHT |
48 |
greg |
1.1 |
}; |
49 |
|
|
|
50 |
greg |
1.8 |
static FILE *ttyin; |
51 |
greg |
1.1 |
|
52 |
greg |
1.8 |
static Frame frame = 0; |
53 |
|
|
static Tty tty = 0; |
54 |
|
|
static Canvas canvas = 0; |
55 |
greg |
1.1 |
|
56 |
greg |
1.8 |
static int xres = 0, yres = 0; |
57 |
greg |
1.1 |
|
58 |
greg |
1.8 |
extern char *progname; |
59 |
greg |
1.1 |
|
60 |
|
|
|
61 |
greg |
1.8 |
struct driver * |
62 |
greg |
1.12 |
sun_init(name, id) /* initialize SunView */ |
63 |
greg |
1.8 |
char *name, *id; |
64 |
greg |
1.1 |
{ |
65 |
greg |
1.15 |
extern Notify_value newinput(), my_destroy_func(); |
66 |
greg |
1.14 |
extern int canvas_resize(); |
67 |
greg |
2.2 |
extern char *getenv(); |
68 |
|
|
char *gv; |
69 |
greg |
1.12 |
char *ttyargv[3], arg1[16]; |
70 |
greg |
1.1 |
int pd[2]; |
71 |
|
|
int com; |
72 |
|
|
|
73 |
|
|
frame = window_create(0, FRAME, |
74 |
greg |
1.8 |
FRAME_LABEL, id==NULL ? name : id, |
75 |
greg |
1.1 |
FRAME_ICON, &sun_icon, |
76 |
|
|
WIN_X, 0, WIN_Y, 0, |
77 |
|
|
0); |
78 |
greg |
1.8 |
if (frame == 0) { |
79 |
gregl |
2.6 |
eputs("cannot create frame\n"); |
80 |
greg |
1.8 |
return(NULL); |
81 |
|
|
} |
82 |
|
|
if (pipe(pd) == -1) { |
83 |
gregl |
2.6 |
eputs("cannot create pipe\n"); |
84 |
greg |
1.8 |
return(NULL); |
85 |
|
|
} |
86 |
greg |
1.12 |
sprintf(arg1, "%d", pd[1]); |
87 |
greg |
1.1 |
ttyargv[0] = TTYPROG; |
88 |
|
|
ttyargv[1] = arg1; |
89 |
greg |
1.12 |
ttyargv[2] = NULL; |
90 |
greg |
1.1 |
#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 |
greg |
1.8 |
TTY_QUIT_ON_CHILD_DEATH, TRUE, |
97 |
greg |
1.1 |
0); |
98 |
greg |
1.8 |
if (tty == 0) { |
99 |
gregl |
2.6 |
eputs("cannot create tty subwindow\n"); |
100 |
greg |
1.8 |
return(NULL); |
101 |
|
|
} |
102 |
greg |
1.1 |
close(pd[1]); |
103 |
greg |
1.8 |
if ((ttyin = fdopen(pd[0], "r")) == NULL) { |
104 |
gregl |
2.6 |
eputs("cannot open tty\n"); |
105 |
greg |
1.8 |
return(NULL); |
106 |
|
|
} |
107 |
greg |
1.12 |
notify_set_input_func(sun_init, newinput, pd[0]); |
108 |
greg |
1.1 |
canvas = window_create(frame, CANVAS, |
109 |
|
|
CANVAS_RETAINED, FALSE, |
110 |
greg |
1.14 |
CANVAS_RESIZE_PROC, canvas_resize, |
111 |
greg |
1.1 |
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 |
greg |
1.8 |
if (canvas == 0) { |
116 |
gregl |
2.6 |
eputs("cannot create canvas\n"); |
117 |
greg |
1.8 |
return(NULL); |
118 |
|
|
} |
119 |
|
|
if (getmap() < 0) { |
120 |
gregl |
2.6 |
eputs("not a color screen\n"); |
121 |
greg |
1.8 |
return(NULL); |
122 |
|
|
} |
123 |
greg |
2.4 |
if ((gv = getenv("DISPLAY_GAMMA")) != NULL) |
124 |
greg |
2.2 |
make_gmap(atof(gv)); |
125 |
|
|
else |
126 |
|
|
make_gmap(GAMMA); |
127 |
greg |
1.1 |
window_set(canvas, CANVAS_RETAINED, TRUE, 0); |
128 |
|
|
|
129 |
greg |
1.15 |
notify_interpose_destroy_func(frame, my_destroy_func); |
130 |
greg |
1.8 |
sun_driver.inpready = 0; |
131 |
|
|
return(&sun_driver); |
132 |
greg |
1.1 |
} |
133 |
|
|
|
134 |
|
|
|
135 |
greg |
1.12 |
static |
136 |
greg |
1.8 |
sun_close() /* all done */ |
137 |
greg |
1.1 |
{ |
138 |
greg |
1.16 |
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 |
greg |
1.1 |
} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
greg |
1.12 |
static Notify_value |
150 |
greg |
1.16 |
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 |
greg |
1.12 |
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 |
greg |
1.15 |
} |
170 |
|
|
|
171 |
|
|
|
172 |
greg |
1.12 |
static |
173 |
greg |
1.14 |
canvas_resize(canvas, width, height) /* canvas being resized */ |
174 |
|
|
Canvas canvas; |
175 |
|
|
int width, height; |
176 |
|
|
{ |
177 |
greg |
1.15 |
if (width == xres && height == yres) |
178 |
greg |
1.14 |
return; |
179 |
greg |
1.15 |
sun_driver.xsiz = width; |
180 |
|
|
sun_driver.ysiz = height; |
181 |
greg |
2.5 |
tocombuf("new\n", &sun_driver); |
182 |
greg |
1.14 |
} |
183 |
|
|
|
184 |
|
|
|
185 |
|
|
static |
186 |
greg |
1.8 |
sun_clear(nwidth, nheight) /* clear our canvas */ |
187 |
|
|
int nwidth, nheight; |
188 |
greg |
1.1 |
{ |
189 |
|
|
register Pixwin *pw; |
190 |
|
|
|
191 |
|
|
pw = canvas_pixwin(canvas); |
192 |
|
|
if (nwidth != xres || nheight != yres) { |
193 |
greg |
1.15 |
xres = nwidth; |
194 |
|
|
yres = nheight; |
195 |
|
|
window_set(frame, WIN_SHOW, FALSE, 0); |
196 |
greg |
1.1 |
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 |
greg |
1.6 |
new_ctab(NCOLORS); |
203 |
greg |
1.1 |
} |
204 |
|
|
|
205 |
|
|
|
206 |
greg |
1.12 |
static |
207 |
greg |
1.8 |
sun_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
208 |
|
|
COLOR col; |
209 |
|
|
int xmin, ymin, xmax, ymax; |
210 |
greg |
1.1 |
{ |
211 |
greg |
1.6 |
extern int newcolr(); |
212 |
greg |
1.1 |
int pval; |
213 |
|
|
register Pixwin *pw; |
214 |
|
|
|
215 |
greg |
1.6 |
pval = get_pixel(col, newcolr) + FIRSTCOLOR; |
216 |
greg |
1.1 |
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 |
greg |
1.12 |
static |
223 |
greg |
1.10 |
sun_comin(buf, prompt) /* input a string from the command line */ |
224 |
|
|
char *buf, *prompt; |
225 |
greg |
1.1 |
{ |
226 |
greg |
1.14 |
extern Notify_value newinput(); |
227 |
|
|
/* check command buffer */ |
228 |
greg |
1.10 |
if (prompt != NULL) |
229 |
greg |
1.14 |
if (fromcombuf(buf, &sun_driver)) |
230 |
|
|
return; |
231 |
|
|
else |
232 |
|
|
sun_comout(prompt); |
233 |
greg |
1.13 |
/* await signal */ |
234 |
|
|
if (sun_driver.inpready <= 0) |
235 |
|
|
newinput(sun_init, fileno(ttyin)); |
236 |
greg |
1.1 |
/* echo characters */ |
237 |
|
|
do { |
238 |
|
|
mygets(buf, ttyin); |
239 |
greg |
1.8 |
sun_comout(buf); |
240 |
greg |
1.1 |
} while (buf[0]); |
241 |
|
|
/* get result */ |
242 |
|
|
mygets(buf, ttyin); |
243 |
greg |
1.12 |
|
244 |
|
|
if (sun_driver.inpready > 0) |
245 |
|
|
sun_driver.inpready--; |
246 |
greg |
1.13 |
/* reinstall handler */ |
247 |
greg |
1.12 |
notify_set_input_func(sun_init, newinput, fileno(ttyin)); |
248 |
greg |
1.1 |
} |
249 |
|
|
|
250 |
|
|
|
251 |
greg |
1.12 |
static int |
252 |
greg |
1.8 |
sun_getcur(xpp, ypp) /* get cursor position */ |
253 |
|
|
int *xpp, *ypp; |
254 |
greg |
1.1 |
{ |
255 |
greg |
1.3 |
Event ev; |
256 |
greg |
1.1 |
int c; |
257 |
|
|
|
258 |
|
|
notify_no_dispatch(); /* allow read to block */ |
259 |
|
|
window_set(canvas, WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
260 |
greg |
1.3 |
again: |
261 |
|
|
if (window_read_event(canvas, &ev) == -1) { |
262 |
|
|
notify_perror(); |
263 |
gregl |
2.6 |
eputs("window event read error\n"); |
264 |
greg |
1.8 |
quit(1); |
265 |
greg |
1.3 |
} |
266 |
|
|
c = event_id(&ev); |
267 |
|
|
switch (c) { |
268 |
|
|
case MS_LEFT: |
269 |
|
|
c = MB1; |
270 |
greg |
1.1 |
break; |
271 |
greg |
1.3 |
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 |
greg |
1.1 |
} |
282 |
greg |
1.8 |
*xpp = event_x(&ev); |
283 |
|
|
*ypp = yres-1 - event_y(&ev); |
284 |
greg |
1.3 |
|
285 |
greg |
1.1 |
window_set(canvas, WIN_IGNORE_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
286 |
|
|
notify_do_dispatch(); |
287 |
greg |
1.8 |
return(c); |
288 |
greg |
1.1 |
} |
289 |
|
|
|
290 |
|
|
|
291 |
greg |
1.12 |
static |
292 |
greg |
1.8 |
sun_comout(s) /* put string out to tty subwindow */ |
293 |
greg |
1.1 |
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 |
greg |
1.8 |
/* must be a single call */ |
304 |
greg |
1.1 |
ttysw_output(tty, buf, cp-buf); |
305 |
|
|
} |
306 |
|
|
|
307 |
|
|
|
308 |
greg |
1.12 |
static |
309 |
greg |
1.4 |
getmap() /* allocate color map segments */ |
310 |
greg |
1.1 |
{ |
311 |
greg |
1.4 |
char cmsname[20]; |
312 |
greg |
1.5 |
unsigned char red[256], green[256], blue[256]; |
313 |
greg |
1.1 |
register Pixwin *pw; |
314 |
greg |
1.5 |
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 |
greg |
1.4 |
/* name shared segment */ |
324 |
|
|
sprintf(cmsname, "rv%d", getpid()); |
325 |
|
|
/* set canvas */ |
326 |
greg |
1.1 |
pw = canvas_pixwin(canvas); |
327 |
greg |
1.4 |
if (pw->pw_pixrect->pr_depth < 8) |
328 |
|
|
return(-1); |
329 |
|
|
pw_setcmsname(pw, cmsname); |
330 |
greg |
1.5 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
331 |
greg |
1.4 |
/* set tty subwindow */ |
332 |
greg |
1.1 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
333 |
greg |
1.4 |
pw_setcmsname(pw, cmsname); |
334 |
greg |
1.5 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
335 |
greg |
1.4 |
/* set frame */ |
336 |
|
|
pw = (Pixwin *)window_get(frame, WIN_PIXWIN); |
337 |
|
|
pw_setcmsname(pw, cmsname); |
338 |
greg |
1.5 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
339 |
greg |
1.1 |
|
340 |
greg |
1.4 |
return(0); |
341 |
greg |
1.1 |
} |
342 |
|
|
|
343 |
|
|
|
344 |
greg |
1.12 |
static |
345 |
greg |
1.5 |
newcolr(ndx, r, g, b) /* enter a color into hardware table */ |
346 |
|
|
int ndx; |
347 |
|
|
unsigned char r, g, b; |
348 |
greg |
1.1 |
{ |
349 |
|
|
register Pixwin *pw; |
350 |
greg |
1.5 |
|
351 |
greg |
1.1 |
pw = canvas_pixwin(canvas); |
352 |
greg |
1.5 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
353 |
|
|
&r, &g, &b); |
354 |
greg |
1.1 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
355 |
greg |
1.5 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
356 |
|
|
&r, &g, &b); |
357 |
greg |
1.1 |
} |