21 |
|
#include "color.h" |
22 |
|
#include "driver.h" |
23 |
|
#include "x11twind.h" |
24 |
+ |
#include "x11icon.h" |
25 |
|
|
26 |
|
#define GAMMA 2.2 /* exponent for color correction */ |
27 |
|
|
38 |
|
|
39 |
|
#define ourscreen DefaultScreen(ourdisplay) |
40 |
|
#define ourroot RootWindow(ourdisplay,ourscreen) |
40 |
– |
#define ourwhite WhitePixel(ourdisplay,ourscreen) |
41 |
– |
#define ourblack BlackPixel(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 *pixval = NULL; /* allocated pixels */ |
47 |
> |
static unsigned long *pixval = NULL; /* allocated pixels */ |
48 |
> |
static unsigned long ourblack=0, ourwhite=1; |
49 |
|
|
50 |
|
static Display *ourdisplay = NULL; /* our display */ |
51 |
|
|
52 |
< |
static Visual *ourvisual; /* our visual structure */ |
52 |
> |
static XVisualInfo ourvinfo; /* our visual information */ |
53 |
|
|
54 |
|
static Window gwind = 0; /* our graphics window */ |
55 |
|
|
67 |
|
|
68 |
|
static Colormap ourmap = 0; /* our color map */ |
69 |
|
|
70 |
< |
extern char *malloc(); |
70 |
> |
extern char *malloc(), *getcombuf(); |
71 |
|
|
72 |
< |
int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
72 |
> |
static int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
73 |
|
x11_getcur(), x11_comout(), x11_comin(), x11_flush(); |
74 |
|
|
75 |
|
static struct driver x11_driver = { |
83 |
|
char *name, *id; |
84 |
|
{ |
85 |
|
int nplanes; |
86 |
< |
XVisualInfo ourvinfo; |
86 |
> |
XSetWindowAttributes ourwinattr; |
87 |
|
XWMHints ourxwmhints; |
88 |
< |
Pixmap bmCursorSrc, bmCursorMsk; |
88 |
> |
XSizeHints oursizhints; |
89 |
|
|
90 |
|
ourdisplay = XOpenDisplay(NULL); |
91 |
|
if (ourdisplay == NULL) { |
92 |
|
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
93 |
|
return(NULL); |
94 |
|
} |
95 |
+ |
/* find a usable visual */ |
96 |
|
nplanes = DisplayPlanes(ourdisplay, ourscreen); |
97 |
< |
if (nplanes < 4) { |
98 |
< |
stderr_v("not enough colors\n"); |
99 |
< |
return(NULL); |
100 |
< |
} else if (nplanes <= 12) { |
101 |
< |
if (!XMatchVisualInfo(ourdisplay,ourscreen, |
102 |
< |
nplanes,PseudoColor,&ourvinfo)) { |
102 |
< |
stderr_v("PseudoColor not supported\n"); |
97 |
> |
if ( !XMatchVisualInfo(ourdisplay,ourscreen, |
98 |
> |
24,TrueColor,&ourvinfo) && |
99 |
> |
!XMatchVisualInfo(ourdisplay,ourscreen, |
100 |
> |
24,DirectColor,&ourvinfo) ) { |
101 |
> |
if (nplanes < 4) { |
102 |
> |
stderr_v("not enough colors\n"); |
103 |
|
return(NULL); |
104 |
+ |
} else if (!XMatchVisualInfo(ourdisplay,ourscreen, |
105 |
+ |
nplanes,PseudoColor,&ourvinfo) && |
106 |
+ |
!XMatchVisualInfo(ourdisplay,ourscreen, |
107 |
+ |
nplanes,GrayScale,&ourvinfo)) { |
108 |
+ |
stderr_v("unsupported visual type\n"); |
109 |
+ |
return(NULL); |
110 |
|
} |
111 |
< |
} else if (!XMatchVisualInfo(ourdisplay,ourscreen, |
112 |
< |
nplanes,TrueColor,&ourvinfo)) { |
113 |
< |
stderr_v("TrueColor not supported\n"); |
114 |
< |
return(NULL); |
111 |
> |
ourblack = BlackPixel(ourdisplay,ourscreen); |
112 |
> |
ourwhite = WhitePixel(ourdisplay,ourscreen); |
113 |
> |
} else { |
114 |
> |
ourblack = 0; |
115 |
> |
ourwhite = ~0; |
116 |
|
} |
110 |
– |
ourvisual = ourvinfo.visual; |
117 |
|
make_gmap(GAMMA); |
112 |
– |
/* create a cursor */ |
113 |
– |
pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); |
118 |
|
/* open window */ |
119 |
< |
gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0, |
119 |
> |
ourwinattr.background_pixel = ourblack; |
120 |
> |
ourwinattr.border_pixel = ourblack; |
121 |
> |
/* this is a waste! */ |
122 |
> |
ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot, |
123 |
> |
ourvinfo.visual, AllocNone); |
124 |
> |
gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, |
125 |
|
DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, |
126 |
|
DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, |
127 |
< |
BORWIDTH, ourblack, ourwhite); |
127 |
> |
BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual, |
128 |
> |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
129 |
|
if (gwind == 0) { |
130 |
|
stderr_v("cannot create window\n"); |
131 |
|
return(NULL); |
132 |
|
} |
133 |
|
XStoreName(ourdisplay, gwind, id); |
134 |
+ |
/* create a cursor */ |
135 |
+ |
pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross); |
136 |
|
ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); |
137 |
< |
ourxwmhints.flags = InputHint; |
137 |
> |
ourxwmhints.flags = InputHint|IconPixmapHint; |
138 |
|
ourxwmhints.input = True; |
139 |
+ |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, |
140 |
+ |
gwind, x11icon_bits, x11icon_width, x11icon_height); |
141 |
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
142 |
+ |
oursizhints.min_width = MINWIDTH; |
143 |
+ |
oursizhints.min_height = MINHEIGHT+COMHEIGHT; |
144 |
+ |
oursizhints.flags = PMinSize; |
145 |
+ |
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
146 |
|
XSelectInput(ourdisplay, gwind, ExposureMask); |
147 |
|
XMapWindow(ourdisplay, gwind); |
148 |
< |
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent)); |
148 |
> |
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent)); |
149 |
|
gwidth = levptr(XExposeEvent)->width; |
150 |
|
gheight = levptr(XExposeEvent)->height - COMHEIGHT; |
151 |
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
170 |
|
xt_close(comline); |
171 |
|
comline = NULL; |
172 |
|
} |
173 |
< |
if (gwind != 0) { |
174 |
< |
freepixels(); |
175 |
< |
XFreeGC(ourdisplay, ourgc); |
176 |
< |
XDestroyWindow(ourdisplay, gwind); |
177 |
< |
gwind = 0; |
160 |
< |
ourgc = 0; |
161 |
< |
} |
173 |
> |
freepixels(); |
174 |
> |
XFreeGC(ourdisplay, ourgc); |
175 |
> |
XDestroyWindow(ourdisplay, gwind); |
176 |
> |
gwind = 0; |
177 |
> |
ourgc = 0; |
178 |
|
XFreeCursor(ourdisplay, pickcursor); |
179 |
|
XCloseDisplay(ourdisplay); |
180 |
|
ourdisplay = NULL; |
185 |
|
x11_clear(xres, yres) /* clear our display */ |
186 |
|
int xres, yres; |
187 |
|
{ |
188 |
< |
if (xres != gwidth || yres != gheight) { /* change window */ |
189 |
< |
if (comline != NULL) |
190 |
< |
xt_close(comline); |
188 |
> |
/* check limits */ |
189 |
> |
if (xres < MINWIDTH) |
190 |
> |
xres = MINWIDTH; |
191 |
> |
if (yres < MINHEIGHT) |
192 |
> |
yres = MINHEIGHT; |
193 |
> |
/* resize window */ |
194 |
> |
if (xres != gwidth || yres != gheight) { |
195 |
|
XSelectInput(ourdisplay, gwind, 0); |
196 |
|
XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); |
177 |
– |
comline = xt_open(ourdisplay, |
178 |
– |
DefaultGC(ourdisplay,ourscreen), |
179 |
– |
gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
180 |
– |
if (comline == NULL) { |
181 |
– |
stderr_v("Cannot open command line window\n"); |
182 |
– |
quit(1); |
183 |
– |
} |
184 |
– |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
197 |
|
gwidth = xres; |
198 |
|
gheight = yres; |
199 |
< |
XSync(ourdisplay, 1); /* discard input */ |
199 |
> |
XFlush(ourdisplay); |
200 |
|
sleep(2); /* wait for window manager */ |
201 |
+ |
XSync(ourdisplay, 1); /* discard input */ |
202 |
|
} |
203 |
|
XClearWindow(ourdisplay, gwind); |
204 |
< |
if (ourvisual->class == PseudoColor) /* reinitialize color table */ |
204 |
> |
/* reinitialize color table */ |
205 |
> |
if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) |
206 |
|
if (getpixels() == 0) |
207 |
|
stderr_v("cannot allocate colors\n"); |
208 |
|
else |
209 |
|
new_ctab(ncolors); |
210 |
< |
|
210 |
> |
/* get new command line */ |
211 |
> |
if (comline != NULL) |
212 |
> |
xt_close(comline); |
213 |
> |
comline = xt_open(ourdisplay, gwind, 0, gheight, |
214 |
> |
gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN); |
215 |
> |
if (comline == NULL) { |
216 |
> |
stderr_v("Cannot open command line window\n"); |
217 |
> |
quit(1); |
218 |
> |
} |
219 |
> |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
220 |
> |
/* remove earmuffs */ |
221 |
|
XSelectInput(ourdisplay, gwind, |
222 |
|
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
223 |
|
} |
234 |
|
|
235 |
|
if (ncolors > 0) |
236 |
|
pixel = pixval[get_pixel(col, xnewcolr)]; |
237 |
< |
else if (ourvisual->class == TrueColor) |
237 |
> |
else if (ourvinfo.class == TrueColor || ourvinfo.class == DirectColor) |
238 |
|
pixel = true_pixel(col); |
239 |
|
else |
240 |
|
return; |
247 |
|
static |
248 |
|
x11_flush() /* flush output */ |
249 |
|
{ |
250 |
< |
if (ncolors <= 0) /* output necessary for death */ |
227 |
< |
XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1); |
250 |
> |
XNoOp(ourdisplay); |
251 |
|
while (XPending(ourdisplay) > 0) |
252 |
|
getevent(); |
253 |
|
} |
257 |
|
x11_comin(inp, prompt) /* read in a command line */ |
258 |
|
char *inp, *prompt; |
259 |
|
{ |
260 |
< |
int x11_getc(), x11_comout(); |
260 |
> |
extern int x11_getc(); |
261 |
|
|
262 |
|
if (prompt != NULL) |
263 |
|
if (fromcombuf(inp, &x11_driver)) |
274 |
|
x11_comout(out) /* output a string to command line */ |
275 |
|
char *out; |
276 |
|
{ |
277 |
< |
if (comline != NULL) |
278 |
< |
xt_puts(out, comline); |
279 |
< |
XFlush(ourdisplay); |
277 |
> |
if (comline == NULL) |
278 |
> |
return; |
279 |
> |
xt_puts(out, comline); |
280 |
> |
if (out[strlen(out)-1] == '\n') |
281 |
> |
XFlush(ourdisplay); |
282 |
|
} |
283 |
|
|
284 |
|
|
286 |
|
x11_errout(msg) /* output an error message */ |
287 |
|
char *msg; |
288 |
|
{ |
264 |
– |
x11_comout(msg); |
289 |
|
stderr_v(msg); /* send to stderr also! */ |
290 |
+ |
x11_comout(msg); |
291 |
|
} |
292 |
|
|
293 |
|
|
342 |
|
static int |
343 |
|
getpixels() /* get the color map */ |
344 |
|
{ |
345 |
+ |
XColor thiscolor; |
346 |
+ |
register int i, j; |
347 |
+ |
|
348 |
|
if (ncolors > 0) |
349 |
|
return(ncolors); |
350 |
< |
if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) { |
350 |
> |
if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) { |
351 |
|
ourmap = DefaultColormap(ourdisplay,ourscreen); |
352 |
|
goto loop; |
353 |
|
} |
354 |
|
newmap: |
355 |
< |
ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone); |
355 |
> |
ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone); |
356 |
|
loop: |
357 |
< |
for (ncolors = ourvisual->map_entries; |
358 |
< |
ncolors > ourvisual->map_entries/3; |
357 |
> |
for (ncolors = ourvinfo.colormap_size; |
358 |
> |
ncolors > ourvinfo.colormap_size/3; |
359 |
|
ncolors = ncolors*.937) { |
360 |
< |
pixval = (int *)malloc(ncolors*sizeof(int)); |
360 |
> |
pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long)); |
361 |
|
if (pixval == NULL) |
362 |
|
return(ncolors = 0); |
363 |
|
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, |
372 |
|
else |
373 |
|
return(ncolors = 0); /* failed */ |
374 |
|
} |
375 |
< |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) { |
376 |
< |
XColor thiscolor; |
349 |
< |
register int i, j; |
350 |
< |
/* reset black and white */ |
351 |
< |
for (i = 0; i < ncolors; i++) { |
375 |
> |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
376 |
> |
for (i = 0; i < ncolors; i++) { /* reset black and white */ |
377 |
|
if (pixval[i] != ourblack && pixval[i] != ourwhite) |
378 |
|
continue; |
379 |
|
thiscolor.pixel = pixval[i]; |
387 |
|
ncolors--; |
388 |
|
i--; |
389 |
|
} |
365 |
– |
} |
390 |
|
XSetWindowColormap(ourdisplay, gwind, ourmap); |
391 |
|
return(ncolors); |
392 |
|
} |
399 |
|
return; |
400 |
|
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
401 |
|
ncolors = 0; |
402 |
< |
if (ourmap != 0 && ourmap != DefaultColormap(ourdisplay,ourscreen)) |
402 |
> |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
403 |
|
XFreeColormap(ourdisplay, ourmap); |
404 |
|
ourmap = 0; |
405 |
|
} |
413 |
|
BYTE rgb[3]; |
414 |
|
|
415 |
|
map_color(rgb, col); |
416 |
< |
rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask; |
417 |
< |
rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask; |
418 |
< |
rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask; |
416 |
> |
rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask; |
417 |
> |
rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask; |
418 |
> |
rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask; |
419 |
|
return(rval); |
420 |
|
} |
421 |
|
|
444 |
|
freepixels(); |
445 |
|
break; |
446 |
|
case MapNotify: |
447 |
< |
if (ourvisual->class == PseudoColor) |
447 |
> |
if (ourvinfo.class == PseudoColor || |
448 |
> |
ourvinfo.class == GrayScale) |
449 |
|
if (getpixels() == 0) |
450 |
|
stderr_v("Cannot allocate colors\n"); |
451 |
|
else |
467 |
|
getkey(ekey) /* get input key */ |
468 |
|
register XKeyPressedEvent *ekey; |
469 |
|
{ |
470 |
< |
int n; |
470 |
> |
register int n; |
471 |
|
|
472 |
|
n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
473 |
|
NULL, NULL); |