1 |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* x10.c - driver for X-windows version 10.4 |
9 |
* |
10 |
* 5/7/87 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include <sys/ioctl.h> |
16 |
|
17 |
#include <X/Xlib.h> |
18 |
#include <X/cursors/bcross.cursor> |
19 |
#include <X/cursors/bcross_mask.cursor> |
20 |
|
21 |
#include "color.h" |
22 |
|
23 |
#include "driver.h" |
24 |
|
25 |
#include "xtwind.h" |
26 |
|
27 |
#define GAMMA 2.2 /* exponent for color correction */ |
28 |
|
29 |
#define BORWIDTH 5 /* border width */ |
30 |
#define BARHEIGHT 25 /* menu bar size */ |
31 |
#define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ |
32 |
|
33 |
#define COMFN "8x13" /* command line font name */ |
34 |
#define COMLH 3 /* number of command lines */ |
35 |
#define COMCW 8 /* approx. character width (pixels) */ |
36 |
#define COMCH 13 /* approx. character height (pixels) */ |
37 |
|
38 |
#ifndef WFLUSH |
39 |
#define WFLUSH 30 /* flush after this many rays */ |
40 |
#endif |
41 |
|
42 |
#define checkinp() while (XPending() > 0) getevent() |
43 |
|
44 |
#define levptr(etype) ((etype *)&thisevent) |
45 |
|
46 |
static char *clientname; /* calling client's name */ |
47 |
|
48 |
static XEvent thisevent; /* current event */ |
49 |
|
50 |
static int ncolors = 0; /* color table size */ |
51 |
static int *pixval; /* allocated pixel values */ |
52 |
|
53 |
static Display *ourdisplay = NULL; /* our display */ |
54 |
|
55 |
static Window gwind = 0; /* our graphics window */ |
56 |
|
57 |
static Cursor pickcursor = 0; /* cursor used for picking */ |
58 |
|
59 |
static int gwidth = 0; /* graphics window width */ |
60 |
static int gheight = 0; /* graphics window height */ |
61 |
|
62 |
static TEXTWIND *comline = NULL; /* our command line */ |
63 |
|
64 |
static char c_queue[64]; /* input queue */ |
65 |
static int c_first = 0; /* first character in queue */ |
66 |
static int c_last = 0; /* last character in queue */ |
67 |
|
68 |
extern char *malloc(); |
69 |
|
70 |
int x_close(), x_clear(), x_paintr(), x_errout(), |
71 |
x_getcur(), x_comout(), x_comin(); |
72 |
|
73 |
static struct driver x_driver = { |
74 |
x_close, x_clear, x_paintr, x_getcur, |
75 |
x_comout, x_comin, |
76 |
MAXRES, MAXRES |
77 |
}; |
78 |
|
79 |
|
80 |
struct driver * |
81 |
x_init(name) /* initialize driver */ |
82 |
char *name; |
83 |
{ |
84 |
ourdisplay = XOpenDisplay(NULL); |
85 |
if (ourdisplay == NULL) { |
86 |
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
87 |
return(NULL); |
88 |
} |
89 |
if (DisplayPlanes() < 4) { |
90 |
stderr_v("not enough colors\n"); |
91 |
return(NULL); |
92 |
} |
93 |
make_gmap(GAMMA); /* make color map */ |
94 |
if (getpixels() < 0) /* get pixels */ |
95 |
stderr_v("cannot allocate colors\n"); |
96 |
|
97 |
pickcursor = XCreateCursor(bcross_width, bcross_height, |
98 |
bcross_bits, bcross_mask_bits, |
99 |
bcross_x_hot, bcross_y_hot, |
100 |
BlackPixel, WhitePixel, GXcopy); |
101 |
clientname = name; |
102 |
x_driver.inpready = 0; |
103 |
cmdvec = x_comout; /* set error vectors */ |
104 |
if (wrnvec != NULL) |
105 |
wrnvec = x_errout; |
106 |
return(&x_driver); |
107 |
} |
108 |
|
109 |
|
110 |
static |
111 |
x_close() /* close our display */ |
112 |
{ |
113 |
cmdvec = NULL; /* reset error vectors */ |
114 |
if (wrnvec != NULL) |
115 |
wrnvec = stderr_v; |
116 |
if (ourdisplay == NULL) |
117 |
return; |
118 |
if (comline != NULL) { |
119 |
xt_close(comline); |
120 |
comline = NULL; |
121 |
} |
122 |
if (gwind != 0) { |
123 |
XDestroyWindow(gwind); |
124 |
gwind = 0; |
125 |
gwidth = gheight = 0; |
126 |
} |
127 |
XFreeCursor(pickcursor); |
128 |
freepixels(); |
129 |
XCloseDisplay(ourdisplay); |
130 |
ourdisplay = NULL; |
131 |
} |
132 |
|
133 |
|
134 |
static |
135 |
x_clear(xres, yres) /* clear our display */ |
136 |
int xres, yres; |
137 |
{ |
138 |
if (xres != gwidth || yres != gheight) { /* new window */ |
139 |
if (comline != NULL) |
140 |
xt_close(comline); |
141 |
if (gwind != 0) |
142 |
XDestroyWindow(gwind); |
143 |
gwind = XCreateWindow(RootWindow, 0, BARHEIGHT, |
144 |
xres, yres+COMHEIGHT, BORWIDTH, |
145 |
BlackPixmap, BlackPixmap); |
146 |
if (gwind == 0) |
147 |
goto fail; |
148 |
comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
149 |
if (comline == NULL) |
150 |
goto fail; |
151 |
XMapWindow(gwind); |
152 |
XSelectInput(gwind, |
153 |
KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow); |
154 |
XStoreName(gwind, clientname); |
155 |
gwidth = xres; |
156 |
gheight = yres; |
157 |
} else /* just clear */ |
158 |
XClear(gwind); |
159 |
if (new_ctab(ncolors) == 0) { |
160 |
stderr_v("Color allocation error\n"); |
161 |
quit(1); |
162 |
} |
163 |
checkinp(); |
164 |
return; |
165 |
fail: |
166 |
stderr_v("Failure opening window in x_clear\n"); |
167 |
quit(1); |
168 |
} |
169 |
|
170 |
|
171 |
static |
172 |
x_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
173 |
COLOR col; |
174 |
int xmin, ymin, xmax, ymax; |
175 |
{ |
176 |
extern long nrays; /* global ray count */ |
177 |
extern int xnewcolr(); /* pixel assignment routine */ |
178 |
static long lastflush = 0; /* ray count at last flush */ |
179 |
|
180 |
if (ncolors > 0) { |
181 |
XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin, |
182 |
pixval[get_pixel(col, xnewcolr)]); |
183 |
} |
184 |
if (nrays - lastflush >= WFLUSH) { |
185 |
if (ncolors <= 0) /* output necessary for death */ |
186 |
XPixSet(gwind,0,0,1,1,BlackPixel); |
187 |
checkinp(); |
188 |
lastflush = nrays; |
189 |
} |
190 |
} |
191 |
|
192 |
|
193 |
static |
194 |
x_comin(inp) /* read in a command line */ |
195 |
char *inp; |
196 |
{ |
197 |
int x_getc(), x_comout(); |
198 |
|
199 |
xt_cursor(comline, TBLKCURS); |
200 |
editline(inp, x_getc, x_comout); |
201 |
xt_cursor(comline, TNOCURS); |
202 |
} |
203 |
|
204 |
|
205 |
static |
206 |
x_comout(out) /* output a string to command line */ |
207 |
char *out; |
208 |
{ |
209 |
if (comline != NULL) |
210 |
xt_puts(out, comline); |
211 |
XFlush(); |
212 |
} |
213 |
|
214 |
|
215 |
static |
216 |
x_errout(msg) /* output an error message */ |
217 |
char *msg; |
218 |
{ |
219 |
x_comout(msg); |
220 |
stderr_v(msg); /* send to stderr also! */ |
221 |
} |
222 |
|
223 |
|
224 |
static int |
225 |
x_getcur(xp, yp) /* get cursor position */ |
226 |
int *xp, *yp; |
227 |
{ |
228 |
while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0) |
229 |
sleep(1); |
230 |
XFocusKeyboard(gwind); |
231 |
do |
232 |
getevent(); |
233 |
while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed); |
234 |
*xp = levptr(XKeyOrButtonEvent)->x; |
235 |
*yp = gheight-1 - levptr(XKeyOrButtonEvent)->y; |
236 |
XFocusKeyboard(RootWindow); |
237 |
XUngrabMouse(); |
238 |
XFlush(); /* insure release */ |
239 |
if (c_last > c_first) /* key pressed */ |
240 |
return(x_getc()); |
241 |
/* button pressed */ |
242 |
switch (levptr(XKeyOrButtonEvent)->detail & 0377) { |
243 |
case LeftButton: |
244 |
return(MB1); |
245 |
case MiddleButton: |
246 |
return(MB2); |
247 |
case RightButton: |
248 |
return(MB3); |
249 |
} |
250 |
return(ABORT); |
251 |
} |
252 |
|
253 |
|
254 |
static |
255 |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
256 |
int ndx; |
257 |
int r, g, b; |
258 |
{ |
259 |
Color xcolor; |
260 |
|
261 |
xcolor.pixel = pixval[ndx]; |
262 |
xcolor.red = r << 8; |
263 |
xcolor.green = g << 8; |
264 |
xcolor.blue = b << 8; |
265 |
|
266 |
XStoreColor(&xcolor); |
267 |
} |
268 |
|
269 |
|
270 |
static |
271 |
getpixels() /* get the color map */ |
272 |
{ |
273 |
int planes; |
274 |
|
275 |
for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){ |
276 |
pixval = (int *)malloc(ncolors*sizeof(int)); |
277 |
if (pixval == NULL) |
278 |
break; |
279 |
if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0) |
280 |
return(0); |
281 |
free((char *)pixval); |
282 |
} |
283 |
ncolors = 0; |
284 |
return(-1); |
285 |
} |
286 |
|
287 |
|
288 |
static |
289 |
freepixels() /* free our pixels */ |
290 |
{ |
291 |
if (ncolors == 0) |
292 |
return; |
293 |
XFreeColors(pixval, ncolors, 0); |
294 |
free((char *)pixval); |
295 |
ncolors = 0; |
296 |
} |
297 |
|
298 |
|
299 |
static int |
300 |
x_getc() /* get a command character */ |
301 |
{ |
302 |
while (c_last <= c_first) { |
303 |
c_first = c_last = 0; /* reset */ |
304 |
getevent(); /* wait for key */ |
305 |
} |
306 |
x_driver.inpready--; |
307 |
return(c_queue[c_first++]); |
308 |
} |
309 |
|
310 |
|
311 |
static |
312 |
getevent() /* get next event */ |
313 |
{ |
314 |
XNextEvent(levptr(XEvent)); |
315 |
switch (levptr(XEvent)->type) { |
316 |
case KeyPressed: |
317 |
getkey(levptr(XKeyPressedEvent)); |
318 |
break; |
319 |
case ExposeWindow: |
320 |
if (levptr(XExposeEvent)->subwindow == 0) { |
321 |
if (ncolors == 0 && getpixels() < 0) { |
322 |
stderr_v("cannot allocate colors\n"); |
323 |
break; |
324 |
} |
325 |
new_ctab(ncolors); |
326 |
} |
327 |
/* fall through */ |
328 |
case ExposeRegion: |
329 |
fixwindow(levptr(XExposeEvent)); |
330 |
break; |
331 |
case UnmapWindow: |
332 |
if (levptr(XUnmapEvent)->subwindow == 0) |
333 |
freepixels(); |
334 |
break; |
335 |
case ButtonPressed: /* handled in x_getcur() */ |
336 |
break; |
337 |
} |
338 |
} |
339 |
|
340 |
|
341 |
static |
342 |
getkey(ekey) /* get input key */ |
343 |
register XKeyPressedEvent *ekey; |
344 |
{ |
345 |
int n; |
346 |
register char *str; |
347 |
|
348 |
str = XLookupMapping(ekey, &n); |
349 |
while (n-- > 0 && c_last < sizeof(c_queue)) |
350 |
c_queue[c_last++] = *str++; |
351 |
x_driver.inpready = c_last - c_first; |
352 |
} |
353 |
|
354 |
|
355 |
static |
356 |
fixwindow(eexp) /* repair damage to window */ |
357 |
register XExposeEvent *eexp; |
358 |
{ |
359 |
if (eexp->subwindow == 0) |
360 |
repaint(eexp->x, gheight - eexp->y - eexp->height, |
361 |
eexp->x + eexp->width, gheight - eexp->y); |
362 |
else if (eexp->subwindow == comline->w) |
363 |
xt_redraw(comline); |
364 |
} |