24 |
|
|
25 |
|
#define GAMMA 2.2 /* exponent for color correction */ |
26 |
|
|
27 |
< |
#define MINWIDTH (4*COMCW) /* minimum graphics window width */ |
28 |
< |
#define MINHEIGHT 16 /* minimum graphics window height */ |
27 |
> |
#define MINWIDTH (32*COMCW) /* minimum graphics window width */ |
28 |
> |
#define MINHEIGHT MINWIDTH /* minimum graphics window height */ |
29 |
|
|
30 |
|
#define BORWIDTH 5 /* border width */ |
31 |
|
#define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ |
49 |
|
|
50 |
|
static Display *ourdisplay = NULL; /* our display */ |
51 |
|
|
52 |
+ |
static Visual *ourvisual; /* our visual structure */ |
53 |
+ |
|
54 |
|
static Window gwind = 0; /* our graphics window */ |
55 |
|
|
56 |
|
static Cursor pickcursor = 0; /* cursor used for picking */ |
65 |
|
|
66 |
|
static GC ourgc = 0; /* our graphics context for drawing */ |
67 |
|
|
68 |
< |
static Colormap ourmap; /* our color map */ |
68 |
> |
static Colormap ourmap = 0; /* our color map */ |
69 |
|
|
70 |
|
extern char *malloc(); |
71 |
|
|
82 |
|
x11_init(name, id) /* initialize driver */ |
83 |
|
char *name, *id; |
84 |
|
{ |
85 |
+ |
int nplanes; |
86 |
+ |
XVisualInfo ourvinfo; |
87 |
+ |
XSetWindowAttributes ourwinattr; |
88 |
|
XWMHints ourxwmhints; |
84 |
– |
Pixmap bmCursorSrc, bmCursorMsk; |
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 |
< |
if (DisplayPlanes(ourdisplay, ourscreen) < 4) { |
95 |
> |
nplanes = DisplayPlanes(ourdisplay, ourscreen); |
96 |
> |
if (nplanes < 4) { |
97 |
|
stderr_v("not enough colors\n"); |
98 |
|
return(NULL); |
99 |
+ |
} else if (nplanes <= 12) { |
100 |
+ |
if (!XMatchVisualInfo(ourdisplay,ourscreen, |
101 |
+ |
nplanes,PseudoColor,&ourvinfo)) { |
102 |
+ |
stderr_v("PseudoColor not supported\n"); |
103 |
+ |
return(NULL); |
104 |
+ |
} |
105 |
+ |
} else if (!XMatchVisualInfo(ourdisplay,ourscreen, |
106 |
+ |
nplanes,TrueColor,&ourvinfo)) { |
107 |
+ |
stderr_v("TrueColor not supported\n"); |
108 |
+ |
return(NULL); |
109 |
|
} |
110 |
< |
ourmap = DefaultColormap(ourdisplay,ourscreen); |
111 |
< |
make_gmap(GAMMA); /* make color map */ |
97 |
< |
/* create a cursor */ |
98 |
< |
pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); |
110 |
> |
ourvisual = ourvinfo.visual; |
111 |
> |
make_gmap(GAMMA); |
112 |
|
/* open window */ |
113 |
< |
gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0, |
113 |
> |
ourwinattr.background_pixel = ourwhite; |
114 |
> |
ourwinattr.border_pixel = ourblack; |
115 |
> |
gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, |
116 |
|
DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, |
117 |
|
DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, |
118 |
< |
BORWIDTH, ourblack, ourwhite); |
118 |
> |
BORWIDTH, nplanes, InputOutput, ourvisual, |
119 |
> |
CWBackPixel|CWBorderPixel, &ourwinattr); |
120 |
|
if (gwind == 0) { |
121 |
|
stderr_v("cannot create window\n"); |
122 |
|
return(NULL); |
123 |
|
} |
124 |
|
XStoreName(ourdisplay, gwind, id); |
125 |
+ |
/* create a cursor */ |
126 |
+ |
pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross); |
127 |
|
ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); |
128 |
|
ourxwmhints.flags = InputHint; |
129 |
|
ourxwmhints.input = True; |
155 |
|
xt_close(comline); |
156 |
|
comline = NULL; |
157 |
|
} |
140 |
– |
if (gwind != 0) { |
141 |
– |
XFreeGC(ourdisplay, ourgc); |
142 |
– |
XDestroyWindow(ourdisplay, gwind); |
143 |
– |
gwind = 0; |
144 |
– |
ourgc = 0; |
145 |
– |
} |
146 |
– |
XFreeCursor(ourdisplay, pickcursor); |
158 |
|
freepixels(); |
159 |
+ |
XFreeGC(ourdisplay, ourgc); |
160 |
+ |
XDestroyWindow(ourdisplay, gwind); |
161 |
+ |
gwind = 0; |
162 |
+ |
ourgc = 0; |
163 |
+ |
XFreeCursor(ourdisplay, pickcursor); |
164 |
|
XCloseDisplay(ourdisplay); |
165 |
|
ourdisplay = NULL; |
166 |
|
} |
185 |
|
XSelectInput(ourdisplay, comline->w, ExposureMask); |
186 |
|
gwidth = xres; |
187 |
|
gheight = yres; |
188 |
< |
XSync(ourdisplay, 1); /* discard input */ |
188 |
> |
XFlush(ourdisplay); |
189 |
|
sleep(2); /* wait for window manager */ |
190 |
+ |
XSync(ourdisplay, 1); /* discard input */ |
191 |
|
} |
192 |
|
XClearWindow(ourdisplay, gwind); |
193 |
< |
/* reinitialize color table */ |
194 |
< |
if (getpixels() == 0) |
195 |
< |
stderr_v("cannot allocate colors\n"); |
196 |
< |
else |
197 |
< |
new_ctab(ncolors); |
193 |
> |
if (ourvisual->class == PseudoColor) /* reinitialize color table */ |
194 |
> |
if (getpixels() == 0) |
195 |
> |
stderr_v("cannot allocate colors\n"); |
196 |
> |
else |
197 |
> |
new_ctab(ncolors); |
198 |
|
|
199 |
|
XSelectInput(ourdisplay, gwind, |
200 |
|
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
207 |
|
int xmin, ymin, xmax, ymax; |
208 |
|
{ |
209 |
|
extern int xnewcolr(); /* pixel assignment routine */ |
210 |
+ |
extern unsigned long true_pixel(); |
211 |
+ |
unsigned long pixel; |
212 |
|
|
213 |
< |
if (ncolors > 0) { |
214 |
< |
XSetForeground(ourdisplay, ourgc, |
215 |
< |
pixval[get_pixel(col, xnewcolr)]); |
216 |
< |
XFillRectangle(ourdisplay, gwind, |
217 |
< |
ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); |
218 |
< |
} |
213 |
> |
if (ncolors > 0) |
214 |
> |
pixel = pixval[get_pixel(col, xnewcolr)]; |
215 |
> |
else if (ourvisual->class == TrueColor) |
216 |
> |
pixel = true_pixel(col); |
217 |
> |
else |
218 |
> |
return; |
219 |
> |
XSetForeground(ourdisplay, ourgc, pixel); |
220 |
> |
XFillRectangle(ourdisplay, gwind, |
221 |
> |
ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
static |
226 |
|
x11_flush() /* flush output */ |
227 |
|
{ |
228 |
< |
if (ncolors <= 0) /* output necessary for death */ |
207 |
< |
XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1); |
228 |
> |
XNoOp(ourdisplay); |
229 |
|
while (XPending(ourdisplay) > 0) |
230 |
|
getevent(); |
231 |
|
} |
254 |
|
{ |
255 |
|
if (comline != NULL) |
256 |
|
xt_puts(out, comline); |
236 |
– |
XFlush(ourdisplay); |
257 |
|
} |
258 |
|
|
259 |
|
|
261 |
|
x11_errout(msg) /* output an error message */ |
262 |
|
char *msg; |
263 |
|
{ |
244 |
– |
x11_comout(msg); |
264 |
|
stderr_v(msg); /* send to stderr also! */ |
265 |
+ |
x11_comout(msg); |
266 |
+ |
x11_flush(); |
267 |
|
} |
268 |
|
|
269 |
|
|
318 |
|
static int |
319 |
|
getpixels() /* get the color map */ |
320 |
|
{ |
300 |
– |
Visual *ourvis = DefaultVisual(ourdisplay,ourscreen); |
301 |
– |
|
321 |
|
if (ncolors > 0) |
322 |
|
return(ncolors); |
323 |
< |
for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){ |
323 |
> |
if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) { |
324 |
> |
ourmap = DefaultColormap(ourdisplay,ourscreen); |
325 |
> |
goto loop; |
326 |
> |
} |
327 |
> |
newmap: |
328 |
> |
ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone); |
329 |
> |
loop: |
330 |
> |
for (ncolors = ourvisual->map_entries; |
331 |
> |
ncolors > ourvisual->map_entries/3; |
332 |
> |
ncolors = ncolors*.937) { |
333 |
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
334 |
|
if (pixval == NULL) |
335 |
< |
break; |
335 |
> |
return(ncolors = 0); |
336 |
|
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, |
337 |
|
pixval,ncolors) != 0) |
338 |
< |
return(ncolors); |
338 |
> |
break; |
339 |
|
free((char *)pixval); |
340 |
+ |
pixval = NULL; |
341 |
|
} |
342 |
< |
return(ncolors = 0); |
342 |
> |
if (pixval == NULL) { |
343 |
> |
if (ourmap == DefaultColormap(ourdisplay,ourscreen)) |
344 |
> |
goto newmap; /* try it with our map */ |
345 |
> |
else |
346 |
> |
return(ncolors = 0); /* failed */ |
347 |
> |
} |
348 |
> |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) { |
349 |
> |
XColor thiscolor; |
350 |
> |
register int i, j; |
351 |
> |
/* reset black and white */ |
352 |
> |
for (i = 0; i < ncolors; i++) { |
353 |
> |
if (pixval[i] != ourblack && pixval[i] != ourwhite) |
354 |
> |
continue; |
355 |
> |
thiscolor.pixel = pixval[i]; |
356 |
> |
thiscolor.flags = DoRed|DoGreen|DoBlue; |
357 |
> |
XQueryColor(ourdisplay, |
358 |
> |
DefaultColormap(ourdisplay,ourscreen), |
359 |
> |
&thiscolor); |
360 |
> |
XStoreColor(ourdisplay, ourmap, &thiscolor); |
361 |
> |
for (j = i; j+1 < ncolors; j++) |
362 |
> |
pixval[j] = pixval[j+1]; |
363 |
> |
ncolors--; |
364 |
> |
i--; |
365 |
> |
} |
366 |
> |
} |
367 |
> |
XSetWindowColormap(ourdisplay, gwind, ourmap); |
368 |
> |
return(ncolors); |
369 |
|
} |
370 |
|
|
371 |
|
|
376 |
|
return; |
377 |
|
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
378 |
|
ncolors = 0; |
379 |
+ |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
380 |
+ |
XFreeColormap(ourdisplay, ourmap); |
381 |
+ |
ourmap = 0; |
382 |
|
} |
383 |
|
|
384 |
|
|
385 |
+ |
static unsigned long |
386 |
+ |
true_pixel(col) /* return true pixel value for color */ |
387 |
+ |
COLOR col; |
388 |
+ |
{ |
389 |
+ |
unsigned long rval; |
390 |
+ |
BYTE rgb[3]; |
391 |
+ |
|
392 |
+ |
map_color(rgb, col); |
393 |
+ |
rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask; |
394 |
+ |
rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask; |
395 |
+ |
rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask; |
396 |
+ |
return(rval); |
397 |
+ |
} |
398 |
+ |
|
399 |
+ |
|
400 |
|
static int |
401 |
|
x11_getc() /* get a command character */ |
402 |
|
{ |
421 |
|
freepixels(); |
422 |
|
break; |
423 |
|
case MapNotify: |
424 |
< |
if (getpixels() == 0) |
425 |
< |
stderr_v("Cannot allocate colors\n"); |
426 |
< |
else |
427 |
< |
new_ctab(ncolors); |
424 |
> |
if (ourvisual->class == PseudoColor) |
425 |
> |
if (getpixels() == 0) |
426 |
> |
stderr_v("Cannot allocate colors\n"); |
427 |
> |
else |
428 |
> |
new_ctab(ncolors); |
429 |
|
break; |
430 |
|
case Expose: |
431 |
|
fixwindow(levptr(XExposeEvent)); |
443 |
|
getkey(ekey) /* get input key */ |
444 |
|
register XKeyPressedEvent *ekey; |
445 |
|
{ |
446 |
< |
c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
446 |
> |
register int n; |
447 |
> |
|
448 |
> |
n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
449 |
|
NULL, NULL); |
450 |
< |
x11_driver.inpready = c_last-c_first; |
450 |
> |
c_last += n; |
451 |
> |
x11_driver.inpready += n; |
452 |
|
} |
453 |
|
|
454 |
|
|