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 |
int sun_close(), sun_clear(), sun_paintr(), sun_getcur(), |
40 |
sun_comout(), sun_comin(); |
41 |
|
42 |
static struct driver sun_driver = { |
43 |
sun_close, sun_clear, sun_paintr, |
44 |
sun_getcur, sun_comout, sun_comin, |
45 |
NULL, 1.0, 1100, 800 |
46 |
}; |
47 |
|
48 |
static FILE *ttyin; |
49 |
|
50 |
static Frame frame = 0; |
51 |
static Tty tty = 0; |
52 |
static Canvas canvas = 0; |
53 |
|
54 |
static int xres = 0, yres = 0; |
55 |
|
56 |
extern char *progname; |
57 |
|
58 |
|
59 |
struct driver * |
60 |
sun_init(name, id) /* initialize SunView */ |
61 |
char *name, *id; |
62 |
{ |
63 |
extern Notify_value newinput(); |
64 |
char *ttyargv[3], arg1[16]; |
65 |
int pd[2]; |
66 |
int com; |
67 |
|
68 |
frame = window_create(0, FRAME, |
69 |
FRAME_LABEL, id==NULL ? name : id, |
70 |
FRAME_ICON, &sun_icon, |
71 |
WIN_X, 0, WIN_Y, 0, |
72 |
0); |
73 |
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 |
sprintf(arg1, "%d", pd[1]); |
82 |
ttyargv[0] = TTYPROG; |
83 |
ttyargv[1] = arg1; |
84 |
ttyargv[2] = NULL; |
85 |
#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 |
TTY_QUIT_ON_CHILD_DEATH, TRUE, |
92 |
0); |
93 |
if (tty == 0) { |
94 |
stderr_v("cannot create tty subwindow\n"); |
95 |
return(NULL); |
96 |
} |
97 |
close(pd[1]); |
98 |
if ((ttyin = fdopen(pd[0], "r")) == NULL) { |
99 |
stderr_v("cannot open tty\n"); |
100 |
return(NULL); |
101 |
} |
102 |
notify_set_input_func(sun_init, newinput, pd[0]); |
103 |
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 |
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 |
make_gmap(GAMMA); |
118 |
window_set(canvas, CANVAS_RETAINED, TRUE, 0); |
119 |
|
120 |
sun_driver.inpready = 0; |
121 |
return(&sun_driver); |
122 |
} |
123 |
|
124 |
|
125 |
static |
126 |
sun_close() /* all done */ |
127 |
{ |
128 |
if (frame != 0) { |
129 |
window_set(frame, FRAME_NO_CONFIRM, TRUE, 0); |
130 |
window_destroy(frame); |
131 |
} |
132 |
frame = 0; |
133 |
} |
134 |
|
135 |
|
136 |
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 |
sun_clear(nwidth, nheight) /* clear our canvas */ |
150 |
int nwidth, nheight; |
151 |
{ |
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 |
new_ctab(NCOLORS); |
170 |
} |
171 |
|
172 |
|
173 |
static |
174 |
sun_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
175 |
COLOR col; |
176 |
int xmin, ymin, xmax, ymax; |
177 |
{ |
178 |
extern int newcolr(); |
179 |
int pval; |
180 |
register Pixwin *pw; |
181 |
|
182 |
pval = get_pixel(col, newcolr) + FIRSTCOLOR; |
183 |
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 |
static |
190 |
sun_comin(buf, prompt) /* input a string from the command line */ |
191 |
char *buf, *prompt; |
192 |
{ |
193 |
Notify_value newinput(); |
194 |
|
195 |
if (prompt != NULL) |
196 |
sun_comout(prompt); |
197 |
/* echo characters */ |
198 |
do { |
199 |
mygets(buf, ttyin); |
200 |
sun_comout(buf); |
201 |
} while (buf[0]); |
202 |
/* get result */ |
203 |
mygets(buf, ttyin); |
204 |
|
205 |
if (sun_driver.inpready > 0) |
206 |
sun_driver.inpready--; |
207 |
|
208 |
notify_set_input_func(sun_init, newinput, fileno(ttyin)); |
209 |
} |
210 |
|
211 |
|
212 |
static int |
213 |
sun_getcur(xpp, ypp) /* get cursor position */ |
214 |
int *xpp, *ypp; |
215 |
{ |
216 |
Event ev; |
217 |
int c; |
218 |
|
219 |
notify_no_dispatch(); /* allow read to block */ |
220 |
window_set(canvas, WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
221 |
again: |
222 |
if (window_read_event(canvas, &ev) == -1) { |
223 |
notify_perror(); |
224 |
stderr_v("window event read error\n"); |
225 |
quit(1); |
226 |
} |
227 |
c = event_id(&ev); |
228 |
switch (c) { |
229 |
case MS_LEFT: |
230 |
c = MB1; |
231 |
break; |
232 |
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 |
} |
243 |
*xpp = event_x(&ev); |
244 |
*ypp = yres-1 - event_y(&ev); |
245 |
|
246 |
window_set(canvas, WIN_IGNORE_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
247 |
notify_do_dispatch(); |
248 |
return(c); |
249 |
} |
250 |
|
251 |
|
252 |
static |
253 |
sun_comout(s) /* put string out to tty subwindow */ |
254 |
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 |
/* must be a single call */ |
265 |
ttysw_output(tty, buf, cp-buf); |
266 |
} |
267 |
|
268 |
|
269 |
static |
270 |
getmap() /* allocate color map segments */ |
271 |
{ |
272 |
char cmsname[20]; |
273 |
unsigned char red[256], green[256], blue[256]; |
274 |
register Pixwin *pw; |
275 |
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 |
/* name shared segment */ |
285 |
sprintf(cmsname, "rv%d", getpid()); |
286 |
/* set canvas */ |
287 |
pw = canvas_pixwin(canvas); |
288 |
if (pw->pw_pixrect->pr_depth < 8) |
289 |
return(-1); |
290 |
pw_setcmsname(pw, cmsname); |
291 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
292 |
/* set tty subwindow */ |
293 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
294 |
pw_setcmsname(pw, cmsname); |
295 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
296 |
/* set frame */ |
297 |
pw = (Pixwin *)window_get(frame, WIN_PIXWIN); |
298 |
pw_setcmsname(pw, cmsname); |
299 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
300 |
|
301 |
return(0); |
302 |
} |
303 |
|
304 |
|
305 |
static |
306 |
newcolr(ndx, r, g, b) /* enter a color into hardware table */ |
307 |
int ndx; |
308 |
unsigned char r, g, b; |
309 |
{ |
310 |
register Pixwin *pw; |
311 |
|
312 |
pw = canvas_pixwin(canvas); |
313 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
314 |
&r, &g, &b); |
315 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
316 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
317 |
&r, &g, &b); |
318 |
} |