1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* x11.c - driver for X-windows version 11 |
9 |
– |
* |
10 |
– |
* Jan 1990 |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include <stdio.h> |
9 |
< |
#include <math.h> |
8 |
> |
#include "copyright.h" |
9 |
> |
|
10 |
> |
#include "standard.h" |
11 |
|
#include <sys/ioctl.h> |
12 |
< |
#include <fcntl.h> |
12 |
> |
#ifdef sparc |
13 |
> |
#include <sys/conf.h> |
14 |
> |
#include <sys/file.h> |
15 |
> |
#include <sys/filio.h> |
16 |
> |
#endif |
17 |
> |
#if !defined(FNDELAY) && defined(O_NONBLOCK) |
18 |
> |
#define FNDELAY O_NONBLOCK |
19 |
> |
#endif |
20 |
|
|
21 |
|
#include <X11/Xlib.h> |
22 |
|
#include <X11/cursorfont.h> |
79 |
|
|
80 |
|
static int inpcheck; /* whence to check input */ |
81 |
|
|
82 |
< |
extern char *malloc(), *getcombuf(); |
82 |
> |
static int x11_getcur(); |
83 |
|
|
84 |
< |
static int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
85 |
< |
x11_getcur(), x11_comout(), x11_comin(), x11_flush(); |
84 |
> |
static void x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
85 |
> |
x11_comout(), x11_comin(), x11_flush(); |
86 |
|
|
87 |
< |
static int std_comin(), std_comout(); |
87 |
> |
static void std_comin(), std_comout(); |
88 |
|
|
89 |
|
static struct driver x11_driver = { |
90 |
|
x11_close, x11_clear, x11_paintr, x11_getcur, |
91 |
|
NULL, NULL, x11_flush, 1.0 |
92 |
|
}; |
93 |
|
|
94 |
< |
static int getpixels(), xnewcolr(), freepixels(), resizewindow(), |
95 |
< |
getevent(), getkey(), fixwindow(), x11_getc(); |
94 |
> |
static int getpixels(), x11_getc(); |
95 |
> |
static void xnewcolr(), freepixels(), resizewindow(), |
96 |
> |
getevent(), getkey(), fixwindow(); |
97 |
|
static unsigned long true_pixel(); |
98 |
|
|
99 |
|
|
101 |
|
x11_init(name, id) /* initialize driver */ |
102 |
|
char *name, *id; |
103 |
|
{ |
100 |
– |
extern char *getenv(); |
104 |
|
char *gv; |
105 |
|
int nplanes; |
106 |
|
XSetWindowAttributes ourwinattr; |
109 |
|
/* open display server */ |
110 |
|
ourdisplay = XOpenDisplay(NULL); |
111 |
|
if (ourdisplay == NULL) { |
112 |
< |
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
112 |
> |
eputs("cannot open X-windows; DISPLAY variable set?\n"); |
113 |
|
return(NULL); |
114 |
|
} |
115 |
|
/* find a usable visual */ |
116 |
|
nplanes = DisplayPlanes(ourdisplay, ourscreen); |
117 |
|
if (XMatchVisualInfo(ourdisplay,ourscreen, |
118 |
< |
24,TrueColor,&ourvinfo) || |
118 |
> |
nplanes>12?nplanes:24,TrueColor,&ourvinfo) || |
119 |
|
XMatchVisualInfo(ourdisplay,ourscreen, |
120 |
< |
24,DirectColor,&ourvinfo)) { |
120 |
> |
nplanes>12?nplanes:24,DirectColor,&ourvinfo)) { |
121 |
|
ourblack = 0; |
122 |
|
ourwhite = ourvinfo.red_mask | |
123 |
|
ourvinfo.green_mask | |
124 |
|
ourvinfo.blue_mask ; |
125 |
|
} else { |
126 |
|
if (nplanes < 4) { |
127 |
< |
stderr_v("not enough colors\n"); |
127 |
> |
eputs("not enough colors\n"); |
128 |
|
return(NULL); |
129 |
|
} |
130 |
|
if (!XMatchVisualInfo(ourdisplay,ourscreen, |
131 |
|
nplanes,PseudoColor,&ourvinfo) && |
132 |
|
!XMatchVisualInfo(ourdisplay,ourscreen, |
133 |
|
nplanes,GrayScale,&ourvinfo)) { |
134 |
< |
stderr_v("unsupported visual type\n"); |
134 |
> |
eputs("unsupported visual type\n"); |
135 |
|
return(NULL); |
136 |
|
} |
137 |
|
ourblack = BlackPixel(ourdisplay,ourscreen); |
146 |
|
/* X11 command line or no? */ |
147 |
|
if (!strcmp(name, "x11")) |
148 |
|
comheight = COMHEIGHT; |
149 |
< |
else /* "x11d" */ |
149 |
> |
else /* "x11d" */ { |
150 |
|
comheight = 0; |
151 |
+ |
#ifndef FNDELAY |
152 |
+ |
eputs("warning: x11d driver not fully functional on this machine\n"); |
153 |
+ |
#endif |
154 |
+ |
} |
155 |
|
/* open window */ |
156 |
|
ourwinattr.background_pixel = ourblack; |
157 |
|
ourwinattr.border_pixel = ourblack; |
164 |
|
BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual, |
165 |
|
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
166 |
|
if (gwind == 0) { |
167 |
< |
stderr_v("cannot create window\n"); |
167 |
> |
eputs("cannot create window\n"); |
168 |
|
return(NULL); |
169 |
|
} |
170 |
|
XStoreName(ourdisplay, gwind, id); |
193 |
|
if (comheight) { |
194 |
|
x11_driver.comin = x11_comin; |
195 |
|
x11_driver.comout = x11_comout; |
196 |
< |
cmdvec = x11_comout; |
197 |
< |
if (wrnvec != NULL) |
198 |
< |
wrnvec = x11_errout; |
196 |
> |
erract[COMMAND].pf = x11_comout; |
197 |
> |
if (erract[WARNING].pf != NULL) |
198 |
> |
erract[WARNING].pf = x11_errout; |
199 |
|
inpcheck = IC_X11; |
200 |
|
} else { |
201 |
|
x11_driver.comin = std_comin; |
202 |
|
x11_driver.comout = std_comout; |
203 |
< |
cmdvec = std_comout; |
203 |
> |
erract[COMMAND].pf = std_comout; |
204 |
|
inpcheck = IC_IOCTL; |
205 |
|
} |
206 |
|
return(&x11_driver); |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
< |
static |
210 |
> |
static void |
211 |
|
x11_close() /* close our display */ |
212 |
|
{ |
213 |
< |
cmdvec = NULL; /* reset error vectors */ |
214 |
< |
if (wrnvec != NULL) |
215 |
< |
wrnvec = stderr_v; |
213 |
> |
erract[COMMAND].pf = NULL; /* reset error vectors */ |
214 |
> |
if (erract[WARNING].pf != NULL) |
215 |
> |
erract[WARNING].pf = wputs; |
216 |
|
if (ourdisplay == NULL) |
217 |
|
return; |
218 |
|
if (comline != NULL) { |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
< |
static |
233 |
> |
static void |
234 |
|
x11_clear(xres, yres) /* clear our display */ |
235 |
|
int xres, yres; |
236 |
|
{ |
251 |
|
} |
252 |
|
XClearWindow(ourdisplay, gwind); |
253 |
|
/* reinitialize color table */ |
254 |
< |
if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) |
254 |
> |
if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) { |
255 |
|
if (getpixels() == 0) |
256 |
< |
stderr_v("cannot allocate colors\n"); |
256 |
> |
eputs("cannot allocate colors\n"); |
257 |
|
else |
258 |
|
new_ctab(ncolors); |
259 |
+ |
} |
260 |
|
/* get new command line */ |
261 |
|
if (comline != NULL) |
262 |
|
xt_close(comline); |
264 |
|
comline = xt_open(ourdisplay, gwind, 0, gheight, gwidth, |
265 |
|
comheight, 0, ourblack, ourwhite, COMFN); |
266 |
|
if (comline == NULL) { |
267 |
< |
stderr_v("Cannot open command line window\n"); |
267 |
> |
eputs("cannot open command line window\n"); |
268 |
|
quit(1); |
269 |
|
} |
270 |
|
XSelectInput(ourdisplay, comline->w, ExposureMask); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
< |
static |
280 |
> |
static void |
281 |
|
x11_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
282 |
|
COLOR col; |
283 |
|
int xmin, ymin, xmax, ymax; |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
< |
static |
299 |
> |
static void |
300 |
|
x11_flush() /* flush output */ |
301 |
|
{ |
302 |
|
char buf[256]; |
306 |
|
n = XPending(ourdisplay); /* from X server */ |
307 |
|
while (n-- > 0) |
308 |
|
getevent(); |
309 |
+ |
#ifdef FNDELAY |
310 |
|
if (inpcheck == IC_IOCTL) { /* from stdin */ |
311 |
+ |
#ifdef FIONREAD |
312 |
|
if (ioctl(fileno(stdin), FIONREAD, &n) < 0) { |
313 |
+ |
#else |
314 |
+ |
if (1) { |
315 |
+ |
#endif |
316 |
|
if (fcntl(fileno(stdin), F_SETFL, FNDELAY) < 0) { |
317 |
< |
stderr_v("Cannot change input mode\n"); |
317 |
> |
eputs("cannot change input mode\n"); |
318 |
|
quit(1); |
319 |
|
} |
320 |
|
inpcheck = IC_READ; |
325 |
|
n = read(fileno(stdin), buf, sizeof(buf)-1); |
326 |
|
if (n > 0) { |
327 |
|
buf[n] = '\0'; |
328 |
< |
strcpy(getcombuf(&x11_driver), buf); |
328 |
> |
tocombuf(buf, &x11_driver); |
329 |
|
} |
330 |
|
} |
331 |
+ |
#endif |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
< |
static |
335 |
> |
static void |
336 |
|
x11_comin(inp, prompt) /* read in a command line */ |
337 |
|
char *inp, *prompt; |
338 |
|
{ |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
< |
static |
351 |
> |
static void |
352 |
|
x11_comout(outp) /* output a string to command line */ |
353 |
|
char *outp; |
354 |
|
{ |
355 |
< |
if (comline == NULL) |
355 |
> |
if (comline == NULL || outp == NULL || !outp[0]) |
356 |
|
return; |
357 |
|
xt_puts(outp, comline); |
358 |
|
if (outp[strlen(outp)-1] == '\n') |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
< |
static |
363 |
> |
static void |
364 |
|
x11_errout(msg) /* output an error message */ |
365 |
|
char *msg; |
366 |
|
{ |
367 |
< |
stderr_v(msg); /* send to stderr also! */ |
367 |
> |
eputs(msg); /* send to stderr also! */ |
368 |
|
x11_comout(msg); |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
< |
static |
372 |
> |
static void |
373 |
|
std_comin(inp, prompt) /* read in command line from stdin */ |
374 |
|
char *inp, *prompt; |
375 |
|
{ |
362 |
– |
extern char *gets(); |
363 |
– |
|
376 |
|
if (prompt != NULL) { |
377 |
|
if (fromcombuf(inp, &x11_driver)) |
378 |
|
return; |
379 |
|
if (!x11_driver.inpready) |
380 |
< |
fputs(prompt, stdout); |
380 |
> |
std_comout(prompt); |
381 |
|
} |
382 |
+ |
#ifdef FNDELAY |
383 |
+ |
if (inpcheck == IC_READ) { /* turn off FNDELAY */ |
384 |
+ |
if (fcntl(fileno(stdin), F_SETFL, 0) < 0) { |
385 |
+ |
eputs("cannot change input mode\n"); |
386 |
+ |
quit(1); |
387 |
+ |
} |
388 |
+ |
inpcheck = IC_IOCTL; |
389 |
+ |
} |
390 |
+ |
#endif |
391 |
|
if (gets(inp) == NULL) { |
392 |
|
strcpy(inp, "quit"); |
393 |
|
return; |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
< |
static |
401 |
> |
static void |
402 |
|
std_comout(outp) /* write out string to stdout */ |
403 |
|
char *outp; |
404 |
|
{ |
405 |
|
fputs(outp, stdout); |
406 |
+ |
fflush(stdout); |
407 |
|
} |
408 |
|
|
409 |
|
|
436 |
|
} |
437 |
|
|
438 |
|
|
439 |
< |
static |
439 |
> |
static void |
440 |
|
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
441 |
|
int ndx; |
442 |
|
int r, g, b; |
476 |
|
return(ncolors = 0); |
477 |
|
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) |
478 |
|
break; |
479 |
< |
free((char *)pixval); |
479 |
> |
free((void *)pixval); |
480 |
|
pixval = NULL; |
481 |
|
} |
482 |
|
if (pixval == NULL) { |
505 |
|
} |
506 |
|
|
507 |
|
|
508 |
< |
static |
508 |
> |
static void |
509 |
|
freepixels() /* free our pixels */ |
510 |
|
{ |
511 |
|
if (ncolors == 0) |
512 |
|
return; |
513 |
|
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
514 |
< |
free((char *)pixval); |
514 |
> |
free((void *)pixval); |
515 |
|
pixval = NULL; |
516 |
|
ncolors = 0; |
517 |
|
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
547 |
|
} |
548 |
|
|
549 |
|
|
550 |
< |
static |
550 |
> |
static void |
551 |
|
getevent() /* get next event */ |
552 |
|
{ |
553 |
|
XNextEvent(ourdisplay, levptr(XEvent)); |
561 |
|
break; |
562 |
|
case MapNotify: |
563 |
|
if (ourvinfo.class == PseudoColor || |
564 |
< |
ourvinfo.class == GrayScale) |
564 |
> |
ourvinfo.class == GrayScale) { |
565 |
|
if (getpixels() == 0) |
566 |
< |
stderr_v("Cannot allocate colors\n"); |
566 |
> |
eputs("cannot allocate colors\n"); |
567 |
|
else |
568 |
|
new_ctab(ncolors); |
569 |
+ |
} |
570 |
|
mapped = 1; |
571 |
|
break; |
572 |
|
case Expose: |
581 |
|
} |
582 |
|
|
583 |
|
|
584 |
< |
static |
584 |
> |
static void |
585 |
|
getkey(ekey) /* get input key */ |
586 |
|
register XKeyPressedEvent *ekey; |
587 |
|
{ |
594 |
|
} |
595 |
|
|
596 |
|
|
597 |
< |
static |
597 |
> |
static void |
598 |
|
fixwindow(eexp) /* repair damage to window */ |
599 |
|
register XExposeEvent *eexp; |
600 |
|
{ |
601 |
+ |
char buf[80]; |
602 |
+ |
|
603 |
|
if (eexp->window == gwind) { |
604 |
< |
sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n", |
604 |
> |
sprintf(buf, "repaint %d %d %d %d\n", |
605 |
|
eexp->x, gheight - eexp->y - eexp->height, |
606 |
|
eexp->x + eexp->width, gheight - eexp->y); |
607 |
+ |
tocombuf(buf, &x11_driver); |
608 |
|
} else if (eexp->window == comline->w) { |
609 |
|
if (eexp->count == 0) |
610 |
|
xt_redraw(comline); |
612 |
|
} |
613 |
|
|
614 |
|
|
615 |
< |
static |
615 |
> |
static void |
616 |
|
resizewindow(ersz) /* resize window */ |
617 |
|
register XConfigureEvent *ersz; |
618 |
|
{ |
624 |
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
625 |
|
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
626 |
|
|
627 |
< |
strcpy(getcombuf(&x11_driver), "new\n"); |
627 |
> |
tocombuf("new\n", &x11_driver); |
628 |
|
} |