| 13 |
|
#include <stdio.h> |
| 14 |
|
#include <math.h> |
| 15 |
|
#include <sys/ioctl.h> |
| 16 |
+ |
#include <fcntl.h> |
| 17 |
|
|
| 18 |
|
#include <X11/Xlib.h> |
| 19 |
|
#include <X11/cursorfont.h> |
| 59 |
|
|
| 60 |
|
static int gwidth, gheight; /* graphics window size */ |
| 61 |
|
|
| 62 |
+ |
static int comheight; /* desired comline height */ |
| 63 |
|
static TEXTWIND *comline = NULL; /* our command line */ |
| 64 |
|
|
| 65 |
|
static char c_queue[64]; /* input queue */ |
| 70 |
|
|
| 71 |
|
static Colormap ourmap = 0; /* our color map */ |
| 72 |
|
|
| 73 |
< |
extern char *malloc(), *getcombuf(); |
| 73 |
> |
#define IC_X11 0 |
| 74 |
> |
#define IC_IOCTL 1 |
| 75 |
> |
#define IC_READ 2 |
| 76 |
|
|
| 77 |
+ |
static int inpcheck; /* whence to check input */ |
| 78 |
+ |
|
| 79 |
+ |
extern char *malloc(); |
| 80 |
+ |
|
| 81 |
|
static int x11_close(), x11_clear(), x11_paintr(), x11_errout(), |
| 82 |
|
x11_getcur(), x11_comout(), x11_comin(), x11_flush(); |
| 83 |
|
|
| 84 |
+ |
static int std_comin(), std_comout(); |
| 85 |
+ |
|
| 86 |
|
static struct driver x11_driver = { |
| 87 |
|
x11_close, x11_clear, x11_paintr, x11_getcur, |
| 88 |
< |
x11_comout, x11_comin, x11_flush, 1.0 |
| 88 |
> |
NULL, NULL, x11_flush, 1.0 |
| 89 |
|
}; |
| 90 |
|
|
| 91 |
< |
static int getpixels(), xnewcolr(), freepixels(), |
| 92 |
< |
getevent(), getkey(), fixwindow(); |
| 91 |
> |
static int getpixels(), xnewcolr(), freepixels(), resizewindow(), |
| 92 |
> |
getevent(), getkey(), fixwindow(), x11_getc(); |
| 93 |
|
static unsigned long true_pixel(); |
| 94 |
|
|
| 95 |
|
|
| 103 |
|
XSetWindowAttributes ourwinattr; |
| 104 |
|
XWMHints ourxwmhints; |
| 105 |
|
XSizeHints oursizhints; |
| 106 |
< |
|
| 106 |
> |
/* open display server */ |
| 107 |
|
ourdisplay = XOpenDisplay(NULL); |
| 108 |
|
if (ourdisplay == NULL) { |
| 109 |
|
stderr_v("cannot open X-windows; DISPLAY variable set?\n"); |
| 135 |
|
ourwhite = WhitePixel(ourdisplay,ourscreen); |
| 136 |
|
} |
| 137 |
|
/* set gamma */ |
| 138 |
< |
if ((gv = getenv("GAMMA")) != NULL) |
| 138 |
> |
if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL |
| 139 |
> |
|| (gv = getenv("DISPLAY_GAMMA")) != NULL) |
| 140 |
|
make_gmap(atof(gv)); |
| 141 |
|
else |
| 142 |
|
make_gmap(GAMMA); |
| 143 |
+ |
/* X11 command line or no? */ |
| 144 |
+ |
if (!strcmp(name, "x11")) |
| 145 |
+ |
comheight = COMHEIGHT; |
| 146 |
+ |
else /* "x11d" */ |
| 147 |
+ |
comheight = 0; |
| 148 |
|
/* open window */ |
| 149 |
|
ourwinattr.background_pixel = ourblack; |
| 150 |
|
ourwinattr.border_pixel = ourblack; |
| 170 |
|
gwind, x11icon_bits, x11icon_width, x11icon_height); |
| 171 |
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
| 172 |
|
oursizhints.min_width = MINWIDTH; |
| 173 |
< |
oursizhints.min_height = MINHEIGHT+COMHEIGHT; |
| 173 |
> |
oursizhints.min_height = MINHEIGHT+comheight; |
| 174 |
|
oursizhints.flags = PMinSize; |
| 175 |
|
XSetNormalHints(ourdisplay, gwind, &oursizhints); |
| 176 |
|
XSelectInput(ourdisplay, gwind, ExposureMask); |
| 177 |
|
XMapWindow(ourdisplay, gwind); |
| 178 |
|
XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent)); |
| 179 |
|
gwidth = levptr(XExposeEvent)->width; |
| 180 |
< |
gheight = levptr(XExposeEvent)->height - COMHEIGHT; |
| 180 |
> |
gheight = levptr(XExposeEvent)->height - comheight; |
| 181 |
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
| 182 |
|
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
| 183 |
|
x11_driver.inpready = 0; |
| 184 |
|
mapped = 1; |
| 185 |
< |
cmdvec = x11_comout; /* set error vectors */ |
| 186 |
< |
if (wrnvec != NULL) |
| 187 |
< |
wrnvec = x11_errout; |
| 185 |
> |
/* set i/o vectors */ |
| 186 |
> |
if (comheight) { |
| 187 |
> |
x11_driver.comin = x11_comin; |
| 188 |
> |
x11_driver.comout = x11_comout; |
| 189 |
> |
cmdvec = x11_comout; |
| 190 |
> |
if (wrnvec != NULL) |
| 191 |
> |
wrnvec = x11_errout; |
| 192 |
> |
inpcheck = IC_X11; |
| 193 |
> |
} else { |
| 194 |
> |
x11_driver.comin = std_comin; |
| 195 |
> |
x11_driver.comout = std_comout; |
| 196 |
> |
cmdvec = std_comout; |
| 197 |
> |
inpcheck = IC_IOCTL; |
| 198 |
> |
} |
| 199 |
|
return(&x11_driver); |
| 200 |
|
} |
| 201 |
|
|
| 235 |
|
/* resize window */ |
| 236 |
|
if (xres != gwidth || yres != gheight) { |
| 237 |
|
XSelectInput(ourdisplay, gwind, 0); |
| 238 |
< |
XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); |
| 238 |
> |
XResizeWindow(ourdisplay, gwind, xres, yres+comheight); |
| 239 |
|
gwidth = xres; |
| 240 |
|
gheight = yres; |
| 241 |
|
XFlush(ourdisplay); |
| 252 |
|
/* get new command line */ |
| 253 |
|
if (comline != NULL) |
| 254 |
|
xt_close(comline); |
| 255 |
< |
comline = xt_open(ourdisplay, gwind, 0, gheight, |
| 256 |
< |
gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN); |
| 257 |
< |
if (comline == NULL) { |
| 258 |
< |
stderr_v("Cannot open command line window\n"); |
| 259 |
< |
quit(1); |
| 260 |
< |
} |
| 261 |
< |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
| 255 |
> |
if (comheight) { |
| 256 |
> |
comline = xt_open(ourdisplay, gwind, 0, gheight, gwidth, |
| 257 |
> |
comheight, 0, ourblack, ourwhite, COMFN); |
| 258 |
> |
if (comline == NULL) { |
| 259 |
> |
stderr_v("Cannot open command line window\n"); |
| 260 |
> |
quit(1); |
| 261 |
> |
} |
| 262 |
> |
XSelectInput(ourdisplay, comline->w, ExposureMask); |
| 263 |
|
/* remove earmuffs */ |
| 264 |
< |
XSelectInput(ourdisplay, gwind, |
| 264 |
> |
XSelectInput(ourdisplay, gwind, |
| 265 |
|
StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); |
| 266 |
+ |
} else /* remove earmuffs */ |
| 267 |
+ |
XSelectInput(ourdisplay, gwind, |
| 268 |
+ |
StructureNotifyMask|ExposureMask|ButtonPressMask); |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
|
| 291 |
|
static |
| 292 |
|
x11_flush() /* flush output */ |
| 293 |
|
{ |
| 294 |
+ |
char buf[256]; |
| 295 |
+ |
int n; |
| 296 |
+ |
/* check for input */ |
| 297 |
|
XNoOp(ourdisplay); |
| 298 |
< |
while (XPending(ourdisplay) > 0) |
| 298 |
> |
n = XPending(ourdisplay); /* from X server */ |
| 299 |
> |
while (n-- > 0) |
| 300 |
|
getevent(); |
| 301 |
+ |
if (inpcheck == IC_IOCTL) { /* from stdin */ |
| 302 |
+ |
if (ioctl(fileno(stdin), FIONREAD, &n) < 0) { |
| 303 |
+ |
if (fcntl(fileno(stdin), F_SETFL, FNDELAY) < 0) { |
| 304 |
+ |
stderr_v("Cannot change input mode\n"); |
| 305 |
+ |
quit(1); |
| 306 |
+ |
} |
| 307 |
+ |
inpcheck = IC_READ; |
| 308 |
+ |
} else |
| 309 |
+ |
x11_driver.inpready += n; |
| 310 |
+ |
} |
| 311 |
+ |
if (inpcheck == IC_READ) { |
| 312 |
+ |
n = read(fileno(stdin), buf, sizeof(buf)-1); |
| 313 |
+ |
if (n > 0) { |
| 314 |
+ |
buf[n] = '\0'; |
| 315 |
+ |
tocombuf(buf, &x11_driver); |
| 316 |
+ |
} |
| 317 |
+ |
} |
| 318 |
|
} |
| 319 |
|
|
| 320 |
|
|
| 322 |
|
x11_comin(inp, prompt) /* read in a command line */ |
| 323 |
|
char *inp, *prompt; |
| 324 |
|
{ |
| 325 |
< |
extern int x11_getc(); |
| 326 |
< |
|
| 275 |
< |
if (prompt != NULL) |
| 325 |
> |
if (prompt != NULL) { |
| 326 |
> |
x11_flush(); /* make sure we get everything */ |
| 327 |
|
if (fromcombuf(inp, &x11_driver)) |
| 328 |
|
return; |
| 329 |
< |
else |
| 330 |
< |
xt_puts(prompt, comline); |
| 329 |
> |
xt_puts(prompt, comline); |
| 330 |
> |
} |
| 331 |
|
xt_cursor(comline, TBLKCURS); |
| 332 |
|
editline(inp, x11_getc, x11_comout); |
| 333 |
|
xt_cursor(comline, TNOCURS); |
| 335 |
|
|
| 336 |
|
|
| 337 |
|
static |
| 338 |
< |
x11_comout(out) /* output a string to command line */ |
| 339 |
< |
char *out; |
| 338 |
> |
x11_comout(outp) /* output a string to command line */ |
| 339 |
> |
char *outp; |
| 340 |
|
{ |
| 341 |
|
if (comline == NULL) |
| 342 |
|
return; |
| 343 |
< |
xt_puts(out, comline); |
| 344 |
< |
if (out[strlen(out)-1] == '\n') |
| 343 |
> |
xt_puts(outp, comline); |
| 344 |
> |
if (outp[strlen(outp)-1] == '\n') |
| 345 |
|
XFlush(ourdisplay); |
| 346 |
|
} |
| 347 |
|
|
| 355 |
|
} |
| 356 |
|
|
| 357 |
|
|
| 358 |
+ |
static |
| 359 |
+ |
std_comin(inp, prompt) /* read in command line from stdin */ |
| 360 |
+ |
char *inp, *prompt; |
| 361 |
+ |
{ |
| 362 |
+ |
extern char *gets(); |
| 363 |
+ |
|
| 364 |
+ |
if (prompt != NULL) { |
| 365 |
+ |
if (fromcombuf(inp, &x11_driver)) |
| 366 |
+ |
return; |
| 367 |
+ |
if (!x11_driver.inpready) |
| 368 |
+ |
fputs(prompt, stdout); |
| 369 |
+ |
} |
| 370 |
+ |
if (gets(inp) == NULL) { |
| 371 |
+ |
strcpy(inp, "quit"); |
| 372 |
+ |
return; |
| 373 |
+ |
} |
| 374 |
+ |
x11_driver.inpready -= strlen(inp) + 1; |
| 375 |
+ |
if (x11_driver.inpready < 0) |
| 376 |
+ |
x11_driver.inpready = 0; |
| 377 |
+ |
} |
| 378 |
+ |
|
| 379 |
+ |
|
| 380 |
+ |
static |
| 381 |
+ |
std_comout(outp) /* write out string to stdout */ |
| 382 |
+ |
char *outp; |
| 383 |
+ |
{ |
| 384 |
+ |
fputs(outp, stdout); |
| 385 |
+ |
fflush(stdout); |
| 386 |
+ |
} |
| 387 |
+ |
|
| 388 |
+ |
|
| 389 |
|
static int |
| 390 |
|
x11_getcur(xp, yp) /* get cursor position */ |
| 391 |
|
int *xp, *yp; |
| 576 |
|
fixwindow(eexp) /* repair damage to window */ |
| 577 |
|
register XExposeEvent *eexp; |
| 578 |
|
{ |
| 579 |
+ |
char buf[80]; |
| 580 |
+ |
|
| 581 |
|
if (eexp->window == gwind) { |
| 582 |
< |
sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n", |
| 582 |
> |
sprintf(buf, "repaint %d %d %d %d\n", |
| 583 |
|
eexp->x, gheight - eexp->y - eexp->height, |
| 584 |
|
eexp->x + eexp->width, gheight - eexp->y); |
| 585 |
+ |
tocombuf(buf, &x11_driver); |
| 586 |
|
} else if (eexp->window == comline->w) { |
| 587 |
|
if (eexp->count == 0) |
| 588 |
|
xt_redraw(comline); |
| 594 |
|
resizewindow(ersz) /* resize window */ |
| 595 |
|
register XConfigureEvent *ersz; |
| 596 |
|
{ |
| 597 |
< |
if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight) |
| 597 |
> |
if (ersz->width == gwidth && ersz->height-comheight == gheight) |
| 598 |
|
return; |
| 599 |
|
|
| 600 |
|
gwidth = ersz->width; |
| 601 |
< |
gheight = ersz->height-COMHEIGHT; |
| 601 |
> |
gheight = ersz->height-comheight; |
| 602 |
|
x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; |
| 603 |
|
x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; |
| 604 |
|
|
| 605 |
< |
strcpy(getcombuf(&x11_driver), "new\n"); |
| 605 |
> |
tocombuf("new\n", &x11_driver); |
| 606 |
|
} |