| 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 |
|
|
| 46 |
|
static XEvent currentevent; /* current event */ |
| 47 |
|
|
| 48 |
|
static int ncolors = 0; /* color table size */ |
| 49 |
< |
static int *pixval = NULL; /* allocated pixels */ |
| 49 |
> |
static unsigned long *pixval = NULL; /* allocated pixels */ |
| 50 |
|
|
| 51 |
|
static Display *ourdisplay = NULL; /* our display */ |
| 52 |
|
|
| 53 |
< |
static Visual *ourvisual; /* our visual structure */ |
| 53 |
> |
static XVisualInfo ourvinfo; /* our visual information */ |
| 54 |
|
|
| 55 |
|
static Window gwind = 0; /* our graphics window */ |
| 56 |
|
|
| 68 |
|
|
| 69 |
|
static Colormap ourmap = 0; /* our color map */ |
| 70 |
|
|
| 71 |
< |
extern char *malloc(); |
| 71 |
> |
extern char *malloc(), *getcombuf(); |
| 72 |
|
|
| 73 |
< |
int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
| 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 = { |
| 84 |
|
char *name, *id; |
| 85 |
|
{ |
| 86 |
|
int nplanes; |
| 87 |
< |
XVisualInfo ourvinfo; |
| 87 |
> |
XSetWindowAttributes ourwinattr; |
| 88 |
|
XWMHints ourxwmhints; |
| 89 |
< |
Pixmap bmCursorSrc, bmCursorMsk; |
| 89 |
> |
XSizeHints oursizhints; |
| 90 |
|
|
| 91 |
|
ourdisplay = XOpenDisplay(NULL); |
| 92 |
|
if (ourdisplay == NULL) { |
| 93 |
|
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
| 94 |
|
return(NULL); |
| 95 |
|
} |
| 96 |
+ |
/* find a usable visual */ |
| 97 |
|
nplanes = DisplayPlanes(ourdisplay, ourscreen); |
| 98 |
< |
if (nplanes < 4) { |
| 99 |
< |
stderr_v("not enough colors\n"); |
| 100 |
< |
return(NULL); |
| 101 |
< |
} else if (nplanes <= 12) { |
| 102 |
< |
if (!XMatchVisualInfo(ourdisplay,ourscreen, |
| 103 |
< |
nplanes,PseudoColor,&ourvinfo)) { |
| 102 |
< |
stderr_v("PseudoColor not supported\n"); |
| 98 |
> |
if ( !XMatchVisualInfo(ourdisplay,ourscreen, |
| 99 |
> |
24,TrueColor,&ourvinfo) && |
| 100 |
> |
!XMatchVisualInfo(ourdisplay,ourscreen, |
| 101 |
> |
24,DirectColor,&ourvinfo) ) |
| 102 |
> |
if (nplanes < 4) { |
| 103 |
> |
stderr_v("not enough colors\n"); |
| 104 |
|
return(NULL); |
| 105 |
+ |
} else if (!XMatchVisualInfo(ourdisplay,ourscreen, |
| 106 |
+ |
nplanes,PseudoColor,&ourvinfo) && |
| 107 |
+ |
!XMatchVisualInfo(ourdisplay,ourscreen, |
| 108 |
+ |
nplanes,GrayScale,&ourvinfo)) { |
| 109 |
+ |
stderr_v("unsupported visual type\n"); |
| 110 |
+ |
return(NULL); |
| 111 |
|
} |
| 105 |
– |
} else if (!XMatchVisualInfo(ourdisplay,ourscreen, |
| 106 |
– |
nplanes,TrueColor,&ourvinfo)) { |
| 107 |
– |
stderr_v("TrueColor not supported\n"); |
| 108 |
– |
return(NULL); |
| 109 |
– |
} |
| 110 |
– |
ourvisual = ourvinfo.visual; |
| 112 |
|
make_gmap(GAMMA); |
| 112 |
– |
/* create a cursor */ |
| 113 |
– |
pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); |
| 113 |
|
/* open window */ |
| 114 |
< |
gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0, |
| 114 |
> |
ourwinattr.background_pixel = ourblack; |
| 115 |
> |
ourwinattr.border_pixel = ourblack; |
| 116 |
> |
/* this is a waste! */ |
| 117 |
> |
ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot, |
| 118 |
> |
ourvinfo.visual, AllocNone); |
| 119 |
> |
gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, |
| 120 |
|
DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, |
| 121 |
|
DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, |
| 122 |
< |
BORWIDTH, ourblack, ourwhite); |
| 122 |
> |
BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual, |
| 123 |
> |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
| 124 |
|
if (gwind == 0) { |
| 125 |
|
stderr_v("cannot create window\n"); |
| 126 |
|
return(NULL); |
| 127 |
|
} |
| 128 |
|
XStoreName(ourdisplay, gwind, id); |
| 129 |
+ |
/* create a cursor */ |
| 130 |
+ |
pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross); |
| 131 |
|
ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); |
| 132 |
< |
ourxwmhints.flags = InputHint; |
| 132 |
> |
ourxwmhints.flags = InputHint|IconPixmapHint; |
| 133 |
|
ourxwmhints.input = True; |
| 134 |
+ |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, |
| 135 |
+ |
gwind, x11icon_bits, x11icon_width, x11icon_height); |
| 136 |
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
| 137 |
+ |
oursizhints.min_width = MINWIDTH; |
| 138 |
+ |
oursizhints.min_height = MINHEIGHT+COMHEIGHT; |
| 139 |
+ |
oursizhints.flags = PMinSize; |
| 140 |
+ |
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
| 141 |
|
XSelectInput(ourdisplay, gwind, ExposureMask); |
| 142 |
|
XMapWindow(ourdisplay, gwind); |
| 143 |
< |
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent)); |
| 143 |
> |
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent)); |
| 144 |
|
gwidth = levptr(XExposeEvent)->width; |
| 145 |
|
gheight = levptr(XExposeEvent)->height - COMHEIGHT; |
| 146 |
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
| 165 |
|
xt_close(comline); |
| 166 |
|
comline = NULL; |
| 167 |
|
} |
| 168 |
< |
if (gwind != 0) { |
| 169 |
< |
freepixels(); |
| 170 |
< |
XFreeGC(ourdisplay, ourgc); |
| 171 |
< |
XDestroyWindow(ourdisplay, gwind); |
| 172 |
< |
gwind = 0; |
| 160 |
< |
ourgc = 0; |
| 161 |
< |
} |
| 168 |
> |
freepixels(); |
| 169 |
> |
XFreeGC(ourdisplay, ourgc); |
| 170 |
> |
XDestroyWindow(ourdisplay, gwind); |
| 171 |
> |
gwind = 0; |
| 172 |
> |
ourgc = 0; |
| 173 |
|
XFreeCursor(ourdisplay, pickcursor); |
| 174 |
|
XCloseDisplay(ourdisplay); |
| 175 |
|
ourdisplay = NULL; |
| 180 |
|
x11_clear(xres, yres) /* clear our display */ |
| 181 |
|
int xres, yres; |
| 182 |
|
{ |
| 183 |
< |
if (xres != gwidth || yres != gheight) { /* change window */ |
| 184 |
< |
if (comline != NULL) |
| 185 |
< |
xt_close(comline); |
| 183 |
> |
/* check limits */ |
| 184 |
> |
if (xres < MINWIDTH) |
| 185 |
> |
xres = MINWIDTH; |
| 186 |
> |
if (yres < MINHEIGHT) |
| 187 |
> |
yres = MINHEIGHT; |
| 188 |
> |
/* resize window */ |
| 189 |
> |
if (xres != gwidth || yres != gheight) { |
| 190 |
|
XSelectInput(ourdisplay, gwind, 0); |
| 191 |
|
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); |
| 192 |
|
gwidth = xres; |
| 193 |
|
gheight = yres; |
| 194 |
< |
XSync(ourdisplay, 1); /* discard input */ |
| 194 |
> |
XFlush(ourdisplay); |
| 195 |
|
sleep(2); /* wait for window manager */ |
| 196 |
+ |
XSync(ourdisplay, 1); /* discard input */ |
| 197 |
|
} |
| 198 |
|
XClearWindow(ourdisplay, gwind); |
| 199 |
< |
if (ourvisual->class == PseudoColor) /* reinitialize color table */ |
| 199 |
> |
/* reinitialize color table */ |
| 200 |
> |
if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) |
| 201 |
|
if (getpixels() == 0) |
| 202 |
|
stderr_v("cannot allocate colors\n"); |
| 203 |
|
else |
| 204 |
|
new_ctab(ncolors); |
| 205 |
< |
|
| 205 |
> |
/* get new command line */ |
| 206 |
> |
if (comline != NULL) |
| 207 |
> |
xt_close(comline); |
| 208 |
> |
comline = xt_open(ourdisplay, |
| 209 |
> |
DefaultGC(ourdisplay,ourscreen), |
| 210 |
> |
gwind, 0, gheight, gwidth, COMHEIGHT, 0, COMFN); |
| 211 |
> |
if (comline == NULL) { |
| 212 |
> |
stderr_v("Cannot open command line window\n"); |
| 213 |
> |
quit(1); |
| 214 |
> |
} |
| 215 |
> |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
| 216 |
> |
/* remove earmuffs */ |
| 217 |
|
XSelectInput(ourdisplay, gwind, |
| 218 |
|
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
| 219 |
|
} |
| 230 |
|
|
| 231 |
|
if (ncolors > 0) |
| 232 |
|
pixel = pixval[get_pixel(col, xnewcolr)]; |
| 233 |
< |
else if (ourvisual->class == TrueColor) |
| 233 |
> |
else if (ourvinfo.class == TrueColor || ourvinfo.class == DirectColor) |
| 234 |
|
pixel = true_pixel(col); |
| 235 |
|
else |
| 236 |
|
return; |
| 243 |
|
static |
| 244 |
|
x11_flush() /* flush output */ |
| 245 |
|
{ |
| 246 |
< |
if (ncolors <= 0) /* output necessary for death */ |
| 227 |
< |
XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1); |
| 246 |
> |
XNoOp(ourdisplay); |
| 247 |
|
while (XPending(ourdisplay) > 0) |
| 248 |
|
getevent(); |
| 249 |
|
} |
| 253 |
|
x11_comin(inp, prompt) /* read in a command line */ |
| 254 |
|
char *inp, *prompt; |
| 255 |
|
{ |
| 256 |
< |
int x11_getc(), x11_comout(); |
| 256 |
> |
extern int x11_getc(); |
| 257 |
|
|
| 258 |
|
if (prompt != NULL) |
| 259 |
|
if (fromcombuf(inp, &x11_driver)) |
| 270 |
|
x11_comout(out) /* output a string to command line */ |
| 271 |
|
char *out; |
| 272 |
|
{ |
| 273 |
< |
if (comline != NULL) |
| 274 |
< |
xt_puts(out, comline); |
| 275 |
< |
XFlush(ourdisplay); |
| 273 |
> |
if (comline == NULL) |
| 274 |
> |
return; |
| 275 |
> |
xt_puts(out, comline); |
| 276 |
> |
if (out[strlen(out)-1] == '\n') |
| 277 |
> |
XFlush(ourdisplay); |
| 278 |
|
} |
| 279 |
|
|
| 280 |
|
|
| 282 |
|
x11_errout(msg) /* output an error message */ |
| 283 |
|
char *msg; |
| 284 |
|
{ |
| 264 |
– |
x11_comout(msg); |
| 285 |
|
stderr_v(msg); /* send to stderr also! */ |
| 286 |
+ |
x11_comout(msg); |
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
|
| 338 |
|
static int |
| 339 |
|
getpixels() /* get the color map */ |
| 340 |
|
{ |
| 341 |
+ |
XColor thiscolor; |
| 342 |
+ |
register int i, j; |
| 343 |
+ |
|
| 344 |
|
if (ncolors > 0) |
| 345 |
|
return(ncolors); |
| 346 |
< |
if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) { |
| 346 |
> |
if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) { |
| 347 |
|
ourmap = DefaultColormap(ourdisplay,ourscreen); |
| 348 |
|
goto loop; |
| 349 |
|
} |
| 350 |
|
newmap: |
| 351 |
< |
ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone); |
| 351 |
> |
ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone); |
| 352 |
|
loop: |
| 353 |
< |
for (ncolors = ourvisual->map_entries; |
| 354 |
< |
ncolors > ourvisual->map_entries/3; |
| 353 |
> |
for (ncolors = ourvinfo.colormap_size; |
| 354 |
> |
ncolors > ourvinfo.colormap_size/3; |
| 355 |
|
ncolors = ncolors*.937) { |
| 356 |
< |
pixval = (int *)malloc(ncolors*sizeof(int)); |
| 356 |
> |
pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long)); |
| 357 |
|
if (pixval == NULL) |
| 358 |
|
return(ncolors = 0); |
| 359 |
|
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, |
| 368 |
|
else |
| 369 |
|
return(ncolors = 0); /* failed */ |
| 370 |
|
} |
| 371 |
< |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) { |
| 372 |
< |
XColor thiscolor; |
| 349 |
< |
register int i, j; |
| 350 |
< |
/* reset black and white */ |
| 351 |
< |
for (i = 0; i < ncolors; i++) { |
| 371 |
> |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
| 372 |
> |
for (i = 0; i < ncolors; i++) { /* reset black and white */ |
| 373 |
|
if (pixval[i] != ourblack && pixval[i] != ourwhite) |
| 374 |
|
continue; |
| 375 |
|
thiscolor.pixel = pixval[i]; |
| 383 |
|
ncolors--; |
| 384 |
|
i--; |
| 385 |
|
} |
| 365 |
– |
} |
| 386 |
|
XSetWindowColormap(ourdisplay, gwind, ourmap); |
| 387 |
|
return(ncolors); |
| 388 |
|
} |
| 395 |
|
return; |
| 396 |
|
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
| 397 |
|
ncolors = 0; |
| 398 |
< |
if (ourmap != 0 && ourmap != DefaultColormap(ourdisplay,ourscreen)) |
| 398 |
> |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
| 399 |
|
XFreeColormap(ourdisplay, ourmap); |
| 400 |
|
ourmap = 0; |
| 401 |
|
} |
| 409 |
|
BYTE rgb[3]; |
| 410 |
|
|
| 411 |
|
map_color(rgb, col); |
| 412 |
< |
rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask; |
| 413 |
< |
rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask; |
| 414 |
< |
rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask; |
| 412 |
> |
rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask; |
| 413 |
> |
rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask; |
| 414 |
> |
rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask; |
| 415 |
|
return(rval); |
| 416 |
|
} |
| 417 |
|
|
| 440 |
|
freepixels(); |
| 441 |
|
break; |
| 442 |
|
case MapNotify: |
| 443 |
< |
if (ourvisual->class == PseudoColor) |
| 443 |
> |
if (ourvinfo.class == PseudoColor || |
| 444 |
> |
ourvinfo.class == GrayScale) |
| 445 |
|
if (getpixels() == 0) |
| 446 |
|
stderr_v("Cannot allocate colors\n"); |
| 447 |
|
else |
| 463 |
|
getkey(ekey) /* get input key */ |
| 464 |
|
register XKeyPressedEvent *ekey; |
| 465 |
|
{ |
| 466 |
< |
int n; |
| 466 |
> |
register int n; |
| 467 |
|
|
| 468 |
|
n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
| 469 |
|
NULL, NULL); |