--- ray/src/rt/x11.c 1990/03/12 11:08:48 1.14 +++ 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,8 +21,9 @@ 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 (32*COMCW) /* minimum graphics window width */ #define MINHEIGHT MINWIDTH /* minimum graphics window height */ @@ -37,19 +38,18 @@ static char SCCSid[] = "$SunId$ LBL"; #define ourscreen DefaultScreen(ourdisplay) #define ourroot RootWindow(ourdisplay,ourscreen) -#define ourwhite WhitePixel(ourdisplay,ourscreen) -#define ourblack BlackPixel(ourdisplay,ourscreen) #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 Visual *ourvisual; /* our visual structure */ +static XVisualInfo ourvinfo; /* our visual information */ static Window gwind = 0; /* our graphics window */ @@ -82,8 +82,9 @@ struct driver * x11_init(name, id) /* initialize driver */ char *name, *id; { + extern char *getenv(); + char *gv; int nplanes; - XVisualInfo ourvinfo; XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; XSizeHints oursizhints; @@ -93,41 +94,59 @@ char *name, *id; stderr_v("cannot open X-windows; DISPLAY variable set?\n"); return(NULL); } + /* find a usable visual */ nplanes = DisplayPlanes(ourdisplay, ourscreen); - if (nplanes < 4) { - stderr_v("not enough colors\n"); - return(NULL); - } else if (nplanes <= 12) { + 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)) { - stderr_v("PseudoColor not supported\n"); + nplanes,PseudoColor,&ourvinfo) && + !XMatchVisualInfo(ourdisplay,ourscreen, + nplanes,GrayScale,&ourvinfo)) { + stderr_v("unsupported visual type\n"); return(NULL); } - } else if (!XMatchVisualInfo(ourdisplay,ourscreen, - nplanes,TrueColor,&ourvinfo)) { - stderr_v("TrueColor not supported\n"); - return(NULL); + ourblack = BlackPixel(ourdisplay,ourscreen); + ourwhite = WhitePixel(ourdisplay,ourscreen); } - ourvisual = ourvinfo.visual; - make_gmap(GAMMA); - /* open window */ + /* 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, nplanes, InputOutput, ourvisual, - CWBackPixel|CWBorderPixel, &ourwinattr); + 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; @@ -135,7 +154,7 @@ char *name, *id; 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; @@ -192,7 +211,7 @@ int xres, yres; } XClearWindow(ourdisplay, gwind); /* reinitialize color table */ - if (ourvisual->class == PseudoColor) + if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) if (getpixels() == 0) stderr_v("cannot allocate colors\n"); else @@ -200,9 +219,8 @@ int xres, yres; /* get new command line */ if (comline != NULL) xt_close(comline); - comline = xt_open(ourdisplay, - DefaultGC(ourdisplay,ourscreen), - gwind, 0, gheight, gwidth, COMHEIGHT, 0, COMFN); + 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); @@ -225,7 +243,7 @@ int xmin, ymin, xmax, ymax; if (ncolors > 0) pixel = pixval[get_pixel(col, xnewcolr)]; - else if (ourvisual->class == TrueColor) + else if (ourvinfo.class == TrueColor || ourvinfo.class == DirectColor) pixel = true_pixel(col); else return; @@ -333,19 +351,22 @@ int r, g, b; static int getpixels() /* get the color map */ { + XColor thiscolor; + register int i, j; + if (ncolors > 0) return(ncolors); - if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) { + if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) { ourmap = DefaultColormap(ourdisplay,ourscreen); goto loop; } newmap: - ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone); + ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone); loop: - for (ncolors = ourvisual->map_entries; - ncolors > ourvisual->map_entries/3; + for (ncolors = ourvinfo.colormap_size; + ncolors > ourvinfo.colormap_size/3; ncolors = ncolors*.937) { - pixval = (int *)malloc(ncolors*sizeof(int)); + pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long)); if (pixval == NULL) return(ncolors = 0); if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, @@ -360,11 +381,8 @@ loop: 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 (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]; @@ -378,7 +396,6 @@ loop: ncolors--; i--; } - } XSetWindowColormap(ourdisplay, gwind, ourmap); return(ncolors); } @@ -405,9 +422,9 @@ COLOR col; 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; + 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); } @@ -436,7 +453,8 @@ getevent() /* get next event */ freepixels(); break; case MapNotify: - if (ourvisual->class == PseudoColor) + if (ourvinfo.class == PseudoColor || + ourvinfo.class == GrayScale) if (getpixels() == 0) stderr_v("Cannot allocate colors\n"); else