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 |
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 |
dinit(name, id) /* initialize SunView (independent) */ |
61 |
char *name, *id; |
62 |
{ |
63 |
extern Notify_value my_notice_destroy(); |
64 |
char *ttyargv[4], arg1[16], arg2[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", getppid()); |
82 |
sprintf(arg2, "%d", pd[1]); |
83 |
ttyargv[0] = TTYPROG; |
84 |
ttyargv[1] = arg1; |
85 |
ttyargv[2] = arg2; |
86 |
ttyargv[3] = NULL; |
87 |
#ifdef BSD |
88 |
fcntl(pd[0], F_SETFD, 1); |
89 |
#endif |
90 |
tty = window_create(frame, TTY, |
91 |
TTY_ARGV, ttyargv, |
92 |
WIN_ROWS, COMLH, |
93 |
TTY_QUIT_ON_CHILD_DEATH, TRUE, |
94 |
0); |
95 |
if (tty == 0) { |
96 |
stderr_v("cannot create tty subwindow\n"); |
97 |
return(NULL); |
98 |
} |
99 |
close(pd[1]); |
100 |
if ((ttyin = fdopen(pd[0], "r")) == NULL) { |
101 |
stderr_v("cannot open tty\n"); |
102 |
return(NULL); |
103 |
} |
104 |
canvas = window_create(frame, CANVAS, |
105 |
CANVAS_RETAINED, FALSE, |
106 |
WIN_INPUT_DESIGNEE, window_get(tty,WIN_DEVICE_NUMBER), |
107 |
WIN_CONSUME_PICK_EVENTS, WIN_NO_EVENTS, |
108 |
MS_LEFT, MS_MIDDLE, MS_RIGHT, 0, |
109 |
0); |
110 |
if (canvas == 0) { |
111 |
stderr_v("cannot create canvas\n"); |
112 |
return(NULL); |
113 |
} |
114 |
if (getmap() < 0) { |
115 |
stderr_v("not a color screen\n"); |
116 |
return(NULL); |
117 |
} |
118 |
make_gmap(GAMMA); |
119 |
window_set(canvas, CANVAS_RETAINED, TRUE, 0); |
120 |
|
121 |
sun_driver.inpready = 0; |
122 |
return(&sun_driver); |
123 |
} |
124 |
|
125 |
|
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 |
sun_clear(nwidth, nheight) /* clear our canvas */ |
137 |
int nwidth, nheight; |
138 |
{ |
139 |
register Pixwin *pw; |
140 |
|
141 |
pw = canvas_pixwin(canvas); |
142 |
if (nwidth != xres || nheight != yres) { |
143 |
window_set(frame, WIN_SHOW, FALSE, 0); |
144 |
window_set(canvas, CANVAS_AUTO_SHRINK, TRUE, |
145 |
CANVAS_AUTO_EXPAND, TRUE, 0); |
146 |
window_set(canvas, WIN_WIDTH, nwidth, WIN_HEIGHT, nheight, 0); |
147 |
window_set(canvas, CANVAS_AUTO_SHRINK, FALSE, |
148 |
CANVAS_AUTO_EXPAND, FALSE, 0); |
149 |
window_fit(frame); |
150 |
window_set(frame, WIN_SHOW, TRUE, 0); |
151 |
notify_do_dispatch(); |
152 |
xres = nwidth; |
153 |
yres = nheight; |
154 |
} |
155 |
pw_writebackground(pw, 0, 0, xres, xres, PIX_SRC); |
156 |
new_ctab(NCOLORS); |
157 |
} |
158 |
|
159 |
|
160 |
sun_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
161 |
COLOR col; |
162 |
int xmin, ymin, xmax, ymax; |
163 |
{ |
164 |
extern int newcolr(); |
165 |
int pval; |
166 |
register Pixwin *pw; |
167 |
|
168 |
pval = get_pixel(col, newcolr) + FIRSTCOLOR; |
169 |
pw = canvas_pixwin(canvas); |
170 |
pw_rop(pw, xmin, yres-ymax, xmax-xmin, ymax-ymin, |
171 |
PIX_SRC|PIX_COLOR(pval), NULL, 0, 0); |
172 |
} |
173 |
|
174 |
|
175 |
sun_comin(buf, prompt) /* input a string from the command line */ |
176 |
char *buf, *prompt; |
177 |
{ |
178 |
if (prompt != NULL) |
179 |
sun_comout(prompt); |
180 |
/* echo characters */ |
181 |
do { |
182 |
mygets(buf, ttyin); |
183 |
sun_comout(buf); |
184 |
} while (buf[0]); |
185 |
/* get result */ |
186 |
mygets(buf, ttyin); |
187 |
} |
188 |
|
189 |
|
190 |
int |
191 |
sun_getcur(xpp, ypp) /* get cursor position */ |
192 |
int *xpp, *ypp; |
193 |
{ |
194 |
Event ev; |
195 |
int c; |
196 |
|
197 |
notify_no_dispatch(); /* allow read to block */ |
198 |
window_set(canvas, WIN_CONSUME_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
199 |
again: |
200 |
if (window_read_event(canvas, &ev) == -1) { |
201 |
notify_perror(); |
202 |
stderr_v("window event read error\n"); |
203 |
quit(1); |
204 |
} |
205 |
c = event_id(&ev); |
206 |
switch (c) { |
207 |
case MS_LEFT: |
208 |
c = MB1; |
209 |
break; |
210 |
case MS_MIDDLE: |
211 |
c = MB2; |
212 |
break; |
213 |
case MS_RIGHT: |
214 |
c = MB3; |
215 |
break; |
216 |
default: |
217 |
if (c < ASCII_FIRST || c > ASCII_LAST) |
218 |
goto again; |
219 |
break; |
220 |
} |
221 |
*xpp = event_x(&ev); |
222 |
*ypp = yres-1 - event_y(&ev); |
223 |
|
224 |
window_set(canvas, WIN_IGNORE_KBD_EVENT, WIN_ASCII_EVENTS, 0); |
225 |
notify_do_dispatch(); |
226 |
return(c); |
227 |
} |
228 |
|
229 |
|
230 |
sun_comout(s) /* put string out to tty subwindow */ |
231 |
register char *s; |
232 |
{ |
233 |
char buf[256]; |
234 |
register char *cp; |
235 |
|
236 |
for (cp = buf; *s; s++) { |
237 |
if (*s == '\n') |
238 |
*cp++ = '\r'; |
239 |
*cp++ = *s; |
240 |
} |
241 |
/* must be a single call */ |
242 |
ttysw_output(tty, buf, cp-buf); |
243 |
} |
244 |
|
245 |
|
246 |
getmap() /* allocate color map segments */ |
247 |
{ |
248 |
char cmsname[20]; |
249 |
unsigned char red[256], green[256], blue[256]; |
250 |
register Pixwin *pw; |
251 |
register int i; |
252 |
|
253 |
for (i = 0; i < 256; i++) |
254 |
red[i] = green[i] = blue[i] = 128; |
255 |
red[0] = green[0] = blue[0] = 255; |
256 |
red[1] = green[1] = blue[1] = 0; |
257 |
red[255] = green[255] = blue[255] = 0; |
258 |
red[254] = green[254] = blue[254] = 255; |
259 |
red[253] = green[253] = blue[253] = 0; |
260 |
/* name shared segment */ |
261 |
sprintf(cmsname, "rv%d", getpid()); |
262 |
/* set canvas */ |
263 |
pw = canvas_pixwin(canvas); |
264 |
if (pw->pw_pixrect->pr_depth < 8) |
265 |
return(-1); |
266 |
pw_setcmsname(pw, cmsname); |
267 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
268 |
/* set tty subwindow */ |
269 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
270 |
pw_setcmsname(pw, cmsname); |
271 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
272 |
/* set frame */ |
273 |
pw = (Pixwin *)window_get(frame, WIN_PIXWIN); |
274 |
pw_setcmsname(pw, cmsname); |
275 |
pw_putcolormap(pw, 0, 256, red, green, blue); |
276 |
|
277 |
return(0); |
278 |
} |
279 |
|
280 |
|
281 |
newcolr(ndx, r, g, b) /* enter a color into hardware table */ |
282 |
int ndx; |
283 |
unsigned char r, g, b; |
284 |
{ |
285 |
register Pixwin *pw; |
286 |
|
287 |
pw = canvas_pixwin(canvas); |
288 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
289 |
&r, &g, &b); |
290 |
pw = (Pixwin *)window_get(tty, WIN_PIXWIN); |
291 |
pw_putcolormap(pw, ndx+FIRSTCOLOR, 1, |
292 |
&r, &g, &b); |
293 |
} |