--- ray/src/rt/x11.c 1990/02/22 10:22:01 1.4 +++ ray/src/rt/x11.c 1992/06/24 09:12:50 2.6 @@ -1,11 +1,11 @@ +/* Copyright (c) 1992 Regents of the University of California */ + #ifndef lint static char SCCSid[] = "$SunId$ LBL"; #endif -/* Copyright (c) 1989 Regents of the University of California */ - /* - * x11.c - driver for X-windows version 11.3 + * x11.c - driver for X-windows version 11 * * Jan 1990 */ @@ -21,11 +21,12 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" #include "driver.h" #include "x11twind.h" +#include "x11icon.h" -#define GAMMA 2.2 /* exponent for color correction */ +#define GAMMA 2.2 /* default exponent correction */ -#define MINWIDTH (4*COMCW) /* minimum graphics window width */ -#define MINHEIGHT 16 /* minimum graphics window height */ +#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) */ @@ -35,26 +36,21 @@ 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) static XEvent currentevent; /* current event */ static int ncolors = 0; /* color table size */ -static int *pixval = NULL; /* allocated pixels */ +static unsigned long *pixval = NULL; /* allocated pixels */ +static unsigned long ourblack=0, ourwhite=1; static Display *ourdisplay = NULL; /* our display */ +static XVisualInfo ourvinfo; /* our visual information */ + static Window gwind = 0; /* our graphics window */ static Cursor pickcursor = 0; /* cursor used for picking */ @@ -69,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(); +extern char *malloc(), *getcombuf(); -int x11_close(), x11_clear(), x11_paintr(), x11_errout(), - x11_getcur(), x11_comout(), x11_comin(); +static int x11_close(), x11_clear(), x11_paintr(), x11_errout(), + 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 }; @@ -86,39 +82,79 @@ struct driver * x11_init(name, id) /* initialize driver */ char *name, *id; { + extern char *getenv(); + char *gv; + int nplanes; + XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; - Pixmap bmCursorSrc, bmCursorMsk; + XSizeHints oursizhints; ourdisplay = XOpenDisplay(NULL); if (ourdisplay == NULL) { stderr_v("cannot open X-windows; DISPLAY variable set?\n"); return(NULL); } - if (DisplayPlanes(ourdisplay, ourscreen) < 4) { - stderr_v("not enough colors\n"); - return(NULL); + /* find a usable visual */ + nplanes = DisplayPlanes(ourdisplay, ourscreen); + if (XMatchVisualInfo(ourdisplay,ourscreen, + 24,TrueColor,&ourvinfo) || + XMatchVisualInfo(ourdisplay,ourscreen, + 24,DirectColor,&ourvinfo)) { + ourblack = 0; + ourwhite = ourvinfo.red_mask | + ourvinfo.green_mask | + ourvinfo.blue_mask ; + } else { + if (nplanes < 4) { + stderr_v("not enough colors\n"); + return(NULL); + } + if (!XMatchVisualInfo(ourdisplay,ourscreen, + nplanes,PseudoColor,&ourvinfo) && + !XMatchVisualInfo(ourdisplay,ourscreen, + nplanes,GrayScale,&ourvinfo)) { + stderr_v("unsupported visual type\n"); + return(NULL); + } + ourblack = BlackPixel(ourdisplay,ourscreen); + ourwhite = WhitePixel(ourdisplay,ourscreen); } - ourmap = DefaultColormap(ourdisplay,ourscreen); - make_gmap(GAMMA); /* make color map */ - /* create a cursor */ - pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); - /* open window */ - gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0, + /* set gamma */ + if ((gv = getenv("GAMMA")) != NULL) + make_gmap(atof(gv)); + else + make_gmap(GAMMA); + /* open window */ + ourwinattr.background_pixel = ourblack; + ourwinattr.border_pixel = ourblack; + /* this is stupid */ + ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot, + ourvinfo.visual, AllocNone); + gwind = XCreateWindow(ourdisplay, ourroot, 0, 0, DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH, DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH, - BORWIDTH, ourblack, ourwhite); + BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual, + CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); if (gwind == 0) { stderr_v("cannot create window\n"); return(NULL); } - XStoreName(ourdisplay, gwind, id); + XStoreName(ourdisplay, gwind, id); + /* create a cursor */ + pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross); ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); - ourxwmhints.flags = InputHint; + ourxwmhints.flags = InputHint|IconPixmapHint; ourxwmhints.input = True; + ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, + gwind, x11icon_bits, x11icon_width, x11icon_height); XSetWMHints(ourdisplay, gwind, &ourxwmhints); + oursizhints.min_width = MINWIDTH; + oursizhints.min_height = MINHEIGHT+COMHEIGHT; + oursizhints.flags = PMinSize; + XSetNormalHints(ourdisplay, gwind, &oursizhints); XSelectInput(ourdisplay, gwind, ExposureMask); XMapWindow(ourdisplay, gwind); - XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent)); + XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent)); gwidth = levptr(XExposeEvent)->width; gheight = levptr(XExposeEvent)->height - COMHEIGHT; x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth; @@ -143,14 +179,12 @@ x11_close() /* close our display */ xt_close(comline); comline = NULL; } - if (gwind != 0) { - XFreeGC(ourdisplay, ourgc); - XDestroyWindow(ourdisplay, gwind); - gwind = 0; - ourgc = 0; - } - XFreeCursor(ourdisplay, pickcursor); freepixels(); + XFreeGC(ourdisplay, ourgc); + XDestroyWindow(ourdisplay, gwind); + gwind = 0; + ourgc = 0; + XFreeCursor(ourdisplay, pickcursor); XCloseDisplay(ourdisplay); ourdisplay = NULL; } @@ -160,31 +194,39 @@ static x11_clear(xres, yres) /* clear our display */ int xres, yres; { - if (xres != gwidth || yres != gheight) { /* change window */ - if (comline != NULL) - xt_close(comline); + /* check limits */ + if (xres < MINWIDTH) + xres = MINWIDTH; + if (yres < MINHEIGHT) + yres = MINHEIGHT; + /* resize window */ + if (xres != gwidth || yres != gheight) { XSelectInput(ourdisplay, gwind, 0); XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT); - comline = xt_open(ourdisplay, - DefaultGC(ourdisplay,ourscreen), - gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); - if (comline == NULL) { - stderr_v("Cannot open command line window\n"); - quit(1); - } - XSelectInput(ourdisplay, comline->w, ExposureMask); gwidth = xres; gheight = yres; - XSync(ourdisplay, 1); /* discard input */ + XFlush(ourdisplay); sleep(2); /* wait for window manager */ + XSync(ourdisplay, 1); /* discard input */ } XClearWindow(ourdisplay, gwind); /* reinitialize color table */ - if (getpixels() == 0) - stderr_v("cannot allocate colors\n"); - else - new_ctab(ncolors); - + if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) + if (getpixels() == 0) + stderr_v("cannot allocate colors\n"); + else + new_ctab(ncolors); + /* get new command line */ + if (comline != NULL) + xt_close(comline); + comline = xt_open(ourdisplay, gwind, 0, gheight, + gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN); + if (comline == NULL) { + stderr_v("Cannot open command line window\n"); + quit(1); + } + XSelectInput(ourdisplay, comline->w, ExposureMask); + /* remove earmuffs */ XSelectInput(ourdisplay, gwind, StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask); } @@ -195,31 +237,36 @@ 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 (ourvinfo.class == TrueColor || ourvinfo.class == DirectColor) + 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 */ +{ + XNoOp(ourdisplay); + while (XPending(ourdisplay) > 0) + getevent(); +} + + +static x11_comin(inp, prompt) /* read in a command line */ char *inp, *prompt; { - int x11_getc(), x11_comout(); + extern int x11_getc(); if (prompt != NULL) if (fromcombuf(inp, &x11_driver)) @@ -236,9 +283,11 @@ static x11_comout(out) /* output a string to command line */ char *out; { - if (comline != NULL) - xt_puts(out, comline); - XFlush(ourdisplay); + if (comline == NULL) + return; + xt_puts(out, comline); + if (out[strlen(out)-1] == '\n') + XFlush(ourdisplay); } @@ -246,8 +295,8 @@ static x11_errout(msg) /* output an error message */ char *msg; { - x11_comout(msg); stderr_v(msg); /* send to stderr also! */ + x11_comout(msg); } @@ -302,20 +351,53 @@ int r, g, b; static int getpixels() /* get the color map */ { - Visual *ourvis = DefaultVisual(ourdisplay,ourscreen); + XColor thiscolor; + register int i, j; if (ncolors > 0) return(ncolors); - for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){ - pixval = (int *)malloc(ncolors*sizeof(int)); + if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) { + ourmap = DefaultColormap(ourdisplay,ourscreen); + goto loop; + } +newmap: + ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone); +loop: + for (ncolors = ourvinfo.colormap_size; + ncolors > ourvinfo.colormap_size/3; + ncolors = ncolors*.937) { + pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long)); 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)) + for (i = 0; i < ncolors; i++) { /* reset black and white */ + 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); } @@ -326,9 +408,27 @@ freepixels() /* free our pixels */ return; XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); ncolors = 0; + if (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 = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask; + rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask; + rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask; + return(rval); +} + + static int x11_getc() /* get a command character */ { @@ -353,10 +453,12 @@ getevent() /* get next event */ freepixels(); break; case MapNotify: - if (getpixels() == 0) - stderr_v("Cannot allocate colors\n"); - else - new_ctab(ncolors); + if (ourvinfo.class == PseudoColor || + ourvinfo.class == GrayScale) + if (getpixels() == 0) + stderr_v("Cannot allocate colors\n"); + else + new_ctab(ncolors); break; case Expose: fixwindow(levptr(XExposeEvent)); @@ -374,9 +476,12 @@ static getkey(ekey) /* get input key */ register XKeyPressedEvent *ekey; { - c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, + register int n; + + n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last, NULL, NULL); - x11_driver.inpready = c_last-c_first; + c_last += n; + x11_driver.inpready += n; }