1 |
greg |
1.1 |
#ifndef lint |
2 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
3 |
|
|
#endif |
4 |
|
|
|
5 |
|
|
/* Copyright (c) 1989 Regents of the University of California */ |
6 |
|
|
|
7 |
|
|
/* |
8 |
greg |
1.4 |
* x11.c - driver for X-windows version 11.3 |
9 |
greg |
1.1 |
* |
10 |
greg |
1.4 |
* Jan 1990 |
11 |
greg |
1.1 |
*/ |
12 |
|
|
|
13 |
|
|
#include <stdio.h> |
14 |
|
|
|
15 |
|
|
#include <sys/ioctl.h> |
16 |
|
|
|
17 |
|
|
#include <X11/Xlib.h> |
18 |
greg |
1.4 |
#include <X11/cursorfont.h> |
19 |
|
|
#include <X11/Xutil.h> |
20 |
greg |
1.1 |
|
21 |
|
|
#include "color.h" |
22 |
|
|
#include "driver.h" |
23 |
|
|
#include "x11twind.h" |
24 |
|
|
|
25 |
|
|
#define GAMMA 2.2 /* exponent for color correction */ |
26 |
|
|
|
27 |
greg |
1.4 |
#define MINWIDTH (4*COMCW) /* minimum graphics window width */ |
28 |
|
|
#define MINHEIGHT 16 /* minimum graphics window height */ |
29 |
|
|
|
30 |
greg |
1.1 |
#define BORWIDTH 5 /* border width */ |
31 |
|
|
#define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ |
32 |
|
|
|
33 |
|
|
#define COMFN "8x13" /* command line font name */ |
34 |
|
|
#define COMLH 3 /* number of command lines */ |
35 |
|
|
#define COMCW 8 /* approx. character width (pixels) */ |
36 |
|
|
#define COMCH 14 /* approx. character height (pixels) */ |
37 |
|
|
|
38 |
greg |
1.4 |
#define ourscreen DefaultScreen(ourdisplay) |
39 |
|
|
#define ourroot RootWindow(ourdisplay,ourscreen) |
40 |
|
|
#define ourwhite WhitePixel(ourdisplay,ourscreen) |
41 |
|
|
#define ourblack BlackPixel(ourdisplay,ourscreen) |
42 |
|
|
|
43 |
|
|
#define levptr(etype) ((etype *)¤tevent) |
44 |
greg |
1.1 |
|
45 |
greg |
1.4 |
static XEvent currentevent; /* current event */ |
46 |
greg |
1.1 |
|
47 |
|
|
static int ncolors = 0; /* color table size */ |
48 |
|
|
static int *pixval = NULL; /* allocated pixels */ |
49 |
|
|
|
50 |
|
|
static Display *ourdisplay = NULL; /* our display */ |
51 |
|
|
|
52 |
|
|
static Window gwind = 0; /* our graphics window */ |
53 |
|
|
|
54 |
|
|
static Cursor pickcursor = 0; /* cursor used for picking */ |
55 |
|
|
|
56 |
greg |
1.4 |
static int gwidth, gheight; /* graphics window size */ |
57 |
greg |
1.1 |
|
58 |
|
|
static TEXTWIND *comline = NULL; /* our command line */ |
59 |
|
|
|
60 |
|
|
static char c_queue[64]; /* input queue */ |
61 |
|
|
static int c_first = 0; /* first character in queue */ |
62 |
|
|
static int c_last = 0; /* last character in queue */ |
63 |
|
|
|
64 |
|
|
static GC ourgc = 0; /* our graphics context for drawing */ |
65 |
|
|
|
66 |
|
|
static Colormap ourmap; /* our color map */ |
67 |
|
|
|
68 |
|
|
extern char *malloc(); |
69 |
|
|
|
70 |
|
|
int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
71 |
greg |
1.5 |
x11_getcur(), x11_comout(), x11_comin(), x11_flush(); |
72 |
greg |
1.1 |
|
73 |
|
|
static struct driver x11_driver = { |
74 |
|
|
x11_close, x11_clear, x11_paintr, x11_getcur, |
75 |
greg |
1.5 |
x11_comout, x11_comin, x11_flush, 1.0 |
76 |
greg |
1.1 |
}; |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
struct driver * |
80 |
|
|
x11_init(name, id) /* initialize driver */ |
81 |
|
|
char *name, *id; |
82 |
|
|
{ |
83 |
greg |
1.4 |
XWMHints ourxwmhints; |
84 |
greg |
1.1 |
Pixmap bmCursorSrc, bmCursorMsk; |
85 |
|
|
|
86 |
|
|
ourdisplay = XOpenDisplay(NULL); |
87 |
|
|
if (ourdisplay == NULL) { |
88 |
|
|
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
89 |
|
|
return(NULL); |
90 |
|
|
} |
91 |
greg |
1.4 |
if (DisplayPlanes(ourdisplay, ourscreen) < 4) { |
92 |
greg |
1.1 |
stderr_v("not enough colors\n"); |
93 |
|
|
return(NULL); |
94 |
|
|
} |
95 |
greg |
1.4 |
ourmap = DefaultColormap(ourdisplay,ourscreen); |
96 |
greg |
1.1 |
make_gmap(GAMMA); /* make color map */ |
97 |
|
|
/* create a cursor */ |
98 |
|
|
pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); |
99 |
greg |
1.4 |
/* open window */ |
100 |
|
|
gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0, |
101 |
|
|
DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, |
102 |
|
|
DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, |
103 |
|
|
BORWIDTH, ourblack, ourwhite); |
104 |
|
|
if (gwind == 0) { |
105 |
|
|
stderr_v("cannot create window\n"); |
106 |
|
|
return(NULL); |
107 |
|
|
} |
108 |
|
|
XStoreName(ourdisplay, gwind, id); |
109 |
|
|
ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); |
110 |
|
|
ourxwmhints.flags = InputHint; |
111 |
|
|
ourxwmhints.input = True; |
112 |
|
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
113 |
|
|
XSelectInput(ourdisplay, gwind, ExposureMask); |
114 |
|
|
XMapWindow(ourdisplay, gwind); |
115 |
|
|
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent)); |
116 |
|
|
gwidth = levptr(XExposeEvent)->width; |
117 |
|
|
gheight = levptr(XExposeEvent)->height - COMHEIGHT; |
118 |
|
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
119 |
|
|
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
120 |
greg |
1.1 |
x11_driver.inpready = 0; |
121 |
|
|
cmdvec = x11_comout; /* set error vectors */ |
122 |
|
|
if (wrnvec != NULL) |
123 |
|
|
wrnvec = x11_errout; |
124 |
|
|
return(&x11_driver); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
static |
129 |
|
|
x11_close() /* close our display */ |
130 |
|
|
{ |
131 |
|
|
cmdvec = NULL; /* reset error vectors */ |
132 |
|
|
if (wrnvec != NULL) |
133 |
|
|
wrnvec = stderr_v; |
134 |
|
|
if (ourdisplay == NULL) |
135 |
|
|
return; |
136 |
|
|
if (comline != NULL) { |
137 |
|
|
xt_close(comline); |
138 |
|
|
comline = NULL; |
139 |
|
|
} |
140 |
|
|
if (gwind != 0) { |
141 |
|
|
XFreeGC(ourdisplay, ourgc); |
142 |
|
|
XDestroyWindow(ourdisplay, gwind); |
143 |
|
|
gwind = 0; |
144 |
|
|
ourgc = 0; |
145 |
|
|
} |
146 |
|
|
XFreeCursor(ourdisplay, pickcursor); |
147 |
|
|
freepixels(); |
148 |
|
|
XCloseDisplay(ourdisplay); |
149 |
|
|
ourdisplay = NULL; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
|
153 |
|
|
static |
154 |
|
|
x11_clear(xres, yres) /* clear our display */ |
155 |
|
|
int xres, yres; |
156 |
|
|
{ |
157 |
|
|
if (xres != gwidth || yres != gheight) { /* change window */ |
158 |
|
|
if (comline != NULL) |
159 |
|
|
xt_close(comline); |
160 |
greg |
1.4 |
XSelectInput(ourdisplay, gwind, 0); |
161 |
|
|
XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); |
162 |
greg |
1.1 |
comline = xt_open(ourdisplay, |
163 |
greg |
1.4 |
DefaultGC(ourdisplay,ourscreen), |
164 |
greg |
1.1 |
gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); |
165 |
greg |
1.4 |
if (comline == NULL) { |
166 |
|
|
stderr_v("Cannot open command line window\n"); |
167 |
|
|
quit(1); |
168 |
|
|
} |
169 |
|
|
XSelectInput(ourdisplay, comline->w, ExposureMask); |
170 |
greg |
1.1 |
gwidth = xres; |
171 |
|
|
gheight = yres; |
172 |
greg |
1.4 |
XSync(ourdisplay, 1); /* discard input */ |
173 |
|
|
sleep(2); /* wait for window manager */ |
174 |
|
|
} |
175 |
|
|
XClearWindow(ourdisplay, gwind); |
176 |
greg |
1.1 |
/* reinitialize color table */ |
177 |
greg |
1.4 |
if (getpixels() == 0) |
178 |
greg |
1.1 |
stderr_v("cannot allocate colors\n"); |
179 |
|
|
else |
180 |
|
|
new_ctab(ncolors); |
181 |
greg |
1.4 |
|
182 |
|
|
XSelectInput(ourdisplay, gwind, |
183 |
|
|
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
184 |
greg |
1.1 |
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
static |
188 |
|
|
x11_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
189 |
|
|
COLOR col; |
190 |
|
|
int xmin, ymin, xmax, ymax; |
191 |
|
|
{ |
192 |
|
|
extern int xnewcolr(); /* pixel assignment routine */ |
193 |
|
|
|
194 |
|
|
if (ncolors > 0) { |
195 |
|
|
XSetForeground(ourdisplay, ourgc, |
196 |
|
|
pixval[get_pixel(col, xnewcolr)]); |
197 |
|
|
XFillRectangle(ourdisplay, gwind, |
198 |
|
|
ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); |
199 |
|
|
} |
200 |
greg |
1.5 |
} |
201 |
|
|
|
202 |
|
|
|
203 |
|
|
static |
204 |
|
|
x11_flush() /* flush output */ |
205 |
|
|
{ |
206 |
|
|
if (ncolors <= 0) /* output necessary for death */ |
207 |
|
|
XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1); |
208 |
|
|
while (XPending(ourdisplay) > 0) |
209 |
|
|
getevent(); |
210 |
greg |
1.1 |
} |
211 |
|
|
|
212 |
|
|
|
213 |
|
|
static |
214 |
greg |
1.3 |
x11_comin(inp, prompt) /* read in a command line */ |
215 |
|
|
char *inp, *prompt; |
216 |
greg |
1.1 |
{ |
217 |
|
|
int x11_getc(), x11_comout(); |
218 |
|
|
|
219 |
greg |
1.3 |
if (prompt != NULL) |
220 |
greg |
1.4 |
if (fromcombuf(inp, &x11_driver)) |
221 |
|
|
return; |
222 |
|
|
else |
223 |
|
|
xt_puts(prompt, comline); |
224 |
greg |
1.1 |
xt_cursor(comline, TBLKCURS); |
225 |
|
|
editline(inp, x11_getc, x11_comout); |
226 |
|
|
xt_cursor(comline, TNOCURS); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
|
230 |
|
|
static |
231 |
|
|
x11_comout(out) /* output a string to command line */ |
232 |
|
|
char *out; |
233 |
|
|
{ |
234 |
|
|
if (comline != NULL) |
235 |
|
|
xt_puts(out, comline); |
236 |
|
|
XFlush(ourdisplay); |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
|
240 |
|
|
static |
241 |
|
|
x11_errout(msg) /* output an error message */ |
242 |
|
|
char *msg; |
243 |
|
|
{ |
244 |
|
|
x11_comout(msg); |
245 |
|
|
stderr_v(msg); /* send to stderr also! */ |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
|
249 |
|
|
static int |
250 |
|
|
x11_getcur(xp, yp) /* get cursor position */ |
251 |
|
|
int *xp, *yp; |
252 |
|
|
{ |
253 |
|
|
while (XGrabPointer(ourdisplay, gwind, True, ButtonPressMask, |
254 |
|
|
GrabModeAsync, GrabModeAsync, None, pickcursor, |
255 |
|
|
CurrentTime) != GrabSuccess) |
256 |
|
|
sleep(2); |
257 |
|
|
|
258 |
|
|
do |
259 |
|
|
getevent(); |
260 |
|
|
while (c_last <= c_first && levptr(XEvent)->type != ButtonPress); |
261 |
|
|
*xp = levptr(XButtonPressedEvent)->x; |
262 |
|
|
*yp = gheight-1 - levptr(XButtonPressedEvent)->y; |
263 |
|
|
XUngrabPointer(ourdisplay, CurrentTime); |
264 |
|
|
XFlush(ourdisplay); /* insure release */ |
265 |
|
|
if (c_last > c_first) /* key pressed */ |
266 |
|
|
return(x11_getc()); |
267 |
|
|
/* button pressed */ |
268 |
|
|
if (levptr(XButtonPressedEvent)->button & Button1) |
269 |
|
|
return(MB1); |
270 |
|
|
if (levptr(XButtonPressedEvent)->button & Button2) |
271 |
|
|
return(MB2); |
272 |
|
|
if (levptr(XButtonPressedEvent)->button & Button3) |
273 |
|
|
return(MB3); |
274 |
|
|
if (levptr(XButtonPressedEvent)->button & (Button4|Button5)) |
275 |
|
|
return(MB1); |
276 |
|
|
return(ABORT); |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
|
280 |
|
|
static |
281 |
|
|
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
282 |
|
|
int ndx; |
283 |
|
|
int r, g, b; |
284 |
|
|
{ |
285 |
|
|
XColor xcolor; |
286 |
|
|
|
287 |
|
|
xcolor.pixel = pixval[ndx]; |
288 |
|
|
xcolor.red = r << 8; |
289 |
|
|
xcolor.green = g << 8; |
290 |
|
|
xcolor.blue = b << 8; |
291 |
|
|
xcolor.flags = DoRed|DoGreen|DoBlue; |
292 |
|
|
|
293 |
|
|
XStoreColor(ourdisplay, ourmap, &xcolor); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
|
297 |
|
|
static int |
298 |
|
|
getpixels() /* get the color map */ |
299 |
|
|
{ |
300 |
greg |
1.4 |
Visual *ourvis = DefaultVisual(ourdisplay,ourscreen); |
301 |
greg |
1.1 |
|
302 |
greg |
1.4 |
if (ncolors > 0) |
303 |
|
|
return(ncolors); |
304 |
greg |
1.1 |
for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){ |
305 |
|
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
306 |
|
|
if (pixval == NULL) |
307 |
|
|
break; |
308 |
|
|
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, |
309 |
|
|
pixval,ncolors) != 0) |
310 |
|
|
return(ncolors); |
311 |
|
|
free((char *)pixval); |
312 |
|
|
} |
313 |
|
|
return(ncolors = 0); |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
|
317 |
|
|
static |
318 |
|
|
freepixels() /* free our pixels */ |
319 |
|
|
{ |
320 |
|
|
if (ncolors == 0) |
321 |
|
|
return; |
322 |
|
|
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
323 |
|
|
ncolors = 0; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
|
327 |
|
|
static int |
328 |
|
|
x11_getc() /* get a command character */ |
329 |
|
|
{ |
330 |
|
|
while (c_last <= c_first) { |
331 |
|
|
c_first = c_last = 0; /* reset */ |
332 |
|
|
getevent(); /* wait for key */ |
333 |
|
|
} |
334 |
|
|
x11_driver.inpready--; |
335 |
|
|
return(c_queue[c_first++]); |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
|
339 |
|
|
static |
340 |
|
|
getevent() /* get next event */ |
341 |
|
|
{ |
342 |
|
|
XNextEvent(ourdisplay, levptr(XEvent)); |
343 |
|
|
switch (levptr(XEvent)->type) { |
344 |
greg |
1.4 |
case ConfigureNotify: |
345 |
|
|
resizewindow(levptr(XConfigureEvent)); |
346 |
|
|
break; |
347 |
|
|
case UnmapNotify: |
348 |
|
|
freepixels(); |
349 |
|
|
break; |
350 |
|
|
case MapNotify: |
351 |
|
|
if (getpixels() == 0) |
352 |
|
|
stderr_v("Cannot allocate colors\n"); |
353 |
|
|
else |
354 |
|
|
new_ctab(ncolors); |
355 |
|
|
break; |
356 |
|
|
case Expose: |
357 |
|
|
fixwindow(levptr(XExposeEvent)); |
358 |
|
|
break; |
359 |
greg |
1.1 |
case KeyPress: |
360 |
|
|
getkey(levptr(XKeyPressedEvent)); |
361 |
|
|
break; |
362 |
|
|
case ButtonPress: |
363 |
|
|
break; |
364 |
|
|
} |
365 |
|
|
} |
366 |
|
|
|
367 |
|
|
|
368 |
|
|
static |
369 |
|
|
getkey(ekey) /* get input key */ |
370 |
|
|
register XKeyPressedEvent *ekey; |
371 |
|
|
{ |
372 |
|
|
c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, |
373 |
|
|
NULL, NULL); |
374 |
|
|
x11_driver.inpready = c_last-c_first; |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
|
378 |
|
|
static |
379 |
|
|
fixwindow(eexp) /* repair damage to window */ |
380 |
|
|
register XExposeEvent *eexp; |
381 |
|
|
{ |
382 |
greg |
1.4 |
if (eexp->window == gwind) { |
383 |
|
|
sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n", |
384 |
|
|
eexp->x, gheight - eexp->y - eexp->height, |
385 |
greg |
1.1 |
eexp->x + eexp->width, gheight - eexp->y); |
386 |
greg |
1.4 |
} else if (eexp->window == comline->w) { |
387 |
|
|
if (eexp->count == 0) |
388 |
|
|
xt_redraw(comline); |
389 |
|
|
} |
390 |
greg |
1.1 |
} |
391 |
greg |
1.4 |
|
392 |
|
|
|
393 |
|
|
static |
394 |
|
|
resizewindow(ersz) /* resize window */ |
395 |
|
|
register XConfigureEvent *ersz; |
396 |
|
|
{ |
397 |
|
|
if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight) |
398 |
|
|
return; |
399 |
|
|
|
400 |
|
|
gwidth = ersz->width; |
401 |
|
|
gheight = ersz->height-COMHEIGHT; |
402 |
|
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
403 |
|
|
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
404 |
|
|
|
405 |
|
|
strcpy(getcombuf(&x11_driver), "new\n"); |
406 |
|
|
} |