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 */ |
30 |
|
#define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ |
31 |
+ |
#define MINWIDTH (32*COMCW) /* minimum graphics window width */ |
32 |
+ |
#define MINHEIGHT MINWIDTH /* minimum graphics window height */ |
33 |
|
|
34 |
|
#define COMFN "8x13" /* command line font name */ |
35 |
|
#define COMLH 3 /* number of command lines */ |
36 |
|
#define COMCW 8 /* approx. character width (pixels) */ |
37 |
|
#define COMCH 13 /* approx. character height (pixels) */ |
38 |
|
|
38 |
– |
#ifndef WFLUSH |
39 |
– |
#define WFLUSH 50 /* flush after this many rays */ |
40 |
– |
#endif |
41 |
– |
|
42 |
– |
#define hashcolr(c) ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%ncolors) |
43 |
– |
|
44 |
– |
#define checkinp() while (XPending() > 0) getevent() |
45 |
– |
|
39 |
|
#define levptr(etype) ((etype *)&thisevent) |
40 |
|
|
48 |
– |
static char *clientname; /* calling client's name */ |
49 |
– |
|
41 |
|
static XEvent thisevent; /* current event */ |
42 |
|
|
52 |
– |
static int colres = 128; /* color resolution */ |
53 |
– |
static COLR colrmap[256]; /* our color mapping */ |
54 |
– |
|
43 |
|
static int ncolors = 0; /* color table size */ |
44 |
< |
static COLR *colrtbl; /* our color table */ |
57 |
< |
static int *pixval; /* associated pixel values */ |
44 |
> |
static int *pixval; /* allocated pixel values */ |
45 |
|
|
46 |
|
static Display *ourdisplay = NULL; /* our display */ |
47 |
|
|
49 |
|
|
50 |
|
static Cursor pickcursor = 0; /* cursor used for picking */ |
51 |
|
|
52 |
< |
static int gwidth = 0; /* graphics window width */ |
66 |
< |
static int gheight = 0; /* graphics window height */ |
52 |
> |
static int gwidth, gheight; /* graphics window size */ |
53 |
|
|
54 |
|
static TEXTWIND *comline = NULL; /* our command line */ |
55 |
|
|
70 |
– |
static int c_erase, c_kill; /* erase and kill characters */ |
71 |
– |
|
56 |
|
static char c_queue[64]; /* input queue */ |
57 |
|
static int c_first = 0; /* first character in queue */ |
58 |
|
static int c_last = 0; /* last character in queue */ |
59 |
|
|
60 |
< |
extern char *malloc(); |
60 |
> |
extern char *malloc(), *getcombuf(); |
61 |
|
|
62 |
+ |
extern char *progname; |
63 |
+ |
|
64 |
|
int x_close(), x_clear(), x_paintr(), x_errout(), |
65 |
< |
x_getcur(), x_comout(), x_comin(); |
65 |
> |
x_getcur(), x_comout(), x_comin(), x_flush(); |
66 |
|
|
67 |
|
static struct driver x_driver = { |
68 |
|
x_close, x_clear, x_paintr, x_getcur, |
69 |
< |
x_comout, x_comin, |
84 |
< |
MAXRES, MAXRES |
69 |
> |
x_comout, x_comin, x_flush, 1.0 |
70 |
|
}; |
71 |
|
|
72 |
|
|
73 |
|
struct driver * |
74 |
< |
x_init(name) /* initialize driver */ |
75 |
< |
char *name; |
74 |
> |
x_init(name, id) /* initialize driver */ |
75 |
> |
char *name, *id; |
76 |
|
{ |
77 |
< |
struct sgttyb ttymode; |
78 |
< |
|
79 |
< |
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 |
< |
} |
77 |
> |
char defgeom[32]; |
78 |
> |
OpaqueFrame mainframe; |
79 |
> |
|
80 |
|
ourdisplay = XOpenDisplay(NULL); |
81 |
|
if (ourdisplay == NULL) { |
82 |
|
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
86 |
|
stderr_v("not enough colors\n"); |
87 |
|
return(NULL); |
88 |
|
} |
89 |
< |
if (getmap() < 0) /* not fatal */ |
112 |
< |
stderr_v("cannot allocate colors\n"); |
89 |
> |
make_gmap(GAMMA); /* make color map */ |
90 |
|
|
91 |
|
pickcursor = XCreateCursor(bcross_width, bcross_height, |
92 |
|
bcross_bits, bcross_mask_bits, |
93 |
|
bcross_x_hot, bcross_y_hot, |
94 |
|
BlackPixel, WhitePixel, GXcopy); |
95 |
< |
clientname = name; |
95 |
> |
mainframe.bdrwidth = BORWIDTH; |
96 |
> |
mainframe.border = BlackPixmap; |
97 |
> |
mainframe.background = BlackPixmap; |
98 |
> |
sprintf(defgeom, "=%dx%d+0+22", DisplayWidth()-(2*BORWIDTH), |
99 |
> |
DisplayHeight()-(2*BORWIDTH+22)); |
100 |
> |
gwind = XCreate("X10 display driver", progname, NULL, defgeom, |
101 |
> |
&mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT); |
102 |
> |
if (gwind == 0) { |
103 |
> |
stderr_v("can't create window\n"); |
104 |
> |
return(NULL); |
105 |
> |
} |
106 |
> |
XStoreName(gwind, id); |
107 |
> |
XSelectInput(gwind, KeyPressed|ButtonPressed| |
108 |
> |
ExposeWindow|ExposeRegion|UnmapWindow); |
109 |
> |
gwidth = mainframe.width; |
110 |
> |
gheight = mainframe.height-COMHEIGHT; |
111 |
> |
x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
112 |
> |
x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
113 |
|
x_driver.inpready = 0; |
114 |
|
cmdvec = x_comout; /* set error vectors */ |
115 |
|
if (wrnvec != NULL) |
133 |
|
if (gwind != 0) { |
134 |
|
XDestroyWindow(gwind); |
135 |
|
gwind = 0; |
142 |
– |
gwidth = gheight = 0; |
136 |
|
} |
137 |
|
XFreeCursor(pickcursor); |
138 |
< |
freemap(); |
138 |
> |
freepixels(); |
139 |
|
XCloseDisplay(ourdisplay); |
140 |
|
ourdisplay = NULL; |
141 |
|
} |
148 |
|
if (xres != gwidth || yres != gheight) { /* new window */ |
149 |
|
if (comline != NULL) |
150 |
|
xt_close(comline); |
151 |
< |
if (gwind != 0) |
159 |
< |
XDestroyWindow(gwind); |
160 |
< |
gwind = XCreateWindow(RootWindow, 0, BARHEIGHT, |
161 |
< |
xres, yres+COMHEIGHT, BORWIDTH, |
162 |
< |
BlackPixmap, BlackPixmap); |
163 |
< |
if (gwind == 0) |
164 |
< |
goto fail; |
151 |
> |
XChangeWindow(gwind, xres, yres+COMHEIGHT); |
152 |
|
comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
153 |
< |
if (comline == NULL) |
154 |
< |
goto fail; |
155 |
< |
XMapWindow(gwind); |
156 |
< |
XSelectInput(gwind, |
170 |
< |
KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow); |
171 |
< |
XStoreName(gwind, clientname); |
153 |
> |
if (comline == NULL) { |
154 |
> |
stderr_v("Cannot open command line window\n"); |
155 |
> |
quit(1); |
156 |
> |
} |
157 |
|
gwidth = xres; |
158 |
|
gheight = yres; |
159 |
|
} else /* just clear */ |
160 |
|
XClear(gwind); |
161 |
< |
newmap(); |
162 |
< |
checkinp(); |
161 |
> |
/* reinitialize color table */ |
162 |
> |
if (getpixels() == 0) |
163 |
> |
stderr_v("cannot allocate colors\n"); |
164 |
> |
else |
165 |
> |
new_ctab(ncolors); |
166 |
> |
|
167 |
> |
XMapWindow(gwind); /* make sure it's mapped */ |
168 |
> |
XSync(1); /* discard input */ |
169 |
|
return; |
179 |
– |
fail: |
180 |
– |
stderr_v("Failure opening window in x_clear\n"); |
181 |
– |
quit(1); |
170 |
|
} |
171 |
|
|
172 |
|
|
175 |
|
COLOR col; |
176 |
|
int xmin, ymin, xmax, ymax; |
177 |
|
{ |
178 |
< |
extern long nrays; /* global ray count */ |
191 |
< |
static long lastflush = 0; /* ray count at last flush */ |
192 |
< |
int ndx; |
178 |
> |
extern int xnewcolr(); /* pixel assignment routine */ |
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 |
|
} |
203 |
– |
if (nrays - lastflush >= WFLUSH) { |
204 |
– |
if (ncolors <= 0) /* output necessary for death */ |
205 |
– |
XPixSet(gwind,0,0,1,1,BlackPixel); |
206 |
– |
checkinp(); |
207 |
– |
lastflush = nrays; |
208 |
– |
} |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
static |
188 |
< |
x_comin(inp) /* read in a command line */ |
214 |
< |
char *inp; |
188 |
> |
x_flush() /* flush output */ |
189 |
|
{ |
190 |
+ |
if (ncolors <= 0) /* output necessary for death */ |
191 |
+ |
XPixSet(gwind,0,0,1,1,BlackPixel); |
192 |
+ |
while (XPending() > 0) |
193 |
+ |
getevent(); |
194 |
+ |
|
195 |
+ |
} |
196 |
+ |
|
197 |
+ |
|
198 |
+ |
static |
199 |
+ |
x_comin(inp, prompt) /* read in a command line */ |
200 |
+ |
char *inp, *prompt; |
201 |
+ |
{ |
202 |
|
int x_getc(), x_comout(); |
203 |
|
|
204 |
+ |
if (prompt != NULL) |
205 |
+ |
if (fromcombuf(inp, &x_driver)) |
206 |
+ |
return; |
207 |
+ |
else |
208 |
+ |
xt_puts(prompt, comline); |
209 |
|
xt_cursor(comline, TBLKCURS); |
210 |
< |
editline(inp, x_getc, x_comout, c_erase, c_kill); |
210 |
> |
editline(inp, x_getc, x_comout); |
211 |
|
xt_cursor(comline, TNOCURS); |
212 |
|
} |
213 |
|
|
261 |
|
} |
262 |
|
|
263 |
|
|
273 |
– |
static int |
274 |
– |
colindex(col) /* return index for color */ |
275 |
– |
COLOR col; |
276 |
– |
{ |
277 |
– |
static COLR clr; |
278 |
– |
int hval; |
279 |
– |
register int ndx, i; |
280 |
– |
|
281 |
– |
mapcolor(clr, col); |
282 |
– |
|
283 |
– |
hval = ndx = hashcolr(clr); |
284 |
– |
|
285 |
– |
for (i = 1; i < ncolors; i++) { |
286 |
– |
if (colrtbl[ndx][EXP] == 0) { |
287 |
– |
colrtbl[ndx][RED] = clr[RED]; |
288 |
– |
colrtbl[ndx][GRN] = clr[GRN]; |
289 |
– |
colrtbl[ndx][BLU] = clr[BLU]; |
290 |
– |
colrtbl[ndx][EXP] = COLXS; |
291 |
– |
newcolr(ndx, clr); |
292 |
– |
return(ndx); |
293 |
– |
} |
294 |
– |
if ( colrtbl[ndx][RED] == clr[RED] && |
295 |
– |
colrtbl[ndx][GRN] == clr[GRN] && |
296 |
– |
colrtbl[ndx][BLU] == clr[BLU] ) |
297 |
– |
return(ndx); |
298 |
– |
ndx = (hval + i*i) % ncolors; |
299 |
– |
} |
300 |
– |
return(-1); |
301 |
– |
} |
302 |
– |
|
303 |
– |
|
264 |
|
static |
265 |
< |
newcolr(ndx, clr) /* enter a color into hardware table */ |
265 |
> |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
266 |
|
int ndx; |
267 |
< |
COLR clr; |
267 |
> |
int r, g, b; |
268 |
|
{ |
269 |
|
Color xcolor; |
270 |
|
|
271 |
|
xcolor.pixel = pixval[ndx]; |
272 |
< |
xcolor.red = clr[RED] << 8; |
273 |
< |
xcolor.green = clr[GRN] << 8; |
274 |
< |
xcolor.blue = clr[BLU] << 8; |
272 |
> |
xcolor.red = r << 8; |
273 |
> |
xcolor.green = g << 8; |
274 |
> |
xcolor.blue = b << 8; |
275 |
|
|
276 |
|
XStoreColor(&xcolor); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
< |
static |
281 |
< |
mapcolor(clr, col) /* map to our color space */ |
322 |
< |
COLR clr; |
323 |
< |
COLOR col; |
280 |
> |
static int |
281 |
> |
getpixels() /* get the color map */ |
282 |
|
{ |
325 |
– |
register int i, p; |
326 |
– |
/* compute color table value */ |
327 |
– |
for (i = 0; i < 3; i++) { |
328 |
– |
p = colval(col,i) * 255.0 + 0.5; |
329 |
– |
if (p < 0) p = 0; |
330 |
– |
else if (p > 255) p = 255; |
331 |
– |
clr[i] = colrmap[p][i]; |
332 |
– |
} |
333 |
– |
clr[EXP] = COLXS; |
334 |
– |
} |
335 |
– |
|
336 |
– |
|
337 |
– |
static |
338 |
– |
getmap() /* get the color map */ |
339 |
– |
{ |
283 |
|
int planes; |
284 |
|
|
285 |
+ |
if (ncolors > 0) |
286 |
+ |
return(ncolors); |
287 |
|
for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){ |
343 |
– |
colrtbl = (COLR *)malloc(ncolors*sizeof(COLR)); |
288 |
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
289 |
< |
if (colrtbl == NULL || pixval == NULL) |
290 |
< |
return(-1); |
289 |
> |
if (pixval == NULL) |
290 |
> |
break; |
291 |
|
if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0) |
292 |
< |
return(0); |
349 |
< |
free((char *)colrtbl); |
292 |
> |
return(ncolors); |
293 |
|
free((char *)pixval); |
294 |
|
} |
295 |
< |
ncolors = 0; |
353 |
< |
return(-1); |
295 |
> |
return(ncolors = 0); |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
static |
300 |
< |
freemap() /* free our color map */ |
300 |
> |
freepixels() /* free our pixels */ |
301 |
|
{ |
302 |
|
if (ncolors == 0) |
303 |
|
return; |
304 |
|
XFreeColors(pixval, ncolors, 0); |
363 |
– |
free((char *)colrtbl); |
305 |
|
free((char *)pixval); |
306 |
|
ncolors = 0; |
307 |
|
} |
308 |
|
|
309 |
|
|
369 |
– |
static |
370 |
– |
newmap() /* initialize the color map */ |
371 |
– |
{ |
372 |
– |
double pow(); |
373 |
– |
int val; |
374 |
– |
register int i; |
375 |
– |
|
376 |
– |
for (i = 0; i < 256; i++) { |
377 |
– |
val = pow(i/256.0, 1.0/GAMMA) * colres; |
378 |
– |
val = (val*256 + 128) / colres; |
379 |
– |
colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val; |
380 |
– |
colrmap[i][EXP] = COLXS; |
381 |
– |
} |
382 |
– |
for (i = 0; i < ncolors; i++) |
383 |
– |
colrtbl[i][EXP] = 0; |
384 |
– |
} |
385 |
– |
|
386 |
– |
|
310 |
|
static int |
311 |
|
x_getc() /* get a command character */ |
312 |
|
{ |
328 |
|
getkey(levptr(XKeyPressedEvent)); |
329 |
|
break; |
330 |
|
case ExposeWindow: |
331 |
< |
if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0) |
332 |
< |
if (getmap() < 0) |
410 |
< |
stderr_v("cannot grab colors\n"); |
411 |
< |
else |
412 |
< |
newmap(); |
413 |
< |
/* fall through */ |
331 |
> |
windowchange(levptr(XExposeEvent)); |
332 |
> |
break; |
333 |
|
case ExposeRegion: |
334 |
|
fixwindow(levptr(XExposeEvent)); |
335 |
|
break; |
336 |
|
case UnmapWindow: |
337 |
|
if (levptr(XUnmapEvent)->subwindow == 0) |
338 |
< |
freemap(); |
338 |
> |
freepixels(); |
339 |
|
break; |
340 |
|
case ButtonPressed: /* handled in x_getcur() */ |
341 |
|
break; |
344 |
|
|
345 |
|
|
346 |
|
static |
347 |
+ |
windowchange(eexp) /* process window change event */ |
348 |
+ |
register XExposeEvent *eexp; |
349 |
+ |
{ |
350 |
+ |
if (eexp->subwindow != 0) { |
351 |
+ |
fixwindow(eexp); |
352 |
+ |
return; |
353 |
+ |
} |
354 |
+ |
/* check for change in size */ |
355 |
+ |
if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) { |
356 |
+ |
gwidth = eexp->width; |
357 |
+ |
gheight = eexp->height-COMHEIGHT; |
358 |
+ |
x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
359 |
+ |
x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
360 |
+ |
strcpy(getcombuf(&x_driver), "new\n"); |
361 |
+ |
return; |
362 |
+ |
} |
363 |
+ |
/* remap colors */ |
364 |
+ |
if (getpixels() == 0) { |
365 |
+ |
stderr_v("cannot allocate colors\n"); |
366 |
+ |
return; |
367 |
+ |
} |
368 |
+ |
new_ctab(ncolors); |
369 |
+ |
/* redraw */ |
370 |
+ |
fixwindow(eexp); |
371 |
+ |
} |
372 |
+ |
|
373 |
+ |
|
374 |
+ |
static |
375 |
|
getkey(ekey) /* get input key */ |
376 |
|
register XKeyPressedEvent *ekey; |
377 |
|
{ |
379 |
|
register char *str; |
380 |
|
|
381 |
|
str = XLookupMapping(ekey, &n); |
382 |
< |
while (n-- > 0 && c_last < sizeof(c_queue)) |
382 |
> |
while (n-- > 0 && c_last < sizeof(c_queue)) { |
383 |
|
c_queue[c_last++] = *str++; |
384 |
< |
x_driver.inpready = c_last - c_first; |
384 |
> |
x_driver.inpready++; |
385 |
> |
} |
386 |
|
} |
387 |
|
|
388 |
|
|
391 |
|
register XExposeEvent *eexp; |
392 |
|
{ |
393 |
|
if (eexp->subwindow == 0) |
394 |
< |
repaint(eexp->x, gheight - eexp->y - eexp->height, |
394 |
> |
sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n", |
395 |
> |
eexp->x, gheight - eexp->y - eexp->height, |
396 |
|
eexp->x + eexp->width, gheight - eexp->y); |
397 |
|
else if (eexp->subwindow == comline->w) |
398 |
|
xt_redraw(comline); |