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