| 1 |
/* Copyright (c) 1987 Regents of the University of California */
|
| 2 |
|
| 3 |
#ifndef lint
|
| 4 |
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
#endif
|
| 6 |
|
| 7 |
/*
|
| 8 |
* x10.c - driver for X-windows version 10.4
|
| 9 |
*
|
| 10 |
* 5/7/87
|
| 11 |
*/
|
| 12 |
|
| 13 |
#include "standard.h"
|
| 14 |
|
| 15 |
#include <X/Xlib.h>
|
| 16 |
#include <X/cursors/bcross.cursor>
|
| 17 |
#include <X/cursors/bcross_mask.cursor>
|
| 18 |
|
| 19 |
#include "color.h"
|
| 20 |
|
| 21 |
#include "driver.h"
|
| 22 |
|
| 23 |
#include "xtwind.h"
|
| 24 |
|
| 25 |
#define GAMMA 2.2 /* exponent for color correction */
|
| 26 |
|
| 27 |
#define BORWIDTH 5 /* border width */
|
| 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 |
|
| 37 |
#define levptr(etype) ((etype *)&thisevent)
|
| 38 |
|
| 39 |
static XEvent thisevent; /* current event */
|
| 40 |
|
| 41 |
static int ncolors = 0; /* color table size */
|
| 42 |
static int *pixval; /* allocated pixel values */
|
| 43 |
|
| 44 |
static Display *ourdisplay = NULL; /* our display */
|
| 45 |
|
| 46 |
static Window gwind = 0; /* our graphics window */
|
| 47 |
|
| 48 |
static Cursor pickcursor = 0; /* cursor used for picking */
|
| 49 |
|
| 50 |
static int gwidth, gheight; /* graphics window size */
|
| 51 |
|
| 52 |
static TEXTWIND *comline = NULL; /* our command line */
|
| 53 |
|
| 54 |
static char c_queue[64]; /* input queue */
|
| 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(), *getcombuf();
|
| 59 |
|
| 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, x_flush, 1.0
|
| 68 |
};
|
| 69 |
|
| 70 |
|
| 71 |
struct driver *
|
| 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 |
eputs("cannot open X-windows; DISPLAY variable set?\n");
|
| 83 |
return(NULL);
|
| 84 |
}
|
| 85 |
if (DisplayPlanes() < 4) {
|
| 86 |
eputs("not enough colors\n");
|
| 87 |
return(NULL);
|
| 88 |
}
|
| 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 |
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 |
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 |
|
| 124 |
|
| 125 |
static
|
| 126 |
x_close() /* close our display */
|
| 127 |
{
|
| 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) {
|
| 134 |
xt_close(comline);
|
| 135 |
comline = NULL;
|
| 136 |
}
|
| 137 |
if (gwind != 0) {
|
| 138 |
XDestroyWindow(gwind);
|
| 139 |
gwind = 0;
|
| 140 |
}
|
| 141 |
XFreeCursor(pickcursor);
|
| 142 |
freepixels();
|
| 143 |
XCloseDisplay(ourdisplay);
|
| 144 |
ourdisplay = NULL;
|
| 145 |
}
|
| 146 |
|
| 147 |
|
| 148 |
static
|
| 149 |
x_clear(xres, yres) /* clear our display */
|
| 150 |
int xres, yres;
|
| 151 |
{
|
| 152 |
if (xres != gwidth || yres != gheight) { /* new window */
|
| 153 |
XChangeWindow(gwind, xres, yres+COMHEIGHT);
|
| 154 |
gwidth = xres;
|
| 155 |
gheight = yres;
|
| 156 |
} else /* just clear */
|
| 157 |
XClear(gwind);
|
| 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;
|
| 174 |
}
|
| 175 |
|
| 176 |
|
| 177 |
static
|
| 178 |
x_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
|
| 179 |
COLOR col;
|
| 180 |
int xmin, ymin, xmax, ymax;
|
| 181 |
{
|
| 182 |
extern int xnewcolr(); /* pixel assignment routine */
|
| 183 |
|
| 184 |
if (ncolors > 0) {
|
| 185 |
XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
|
| 186 |
pixval[get_pixel(col, xnewcolr)]);
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|
| 190 |
|
| 191 |
static
|
| 192 |
x_flush() /* flush output */
|
| 193 |
{
|
| 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);
|
| 216 |
}
|
| 217 |
|
| 218 |
|
| 219 |
static
|
| 220 |
x_comout(out) /* output a string to command line */
|
| 221 |
char *out;
|
| 222 |
{
|
| 223 |
if (comline == NULL)
|
| 224 |
return;
|
| 225 |
xt_puts(out, comline);
|
| 226 |
if (out[strlen(out)-1] == '\n')
|
| 227 |
XFlush();
|
| 228 |
}
|
| 229 |
|
| 230 |
|
| 231 |
static
|
| 232 |
x_errout(msg) /* output an error message */
|
| 233 |
char *msg;
|
| 234 |
{
|
| 235 |
eputs(msg); /* send to stderr also! */
|
| 236 |
x_comout(msg);
|
| 237 |
}
|
| 238 |
|
| 239 |
|
| 240 |
static int
|
| 241 |
x_getcur(xp, yp) /* get cursor position */
|
| 242 |
int *xp, *yp;
|
| 243 |
{
|
| 244 |
while (XGrabMouse(gwind, pickcursor, ButtonPressed) == 0)
|
| 245 |
sleep(1);
|
| 246 |
XFocusKeyboard(gwind);
|
| 247 |
do
|
| 248 |
getevent();
|
| 249 |
while (c_last <= c_first && levptr(XEvent)->type != ButtonPressed);
|
| 250 |
*xp = levptr(XKeyOrButtonEvent)->x;
|
| 251 |
*yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
|
| 252 |
XFocusKeyboard(RootWindow);
|
| 253 |
XUngrabMouse();
|
| 254 |
XFlush(); /* insure release */
|
| 255 |
if (c_last > c_first) /* key pressed */
|
| 256 |
return(x_getc());
|
| 257 |
/* button pressed */
|
| 258 |
switch (levptr(XKeyOrButtonEvent)->detail & 0377) {
|
| 259 |
case LeftButton:
|
| 260 |
return(MB1);
|
| 261 |
case MiddleButton:
|
| 262 |
return(MB2);
|
| 263 |
case RightButton:
|
| 264 |
return(MB3);
|
| 265 |
}
|
| 266 |
return(ABORT);
|
| 267 |
}
|
| 268 |
|
| 269 |
|
| 270 |
static
|
| 271 |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
|
| 272 |
int ndx;
|
| 273 |
int r, g, b;
|
| 274 |
{
|
| 275 |
Color xcolor;
|
| 276 |
|
| 277 |
xcolor.pixel = pixval[ndx];
|
| 278 |
xcolor.red = r << 8;
|
| 279 |
xcolor.green = g << 8;
|
| 280 |
xcolor.blue = b << 8;
|
| 281 |
|
| 282 |
XStoreColor(&xcolor);
|
| 283 |
}
|
| 284 |
|
| 285 |
|
| 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(ncolors);
|
| 299 |
free((char *)pixval);
|
| 300 |
}
|
| 301 |
return(ncolors = 0);
|
| 302 |
}
|
| 303 |
|
| 304 |
|
| 305 |
static
|
| 306 |
freepixels() /* free our pixels */
|
| 307 |
{
|
| 308 |
if (ncolors == 0)
|
| 309 |
return;
|
| 310 |
XFreeColors(pixval, ncolors, 0);
|
| 311 |
free((char *)pixval);
|
| 312 |
ncolors = 0;
|
| 313 |
}
|
| 314 |
|
| 315 |
|
| 316 |
static int
|
| 317 |
x_getc() /* get a command character */
|
| 318 |
{
|
| 319 |
while (c_last <= c_first) {
|
| 320 |
c_first = c_last = 0; /* reset */
|
| 321 |
getevent(); /* wait for key */
|
| 322 |
}
|
| 323 |
x_driver.inpready--;
|
| 324 |
return(c_queue[c_first++]);
|
| 325 |
}
|
| 326 |
|
| 327 |
|
| 328 |
static
|
| 329 |
getevent() /* get next event */
|
| 330 |
{
|
| 331 |
XNextEvent(levptr(XEvent));
|
| 332 |
switch (levptr(XEvent)->type) {
|
| 333 |
case KeyPressed:
|
| 334 |
getkey(levptr(XKeyPressedEvent));
|
| 335 |
break;
|
| 336 |
case ExposeWindow:
|
| 337 |
windowchange(levptr(XExposeEvent));
|
| 338 |
break;
|
| 339 |
case ExposeRegion:
|
| 340 |
fixwindow(levptr(XExposeEvent));
|
| 341 |
break;
|
| 342 |
case UnmapWindow:
|
| 343 |
if (levptr(XUnmapEvent)->subwindow == 0)
|
| 344 |
freepixels();
|
| 345 |
break;
|
| 346 |
case ButtonPressed: /* handled in x_getcur() */
|
| 347 |
break;
|
| 348 |
}
|
| 349 |
}
|
| 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 |
{
|
| 384 |
int n;
|
| 385 |
register char *str;
|
| 386 |
|
| 387 |
str = XLookupMapping(ekey, &n);
|
| 388 |
while (n-- > 0 && c_last < sizeof(c_queue)) {
|
| 389 |
c_queue[c_last++] = *str++;
|
| 390 |
x_driver.inpready++;
|
| 391 |
}
|
| 392 |
}
|
| 393 |
|
| 394 |
|
| 395 |
static
|
| 396 |
fixwindow(eexp) /* repair damage to window */
|
| 397 |
register XExposeEvent *eexp;
|
| 398 |
{
|
| 399 |
if (eexp->subwindow == 0)
|
| 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);
|
| 405 |
}
|