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 30 /* 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 flush() XFlush() |
45 |
– |
|
46 |
– |
#define checkinp() while (QLength() > 0) getevent() |
47 |
– |
|
39 |
|
#define levptr(etype) ((etype *)&thisevent) |
40 |
|
|
50 |
– |
static char *clientname; /* calling client's name */ |
51 |
– |
|
41 |
|
static XEvent thisevent; /* current event */ |
42 |
|
|
54 |
– |
static int colres = 128; /* color resolution */ |
55 |
– |
static COLR colrmap[256]; /* our color mapping */ |
56 |
– |
|
43 |
|
static int ncolors = 0; /* color table size */ |
44 |
< |
static COLR *colrtbl; /* our color table */ |
59 |
< |
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 */ |
68 |
< |
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 |
|
|
72 |
– |
static int c_erase, c_kill; /* erase and kill characters */ |
73 |
– |
|
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 |
< |
int x_close(), x_clear(), x_paintr(), x_errout(), |
81 |
< |
x_getcur(), x_comout(), x_comin(); |
62 |
> |
extern char *progname; |
63 |
|
|
64 |
+ |
static int x_close(), x_clear(), x_paintr(), x_errout(), |
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, |
86 |
< |
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)) { |
97 |
< |
ioctl(0, TIOCGETP, &ttymode); |
98 |
< |
c_erase = ttymode.sg_erase; |
99 |
< |
c_kill = ttymode.sg_kill; |
100 |
< |
} else { |
101 |
< |
c_erase = 'H'-'@'; |
102 |
< |
c_kill = 'U'-'@'; |
103 |
< |
} |
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 */ |
114 |
< |
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; |
144 |
– |
gwidth = gheight = 0; |
136 |
|
} |
137 |
|
XFreeCursor(pickcursor); |
138 |
< |
freemap(); |
138 |
> |
freepixels(); |
139 |
|
XCloseDisplay(ourdisplay); |
140 |
|
ourdisplay = NULL; |
141 |
|
} |
146 |
|
int xres, yres; |
147 |
|
{ |
148 |
|
if (xres != gwidth || yres != gheight) { /* new window */ |
149 |
< |
if (comline != NULL) |
159 |
< |
xt_close(comline); |
160 |
< |
if (gwind != 0) |
161 |
< |
XDestroyWindow(gwind); |
162 |
< |
gwind = XCreateWindow(RootWindow, 0, BARHEIGHT, |
163 |
< |
xres, yres+COMHEIGHT, BORWIDTH, |
164 |
< |
BlackPixmap, BlackPixmap); |
165 |
< |
if (gwind == 0) |
166 |
< |
goto fail; |
167 |
< |
comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
168 |
< |
if (comline == NULL) |
169 |
< |
goto fail; |
170 |
< |
XMapWindow(gwind); |
171 |
< |
XSelectInput(gwind, |
172 |
< |
KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow); |
173 |
< |
XStoreName(gwind, clientname); |
149 |
> |
XChangeWindow(gwind, xres, yres+COMHEIGHT); |
150 |
|
gwidth = xres; |
151 |
|
gheight = yres; |
152 |
|
} else /* just clear */ |
153 |
|
XClear(gwind); |
154 |
< |
newmap(); |
155 |
< |
flush(); |
154 |
> |
/* reinitialize color table */ |
155 |
> |
if (getpixels() == 0) |
156 |
> |
stderr_v("cannot allocate colors\n"); |
157 |
> |
else |
158 |
> |
new_ctab(ncolors); |
159 |
> |
/* open new command line */ |
160 |
> |
if (comline != NULL) |
161 |
> |
xt_close(comline); |
162 |
> |
comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
163 |
> |
if (comline == NULL) { |
164 |
> |
stderr_v("Cannot open command line window\n"); |
165 |
> |
quit(1); |
166 |
> |
} |
167 |
> |
XMapWindow(gwind); /* make sure it's mapped */ |
168 |
> |
XSync(1); /* discard input */ |
169 |
|
return; |
181 |
– |
fail: |
182 |
– |
stderr_v("Failure opening window in x_clear\n"); |
183 |
– |
quit(1); |
170 |
|
} |
171 |
|
|
172 |
|
|
175 |
|
COLOR col; |
176 |
|
int xmin, ymin, xmax, ymax; |
177 |
|
{ |
178 |
< |
extern long nrays; /* global ray count */ |
193 |
< |
static long lastflush = 0; /* ray count at last flush */ |
194 |
< |
int ndx; |
178 |
> |
extern int xnewcolr(); /* pixel assignment routine */ |
179 |
|
|
180 |
|
if (ncolors > 0) { |
197 |
– |
if ((ndx = colindex(col)) < 0) { |
198 |
– |
colres >>= 1; |
199 |
– |
redraw(); |
200 |
– |
return; |
201 |
– |
} |
181 |
|
XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin, |
182 |
< |
pixval[ndx]); |
182 |
> |
pixval[get_pixel(col, xnewcolr)]); |
183 |
|
} |
205 |
– |
if (nrays - lastflush >= WFLUSH) { |
206 |
– |
flush(); |
207 |
– |
lastflush = nrays; |
208 |
– |
} |
209 |
– |
checkinp(); |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
static |
188 |
< |
x_comin(inp) /* read in a command line */ |
215 |
< |
char *inp; |
188 |
> |
x_flush() /* flush output */ |
189 |
|
{ |
190 |
< |
int x_getc(), x_comout(); |
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 |
+ |
extern int x_getc(); |
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 |
|
|
216 |
|
x_comout(out) /* output a string to command line */ |
217 |
|
char *out; |
218 |
|
{ |
219 |
< |
if (comline != NULL) |
220 |
< |
xt_puts(out, comline); |
221 |
< |
flush(); |
219 |
> |
if (comline == NULL) |
220 |
> |
return; |
221 |
> |
xt_puts(out, comline); |
222 |
> |
if (out[strlen(out)-1] == '\n') |
223 |
> |
XFlush(); |
224 |
|
} |
225 |
|
|
226 |
|
|
228 |
|
x_errout(msg) /* output an error message */ |
229 |
|
char *msg; |
230 |
|
{ |
239 |
– |
x_comout(msg); |
231 |
|
stderr_v(msg); /* send to stderr also! */ |
232 |
+ |
x_comout(msg); |
233 |
|
} |
234 |
|
|
235 |
|
|
247 |
|
*yp = gheight-1 - levptr(XKeyOrButtonEvent)->y; |
248 |
|
XFocusKeyboard(RootWindow); |
249 |
|
XUngrabMouse(); |
250 |
< |
flush(); /* insure release */ |
250 |
> |
XFlush(); /* insure release */ |
251 |
|
if (c_last > c_first) /* key pressed */ |
252 |
|
return(x_getc()); |
253 |
|
/* button pressed */ |
263 |
|
} |
264 |
|
|
265 |
|
|
274 |
– |
static int |
275 |
– |
colindex(col) /* return index for color */ |
276 |
– |
COLOR col; |
277 |
– |
{ |
278 |
– |
static COLR clr; |
279 |
– |
int hval; |
280 |
– |
register int ndx, i; |
281 |
– |
|
282 |
– |
mapcolor(clr, col); |
283 |
– |
|
284 |
– |
hval = ndx = hashcolr(clr); |
285 |
– |
|
286 |
– |
for (i = 1; i < ncolors; i++) { |
287 |
– |
if (colrtbl[ndx][EXP] == 0) { |
288 |
– |
colrtbl[ndx][RED] = clr[RED]; |
289 |
– |
colrtbl[ndx][GRN] = clr[GRN]; |
290 |
– |
colrtbl[ndx][BLU] = clr[BLU]; |
291 |
– |
colrtbl[ndx][EXP] = COLXS; |
292 |
– |
newcolr(ndx, clr); |
293 |
– |
return(ndx); |
294 |
– |
} |
295 |
– |
if ( colrtbl[ndx][RED] == clr[RED] && |
296 |
– |
colrtbl[ndx][GRN] == clr[GRN] && |
297 |
– |
colrtbl[ndx][BLU] == clr[BLU] ) |
298 |
– |
return(ndx); |
299 |
– |
ndx = (hval + i*i) % ncolors; |
300 |
– |
} |
301 |
– |
return(-1); |
302 |
– |
} |
303 |
– |
|
304 |
– |
|
266 |
|
static |
267 |
< |
newcolr(ndx, clr) /* enter a color into hardware table */ |
267 |
> |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
268 |
|
int ndx; |
269 |
< |
COLR clr; |
269 |
> |
int r, g, b; |
270 |
|
{ |
271 |
|
Color xcolor; |
272 |
|
|
273 |
|
xcolor.pixel = pixval[ndx]; |
274 |
< |
xcolor.red = clr[RED] << 8; |
275 |
< |
xcolor.green = clr[GRN] << 8; |
276 |
< |
xcolor.blue = clr[BLU] << 8; |
274 |
> |
xcolor.red = r << 8; |
275 |
> |
xcolor.green = g << 8; |
276 |
> |
xcolor.blue = b << 8; |
277 |
|
|
278 |
|
XStoreColor(&xcolor); |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
< |
static |
283 |
< |
mapcolor(clr, col) /* map to our color space */ |
323 |
< |
COLR clr; |
324 |
< |
COLOR col; |
282 |
> |
static int |
283 |
> |
getpixels() /* get the color map */ |
284 |
|
{ |
326 |
– |
register int i, p; |
327 |
– |
/* compute color table value */ |
328 |
– |
for (i = 0; i < 3; i++) { |
329 |
– |
p = colval(col,i) * 255.0 + 0.5; |
330 |
– |
if (p < 0) p = 0; |
331 |
– |
else if (p > 255) p = 255; |
332 |
– |
clr[i] = colrmap[p][i]; |
333 |
– |
} |
334 |
– |
clr[EXP] = COLXS; |
335 |
– |
} |
336 |
– |
|
337 |
– |
|
338 |
– |
static |
339 |
– |
getmap() /* get the color map */ |
340 |
– |
{ |
285 |
|
int planes; |
286 |
|
|
287 |
+ |
if (ncolors > 0) |
288 |
+ |
return(ncolors); |
289 |
|
for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){ |
344 |
– |
colrtbl = (COLR *)malloc(ncolors*sizeof(COLR)); |
290 |
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
291 |
< |
if (colrtbl == NULL || pixval == NULL) |
292 |
< |
return(-1); |
291 |
> |
if (pixval == NULL) |
292 |
> |
break; |
293 |
|
if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0) |
294 |
< |
return(0); |
350 |
< |
free((char *)colrtbl); |
294 |
> |
return(ncolors); |
295 |
|
free((char *)pixval); |
296 |
|
} |
297 |
< |
ncolors = 0; |
354 |
< |
return(-1); |
297 |
> |
return(ncolors = 0); |
298 |
|
} |
299 |
|
|
300 |
|
|
301 |
|
static |
302 |
< |
freemap() /* free our color map */ |
302 |
> |
freepixels() /* free our pixels */ |
303 |
|
{ |
304 |
|
if (ncolors == 0) |
305 |
|
return; |
306 |
|
XFreeColors(pixval, ncolors, 0); |
364 |
– |
free((char *)colrtbl); |
307 |
|
free((char *)pixval); |
308 |
|
ncolors = 0; |
309 |
|
} |
310 |
|
|
311 |
|
|
370 |
– |
static |
371 |
– |
newmap() /* initialize the color map */ |
372 |
– |
{ |
373 |
– |
double pow(); |
374 |
– |
int val; |
375 |
– |
register int i; |
376 |
– |
|
377 |
– |
for (i = 0; i < 256; i++) { |
378 |
– |
val = pow(i/256.0, 1.0/GAMMA) * colres; |
379 |
– |
val = (val*256 + 128) / colres; |
380 |
– |
colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val; |
381 |
– |
colrmap[i][EXP] = COLXS; |
382 |
– |
} |
383 |
– |
for (i = 0; i < ncolors; i++) |
384 |
– |
colrtbl[i][EXP] = 0; |
385 |
– |
} |
386 |
– |
|
387 |
– |
|
312 |
|
static int |
313 |
|
x_getc() /* get a command character */ |
314 |
|
{ |
330 |
|
getkey(levptr(XKeyPressedEvent)); |
331 |
|
break; |
332 |
|
case ExposeWindow: |
333 |
< |
if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0) |
334 |
< |
if (getmap() < 0) |
411 |
< |
stderr_v("cannot grab colors\n"); |
412 |
< |
else |
413 |
< |
newmap(); |
414 |
< |
/* fall through */ |
333 |
> |
windowchange(levptr(XExposeEvent)); |
334 |
> |
break; |
335 |
|
case ExposeRegion: |
336 |
|
fixwindow(levptr(XExposeEvent)); |
337 |
|
break; |
338 |
|
case UnmapWindow: |
339 |
|
if (levptr(XUnmapEvent)->subwindow == 0) |
340 |
< |
freemap(); |
340 |
> |
freepixels(); |
341 |
|
break; |
342 |
|
case ButtonPressed: /* handled in x_getcur() */ |
343 |
|
break; |
346 |
|
|
347 |
|
|
348 |
|
static |
349 |
+ |
windowchange(eexp) /* process window change event */ |
350 |
+ |
register XExposeEvent *eexp; |
351 |
+ |
{ |
352 |
+ |
if (eexp->subwindow != 0) { |
353 |
+ |
fixwindow(eexp); |
354 |
+ |
return; |
355 |
+ |
} |
356 |
+ |
/* check for change in size */ |
357 |
+ |
if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) { |
358 |
+ |
gwidth = eexp->width; |
359 |
+ |
gheight = eexp->height-COMHEIGHT; |
360 |
+ |
x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
361 |
+ |
x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
362 |
+ |
strcpy(getcombuf(&x_driver), "new\n"); |
363 |
+ |
return; |
364 |
+ |
} |
365 |
+ |
/* remap colors */ |
366 |
+ |
if (getpixels() == 0) { |
367 |
+ |
stderr_v("cannot allocate colors\n"); |
368 |
+ |
return; |
369 |
+ |
} |
370 |
+ |
new_ctab(ncolors); |
371 |
+ |
/* redraw */ |
372 |
+ |
fixwindow(eexp); |
373 |
+ |
} |
374 |
+ |
|
375 |
+ |
|
376 |
+ |
static |
377 |
|
getkey(ekey) /* get input key */ |
378 |
|
register XKeyPressedEvent *ekey; |
379 |
|
{ |
381 |
|
register char *str; |
382 |
|
|
383 |
|
str = XLookupMapping(ekey, &n); |
384 |
< |
while (n-- > 0 && c_last < sizeof(c_queue)) |
384 |
> |
while (n-- > 0 && c_last < sizeof(c_queue)) { |
385 |
|
c_queue[c_last++] = *str++; |
386 |
< |
x_driver.inpready = c_last - c_first; |
386 |
> |
x_driver.inpready++; |
387 |
> |
} |
388 |
|
} |
389 |
|
|
390 |
|
|
393 |
|
register XExposeEvent *eexp; |
394 |
|
{ |
395 |
|
if (eexp->subwindow == 0) |
396 |
< |
repaint(eexp->x, gheight - eexp->y - eexp->height, |
396 |
> |
sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n", |
397 |
> |
eexp->x, gheight - eexp->y - eexp->height, |
398 |
|
eexp->x + eexp->width, gheight - eexp->y); |
399 |
|
else if (eexp->subwindow == comline->w) |
400 |
|
xt_redraw(comline); |