--- ray/src/rt/x11.c 1990/03/02 15:05:26 1.10 +++ ray/src/rt/x11.c 1992/05/29 10:57:15 2.2 @@ -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 */ @@ -45,11 +46,11 @@ static char SCCSid[] = "$SunId$ LBL"; 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 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 +68,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,40 +84,43 @@ 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, - nplanes,PseudoColor,&ourvinfo)) { - stderr_v("PseudoColor not supported\n"); + if ( !XMatchVisualInfo(ourdisplay,ourscreen, + 24,TrueColor,&ourvinfo) && + !XMatchVisualInfo(ourdisplay,ourscreen, + 24,DirectColor,&ourvinfo) ) + if (nplanes < 4) { + stderr_v("not enough colors\n"); return(NULL); + } else if (!XMatchVisualInfo(ourdisplay,ourscreen, + 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); - } - ourvisual = ourvinfo.visual; make_gmap(GAMMA); /* open window */ - ourwinattr.background_pixel = ourwhite; + ourwinattr.background_pixel = ourblack; ourwinattr.border_pixel = ourblack; + /* this is a waste! */ + 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); @@ -125,12 +129,18 @@ char *name, *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; @@ -170,19 +180,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); @@ -190,12 +196,24 @@ 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, + DefaultGC(ourdisplay,ourscreen), + gwind, 0, gheight, gwidth, COMHEIGHT, 0, 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); } @@ -212,7 +230,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; @@ -235,7 +253,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)) @@ -252,8 +270,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); } @@ -263,7 +284,6 @@ char *msg; { stderr_v(msg); /* send to stderr also! */ x11_comout(msg); - x11_flush(); } @@ -318,19 +338,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, @@ -345,11 +368,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]; @@ -363,7 +383,6 @@ loop: ncolors--; i--; } - } XSetWindowColormap(ourdisplay, gwind, ourmap); return(ncolors); } @@ -390,9 +409,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); } @@ -421,7 +440,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