| 1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 10 |
|
* Jan 1990 |
| 11 |
|
*/ |
| 12 |
|
|
| 13 |
< |
#include <stdio.h> |
| 14 |
< |
|
| 13 |
> |
#include "standard.h" |
| 14 |
|
#include <sys/ioctl.h> |
| 15 |
+ |
#ifdef sparc |
| 16 |
+ |
#include <sys/conf.h> |
| 17 |
+ |
#include <sys/file.h> |
| 18 |
+ |
#include <sys/filio.h> |
| 19 |
+ |
#endif |
| 20 |
+ |
#if !defined(FNDELAY) && defined(O_NONBLOCK) |
| 21 |
+ |
#define FNDELAY O_NONBLOCK |
| 22 |
+ |
#endif |
| 23 |
|
|
| 24 |
|
#include <X11/Xlib.h> |
| 25 |
|
#include <X11/cursorfont.h> |
| 65 |
|
|
| 66 |
|
static int gwidth, gheight; /* graphics window size */ |
| 67 |
|
|
| 68 |
+ |
static int comheight; /* desired comline height */ |
| 69 |
|
static TEXTWIND *comline = NULL; /* our command line */ |
| 70 |
|
|
| 71 |
|
static char c_queue[64]; /* input queue */ |
| 76 |
|
|
| 77 |
|
static Colormap ourmap = 0; /* our color map */ |
| 78 |
|
|
| 79 |
< |
extern char *malloc(), *getcombuf(); |
| 79 |
> |
#define IC_X11 0 |
| 80 |
> |
#define IC_IOCTL 1 |
| 81 |
> |
#define IC_READ 2 |
| 82 |
|
|
| 83 |
+ |
static int inpcheck; /* whence to check input */ |
| 84 |
+ |
|
| 85 |
+ |
extern char *malloc(); |
| 86 |
+ |
|
| 87 |
|
static int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
| 88 |
|
x11_getcur(), x11_comout(), x11_comin(), x11_flush(); |
| 89 |
|
|
| 90 |
+ |
static int std_comin(), std_comout(); |
| 91 |
+ |
|
| 92 |
|
static struct driver x11_driver = { |
| 93 |
|
x11_close, x11_clear, x11_paintr, x11_getcur, |
| 94 |
< |
x11_comout, x11_comin, x11_flush, 1.0 |
| 94 |
> |
NULL, NULL, x11_flush, 1.0 |
| 95 |
|
}; |
| 96 |
|
|
| 97 |
+ |
static int getpixels(), xnewcolr(), freepixels(), resizewindow(), |
| 98 |
+ |
getevent(), getkey(), fixwindow(), x11_getc(); |
| 99 |
+ |
static unsigned long true_pixel(); |
| 100 |
|
|
| 101 |
+ |
|
| 102 |
|
struct driver * |
| 103 |
|
x11_init(name, id) /* initialize driver */ |
| 104 |
|
char *name, *id; |
| 109 |
|
XSetWindowAttributes ourwinattr; |
| 110 |
|
XWMHints ourxwmhints; |
| 111 |
|
XSizeHints oursizhints; |
| 112 |
< |
|
| 112 |
> |
/* open display server */ |
| 113 |
|
ourdisplay = XOpenDisplay(NULL); |
| 114 |
|
if (ourdisplay == NULL) { |
| 115 |
< |
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
| 115 |
> |
eputs("cannot open X-windows; DISPLAY variable set?\n"); |
| 116 |
|
return(NULL); |
| 117 |
|
} |
| 118 |
|
/* find a usable visual */ |
| 119 |
|
nplanes = DisplayPlanes(ourdisplay, ourscreen); |
| 120 |
|
if (XMatchVisualInfo(ourdisplay,ourscreen, |
| 121 |
< |
24,TrueColor,&ourvinfo) || |
| 121 |
> |
nplanes>12?nplanes:24,TrueColor,&ourvinfo) || |
| 122 |
|
XMatchVisualInfo(ourdisplay,ourscreen, |
| 123 |
< |
24,DirectColor,&ourvinfo)) { |
| 123 |
> |
nplanes>12?nplanes:24,DirectColor,&ourvinfo)) { |
| 124 |
|
ourblack = 0; |
| 125 |
|
ourwhite = ourvinfo.red_mask | |
| 126 |
|
ourvinfo.green_mask | |
| 127 |
|
ourvinfo.blue_mask ; |
| 128 |
|
} else { |
| 129 |
|
if (nplanes < 4) { |
| 130 |
< |
stderr_v("not enough colors\n"); |
| 130 |
> |
eputs("not enough colors\n"); |
| 131 |
|
return(NULL); |
| 132 |
|
} |
| 133 |
|
if (!XMatchVisualInfo(ourdisplay,ourscreen, |
| 134 |
|
nplanes,PseudoColor,&ourvinfo) && |
| 135 |
|
!XMatchVisualInfo(ourdisplay,ourscreen, |
| 136 |
|
nplanes,GrayScale,&ourvinfo)) { |
| 137 |
< |
stderr_v("unsupported visual type\n"); |
| 137 |
> |
eputs("unsupported visual type\n"); |
| 138 |
|
return(NULL); |
| 139 |
|
} |
| 140 |
|
ourblack = BlackPixel(ourdisplay,ourscreen); |
| 141 |
|
ourwhite = WhitePixel(ourdisplay,ourscreen); |
| 142 |
|
} |
| 143 |
|
/* set gamma */ |
| 144 |
< |
if ((gv = getenv("GAMMA")) != NULL) |
| 144 |
> |
if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL |
| 145 |
> |
|| (gv = getenv("DISPLAY_GAMMA")) != NULL) |
| 146 |
|
make_gmap(atof(gv)); |
| 147 |
|
else |
| 148 |
|
make_gmap(GAMMA); |
| 149 |
+ |
/* X11 command line or no? */ |
| 150 |
+ |
if (!strcmp(name, "x11")) |
| 151 |
+ |
comheight = COMHEIGHT; |
| 152 |
+ |
else /* "x11d" */ { |
| 153 |
+ |
comheight = 0; |
| 154 |
+ |
#ifndef FNDELAY |
| 155 |
+ |
eputs("warning: x11d driver not fully functional on this machine\n"); |
| 156 |
+ |
#endif |
| 157 |
+ |
} |
| 158 |
|
/* open window */ |
| 159 |
|
ourwinattr.background_pixel = ourblack; |
| 160 |
|
ourwinattr.border_pixel = ourblack; |
| 167 |
|
BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual, |
| 168 |
|
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
| 169 |
|
if (gwind == 0) { |
| 170 |
< |
stderr_v("cannot create window\n"); |
| 170 |
> |
eputs("cannot create window\n"); |
| 171 |
|
return(NULL); |
| 172 |
|
} |
| 173 |
|
XStoreName(ourdisplay, gwind, id); |
| 180 |
|
gwind, x11icon_bits, x11icon_width, x11icon_height); |
| 181 |
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
| 182 |
|
oursizhints.min_width = MINWIDTH; |
| 183 |
< |
oursizhints.min_height = MINHEIGHT+COMHEIGHT; |
| 183 |
> |
oursizhints.min_height = MINHEIGHT+comheight; |
| 184 |
|
oursizhints.flags = PMinSize; |
| 185 |
|
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
| 186 |
|
XSelectInput(ourdisplay, gwind, ExposureMask); |
| 187 |
|
XMapWindow(ourdisplay, gwind); |
| 188 |
|
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent)); |
| 189 |
|
gwidth = levptr(XExposeEvent)->width; |
| 190 |
< |
gheight = levptr(XExposeEvent)->height - COMHEIGHT; |
| 190 |
> |
gheight = levptr(XExposeEvent)->height - comheight; |
| 191 |
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
| 192 |
|
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
| 193 |
|
x11_driver.inpready = 0; |
| 194 |
|
mapped = 1; |
| 195 |
< |
cmdvec = x11_comout; /* set error vectors */ |
| 196 |
< |
if (wrnvec != NULL) |
| 197 |
< |
wrnvec = x11_errout; |
| 195 |
> |
/* set i/o vectors */ |
| 196 |
> |
if (comheight) { |
| 197 |
> |
x11_driver.comin = x11_comin; |
| 198 |
> |
x11_driver.comout = x11_comout; |
| 199 |
> |
erract[COMMAND].pf = x11_comout; |
| 200 |
> |
if (erract[WARNING].pf != NULL) |
| 201 |
> |
erract[WARNING].pf = x11_errout; |
| 202 |
> |
inpcheck = IC_X11; |
| 203 |
> |
} else { |
| 204 |
> |
x11_driver.comin = std_comin; |
| 205 |
> |
x11_driver.comout = std_comout; |
| 206 |
> |
erract[COMMAND].pf = std_comout; |
| 207 |
> |
inpcheck = IC_IOCTL; |
| 208 |
> |
} |
| 209 |
|
return(&x11_driver); |
| 210 |
|
} |
| 211 |
|
|
| 213 |
|
static |
| 214 |
|
x11_close() /* close our display */ |
| 215 |
|
{ |
| 216 |
< |
cmdvec = NULL; /* reset error vectors */ |
| 217 |
< |
if (wrnvec != NULL) |
| 218 |
< |
wrnvec = stderr_v; |
| 216 |
> |
erract[COMMAND].pf = NULL; /* reset error vectors */ |
| 217 |
> |
if (erract[WARNING].pf != NULL) |
| 218 |
> |
erract[WARNING].pf = wputs; |
| 219 |
|
if (ourdisplay == NULL) |
| 220 |
|
return; |
| 221 |
|
if (comline != NULL) { |
| 245 |
|
/* resize window */ |
| 246 |
|
if (xres != gwidth || yres != gheight) { |
| 247 |
|
XSelectInput(ourdisplay, gwind, 0); |
| 248 |
< |
XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); |
| 248 |
> |
XResizeWindow(ourdisplay, gwind, xres, yres+comheight); |
| 249 |
|
gwidth = xres; |
| 250 |
|
gheight = yres; |
| 251 |
|
XFlush(ourdisplay); |
| 256 |
|
/* reinitialize color table */ |
| 257 |
|
if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) |
| 258 |
|
if (getpixels() == 0) |
| 259 |
< |
stderr_v("cannot allocate colors\n"); |
| 259 |
> |
eputs("cannot allocate colors\n"); |
| 260 |
|
else |
| 261 |
|
new_ctab(ncolors); |
| 262 |
|
/* get new command line */ |
| 263 |
|
if (comline != NULL) |
| 264 |
|
xt_close(comline); |
| 265 |
< |
comline = xt_open(ourdisplay, gwind, 0, gheight, |
| 266 |
< |
gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN); |
| 267 |
< |
if (comline == NULL) { |
| 268 |
< |
stderr_v("Cannot open command line window\n"); |
| 269 |
< |
quit(1); |
| 270 |
< |
} |
| 271 |
< |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
| 265 |
> |
if (comheight) { |
| 266 |
> |
comline = xt_open(ourdisplay, gwind, 0, gheight, gwidth, |
| 267 |
> |
comheight, 0, ourblack, ourwhite, COMFN); |
| 268 |
> |
if (comline == NULL) { |
| 269 |
> |
eputs("cannot open command line window\n"); |
| 270 |
> |
quit(1); |
| 271 |
> |
} |
| 272 |
> |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
| 273 |
|
/* remove earmuffs */ |
| 274 |
< |
XSelectInput(ourdisplay, gwind, |
| 274 |
> |
XSelectInput(ourdisplay, gwind, |
| 275 |
|
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
| 276 |
+ |
} else /* remove earmuffs */ |
| 277 |
+ |
XSelectInput(ourdisplay, gwind, |
| 278 |
+ |
StructureNotifyMask|ExposureMask|ButtonPressMask); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
|
| 284 |
|
COLOR col; |
| 285 |
|
int xmin, ymin, xmax, ymax; |
| 286 |
|
{ |
| 242 |
– |
extern int xnewcolr(); /* pixel assignment routine */ |
| 243 |
– |
extern unsigned long true_pixel(); |
| 287 |
|
unsigned long pixel; |
| 288 |
|
|
| 289 |
|
if (!mapped) |
| 301 |
|
static |
| 302 |
|
x11_flush() /* flush output */ |
| 303 |
|
{ |
| 304 |
+ |
char buf[256]; |
| 305 |
+ |
int n; |
| 306 |
+ |
/* check for input */ |
| 307 |
|
XNoOp(ourdisplay); |
| 308 |
< |
while (XPending(ourdisplay) > 0) |
| 308 |
> |
n = XPending(ourdisplay); /* from X server */ |
| 309 |
> |
while (n-- > 0) |
| 310 |
|
getevent(); |
| 311 |
+ |
#ifdef FNDELAY |
| 312 |
+ |
if (inpcheck == IC_IOCTL) { /* from stdin */ |
| 313 |
+ |
#ifdef FIONREAD |
| 314 |
+ |
if (ioctl(fileno(stdin), FIONREAD, &n) < 0) { |
| 315 |
+ |
#else |
| 316 |
+ |
if (1) { |
| 317 |
+ |
#endif |
| 318 |
+ |
if (fcntl(fileno(stdin), F_SETFL, FNDELAY) < 0) { |
| 319 |
+ |
eputs("cannot change input mode\n"); |
| 320 |
+ |
quit(1); |
| 321 |
+ |
} |
| 322 |
+ |
inpcheck = IC_READ; |
| 323 |
+ |
} else |
| 324 |
+ |
x11_driver.inpready += n; |
| 325 |
+ |
} |
| 326 |
+ |
if (inpcheck == IC_READ) { |
| 327 |
+ |
n = read(fileno(stdin), buf, sizeof(buf)-1); |
| 328 |
+ |
if (n > 0) { |
| 329 |
+ |
buf[n] = '\0'; |
| 330 |
+ |
tocombuf(buf, &x11_driver); |
| 331 |
+ |
} |
| 332 |
+ |
} |
| 333 |
+ |
#endif |
| 334 |
|
} |
| 335 |
|
|
| 336 |
|
|
| 338 |
|
x11_comin(inp, prompt) /* read in a command line */ |
| 339 |
|
char *inp, *prompt; |
| 340 |
|
{ |
| 341 |
< |
extern int x11_getc(); |
| 342 |
< |
|
| 273 |
< |
if (prompt != NULL) |
| 341 |
> |
if (prompt != NULL) { |
| 342 |
> |
x11_flush(); /* make sure we get everything */ |
| 343 |
|
if (fromcombuf(inp, &x11_driver)) |
| 344 |
|
return; |
| 345 |
< |
else |
| 346 |
< |
xt_puts(prompt, comline); |
| 345 |
> |
xt_puts(prompt, comline); |
| 346 |
> |
} |
| 347 |
|
xt_cursor(comline, TBLKCURS); |
| 348 |
|
editline(inp, x11_getc, x11_comout); |
| 349 |
|
xt_cursor(comline, TNOCURS); |
| 351 |
|
|
| 352 |
|
|
| 353 |
|
static |
| 354 |
< |
x11_comout(out) /* output a string to command line */ |
| 355 |
< |
char *out; |
| 354 |
> |
x11_comout(outp) /* output a string to command line */ |
| 355 |
> |
char *outp; |
| 356 |
|
{ |
| 357 |
|
if (comline == NULL) |
| 358 |
|
return; |
| 359 |
< |
xt_puts(out, comline); |
| 360 |
< |
if (out[strlen(out)-1] == '\n') |
| 359 |
> |
xt_puts(outp, comline); |
| 360 |
> |
if (outp[strlen(outp)-1] == '\n') |
| 361 |
|
XFlush(ourdisplay); |
| 362 |
|
} |
| 363 |
|
|
| 366 |
|
x11_errout(msg) /* output an error message */ |
| 367 |
|
char *msg; |
| 368 |
|
{ |
| 369 |
< |
stderr_v(msg); /* send to stderr also! */ |
| 369 |
> |
eputs(msg); /* send to stderr also! */ |
| 370 |
|
x11_comout(msg); |
| 371 |
|
} |
| 372 |
|
|
| 373 |
|
|
| 374 |
+ |
static |
| 375 |
+ |
std_comin(inp, prompt) /* read in command line from stdin */ |
| 376 |
+ |
char *inp, *prompt; |
| 377 |
+ |
{ |
| 378 |
+ |
extern char *gets(); |
| 379 |
+ |
|
| 380 |
+ |
if (prompt != NULL) { |
| 381 |
+ |
if (fromcombuf(inp, &x11_driver)) |
| 382 |
+ |
return; |
| 383 |
+ |
if (!x11_driver.inpready) |
| 384 |
+ |
std_comout(prompt); |
| 385 |
+ |
} |
| 386 |
+ |
#ifdef FNDELAY |
| 387 |
+ |
if (inpcheck == IC_READ) { /* turn off FNDELAY */ |
| 388 |
+ |
if (fcntl(fileno(stdin), F_SETFL, 0) < 0) { |
| 389 |
+ |
eputs("cannot change input mode\n"); |
| 390 |
+ |
quit(1); |
| 391 |
+ |
} |
| 392 |
+ |
inpcheck = IC_IOCTL; |
| 393 |
+ |
} |
| 394 |
+ |
#endif |
| 395 |
+ |
if (gets(inp) == NULL) { |
| 396 |
+ |
strcpy(inp, "quit"); |
| 397 |
+ |
return; |
| 398 |
+ |
} |
| 399 |
+ |
x11_driver.inpready -= strlen(inp) + 1; |
| 400 |
+ |
if (x11_driver.inpready < 0) |
| 401 |
+ |
x11_driver.inpready = 0; |
| 402 |
+ |
} |
| 403 |
+ |
|
| 404 |
+ |
|
| 405 |
+ |
static |
| 406 |
+ |
std_comout(outp) /* write out string to stdout */ |
| 407 |
+ |
char *outp; |
| 408 |
+ |
{ |
| 409 |
+ |
fputs(outp, stdout); |
| 410 |
+ |
fflush(stdout); |
| 411 |
+ |
} |
| 412 |
+ |
|
| 413 |
+ |
|
| 414 |
|
static int |
| 415 |
|
x11_getcur(xp, yp) /* get cursor position */ |
| 416 |
|
int *xp, *yp; |
| 567 |
|
if (ourvinfo.class == PseudoColor || |
| 568 |
|
ourvinfo.class == GrayScale) |
| 569 |
|
if (getpixels() == 0) |
| 570 |
< |
stderr_v("Cannot allocate colors\n"); |
| 570 |
> |
eputs("cannot allocate colors\n"); |
| 571 |
|
else |
| 572 |
|
new_ctab(ncolors); |
| 573 |
|
mapped = 1; |
| 601 |
|
fixwindow(eexp) /* repair damage to window */ |
| 602 |
|
register XExposeEvent *eexp; |
| 603 |
|
{ |
| 604 |
+ |
char buf[80]; |
| 605 |
+ |
|
| 606 |
|
if (eexp->window == gwind) { |
| 607 |
< |
sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n", |
| 607 |
> |
sprintf(buf, "repaint %d %d %d %d\n", |
| 608 |
|
eexp->x, gheight - eexp->y - eexp->height, |
| 609 |
|
eexp->x + eexp->width, gheight - eexp->y); |
| 610 |
+ |
tocombuf(buf, &x11_driver); |
| 611 |
|
} else if (eexp->window == comline->w) { |
| 612 |
|
if (eexp->count == 0) |
| 613 |
|
xt_redraw(comline); |
| 619 |
|
resizewindow(ersz) /* resize window */ |
| 620 |
|
register XConfigureEvent *ersz; |
| 621 |
|
{ |
| 622 |
< |
if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight) |
| 622 |
> |
if (ersz->width == gwidth && ersz->height-comheight == gheight) |
| 623 |
|
return; |
| 624 |
|
|
| 625 |
|
gwidth = ersz->width; |
| 626 |
< |
gheight = ersz->height-COMHEIGHT; |
| 626 |
> |
gheight = ersz->height-comheight; |
| 627 |
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
| 628 |
|
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
| 629 |
|
|
| 630 |
< |
strcpy(getcombuf(&x11_driver), "new\n"); |
| 630 |
> |
tocombuf("new\n", &x11_driver); |
| 631 |
|
} |