--- ray/src/rt/x11.c 1990/01/30 11:37:54 1.3 +++ ray/src/rt/x11.c 1990/02/23 10:26:30 1.6 @@ -5,9 +5,9 @@ static char SCCSid[] = "$SunId$ LBL"; /* Copyright (c) 1989 Regents of the University of California */ /* - * x11.c - driver for X-windows version 10.4 + * x11.c - driver for X-windows version 11.3 * - * 1989 + * Jan 1990 */ #include @@ -15,8 +15,8 @@ static char SCCSid[] = "$SunId$ LBL"; #include #include -#include -#include +#include +#include #include "color.h" #include "driver.h" @@ -24,6 +24,9 @@ static char SCCSid[] = "$SunId$ LBL"; #define GAMMA 2.2 /* exponent for color correction */ +#define MINWIDTH (32*COMCW) /* minimum graphics window width */ +#define MINHEIGHT MINWIDTH /* minimum graphics window height */ + #define BORWIDTH 5 /* border width */ #define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ @@ -32,29 +35,27 @@ static char SCCSid[] = "$SunId$ LBL"; #define COMCW 8 /* approx. character width (pixels) */ #define COMCH 14 /* approx. character height (pixels) */ -#ifndef WFLUSH -#define WFLUSH 30 /* flush after this many rays */ -#endif +#define ourscreen DefaultScreen(ourdisplay) +#define ourroot RootWindow(ourdisplay,ourscreen) +#define ourwhite WhitePixel(ourdisplay,ourscreen) +#define ourblack BlackPixel(ourdisplay,ourscreen) -#define checkinp() while (XPending(ourdisplay) > 0) getevent() +#define levptr(etype) ((etype *)¤tevent) -#define levptr(etype) ((etype *)&thisevent) +static XEvent currentevent; /* current event */ -static char *clientname; /* calling client's name */ - -static XEvent thisevent; /* current event */ - static int ncolors = 0; /* color table size */ static int *pixval = NULL; /* allocated pixels */ static Display *ourdisplay = NULL; /* our display */ +static Visual *ourvisual; /* our visual structure */ + static Window gwind = 0; /* our graphics window */ static Cursor pickcursor = 0; /* cursor used for picking */ -static int gwidth = 0; /* graphics window width */ -static int gheight = 0; /* graphics window height */ +static int gwidth, gheight; /* graphics window size */ static TEXTWIND *comline = NULL; /* our command line */ @@ -64,16 +65,16 @@ static int c_last = 0; /* last character in queue * static GC ourgc = 0; /* our graphics context for drawing */ -static Colormap ourmap; /* our color map */ +static Colormap ourmap = 0; /* our color map */ extern char *malloc(); int x11_close(), x11_clear(), x11_paintr(), x11_errout(), - x11_getcur(), x11_comout(), x11_comin(); + x11_getcur(), x11_comout(), x11_comin(), x11_flush(); static struct driver x11_driver = { x11_close, x11_clear, x11_paintr, x11_getcur, - x11_comout, x11_comin, 1.0 + x11_comout, x11_comin, x11_flush, 1.0 }; @@ -81,6 +82,9 @@ struct driver * x11_init(name, id) /* initialize driver */ char *name, *id; { + int nplanes; + XVisualInfo ourvinfo; + XWMHints ourxwmhints; Pixmap bmCursorSrc, bmCursorMsk; ourdisplay = XOpenDisplay(NULL); @@ -88,38 +92,46 @@ char *name, *id; stderr_v("cannot open X-windows; DISPLAY variable set?\n"); return(NULL); } - if (DisplayPlanes(ourdisplay, DefaultScreen(ourdisplay)) < 4) { + nplanes = DisplayPlanes(ourdisplay, ourscreen); + if (nplanes < 4) { stderr_v("not enough colors\n"); return(NULL); + } else if (nplanes <= 12) { + if (!XMatchVisualInfo(ourdisplay,ourscreen, + nplanes,PseudoColor,&ourvinfo)) { + stderr_v("PseudoColor not supported\n"); + return(NULL); + } + } else if (!XMatchVisualInfo(ourdisplay,ourscreen, + nplanes,TrueColor,&ourvinfo)) { + stderr_v("TrueColor not supported\n"); + return(NULL); } - ourmap = DefaultColormap(ourdisplay,DefaultScreen(ourdisplay)); - make_gmap(GAMMA); /* make color map */ - /* - bmCursorSrc = XCreateBitmapFromData(ourdisplay, - gwind, bcross_bits, - bcross_width, bcross_height); - bmCursorMsk = XCreateBitmapFromData(ourdisplay, - gwind, bcross_mask_bits, - bcross_width, bcross_height); - - pickcursor = XCreatePixmapCursor(ourdisplay, - bmCursorSrc, bmCursorMsk, - BlackPixel(ourdisplay, - DefaultScreen(ourdisplay)), - WhitePixel(ourdisplay, - DefaultScreen(ourdisplay)), - bcross_x_hot, bcross_y_hot); - XFreePixmap(ourdisplay, bmCursorSrc); - XFreePixmap(ourdisplay, bmCursorMsk); - */ + ourvisual = ourvinfo.visual; + make_gmap(GAMMA); /* create a cursor */ pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); - /* new */ - clientname = id; - x11_driver.xsiz = DisplayWidth(ourdisplay,DefaultScreen(ourdisplay)) - - 2*BORWIDTH; - x11_driver.ysiz = DisplayHeight(ourdisplay,DefaultScreen(ourdisplay)) - - (COMHEIGHT + 2*BORWIDTH); + /* open window */ + gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0, + DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, + DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, + BORWIDTH, ourblack, ourwhite); + if (gwind == 0) { + stderr_v("cannot create window\n"); + return(NULL); + } + XStoreName(ourdisplay, gwind, id); + ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); + ourxwmhints.flags = InputHint; + ourxwmhints.input = True; + XSetWMHints(ourdisplay, gwind, &ourxwmhints); + XSelectInput(ourdisplay, gwind, ExposureMask); + XMapWindow(ourdisplay, gwind); + XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent)); + gwidth = levptr(XExposeEvent)->width; + gheight = levptr(XExposeEvent)->height - COMHEIGHT; + x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; + x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; x11_driver.inpready = 0; cmdvec = x11_comout; /* set error vectors */ if (wrnvec != NULL) @@ -141,14 +153,13 @@ x11_close() /* close our display */ comline = NULL; } if (gwind != 0) { + freepixels(); XFreeGC(ourdisplay, ourgc); XDestroyWindow(ourdisplay, gwind); gwind = 0; - gwidth = gheight = 0; ourgc = 0; } XFreeCursor(ourdisplay, pickcursor); - freepixels(); XCloseDisplay(ourdisplay); ourdisplay = NULL; } @@ -158,55 +169,33 @@ static x11_clear(xres, yres) /* clear our display */ int xres, yres; { - XWMHints ourxwmhints; - XSetWindowAttributes ourwindowattr; - if (xres != gwidth || yres != gheight) { /* change window */ if (comline != NULL) xt_close(comline); - if (gwind == 0) { /* new window */ - ourwindowattr.backing_store = Always; - ourwindowattr.background_pixel = - WhitePixel(ourdisplay, DefaultScreen(ourdisplay)); - ourwindowattr.border_pixel = - BlackPixel(ourdisplay, DefaultScreen(ourdisplay)); - gwind = XCreateWindow(ourdisplay, - RootWindow(ourdisplay, - DefaultScreen(ourdisplay)), - 0, 0, xres, yres+COMHEIGHT, BORWIDTH, - 0, InputOutput, CopyFromParent, - CWBackingStore|CWBackPixel|CWBorderPixel, - &ourwindowattr); - if (gwind == 0) - goto fail; - XStoreName(ourdisplay, gwind, clientname); - ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); - ourxwmhints.flags = InputHint; - ourxwmhints.input = True; - XSetWMHints(ourdisplay, gwind, &ourxwmhints); - XSelectInput(ourdisplay, gwind, - KeyPressMask|ButtonPressMask); - XMapWindow(ourdisplay, gwind); - } else /* resize window */ - XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); + XSelectInput(ourdisplay, gwind, 0); + XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); comline = xt_open(ourdisplay, - DefaultGC(ourdisplay,DefaultScreen(ourdisplay)), + DefaultGC(ourdisplay,ourscreen), gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); - if (comline == NULL) - goto fail; + if (comline == NULL) { + stderr_v("Cannot open command line window\n"); + quit(1); + } + XSelectInput(ourdisplay, comline->w, ExposureMask); gwidth = xres; gheight = yres; - } else /* just clear */ - XClearWindow(ourdisplay, gwind); - /* reinitialize color table */ - if (ncolors == 0 && getpixels() == 0) - stderr_v("cannot allocate colors\n"); - else - new_ctab(ncolors); - return; -fail: - stderr_v("Failure opening window in x11_clear\n"); - quit(1); + XSync(ourdisplay, 1); /* discard input */ + sleep(2); /* wait for window manager */ + } + XClearWindow(ourdisplay, gwind); + if (ourvisual->class == PseudoColor) /* reinitialize color table */ + if (getpixels() == 0) + stderr_v("cannot allocate colors\n"); + else + new_ctab(ncolors); + + XSelectInput(ourdisplay, gwind, + StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); } @@ -215,34 +204,43 @@ x11_paintr(col, xmin, ymin, xmax, ymax) /* fill a rec COLOR col; int xmin, ymin, xmax, ymax; { - extern long nrays; /* global ray count */ extern int xnewcolr(); /* pixel assignment routine */ - static long lastflush = 0; /* ray count at last flush */ + extern unsigned long true_pixel(); + unsigned long pixel; - if (ncolors > 0) { - XSetForeground(ourdisplay, ourgc, - pixval[get_pixel(col, xnewcolr)]); - XFillRectangle(ourdisplay, gwind, - ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); - } - if (nrays - lastflush >= WFLUSH) { - if (ncolors <= 0) /* output necessary for death */ - XFillRectangle(ourdisplay, gwind, - ourgc, 0, 0, 1 ,1); - checkinp(); - lastflush = nrays; - } + if (ncolors > 0) + pixel = pixval[get_pixel(col, xnewcolr)]; + else if (ourvisual->class == TrueColor) + pixel = true_pixel(col); + else + return; + XSetForeground(ourdisplay, ourgc, pixel); + XFillRectangle(ourdisplay, gwind, + ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); } static +x11_flush() /* flush output */ +{ + if (ncolors <= 0) /* output necessary for death */ + XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1); + while (XPending(ourdisplay) > 0) + getevent(); +} + + +static x11_comin(inp, prompt) /* read in a command line */ char *inp, *prompt; { int x11_getc(), x11_comout(); if (prompt != NULL) - xt_puts(prompt, comline); + if (fromcombuf(inp, &x11_driver)) + return; + else + xt_puts(prompt, comline); xt_cursor(comline, TBLKCURS); editline(inp, x11_getc, x11_comout); xt_cursor(comline, TNOCURS); @@ -319,19 +317,54 @@ int r, g, b; static int getpixels() /* get the color map */ { - Visual *ourvis = DefaultVisual(ourdisplay,DefaultScreen(ourdisplay)); - - freepixels(); - for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){ + if (ncolors > 0) + return(ncolors); + if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) { + ourmap = DefaultColormap(ourdisplay,ourscreen); + goto loop; + } +newmap: + ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone); +loop: + for (ncolors = ourvisual->map_entries; + ncolors > ourvisual->map_entries/3; + ncolors = ncolors*.937) { pixval = (int *)malloc(ncolors*sizeof(int)); if (pixval == NULL) - break; + return(ncolors = 0); if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, pixval,ncolors) != 0) - return(ncolors); + break; free((char *)pixval); + pixval = NULL; } - return(ncolors = 0); + if (pixval == NULL) { + if (ourmap == DefaultColormap(ourdisplay,ourscreen)) + goto newmap; /* try it with our map */ + else + return(ncolors = 0); /* failed */ + } + if (ourmap != DefaultColormap(ourdisplay,ourscreen)) { + XColor thiscolor; + register int i, j; + /* reset black and white */ + for (i = 0; i < ncolors; i++) { + if (pixval[i] != ourblack && pixval[i] != ourwhite) + continue; + thiscolor.pixel = pixval[i]; + thiscolor.flags = DoRed|DoGreen|DoBlue; + XQueryColor(ourdisplay, + DefaultColormap(ourdisplay,ourscreen), + &thiscolor); + XStoreColor(ourdisplay, ourmap, &thiscolor); + for (j = i; j+1 < ncolors; j++) + pixval[j] = pixval[j+1]; + ncolors--; + i--; + } + } + XSetWindowColormap(ourdisplay, gwind, ourmap); + return(ncolors); } @@ -342,9 +375,27 @@ freepixels() /* free our pixels */ return; XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); ncolors = 0; + if (ourmap != 0 && ourmap != DefaultColormap(ourdisplay,ourscreen)) + XFreeColormap(ourdisplay, ourmap); + ourmap = 0; } +static unsigned long +true_pixel(col) /* return true pixel value for color */ +COLOR col; +{ + unsigned long rval; + BYTE rgb[3]; + + map_color(rgb, col); + rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask; + rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask; + rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask; + return(rval); +} + + static int x11_getc() /* get a command character */ { @@ -362,6 +413,22 @@ getevent() /* get next event */ { XNextEvent(ourdisplay, levptr(XEvent)); switch (levptr(XEvent)->type) { + case ConfigureNotify: + resizewindow(levptr(XConfigureEvent)); + break; + case UnmapNotify: + freepixels(); + break; + case MapNotify: + if (ourvisual->class == PseudoColor) + if (getpixels() == 0) + stderr_v("Cannot allocate colors\n"); + else + new_ctab(ncolors); + break; + case Expose: + fixwindow(levptr(XExposeEvent)); + break; case KeyPress: getkey(levptr(XKeyPressedEvent)); break; @@ -381,15 +448,32 @@ register XKeyPressedEvent *ekey; } -#ifdef notdef static fixwindow(eexp) /* repair damage to window */ register XExposeEvent *eexp; { - if (eexp->subwindow == 0) - repaint(eexp->x, gheight - eexp->y - eexp->height, + if (eexp->window == gwind) { + sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n", + eexp->x, gheight - eexp->y - eexp->height, eexp->x + eexp->width, gheight - eexp->y); - else if (eexp->subwindow == comline->w) - xt_redraw(comline); + } else if (eexp->window == comline->w) { + if (eexp->count == 0) + xt_redraw(comline); + } } -#endif + + +static +resizewindow(ersz) /* resize window */ +register XConfigureEvent *ersz; +{ + if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight) + return; + + gwidth = ersz->width; + gheight = ersz->height-COMHEIGHT; + x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; + x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight; + + strcpy(getcombuf(&x11_driver), "new\n"); +}