--- ray/src/rt/x11.c 1990/01/30 11:37:54 1.3 +++ ray/src/rt/x11.c 1994/09/20 15:46:34 2.18 @@ -1,29 +1,33 @@ +/* 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 10.4 + * x11.c - driver for X-windows version 11 * - * 1989 + * Jan 1990 */ #include - +#include #include #include -#include -#include +#include +#include #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/2) /* minimum graphics window height */ + #define BORWIDTH 5 /* border width */ #define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */ @@ -32,30 +36,29 @@ 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 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 int mapped = 0; /* window is mapped? */ +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 */ -static int gwidth = 0; /* graphics window width */ -static int gheight = 0; /* graphics window height */ +static int gwidth, gheight; /* graphics window size */ +static int comheight; /* desired comline height */ static TEXTWIND *comline = NULL; /* our command line */ static char c_queue[64]; /* input queue */ @@ -64,66 +67,126 @@ 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 int std_comin(), std_comout(); + static struct driver x11_driver = { x11_close, x11_clear, x11_paintr, x11_getcur, - x11_comout, x11_comin, 1.0 + NULL, NULL, x11_flush, 1.0 }; +static int getpixels(), xnewcolr(), freepixels(), resizewindow(), + getevent(), getkey(), fixwindow(), x11_getc(); +static unsigned long true_pixel(); + struct driver * x11_init(name, id) /* initialize driver */ char *name, *id; { - Pixmap bmCursorSrc, bmCursorMsk; - + extern char *getenv(); + char *gv; + int nplanes; + XSetWindowAttributes ourwinattr; + XWMHints ourxwmhints; + XSizeHints oursizhints; + /* open display server */ ourdisplay = XOpenDisplay(NULL); if (ourdisplay == NULL) { stderr_v("cannot open X-windows; DISPLAY variable set?\n"); return(NULL); } - if (DisplayPlanes(ourdisplay, DefaultScreen(ourdisplay)) < 4) { - stderr_v("not enough colors\n"); + /* 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); + } + /* set gamma */ + if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL + || (gv = getenv("DISPLAY_GAMMA")) != NULL) + make_gmap(atof(gv)); + else + make_gmap(GAMMA); + /* X11 command line or no? */ + if (!strcmp(name, "x11")) + comheight = COMHEIGHT; + else /* "x11d" */ + comheight = 0; + /* 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, ourvinfo.depth, InputOutput, ourvinfo.visual, + CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); + if (gwind == 0) { + stderr_v("cannot create window\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); - */ + XStoreName(ourdisplay, gwind, id); /* 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); + pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross); + ourgc = XCreateGC(ourdisplay, gwind, 0, NULL); + 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(XEvent)); + 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) - wrnvec = x11_errout; + mapped = 1; + /* set i/o vectors */ + if (comheight) { + x11_driver.comin = x11_comin; + x11_driver.comout = x11_comout; + cmdvec = x11_comout; + if (wrnvec != NULL) + wrnvec = x11_errout; + } else { + x11_driver.comin = std_comin; + x11_driver.comout = std_comout; + cmdvec = std_comout; + } return(&x11_driver); } @@ -140,15 +203,12 @@ x11_close() /* close our display */ xt_close(comline); comline = NULL; } - if (gwind != 0) { - XFreeGC(ourdisplay, ourgc); - XDestroyWindow(ourdisplay, gwind); - gwind = 0; - gwidth = gheight = 0; - ourgc = 0; - } - XFreeCursor(ourdisplay, pickcursor); freepixels(); + XFreeGC(ourdisplay, ourgc); + XDestroyWindow(ourdisplay, gwind); + gwind = 0; + ourgc = 0; + XFreeCursor(ourdisplay, pickcursor); XCloseDisplay(ourdisplay); ourdisplay = NULL; } @@ -158,55 +218,45 @@ 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); - comline = xt_open(ourdisplay, - DefaultGC(ourdisplay,DefaultScreen(ourdisplay)), - gwind, 0, yres, xres, COMHEIGHT, 0, COMFN); - if (comline == NULL) - goto fail; + /* 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); gwidth = xres; gheight = yres; - } else /* just clear */ - XClearWindow(ourdisplay, gwind); + XFlush(ourdisplay); + sleep(2); /* wait for window manager */ + XSync(ourdisplay, 1); /* discard input */ + } + 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); + 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); + if (comheight) { + 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); + } else /* remove earmuffs */ + XSelectInput(ourdisplay, gwind, + StructureNotifyMask|ExposureMask|ButtonPressMask); } @@ -215,23 +265,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 */ + 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 (!mapped) + return; + if (ncolors > 0) + pixel = pixval[get_pixel(col, xnewcolr)]; + else + pixel = true_pixel(col); + XSetForeground(ourdisplay, ourgc, pixel); + XFillRectangle(ourdisplay, gwind, + ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin); +} + + +static +x11_flush() /* flush output */ +{ + int n; + /* check for input */ + XNoOp(ourdisplay); + n = XPending(ourdisplay); /* from X server */ + while (n-- > 0) + getevent(); + if (x11_driver.comin == std_comin) { /* from stdin */ + if (ioctl(fileno(stdin), FIONREAD, &n) < 0) { + stderr_v("ioctl error on stdin\n"); + quit(1); + } + x11_driver.inpready += n; } - if (nrays - lastflush >= WFLUSH) { - if (ncolors <= 0) /* output necessary for death */ - XFillRectangle(ourdisplay, gwind, - ourgc, 0, 0, 1 ,1); - checkinp(); - lastflush = nrays; - } } @@ -239,10 +302,12 @@ static x11_comin(inp, prompt) /* read in a command line */ char *inp, *prompt; { - int x11_getc(), x11_comout(); - - if (prompt != NULL) + if (prompt != NULL) { + x11_flush(); /* make sure we get everything */ + if (fromcombuf(inp, &x11_driver)) + return; xt_puts(prompt, comline); + } xt_cursor(comline, TBLKCURS); editline(inp, x11_getc, x11_comout); xt_cursor(comline, TNOCURS); @@ -250,12 +315,14 @@ char *inp, *prompt; static -x11_comout(out) /* output a string to command line */ -char *out; +x11_comout(outp) /* output a string to command line */ +char *outp; { - if (comline != NULL) - xt_puts(out, comline); - XFlush(ourdisplay); + if (comline == NULL) + return; + xt_puts(outp, comline); + if (outp[strlen(outp)-1] == '\n') + XFlush(ourdisplay); } @@ -263,11 +330,41 @@ static x11_errout(msg) /* output an error message */ char *msg; { - x11_comout(msg); stderr_v(msg); /* send to stderr also! */ + x11_comout(msg); } +static +std_comin(inp, prompt) /* read in command line from stdin */ +char *inp, *prompt; +{ + extern char *gets(); + + if (prompt != NULL) { + if (fromcombuf(inp, &x11_driver)) + return; + if (!x11_driver.inpready) + fputs(prompt, stdout); + } + if (gets(inp) == NULL) { + strcpy(inp, "quit"); + return; + } + x11_driver.inpready -= strlen(inp) + 1; + if (x11_driver.inpready < 0) + x11_driver.inpready = 0; +} + + +static +std_comout(outp) /* write out string to stdout */ +char *outp; +{ + fputs(outp, stdout); +} + + static int x11_getcur(xp, yp) /* get cursor position */ int *xp, *yp; @@ -287,14 +384,12 @@ int *xp, *yp; if (c_last > c_first) /* key pressed */ return(x11_getc()); /* button pressed */ - if (levptr(XButtonPressedEvent)->button & Button1) + if (levptr(XButtonPressedEvent)->button == Button1) return(MB1); - if (levptr(XButtonPressedEvent)->button & Button2) + if (levptr(XButtonPressedEvent)->button == Button2) return(MB2); - if (levptr(XButtonPressedEvent)->button & Button3) + if (levptr(XButtonPressedEvent)->button == Button3) return(MB3); - if (levptr(XButtonPressedEvent)->button & (Button4|Button5)) - return(MB1); return(ABORT); } @@ -319,19 +414,52 @@ int r, g, b; static int getpixels() /* get the color map */ { - Visual *ourvis = DefaultVisual(ourdisplay,DefaultScreen(ourdisplay)); + XColor thiscolor; + register int i, j; - freepixels(); - for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){ - pixval = (int *)malloc(ncolors*sizeof(int)); + if (ncolors > 0) + return(ncolors); + 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) + return(ncolors = 0); + if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) break; - if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0, - pixval,ncolors) != 0) - return(ncolors); 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); } @@ -341,10 +469,30 @@ freepixels() /* free our pixels */ if (ncolors == 0) return; XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); + free((char *)pixval); + pixval = NULL; 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 */ { @@ -362,6 +510,25 @@ getevent() /* get next event */ { XNextEvent(ourdisplay, levptr(XEvent)); switch (levptr(XEvent)->type) { + case ConfigureNotify: + resizewindow(levptr(XConfigureEvent)); + break; + case UnmapNotify: + mapped = 0; + freepixels(); + break; + case MapNotify: + if (ourvinfo.class == PseudoColor || + ourvinfo.class == GrayScale) + if (getpixels() == 0) + stderr_v("Cannot allocate colors\n"); + else + new_ctab(ncolors); + mapped = 1; + break; + case Expose: + fixwindow(levptr(XExposeEvent)); + break; case KeyPress: getkey(levptr(XKeyPressedEvent)); break; @@ -375,21 +542,41 @@ 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; } -#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"); +}