24 |
|
|
25 |
|
#include "xtwind.h" |
26 |
|
|
27 |
< |
#define GAMMA 2.0 /* exponent for color correction */ |
27 |
> |
#define GAMMA 2.2 /* exponent for color correction */ |
28 |
|
|
29 |
|
#define BORWIDTH 5 /* border width */ |
30 |
|
#define BARHEIGHT 25 /* menu bar size */ |
39 |
|
#define WFLUSH 30 /* flush after this many rays */ |
40 |
|
#endif |
41 |
|
|
42 |
< |
#define hashcolr(c) ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%ncolors) |
42 |
> |
#define checkinp() while (XPending() > 0) getevent() |
43 |
|
|
44 |
– |
#define flush() while (XPending() > 0) getevent() |
45 |
– |
|
44 |
|
#define levptr(etype) ((etype *)&thisevent) |
45 |
|
|
46 |
|
static char *clientname; /* calling client's name */ |
47 |
|
|
48 |
|
static XEvent thisevent; /* current event */ |
49 |
|
|
52 |
– |
static int colres = 128; /* color resolution */ |
53 |
– |
static COLR colrmap[256]; /* our color mapping */ |
54 |
– |
|
50 |
|
static int ncolors = 0; /* color table size */ |
51 |
< |
static COLR *colrtbl; /* our color table */ |
57 |
< |
static int *pixval; /* associated pixel values */ |
51 |
> |
static int *pixval; /* allocated pixel values */ |
52 |
|
|
53 |
|
static Display *ourdisplay = NULL; /* our display */ |
54 |
|
|
61 |
|
|
62 |
|
static TEXTWIND *comline = NULL; /* our command line */ |
63 |
|
|
70 |
– |
static int c_erase, c_kill; /* erase and kill characters */ |
71 |
– |
|
64 |
|
static char c_queue[64]; /* input queue */ |
65 |
|
static int c_first = 0; /* first character in queue */ |
66 |
|
static int c_last = 0; /* last character in queue */ |
78 |
|
|
79 |
|
|
80 |
|
struct driver * |
81 |
< |
x_init(name) /* initialize driver */ |
82 |
< |
char *name; |
81 |
> |
x_init(name, id) /* initialize driver */ |
82 |
> |
char *name, *id; |
83 |
|
{ |
92 |
– |
struct sgttyb ttymode; |
93 |
– |
|
94 |
– |
if (isatty(0)) { |
95 |
– |
ioctl(0, TIOCGETP, &ttymode); |
96 |
– |
c_erase = ttymode.sg_erase; |
97 |
– |
c_kill = ttymode.sg_kill; |
98 |
– |
} else { |
99 |
– |
c_erase = 'H'-'@'; |
100 |
– |
c_kill = 'U'-'@'; |
101 |
– |
} |
84 |
|
ourdisplay = XOpenDisplay(NULL); |
85 |
|
if (ourdisplay == NULL) { |
86 |
|
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
90 |
|
stderr_v("not enough colors\n"); |
91 |
|
return(NULL); |
92 |
|
} |
93 |
< |
if (getmap() < 0) /* not fatal */ |
112 |
< |
stderr_v("cannot allocate colors\n"); |
93 |
> |
make_gmap(GAMMA); /* make color map */ |
94 |
|
|
95 |
|
pickcursor = XCreateCursor(bcross_width, bcross_height, |
96 |
|
bcross_bits, bcross_mask_bits, |
97 |
|
bcross_x_hot, bcross_y_hot, |
98 |
|
BlackPixel, WhitePixel, GXcopy); |
99 |
< |
clientname = name; |
99 |
> |
clientname = id; |
100 |
|
x_driver.inpready = 0; |
101 |
|
cmdvec = x_comout; /* set error vectors */ |
102 |
|
if (wrnvec != NULL) |
123 |
|
gwidth = gheight = 0; |
124 |
|
} |
125 |
|
XFreeCursor(pickcursor); |
126 |
< |
freemap(); |
126 |
> |
freepixels(); |
127 |
|
XCloseDisplay(ourdisplay); |
128 |
|
ourdisplay = NULL; |
129 |
|
} |
136 |
|
if (xres != gwidth || yres != gheight) { /* new window */ |
137 |
|
if (comline != NULL) |
138 |
|
xt_close(comline); |
139 |
< |
if (gwind != 0) |
140 |
< |
XDestroyWindow(gwind); |
141 |
< |
gwind = XCreateWindow(RootWindow, 0, BARHEIGHT, |
142 |
< |
xres, yres+COMHEIGHT, BORWIDTH, |
143 |
< |
BlackPixmap, BlackPixmap); |
144 |
< |
if (gwind == 0) |
145 |
< |
goto fail; |
139 |
> |
if (gwind == 0) { |
140 |
> |
gwind = XCreateWindow(RootWindow, 0, BARHEIGHT, |
141 |
> |
xres, yres+COMHEIGHT, BORWIDTH, |
142 |
> |
BlackPixmap, BlackPixmap); |
143 |
> |
if (gwind == 0) |
144 |
> |
goto fail; |
145 |
> |
XStoreName(gwind, clientname); |
146 |
> |
XSelectInput(gwind, KeyPressed|ButtonPressed| |
147 |
> |
ExposeWindow|ExposeRegion|UnmapWindow); |
148 |
> |
XMapWindow(gwind); |
149 |
> |
} else |
150 |
> |
XChangeWindow(gwind, xres, yres+COMHEIGHT); |
151 |
|
comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
152 |
|
if (comline == NULL) |
153 |
|
goto fail; |
168 |
– |
XMapWindow(gwind); |
169 |
– |
XSelectInput(gwind, |
170 |
– |
KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow); |
171 |
– |
XStoreName(gwind, clientname); |
154 |
|
gwidth = xres; |
155 |
|
gheight = yres; |
156 |
|
} else /* just clear */ |
157 |
|
XClear(gwind); |
158 |
< |
newmap(); |
159 |
< |
flush(); |
158 |
> |
/* reinitialize color table */ |
159 |
> |
if (ncolors == 0 && getpixels() == 0) |
160 |
> |
stderr_v("cannot allocate colors\n"); |
161 |
> |
else |
162 |
> |
new_ctab(ncolors); |
163 |
> |
XSync(1); /* discard input */ |
164 |
|
return; |
165 |
|
fail: |
166 |
|
stderr_v("Failure opening window in x_clear\n"); |
174 |
|
int xmin, ymin, xmax, ymax; |
175 |
|
{ |
176 |
|
extern long nrays; /* global ray count */ |
177 |
+ |
extern int xnewcolr(); /* pixel assignment routine */ |
178 |
|
static long lastflush = 0; /* ray count at last flush */ |
192 |
– |
int ndx; |
179 |
|
|
180 |
|
if (ncolors > 0) { |
195 |
– |
if ((ndx = colindex(col)) < 0) { |
196 |
– |
colres >>= 1; |
197 |
– |
redraw(); |
198 |
– |
return; |
199 |
– |
} |
181 |
|
XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin, |
182 |
< |
pixval[ndx]); |
182 |
> |
pixval[get_pixel(col, xnewcolr)]); |
183 |
|
} |
184 |
|
if (nrays - lastflush >= WFLUSH) { |
185 |
< |
flush(); /* also checks for input */ |
185 |
> |
if (ncolors <= 0) /* output necessary for death */ |
186 |
> |
XPixSet(gwind,0,0,1,1,BlackPixel); |
187 |
> |
checkinp(); |
188 |
|
lastflush = nrays; |
189 |
|
} |
190 |
|
} |
197 |
|
int x_getc(), x_comout(); |
198 |
|
|
199 |
|
xt_cursor(comline, TBLKCURS); |
200 |
< |
editline(inp, x_getc, x_comout, c_erase, c_kill); |
200 |
> |
editline(inp, x_getc, x_comout); |
201 |
|
xt_cursor(comline, TNOCURS); |
202 |
|
} |
203 |
|
|
208 |
|
{ |
209 |
|
if (comline != NULL) |
210 |
|
xt_puts(out, comline); |
211 |
+ |
XFlush(); |
212 |
|
} |
213 |
|
|
214 |
|
|
235 |
|
*yp = gheight-1 - levptr(XKeyOrButtonEvent)->y; |
236 |
|
XFocusKeyboard(RootWindow); |
237 |
|
XUngrabMouse(); |
238 |
< |
flush(); /* insure release */ |
238 |
> |
XFlush(); /* insure release */ |
239 |
|
if (c_last > c_first) /* key pressed */ |
240 |
|
return(x_getc()); |
241 |
|
/* button pressed */ |
251 |
|
} |
252 |
|
|
253 |
|
|
270 |
– |
static int |
271 |
– |
colindex(col) /* return index for color */ |
272 |
– |
COLOR col; |
273 |
– |
{ |
274 |
– |
static COLR clr; |
275 |
– |
int hval; |
276 |
– |
register int ndx, i; |
277 |
– |
|
278 |
– |
mapcolor(clr, col); |
279 |
– |
|
280 |
– |
hval = ndx = hashcolr(clr); |
281 |
– |
|
282 |
– |
for (i = 1; i < ncolors; i++) { |
283 |
– |
if (colrtbl[ndx][EXP] == 0) { |
284 |
– |
colrtbl[ndx][RED] = clr[RED]; |
285 |
– |
colrtbl[ndx][GRN] = clr[GRN]; |
286 |
– |
colrtbl[ndx][BLU] = clr[BLU]; |
287 |
– |
colrtbl[ndx][EXP] = COLXS; |
288 |
– |
newcolr(ndx, clr); |
289 |
– |
return(ndx); |
290 |
– |
} |
291 |
– |
if ( colrtbl[ndx][RED] == clr[RED] && |
292 |
– |
colrtbl[ndx][GRN] == clr[GRN] && |
293 |
– |
colrtbl[ndx][BLU] == clr[BLU] ) |
294 |
– |
return(ndx); |
295 |
– |
ndx = (hval + i*i) % ncolors; |
296 |
– |
} |
297 |
– |
return(-1); |
298 |
– |
} |
299 |
– |
|
300 |
– |
|
254 |
|
static |
255 |
< |
newcolr(ndx, clr) /* enter a color into hardware table */ |
255 |
> |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
256 |
|
int ndx; |
257 |
< |
COLR clr; |
257 |
> |
int r, g, b; |
258 |
|
{ |
259 |
|
Color xcolor; |
260 |
|
|
261 |
|
xcolor.pixel = pixval[ndx]; |
262 |
< |
xcolor.red = clr[RED] << 8; |
263 |
< |
xcolor.green = clr[GRN] << 8; |
264 |
< |
xcolor.blue = clr[BLU] << 8; |
262 |
> |
xcolor.red = r << 8; |
263 |
> |
xcolor.green = g << 8; |
264 |
> |
xcolor.blue = b << 8; |
265 |
|
|
266 |
|
XStoreColor(&xcolor); |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
< |
static |
271 |
< |
mapcolor(clr, col) /* map to our color space */ |
319 |
< |
COLR clr; |
320 |
< |
COLOR col; |
270 |
> |
static int |
271 |
> |
getpixels() /* get the color map */ |
272 |
|
{ |
322 |
– |
register int i, p; |
323 |
– |
/* compute color table value */ |
324 |
– |
for (i = 0; i < 3; i++) { |
325 |
– |
p = colval(col,i) * 255.0 + 0.5; |
326 |
– |
if (p < 0) p = 0; |
327 |
– |
else if (p > 255) p = 255; |
328 |
– |
clr[i] = colrmap[p][i]; |
329 |
– |
} |
330 |
– |
clr[EXP] = COLXS; |
331 |
– |
} |
332 |
– |
|
333 |
– |
|
334 |
– |
static |
335 |
– |
getmap() /* get the color map */ |
336 |
– |
{ |
273 |
|
int planes; |
274 |
|
|
275 |
+ |
freepixels(); |
276 |
|
for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){ |
340 |
– |
colrtbl = (COLR *)malloc(ncolors*sizeof(COLR)); |
277 |
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
278 |
< |
if (colrtbl == NULL || pixval == NULL) |
279 |
< |
return(-1); |
278 |
> |
if (pixval == NULL) |
279 |
> |
break; |
280 |
|
if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0) |
281 |
< |
return(0); |
346 |
< |
free((char *)colrtbl); |
281 |
> |
return(ncolors); |
282 |
|
free((char *)pixval); |
283 |
|
} |
284 |
< |
ncolors = 0; |
350 |
< |
return(-1); |
284 |
> |
return(ncolors = 0); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
static |
289 |
< |
freemap() /* free our color map */ |
289 |
> |
freepixels() /* free our pixels */ |
290 |
|
{ |
291 |
|
if (ncolors == 0) |
292 |
|
return; |
293 |
|
XFreeColors(pixval, ncolors, 0); |
360 |
– |
free((char *)colrtbl); |
294 |
|
free((char *)pixval); |
295 |
|
ncolors = 0; |
296 |
|
} |
297 |
|
|
298 |
|
|
366 |
– |
static |
367 |
– |
newmap() /* initialize the color map */ |
368 |
– |
{ |
369 |
– |
double pow(); |
370 |
– |
int val; |
371 |
– |
register int i; |
372 |
– |
|
373 |
– |
for (i = 0; i < 256; i++) { |
374 |
– |
val = pow(i/256.0, 1.0/GAMMA) * colres; |
375 |
– |
val = (val*256 + 128) / colres; |
376 |
– |
colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val; |
377 |
– |
colrmap[i][EXP] = COLXS; |
378 |
– |
} |
379 |
– |
for (i = 0; i < ncolors; i++) |
380 |
– |
colrtbl[i][EXP] = 0; |
381 |
– |
} |
382 |
– |
|
383 |
– |
|
299 |
|
static int |
300 |
|
x_getc() /* get a command character */ |
301 |
|
{ |
317 |
|
getkey(levptr(XKeyPressedEvent)); |
318 |
|
break; |
319 |
|
case ExposeWindow: |
320 |
< |
if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0) |
321 |
< |
if (getmap() < 0) |
322 |
< |
stderr_v("cannot grab colors\n"); |
323 |
< |
else |
324 |
< |
newmap(); |
320 |
> |
if (levptr(XExposeEvent)->subwindow == 0) { |
321 |
> |
if (ncolors == 0 && getpixels() == 0) { |
322 |
> |
stderr_v("cannot allocate colors\n"); |
323 |
> |
break; |
324 |
> |
} |
325 |
> |
new_ctab(ncolors); |
326 |
> |
} |
327 |
|
/* fall through */ |
328 |
|
case ExposeRegion: |
329 |
|
fixwindow(levptr(XExposeEvent)); |
330 |
|
break; |
331 |
|
case UnmapWindow: |
332 |
|
if (levptr(XUnmapEvent)->subwindow == 0) |
333 |
< |
freemap(); |
333 |
> |
freepixels(); |
334 |
|
break; |
335 |
|
case ButtonPressed: /* handled in x_getcur() */ |
336 |
|
break; |