--- ray/src/rt/x11.c 1990/03/01 08:22:44 1.8 +++ ray/src/rt/x11.c 1992/06/18 14:55:24 2.5 @@ -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,6 +21,7 @@ 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 */ @@ -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 */ @@ -67,9 +67,9 @@ static GC ourgc = 0; /* our graphics context for dr static Colormap ourmap = 0; /* our color map */ -extern char *malloc(); +extern char *malloc(), *getcombuf(); -int x11_close(), x11_clear(), x11_paintr(), x11_errout(), +static int x11_close(), x11_clear(), x11_paintr(), x11_errout(), x11_getcur(), x11_comout(), x11_comin(), x11_flush(); static struct driver x11_driver = { @@ -83,50 +83,72 @@ x11_init(name, id) /* initialize driver */ char *name, *id; { int nplanes; - XVisualInfo ourvinfo; + XSetWindowAttributes ourwinattr; XWMHints ourxwmhints; + XSizeHints oursizhints; ourdisplay = XOpenDisplay(NULL); if (ourdisplay == NULL) { 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); - /* create a cursor */ - pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross); /* open window */ - gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0, + 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; @@ -166,19 +188,15 @@ 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; XFlush(ourdisplay); @@ -186,12 +204,23 @@ int xres, yres; XSync(ourdisplay, 1); /* discard input */ } XClearWindow(ourdisplay, gwind); - if (ourvisual->class == PseudoColor) /* reinitialize color table */ + /* reinitialize color table */ + 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); } @@ -208,7 +237,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; @@ -221,8 +250,7 @@ int xmin, ymin, xmax, ymax; static x11_flush() /* flush output */ { - if (ncolors <= 0 && ourvisual->class != TrueColor) /* dummy */ - XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1); + XNoOp(ourdisplay); while (XPending(ourdisplay) > 0) getevent(); } @@ -232,7 +260,7 @@ 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)) @@ -249,8 +277,11 @@ static x11_comout(out) /* output a string to command line */ char *out; { - if (comline != NULL) - xt_puts(out, comline); + if (comline == NULL) + return; + xt_puts(out, comline); + if (out[strlen(out)-1] == '\n') + XFlush(ourdisplay); } @@ -258,8 +289,8 @@ static x11_errout(msg) /* output an error message */ char *msg; { - x11_comout(msg); stderr_v(msg); /* send to stderr also! */ + x11_comout(msg); } @@ -314,19 +345,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, @@ -341,11 +375,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]; @@ -359,7 +390,6 @@ loop: ncolors--; i--; } - } XSetWindowColormap(ourdisplay, gwind, ourmap); return(ncolors); } @@ -386,9 +416,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); } @@ -417,7 +447,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