10 |
|
* 5/7/87 |
11 |
|
*/ |
12 |
|
|
13 |
< |
#include <stdio.h> |
13 |
> |
#include "standard.h" |
14 |
|
|
15 |
– |
#include <sys/ioctl.h> |
16 |
– |
|
15 |
|
#include <X/Xlib.h> |
16 |
|
#include <X/cursors/bcross.cursor> |
17 |
|
#include <X/cursors/bcross_mask.cursor> |
25 |
|
#define GAMMA 2.2 /* exponent for color correction */ |
26 |
|
|
27 |
|
#define BORWIDTH 5 /* border width */ |
30 |
– |
#define BARHEIGHT 25 /* menu bar size */ |
28 |
|
#define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ |
29 |
+ |
#define MINWIDTH (32*COMCW) /* minimum graphics window width */ |
30 |
+ |
#define MINHEIGHT MINWIDTH /* minimum graphics window height */ |
31 |
|
|
32 |
|
#define COMFN "8x13" /* command line font name */ |
33 |
|
#define COMLH 3 /* number of command lines */ |
34 |
|
#define COMCW 8 /* approx. character width (pixels) */ |
35 |
|
#define COMCH 13 /* approx. character height (pixels) */ |
36 |
|
|
38 |
– |
#ifndef WFLUSH |
39 |
– |
#define WFLUSH 30 /* flush after this many rays */ |
40 |
– |
#endif |
41 |
– |
|
42 |
– |
#define checkinp() while (XPending() > 0) getevent() |
43 |
– |
|
37 |
|
#define levptr(etype) ((etype *)&thisevent) |
38 |
|
|
46 |
– |
static char *clientname; /* calling client's name */ |
47 |
– |
|
39 |
|
static XEvent thisevent; /* current event */ |
40 |
|
|
41 |
|
static int ncolors = 0; /* color table size */ |
47 |
|
|
48 |
|
static Cursor pickcursor = 0; /* cursor used for picking */ |
49 |
|
|
50 |
< |
static int gwidth = 0; /* graphics window width */ |
60 |
< |
static int gheight = 0; /* graphics window height */ |
50 |
> |
static int gwidth, gheight; /* graphics window size */ |
51 |
|
|
52 |
|
static TEXTWIND *comline = NULL; /* our command line */ |
53 |
|
|
55 |
|
static int c_first = 0; /* first character in queue */ |
56 |
|
static int c_last = 0; /* last character in queue */ |
57 |
|
|
58 |
< |
extern char *malloc(); |
58 |
> |
extern char *malloc(), *getcombuf(); |
59 |
|
|
60 |
< |
int x_close(), x_clear(), x_paintr(), x_errout(), |
71 |
< |
x_getcur(), x_comout(), x_comin(); |
60 |
> |
extern char *progname; |
61 |
|
|
62 |
+ |
static int x_close(), x_clear(), x_paintr(), x_errout(), |
63 |
+ |
x_getcur(), x_comout(), x_comin(), x_flush(); |
64 |
+ |
|
65 |
|
static struct driver x_driver = { |
66 |
|
x_close, x_clear, x_paintr, x_getcur, |
67 |
< |
x_comout, x_comin, |
76 |
< |
MAXRES, MAXRES |
67 |
> |
x_comout, x_comin, x_flush, 1.0 |
68 |
|
}; |
69 |
|
|
70 |
|
|
71 |
|
struct driver * |
72 |
< |
x_init(name) /* initialize driver */ |
73 |
< |
char *name; |
72 |
> |
x_init(name, id) /* initialize driver */ |
73 |
> |
char *name, *id; |
74 |
|
{ |
75 |
+ |
extern char *getenv(); |
76 |
+ |
char *gv; |
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"); |
82 |
> |
eputs("cannot open X-windows; DISPLAY variable set?\n"); |
83 |
|
return(NULL); |
84 |
|
} |
85 |
|
if (DisplayPlanes() < 4) { |
86 |
< |
stderr_v("not enough colors\n"); |
86 |
> |
eputs("not enough colors\n"); |
87 |
|
return(NULL); |
88 |
|
} |
89 |
< |
make_gmap(GAMMA); /* make color map */ |
90 |
< |
if (getpixels() < 0) /* get pixels */ |
91 |
< |
stderr_v("cannot allocate colors\n"); |
89 |
> |
/* make color map */ |
90 |
> |
if ((gv = getenv("DISPLAY_GAMMA")) != NULL) |
91 |
> |
make_gmap(atof(gv)); |
92 |
> |
else |
93 |
> |
make_gmap(GAMMA); |
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 |
> |
mainframe.bdrwidth = BORWIDTH; |
100 |
> |
mainframe.border = BlackPixmap; |
101 |
> |
mainframe.background = BlackPixmap; |
102 |
> |
sprintf(defgeom, "=%dx%d+0+22", DisplayWidth()-(2*BORWIDTH), |
103 |
> |
DisplayHeight()-(2*BORWIDTH+22)); |
104 |
> |
gwind = XCreate("X10 display driver", progname, NULL, defgeom, |
105 |
> |
&mainframe, MINWIDTH, MINHEIGHT+COMHEIGHT); |
106 |
> |
if (gwind == 0) { |
107 |
> |
eputs("can't create window\n"); |
108 |
> |
return(NULL); |
109 |
> |
} |
110 |
> |
XStoreName(gwind, id); |
111 |
> |
XSelectInput(gwind, KeyPressed|ButtonPressed| |
112 |
> |
ExposeWindow|ExposeRegion|UnmapWindow); |
113 |
> |
gwidth = mainframe.width; |
114 |
> |
gheight = mainframe.height-COMHEIGHT; |
115 |
> |
x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
116 |
> |
x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
117 |
|
x_driver.inpready = 0; |
118 |
< |
cmdvec = x_comout; /* set error vectors */ |
119 |
< |
if (wrnvec != NULL) |
120 |
< |
wrnvec = x_errout; |
118 |
> |
erract[COMMAND].pf = x_comout; /* set error vectors */ |
119 |
> |
if (erract[WARNING].pf != NULL) |
120 |
> |
erract[WARNING].pf = x_errout; |
121 |
|
return(&x_driver); |
122 |
|
} |
123 |
|
|
125 |
|
static |
126 |
|
x_close() /* close our display */ |
127 |
|
{ |
128 |
< |
cmdvec = NULL; /* reset error vectors */ |
129 |
< |
if (wrnvec != NULL) |
130 |
< |
wrnvec = stderr_v; |
128 |
> |
erract[COMMAND].pf = NULL; /* reset error vectors */ |
129 |
> |
if (erract[WARNING].pf != NULL) |
130 |
> |
erract[WARNING].pf = wputs; |
131 |
|
if (ourdisplay == NULL) |
132 |
|
return; |
133 |
|
if (comline != NULL) { |
137 |
|
if (gwind != 0) { |
138 |
|
XDestroyWindow(gwind); |
139 |
|
gwind = 0; |
125 |
– |
gwidth = gheight = 0; |
140 |
|
} |
141 |
|
XFreeCursor(pickcursor); |
142 |
|
freepixels(); |
150 |
|
int xres, yres; |
151 |
|
{ |
152 |
|
if (xres != gwidth || yres != gheight) { /* new window */ |
153 |
< |
if (comline != NULL) |
140 |
< |
xt_close(comline); |
141 |
< |
if (gwind != 0) |
142 |
< |
XDestroyWindow(gwind); |
143 |
< |
gwind = XCreateWindow(RootWindow, 0, BARHEIGHT, |
144 |
< |
xres, yres+COMHEIGHT, BORWIDTH, |
145 |
< |
BlackPixmap, BlackPixmap); |
146 |
< |
if (gwind == 0) |
147 |
< |
goto fail; |
148 |
< |
comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
149 |
< |
if (comline == NULL) |
150 |
< |
goto fail; |
151 |
< |
XMapWindow(gwind); |
152 |
< |
XSelectInput(gwind, |
153 |
< |
KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow); |
154 |
< |
XStoreName(gwind, clientname); |
153 |
> |
XChangeWindow(gwind, xres, yres+COMHEIGHT); |
154 |
|
gwidth = xres; |
155 |
|
gheight = yres; |
156 |
|
} else /* just clear */ |
157 |
|
XClear(gwind); |
158 |
< |
/* redo color table */ |
159 |
< |
new_ctab(ncolors); |
160 |
< |
checkinp(); |
158 |
> |
/* reinitialize color table */ |
159 |
> |
if (getpixels() == 0) |
160 |
> |
eputs("cannot allocate colors\n"); |
161 |
> |
else |
162 |
> |
new_ctab(ncolors); |
163 |
> |
/* open new command line */ |
164 |
> |
if (comline != NULL) |
165 |
> |
xt_close(comline); |
166 |
> |
comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
167 |
> |
if (comline == NULL) { |
168 |
> |
eputs("Cannot open command line window\n"); |
169 |
> |
quit(1); |
170 |
> |
} |
171 |
> |
XMapWindow(gwind); /* make sure it's mapped */ |
172 |
> |
XSync(1); /* discard input */ |
173 |
|
return; |
163 |
– |
fail: |
164 |
– |
stderr_v("Failure opening window in x_clear\n"); |
165 |
– |
quit(1); |
174 |
|
} |
175 |
|
|
176 |
|
|
179 |
|
COLOR col; |
180 |
|
int xmin, ymin, xmax, ymax; |
181 |
|
{ |
174 |
– |
extern long nrays; /* global ray count */ |
182 |
|
extern int xnewcolr(); /* pixel assignment routine */ |
176 |
– |
static long lastflush = 0; /* ray count at last flush */ |
183 |
|
|
184 |
|
if (ncolors > 0) { |
185 |
|
XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin, |
186 |
|
pixval[get_pixel(col, xnewcolr)]); |
187 |
|
} |
182 |
– |
if (nrays - lastflush >= WFLUSH) { |
183 |
– |
if (ncolors <= 0) /* output necessary for death */ |
184 |
– |
XPixSet(gwind,0,0,1,1,BlackPixel); |
185 |
– |
checkinp(); |
186 |
– |
lastflush = nrays; |
187 |
– |
} |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
static |
192 |
< |
x_comin(inp) /* read in a command line */ |
193 |
< |
char *inp; |
192 |
> |
x_flush() /* flush output */ |
193 |
|
{ |
194 |
< |
int x_getc(), x_comout(); |
194 |
> |
if (ncolors <= 0) /* output necessary for death */ |
195 |
> |
XPixSet(gwind,0,0,1,1,BlackPixel); |
196 |
> |
while (XPending() > 0) |
197 |
> |
getevent(); |
198 |
|
|
199 |
+ |
} |
200 |
+ |
|
201 |
+ |
|
202 |
+ |
static |
203 |
+ |
x_comin(inp, prompt) /* read in a command line */ |
204 |
+ |
char *inp, *prompt; |
205 |
+ |
{ |
206 |
+ |
extern int x_getc(); |
207 |
+ |
|
208 |
+ |
if (prompt != NULL) |
209 |
+ |
if (fromcombuf(inp, &x_driver)) |
210 |
+ |
return; |
211 |
+ |
else |
212 |
+ |
xt_puts(prompt, comline); |
213 |
|
xt_cursor(comline, TBLKCURS); |
214 |
|
editline(inp, x_getc, x_comout); |
215 |
|
xt_cursor(comline, TNOCURS); |
220 |
|
x_comout(out) /* output a string to command line */ |
221 |
|
char *out; |
222 |
|
{ |
223 |
< |
if (comline != NULL) |
224 |
< |
xt_puts(out, comline); |
225 |
< |
XFlush(); |
223 |
> |
if (comline == NULL) |
224 |
> |
return; |
225 |
> |
xt_puts(out, comline); |
226 |
> |
if (out[strlen(out)-1] == '\n') |
227 |
> |
XFlush(); |
228 |
|
} |
229 |
|
|
230 |
|
|
232 |
|
x_errout(msg) /* output an error message */ |
233 |
|
char *msg; |
234 |
|
{ |
235 |
+ |
eputs(msg); /* send to stderr also! */ |
236 |
|
x_comout(msg); |
218 |
– |
stderr_v(msg); /* send to stderr also! */ |
237 |
|
} |
238 |
|
|
239 |
|
|
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
< |
static |
286 |
> |
static int |
287 |
|
getpixels() /* get the color map */ |
288 |
|
{ |
289 |
|
int planes; |
290 |
|
|
291 |
+ |
if (ncolors > 0) |
292 |
+ |
return(ncolors); |
293 |
|
for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){ |
294 |
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
295 |
|
if (pixval == NULL) |
296 |
|
break; |
297 |
|
if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0) |
298 |
< |
return(0); |
298 |
> |
return(ncolors); |
299 |
|
free((char *)pixval); |
300 |
|
} |
301 |
< |
ncolors = 0; |
282 |
< |
return(-1); |
301 |
> |
return(ncolors = 0); |
302 |
|
} |
303 |
|
|
304 |
|
|
334 |
|
getkey(levptr(XKeyPressedEvent)); |
335 |
|
break; |
336 |
|
case ExposeWindow: |
337 |
< |
if (levptr(XExposeEvent)->subwindow == 0) { |
338 |
< |
if (ncolors == 0 && getpixels() < 0) { |
320 |
< |
stderr_v("cannot allocate colors\n"); |
321 |
< |
break; |
322 |
< |
} |
323 |
< |
new_ctab(ncolors); |
324 |
< |
} |
325 |
< |
/* fall through */ |
337 |
> |
windowchange(levptr(XExposeEvent)); |
338 |
> |
break; |
339 |
|
case ExposeRegion: |
340 |
|
fixwindow(levptr(XExposeEvent)); |
341 |
|
break; |
350 |
|
|
351 |
|
|
352 |
|
static |
353 |
+ |
windowchange(eexp) /* process window change event */ |
354 |
+ |
register XExposeEvent *eexp; |
355 |
+ |
{ |
356 |
+ |
if (eexp->subwindow != 0) { |
357 |
+ |
fixwindow(eexp); |
358 |
+ |
return; |
359 |
+ |
} |
360 |
+ |
/* check for change in size */ |
361 |
+ |
if (eexp->width != gwidth || eexp->height-COMHEIGHT != gheight) { |
362 |
+ |
gwidth = eexp->width; |
363 |
+ |
gheight = eexp->height-COMHEIGHT; |
364 |
+ |
x_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
365 |
+ |
x_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
366 |
+ |
strcpy(getcombuf(&x_driver), "new\n"); |
367 |
+ |
return; |
368 |
+ |
} |
369 |
+ |
/* remap colors */ |
370 |
+ |
if (getpixels() == 0) { |
371 |
+ |
eputs("cannot allocate colors\n"); |
372 |
+ |
return; |
373 |
+ |
} |
374 |
+ |
new_ctab(ncolors); |
375 |
+ |
/* redraw */ |
376 |
+ |
fixwindow(eexp); |
377 |
+ |
} |
378 |
+ |
|
379 |
+ |
|
380 |
+ |
static |
381 |
|
getkey(ekey) /* get input key */ |
382 |
|
register XKeyPressedEvent *ekey; |
383 |
|
{ |
385 |
|
register char *str; |
386 |
|
|
387 |
|
str = XLookupMapping(ekey, &n); |
388 |
< |
while (n-- > 0 && c_last < sizeof(c_queue)) |
388 |
> |
while (n-- > 0 && c_last < sizeof(c_queue)) { |
389 |
|
c_queue[c_last++] = *str++; |
390 |
< |
x_driver.inpready = c_last - c_first; |
390 |
> |
x_driver.inpready++; |
391 |
> |
} |
392 |
|
} |
393 |
|
|
394 |
|
|
397 |
|
register XExposeEvent *eexp; |
398 |
|
{ |
399 |
|
if (eexp->subwindow == 0) |
400 |
< |
repaint(eexp->x, gheight - eexp->y - eexp->height, |
400 |
> |
sprintf(getcombuf(&x_driver), "repaint %d %d %d %d\n", |
401 |
> |
eexp->x, gheight - eexp->y - eexp->height, |
402 |
|
eexp->x + eexp->width, gheight - eexp->y); |
403 |
|
else if (eexp->subwindow == comline->w) |
404 |
|
xt_redraw(comline); |