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) */ |
35 |
|
#define COMCW 8 /* approx. character width (pixels) */ |
36 |
|
#define COMCH 14 /* approx. character height (pixels) */ |
37 |
|
|
38 |
– |
#ifndef WFLUSH |
39 |
– |
#define WFLUSH 30 /* flush after this many rays */ |
40 |
– |
#endif |
41 |
– |
|
38 |
|
#define ourscreen DefaultScreen(ourdisplay) |
39 |
|
#define ourroot RootWindow(ourdisplay,ourscreen) |
40 |
|
#define ourwhite WhitePixel(ourdisplay,ourscreen) |
41 |
|
#define ourblack BlackPixel(ourdisplay,ourscreen) |
42 |
|
|
47 |
– |
#define checkinp() while (XPending(ourdisplay) > 0) getevent() |
48 |
– |
|
43 |
|
#define levptr(etype) ((etype *)¤tevent) |
44 |
|
|
45 |
|
static XEvent currentevent; /* current event */ |
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 |
|
|
72 |
|
int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
73 |
< |
x11_getcur(), x11_comout(), x11_comin(); |
73 |
> |
x11_getcur(), x11_comout(), x11_comin(), x11_flush(); |
74 |
|
|
75 |
|
static struct driver x11_driver = { |
76 |
|
x11_close, x11_clear, x11_paintr, x11_getcur, |
77 |
< |
x11_comout, x11_comin, 1.0 |
77 |
> |
x11_comout, x11_comin, x11_flush, 1.0 |
78 |
|
}; |
79 |
|
|
80 |
|
|
82 |
|
x11_init(name, id) /* initialize driver */ |
83 |
|
char *name, *id; |
84 |
|
{ |
85 |
+ |
int nplanes; |
86 |
+ |
XVisualInfo ourvinfo; |
87 |
|
XWMHints ourxwmhints; |
88 |
|
Pixmap bmCursorSrc, bmCursorMsk; |
89 |
|
|
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 */ |
110 |
> |
ourvisual = ourvinfo.visual; |
111 |
> |
make_gmap(GAMMA); |
112 |
|
/* create a cursor */ |
113 |
|
pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); |
114 |
|
/* open window */ |
153 |
|
comline = NULL; |
154 |
|
} |
155 |
|
if (gwind != 0) { |
156 |
+ |
freepixels(); |
157 |
|
XFreeGC(ourdisplay, ourgc); |
158 |
|
XDestroyWindow(ourdisplay, gwind); |
159 |
|
gwind = 0; |
160 |
|
ourgc = 0; |
161 |
|
} |
162 |
|
XFreeCursor(ourdisplay, pickcursor); |
153 |
– |
freepixels(); |
163 |
|
XCloseDisplay(ourdisplay); |
164 |
|
ourdisplay = NULL; |
165 |
|
} |
188 |
|
sleep(2); /* wait for window manager */ |
189 |
|
} |
190 |
|
XClearWindow(ourdisplay, gwind); |
191 |
< |
/* reinitialize color table */ |
192 |
< |
if (getpixels() == 0) |
193 |
< |
stderr_v("cannot allocate colors\n"); |
194 |
< |
else |
195 |
< |
new_ctab(ncolors); |
191 |
> |
if (ourvisual->class == PseudoColor) /* reinitialize color table */ |
192 |
> |
if (getpixels() == 0) |
193 |
> |
stderr_v("cannot allocate colors\n"); |
194 |
> |
else |
195 |
> |
new_ctab(ncolors); |
196 |
|
|
197 |
|
XSelectInput(ourdisplay, gwind, |
198 |
|
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
204 |
|
COLOR col; |
205 |
|
int xmin, ymin, xmax, ymax; |
206 |
|
{ |
198 |
– |
extern long nrays; /* global ray count */ |
207 |
|
extern int xnewcolr(); /* pixel assignment routine */ |
208 |
< |
static long lastflush = 0; /* ray count at last flush */ |
208 |
> |
extern unsigned long true_pixel(); |
209 |
> |
unsigned long pixel; |
210 |
|
|
211 |
< |
if (ncolors > 0) { |
212 |
< |
XSetForeground(ourdisplay, ourgc, |
213 |
< |
pixval[get_pixel(col, xnewcolr)]); |
214 |
< |
XFillRectangle(ourdisplay, gwind, |
215 |
< |
ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); |
216 |
< |
} |
217 |
< |
if (nrays - lastflush >= WFLUSH) { |
218 |
< |
if (ncolors <= 0) /* output necessary for death */ |
219 |
< |
XFillRectangle(ourdisplay, gwind, |
211 |
< |
ourgc, 0, 0, 1 ,1); |
212 |
< |
checkinp(); |
213 |
< |
lastflush = nrays; |
214 |
< |
} |
211 |
> |
if (ncolors > 0) |
212 |
> |
pixel = pixval[get_pixel(col, xnewcolr)]; |
213 |
> |
else if (ourvisual->class == TrueColor) |
214 |
> |
pixel = true_pixel(col); |
215 |
> |
else |
216 |
> |
return; |
217 |
> |
XSetForeground(ourdisplay, ourgc, pixel); |
218 |
> |
XFillRectangle(ourdisplay, gwind, |
219 |
> |
ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
static |
224 |
+ |
x11_flush() /* flush output */ |
225 |
+ |
{ |
226 |
+ |
if (ncolors <= 0) /* output necessary for death */ |
227 |
+ |
XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1); |
228 |
+ |
while (XPending(ourdisplay) > 0) |
229 |
+ |
getevent(); |
230 |
+ |
} |
231 |
+ |
|
232 |
+ |
|
233 |
+ |
static |
234 |
|
x11_comin(inp, prompt) /* read in a command line */ |
235 |
|
char *inp, *prompt; |
236 |
|
{ |
317 |
|
static int |
318 |
|
getpixels() /* get the color map */ |
319 |
|
{ |
305 |
– |
Visual *ourvis = DefaultVisual(ourdisplay,ourscreen); |
306 |
– |
|
320 |
|
if (ncolors > 0) |
321 |
|
return(ncolors); |
322 |
< |
for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){ |
322 |
> |
if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) { |
323 |
> |
ourmap = DefaultColormap(ourdisplay,ourscreen); |
324 |
> |
goto loop; |
325 |
> |
} |
326 |
> |
newmap: |
327 |
> |
ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone); |
328 |
> |
loop: |
329 |
> |
for (ncolors = ourvisual->map_entries; |
330 |
> |
ncolors > ourvisual->map_entries/3; |
331 |
> |
ncolors = ncolors*.937) { |
332 |
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
333 |
|
if (pixval == NULL) |
334 |
< |
break; |
334 |
> |
return(ncolors = 0); |
335 |
|
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, |
336 |
|
pixval,ncolors) != 0) |
337 |
< |
return(ncolors); |
337 |
> |
break; |
338 |
|
free((char *)pixval); |
339 |
+ |
pixval = NULL; |
340 |
|
} |
341 |
< |
return(ncolors = 0); |
341 |
> |
if (pixval == NULL) { |
342 |
> |
if (ourmap == DefaultColormap(ourdisplay,ourscreen)) |
343 |
> |
goto newmap; /* try it with our map */ |
344 |
> |
else |
345 |
> |
return(ncolors = 0); /* failed */ |
346 |
> |
} |
347 |
> |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) { |
348 |
> |
XColor thiscolor; |
349 |
> |
register int i, j; |
350 |
> |
/* reset black and white */ |
351 |
> |
for (i = 0; i < ncolors; i++) { |
352 |
> |
if (pixval[i] != ourblack && pixval[i] != ourwhite) |
353 |
> |
continue; |
354 |
> |
thiscolor.pixel = pixval[i]; |
355 |
> |
thiscolor.flags = DoRed|DoGreen|DoBlue; |
356 |
> |
XQueryColor(ourdisplay, |
357 |
> |
DefaultColormap(ourdisplay,ourscreen), |
358 |
> |
&thiscolor); |
359 |
> |
XStoreColor(ourdisplay, ourmap, &thiscolor); |
360 |
> |
for (j = i; j+1 < ncolors; j++) |
361 |
> |
pixval[j] = pixval[j+1]; |
362 |
> |
ncolors--; |
363 |
> |
i--; |
364 |
> |
} |
365 |
> |
} |
366 |
> |
XSetWindowColormap(ourdisplay, gwind, ourmap); |
367 |
> |
return(ncolors); |
368 |
|
} |
369 |
|
|
370 |
|
|
375 |
|
return; |
376 |
|
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
377 |
|
ncolors = 0; |
378 |
+ |
if (ourmap != 0 && ourmap != DefaultColormap(ourdisplay,ourscreen)) |
379 |
+ |
XFreeColormap(ourdisplay, ourmap); |
380 |
+ |
ourmap = 0; |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
+ |
static unsigned long |
385 |
+ |
true_pixel(col) /* return true pixel value for color */ |
386 |
+ |
COLOR col; |
387 |
+ |
{ |
388 |
+ |
unsigned long rval; |
389 |
+ |
BYTE rgb[3]; |
390 |
+ |
|
391 |
+ |
map_color(rgb, col); |
392 |
+ |
rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask; |
393 |
+ |
rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask; |
394 |
+ |
rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask; |
395 |
+ |
return(rval); |
396 |
+ |
} |
397 |
+ |
|
398 |
+ |
|
399 |
|
static int |
400 |
|
x11_getc() /* get a command character */ |
401 |
|
{ |
420 |
|
freepixels(); |
421 |
|
break; |
422 |
|
case MapNotify: |
423 |
< |
if (getpixels() == 0) |
424 |
< |
stderr_v("Cannot allocate colors\n"); |
425 |
< |
else |
426 |
< |
new_ctab(ncolors); |
423 |
> |
if (ourvisual->class == PseudoColor) |
424 |
> |
if (getpixels() == 0) |
425 |
> |
stderr_v("Cannot allocate colors\n"); |
426 |
> |
else |
427 |
> |
new_ctab(ncolors); |
428 |
|
break; |
429 |
|
case Expose: |
430 |
|
fixwindow(levptr(XExposeEvent)); |
442 |
|
getkey(ekey) /* get input key */ |
443 |
|
register XKeyPressedEvent *ekey; |
444 |
|
{ |
445 |
< |
c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
445 |
> |
int n; |
446 |
> |
|
447 |
> |
n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
448 |
|
NULL, NULL); |
449 |
< |
x11_driver.inpready = c_last-c_first; |
449 |
> |
c_last += n; |
450 |
> |
x11_driver.inpready += n; |
451 |
|
} |
452 |
|
|
453 |
|
|