1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* x11.c - driver for X-windows version 11 |
9 |
* |
10 |
* Jan 1990 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
#include <math.h> |
15 |
#include <sys/ioctl.h> |
16 |
|
17 |
#include <X11/Xlib.h> |
18 |
#include <X11/cursorfont.h> |
19 |
#include <X11/Xutil.h> |
20 |
|
21 |
#include "color.h" |
22 |
#include "driver.h" |
23 |
#include "x11twind.h" |
24 |
#include "x11icon.h" |
25 |
|
26 |
#define GAMMA 2.2 /* default exponent correction */ |
27 |
|
28 |
#define MINWIDTH (32*COMCW) /* minimum graphics window width */ |
29 |
#define MINHEIGHT (MINWIDTH/2) /* minimum graphics window height */ |
30 |
|
31 |
#define BORWIDTH 5 /* border width */ |
32 |
#define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ |
33 |
|
34 |
#define COMFN "8x13" /* command line font name */ |
35 |
#define COMLH 3 /* number of command lines */ |
36 |
#define COMCW 8 /* approx. character width (pixels) */ |
37 |
#define COMCH 14 /* approx. character height (pixels) */ |
38 |
|
39 |
#define ourscreen DefaultScreen(ourdisplay) |
40 |
#define ourroot RootWindow(ourdisplay,ourscreen) |
41 |
|
42 |
#define levptr(etype) ((etype *)¤tevent) |
43 |
|
44 |
static XEvent currentevent; /* current event */ |
45 |
|
46 |
static int ncolors = 0; /* color table size */ |
47 |
static int mapped = 0; /* window is mapped? */ |
48 |
static unsigned long *pixval = NULL; /* allocated pixels */ |
49 |
static unsigned long ourblack=0, ourwhite=1; |
50 |
|
51 |
static Display *ourdisplay = NULL; /* our display */ |
52 |
|
53 |
static XVisualInfo ourvinfo; /* our visual information */ |
54 |
|
55 |
static Window gwind = 0; /* our graphics window */ |
56 |
|
57 |
static Cursor pickcursor = 0; /* cursor used for picking */ |
58 |
|
59 |
static int gwidth, gheight; /* graphics window size */ |
60 |
|
61 |
static TEXTWIND *comline = NULL; /* our command line */ |
62 |
|
63 |
static char c_queue[64]; /* input queue */ |
64 |
static int c_first = 0; /* first character in queue */ |
65 |
static int c_last = 0; /* last character in queue */ |
66 |
|
67 |
static GC ourgc = 0; /* our graphics context for drawing */ |
68 |
|
69 |
static Colormap ourmap = 0; /* our color map */ |
70 |
|
71 |
extern char *malloc(), *getcombuf(); |
72 |
|
73 |
static int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
74 |
x11_getcur(), x11_comout(), x11_comin(), x11_flush(); |
75 |
|
76 |
static struct driver x11_driver = { |
77 |
x11_close, x11_clear, x11_paintr, x11_getcur, |
78 |
x11_comout, x11_comin, x11_flush, 1.0 |
79 |
}; |
80 |
|
81 |
static int getpixels(), xnewcolr(), freepixels(), resizewindow(), |
82 |
getevent(), getkey(), fixwindow(), x11_getc(); |
83 |
static unsigned long true_pixel(); |
84 |
|
85 |
|
86 |
struct driver * |
87 |
x11_init(name, id) /* initialize driver */ |
88 |
char *name, *id; |
89 |
{ |
90 |
extern char *getenv(); |
91 |
char *gv; |
92 |
int nplanes; |
93 |
XSetWindowAttributes ourwinattr; |
94 |
XWMHints ourxwmhints; |
95 |
XSizeHints oursizhints; |
96 |
|
97 |
ourdisplay = XOpenDisplay(NULL); |
98 |
if (ourdisplay == NULL) { |
99 |
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
100 |
return(NULL); |
101 |
} |
102 |
/* find a usable visual */ |
103 |
nplanes = DisplayPlanes(ourdisplay, ourscreen); |
104 |
if (XMatchVisualInfo(ourdisplay,ourscreen, |
105 |
24,TrueColor,&ourvinfo) || |
106 |
XMatchVisualInfo(ourdisplay,ourscreen, |
107 |
24,DirectColor,&ourvinfo)) { |
108 |
ourblack = 0; |
109 |
ourwhite = ourvinfo.red_mask | |
110 |
ourvinfo.green_mask | |
111 |
ourvinfo.blue_mask ; |
112 |
} else { |
113 |
if (nplanes < 4) { |
114 |
stderr_v("not enough colors\n"); |
115 |
return(NULL); |
116 |
} |
117 |
if (!XMatchVisualInfo(ourdisplay,ourscreen, |
118 |
nplanes,PseudoColor,&ourvinfo) && |
119 |
!XMatchVisualInfo(ourdisplay,ourscreen, |
120 |
nplanes,GrayScale,&ourvinfo)) { |
121 |
stderr_v("unsupported visual type\n"); |
122 |
return(NULL); |
123 |
} |
124 |
ourblack = BlackPixel(ourdisplay,ourscreen); |
125 |
ourwhite = WhitePixel(ourdisplay,ourscreen); |
126 |
} |
127 |
/* set gamma */ |
128 |
if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL |
129 |
|| (gv = getenv("GAMMA")) != NULL) |
130 |
make_gmap(atof(gv)); |
131 |
else |
132 |
make_gmap(GAMMA); |
133 |
/* open window */ |
134 |
ourwinattr.background_pixel = ourblack; |
135 |
ourwinattr.border_pixel = ourblack; |
136 |
/* this is stupid */ |
137 |
ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot, |
138 |
ourvinfo.visual, AllocNone); |
139 |
gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, |
140 |
DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, |
141 |
DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, |
142 |
BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual, |
143 |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
144 |
if (gwind == 0) { |
145 |
stderr_v("cannot create window\n"); |
146 |
return(NULL); |
147 |
} |
148 |
XStoreName(ourdisplay, gwind, id); |
149 |
/* create a cursor */ |
150 |
pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross); |
151 |
ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); |
152 |
ourxwmhints.flags = InputHint|IconPixmapHint; |
153 |
ourxwmhints.input = True; |
154 |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, |
155 |
gwind, x11icon_bits, x11icon_width, x11icon_height); |
156 |
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
157 |
oursizhints.min_width = MINWIDTH; |
158 |
oursizhints.min_height = MINHEIGHT+COMHEIGHT; |
159 |
oursizhints.flags = PMinSize; |
160 |
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
161 |
XSelectInput(ourdisplay, gwind, ExposureMask); |
162 |
XMapWindow(ourdisplay, gwind); |
163 |
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent)); |
164 |
gwidth = levptr(XExposeEvent)->width; |
165 |
gheight = levptr(XExposeEvent)->height - COMHEIGHT; |
166 |
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
167 |
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
168 |
x11_driver.inpready = 0; |
169 |
mapped = 1; |
170 |
cmdvec = x11_comout; /* set error vectors */ |
171 |
if (wrnvec != NULL) |
172 |
wrnvec = x11_errout; |
173 |
return(&x11_driver); |
174 |
} |
175 |
|
176 |
|
177 |
static |
178 |
x11_close() /* close our display */ |
179 |
{ |
180 |
cmdvec = NULL; /* reset error vectors */ |
181 |
if (wrnvec != NULL) |
182 |
wrnvec = stderr_v; |
183 |
if (ourdisplay == NULL) |
184 |
return; |
185 |
if (comline != NULL) { |
186 |
xt_close(comline); |
187 |
comline = NULL; |
188 |
} |
189 |
freepixels(); |
190 |
XFreeGC(ourdisplay, ourgc); |
191 |
XDestroyWindow(ourdisplay, gwind); |
192 |
gwind = 0; |
193 |
ourgc = 0; |
194 |
XFreeCursor(ourdisplay, pickcursor); |
195 |
XCloseDisplay(ourdisplay); |
196 |
ourdisplay = NULL; |
197 |
} |
198 |
|
199 |
|
200 |
static |
201 |
x11_clear(xres, yres) /* clear our display */ |
202 |
int xres, yres; |
203 |
{ |
204 |
/* check limits */ |
205 |
if (xres < MINWIDTH) |
206 |
xres = MINWIDTH; |
207 |
if (yres < MINHEIGHT) |
208 |
yres = MINHEIGHT; |
209 |
/* resize window */ |
210 |
if (xres != gwidth || yres != gheight) { |
211 |
XSelectInput(ourdisplay, gwind, 0); |
212 |
XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); |
213 |
gwidth = xres; |
214 |
gheight = yres; |
215 |
XFlush(ourdisplay); |
216 |
sleep(2); /* wait for window manager */ |
217 |
XSync(ourdisplay, 1); /* discard input */ |
218 |
} |
219 |
XClearWindow(ourdisplay, gwind); |
220 |
/* reinitialize color table */ |
221 |
if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) |
222 |
if (getpixels() == 0) |
223 |
stderr_v("cannot allocate colors\n"); |
224 |
else |
225 |
new_ctab(ncolors); |
226 |
/* get new command line */ |
227 |
if (comline != NULL) |
228 |
xt_close(comline); |
229 |
comline = xt_open(ourdisplay, gwind, 0, gheight, |
230 |
gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN); |
231 |
if (comline == NULL) { |
232 |
stderr_v("Cannot open command line window\n"); |
233 |
quit(1); |
234 |
} |
235 |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
236 |
/* remove earmuffs */ |
237 |
XSelectInput(ourdisplay, gwind, |
238 |
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
239 |
} |
240 |
|
241 |
|
242 |
static |
243 |
x11_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
244 |
COLOR col; |
245 |
int xmin, ymin, xmax, ymax; |
246 |
{ |
247 |
unsigned long pixel; |
248 |
|
249 |
if (!mapped) |
250 |
return; |
251 |
if (ncolors > 0) |
252 |
pixel = pixval[get_pixel(col, xnewcolr)]; |
253 |
else |
254 |
pixel = true_pixel(col); |
255 |
XSetForeground(ourdisplay, ourgc, pixel); |
256 |
XFillRectangle(ourdisplay, gwind, |
257 |
ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); |
258 |
} |
259 |
|
260 |
|
261 |
static |
262 |
x11_flush() /* flush output */ |
263 |
{ |
264 |
int n; |
265 |
register char *buf; |
266 |
|
267 |
XNoOp(ourdisplay); |
268 |
while (XPending(ourdisplay) > 0) |
269 |
getevent(); |
270 |
#ifdef FIONREAD |
271 |
if (ioctl(0, FIONREAD, &n) == 0 && n) { /* from stdin */ |
272 |
buf = getcombuf(&x11_driver); |
273 |
n = read(0, buf, n); |
274 |
buf[n] = '\0'; |
275 |
} |
276 |
#endif |
277 |
} |
278 |
|
279 |
|
280 |
static |
281 |
x11_comin(inp, prompt) /* read in a command line */ |
282 |
char *inp, *prompt; |
283 |
{ |
284 |
if (prompt != NULL) { |
285 |
x11_flush(); /* make sure we get everything */ |
286 |
if (fromcombuf(inp, &x11_driver)) |
287 |
return; |
288 |
xt_puts(prompt, comline); |
289 |
} |
290 |
xt_cursor(comline, TBLKCURS); |
291 |
editline(inp, x11_getc, x11_comout); |
292 |
xt_cursor(comline, TNOCURS); |
293 |
} |
294 |
|
295 |
|
296 |
static |
297 |
x11_comout(out) /* output a string to command line */ |
298 |
char *out; |
299 |
{ |
300 |
if (comline == NULL) |
301 |
return; |
302 |
xt_puts(out, comline); |
303 |
if (out[strlen(out)-1] == '\n') |
304 |
XFlush(ourdisplay); |
305 |
} |
306 |
|
307 |
|
308 |
static |
309 |
x11_errout(msg) /* output an error message */ |
310 |
char *msg; |
311 |
{ |
312 |
stderr_v(msg); /* send to stderr also! */ |
313 |
x11_comout(msg); |
314 |
} |
315 |
|
316 |
|
317 |
static int |
318 |
x11_getcur(xp, yp) /* get cursor position */ |
319 |
int *xp, *yp; |
320 |
{ |
321 |
while (XGrabPointer(ourdisplay, gwind, True, ButtonPressMask, |
322 |
GrabModeAsync, GrabModeAsync, None, pickcursor, |
323 |
CurrentTime) != GrabSuccess) |
324 |
sleep(2); |
325 |
|
326 |
do |
327 |
getevent(); |
328 |
while (c_last <= c_first && levptr(XEvent)->type != ButtonPress); |
329 |
*xp = levptr(XButtonPressedEvent)->x; |
330 |
*yp = gheight-1 - levptr(XButtonPressedEvent)->y; |
331 |
XUngrabPointer(ourdisplay, CurrentTime); |
332 |
XFlush(ourdisplay); /* insure release */ |
333 |
if (c_last > c_first) /* key pressed */ |
334 |
return(x11_getc()); |
335 |
/* button pressed */ |
336 |
if (levptr(XButtonPressedEvent)->button == Button1) |
337 |
return(MB1); |
338 |
if (levptr(XButtonPressedEvent)->button == Button2) |
339 |
return(MB2); |
340 |
if (levptr(XButtonPressedEvent)->button == Button3) |
341 |
return(MB3); |
342 |
return(ABORT); |
343 |
} |
344 |
|
345 |
|
346 |
static |
347 |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
348 |
int ndx; |
349 |
int r, g, b; |
350 |
{ |
351 |
XColor xcolor; |
352 |
|
353 |
xcolor.pixel = pixval[ndx]; |
354 |
xcolor.red = r << 8; |
355 |
xcolor.green = g << 8; |
356 |
xcolor.blue = b << 8; |
357 |
xcolor.flags = DoRed|DoGreen|DoBlue; |
358 |
|
359 |
XStoreColor(ourdisplay, ourmap, &xcolor); |
360 |
} |
361 |
|
362 |
|
363 |
static int |
364 |
getpixels() /* get the color map */ |
365 |
{ |
366 |
XColor thiscolor; |
367 |
register int i, j; |
368 |
|
369 |
if (ncolors > 0) |
370 |
return(ncolors); |
371 |
if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) { |
372 |
ourmap = DefaultColormap(ourdisplay,ourscreen); |
373 |
goto loop; |
374 |
} |
375 |
newmap: |
376 |
ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone); |
377 |
loop: |
378 |
for (ncolors = ourvinfo.colormap_size; |
379 |
ncolors > ourvinfo.colormap_size/3; |
380 |
ncolors = ncolors*.937) { |
381 |
pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long)); |
382 |
if (pixval == NULL) |
383 |
return(ncolors = 0); |
384 |
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) |
385 |
break; |
386 |
free((char *)pixval); |
387 |
pixval = NULL; |
388 |
} |
389 |
if (pixval == NULL) { |
390 |
if (ourmap == DefaultColormap(ourdisplay,ourscreen)) |
391 |
goto newmap; /* try it with our map */ |
392 |
else |
393 |
return(ncolors = 0); /* failed */ |
394 |
} |
395 |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
396 |
for (i = 0; i < ncolors; i++) { /* reset black and white */ |
397 |
if (pixval[i] != ourblack && pixval[i] != ourwhite) |
398 |
continue; |
399 |
thiscolor.pixel = pixval[i]; |
400 |
thiscolor.flags = DoRed|DoGreen|DoBlue; |
401 |
XQueryColor(ourdisplay, |
402 |
DefaultColormap(ourdisplay,ourscreen), |
403 |
&thiscolor); |
404 |
XStoreColor(ourdisplay, ourmap, &thiscolor); |
405 |
for (j = i; j+1 < ncolors; j++) |
406 |
pixval[j] = pixval[j+1]; |
407 |
ncolors--; |
408 |
i--; |
409 |
} |
410 |
XSetWindowColormap(ourdisplay, gwind, ourmap); |
411 |
return(ncolors); |
412 |
} |
413 |
|
414 |
|
415 |
static |
416 |
freepixels() /* free our pixels */ |
417 |
{ |
418 |
if (ncolors == 0) |
419 |
return; |
420 |
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
421 |
free((char *)pixval); |
422 |
pixval = NULL; |
423 |
ncolors = 0; |
424 |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
425 |
XFreeColormap(ourdisplay, ourmap); |
426 |
ourmap = 0; |
427 |
} |
428 |
|
429 |
|
430 |
static unsigned long |
431 |
true_pixel(col) /* return true pixel value for color */ |
432 |
COLOR col; |
433 |
{ |
434 |
unsigned long rval; |
435 |
BYTE rgb[3]; |
436 |
|
437 |
map_color(rgb, col); |
438 |
rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask; |
439 |
rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask; |
440 |
rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask; |
441 |
return(rval); |
442 |
} |
443 |
|
444 |
|
445 |
static int |
446 |
x11_getc() /* get a command character */ |
447 |
{ |
448 |
while (c_last <= c_first) { |
449 |
c_first = c_last = 0; /* reset */ |
450 |
getevent(); /* wait for key */ |
451 |
} |
452 |
x11_driver.inpready--; |
453 |
return(c_queue[c_first++]); |
454 |
} |
455 |
|
456 |
|
457 |
static |
458 |
getevent() /* get next event */ |
459 |
{ |
460 |
XNextEvent(ourdisplay, levptr(XEvent)); |
461 |
switch (levptr(XEvent)->type) { |
462 |
case ConfigureNotify: |
463 |
resizewindow(levptr(XConfigureEvent)); |
464 |
break; |
465 |
case UnmapNotify: |
466 |
mapped = 0; |
467 |
freepixels(); |
468 |
break; |
469 |
case MapNotify: |
470 |
if (ourvinfo.class == PseudoColor || |
471 |
ourvinfo.class == GrayScale) |
472 |
if (getpixels() == 0) |
473 |
stderr_v("Cannot allocate colors\n"); |
474 |
else |
475 |
new_ctab(ncolors); |
476 |
mapped = 1; |
477 |
break; |
478 |
case Expose: |
479 |
fixwindow(levptr(XExposeEvent)); |
480 |
break; |
481 |
case KeyPress: |
482 |
getkey(levptr(XKeyPressedEvent)); |
483 |
break; |
484 |
case ButtonPress: |
485 |
break; |
486 |
} |
487 |
} |
488 |
|
489 |
|
490 |
static |
491 |
getkey(ekey) /* get input key */ |
492 |
register XKeyPressedEvent *ekey; |
493 |
{ |
494 |
register int n; |
495 |
|
496 |
n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
497 |
NULL, NULL); |
498 |
c_last += n; |
499 |
x11_driver.inpready += n; |
500 |
} |
501 |
|
502 |
|
503 |
static |
504 |
fixwindow(eexp) /* repair damage to window */ |
505 |
register XExposeEvent *eexp; |
506 |
{ |
507 |
if (eexp->window == gwind) { |
508 |
sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n", |
509 |
eexp->x, gheight - eexp->y - eexp->height, |
510 |
eexp->x + eexp->width, gheight - eexp->y); |
511 |
} else if (eexp->window == comline->w) { |
512 |
if (eexp->count == 0) |
513 |
xt_redraw(comline); |
514 |
} |
515 |
} |
516 |
|
517 |
|
518 |
static |
519 |
resizewindow(ersz) /* resize window */ |
520 |
register XConfigureEvent *ersz; |
521 |
{ |
522 |
if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight) |
523 |
return; |
524 |
|
525 |
gwidth = ersz->width; |
526 |
gheight = ersz->height-COMHEIGHT; |
527 |
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
528 |
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
529 |
|
530 |
strcpy(getcombuf(&x11_driver), "new\n"); |
531 |
} |