ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
(Generate patch)

Comparing ray/src/rt/x11.c (file contents):
Revision 1.5 by greg, Thu Feb 22 11:46:26 1990 UTC vs.
Revision 2.6 by greg, Wed Jun 24 09:12:50 1992 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1992 Regents of the University of California */
2 +
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
5   #endif
6  
5 /* Copyright (c) 1989 Regents of the University of California */
6
7   /*
8 < *  x11.c - driver for X-windows version 11.3
8 > *  x11.c - driver for X-windows version 11
9   *
10   *     Jan 1990
11   */
# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21   #include  "color.h"
22   #include  "driver.h"
23   #include  "x11twind.h"
24 + #include  "x11icon.h"
25  
26 < #define GAMMA           2.2             /* exponent for color correction */
26 > #define GAMMA           2.2             /* default exponent correction */
27  
28 < #define MINWIDTH        (4*COMCW)       /* minimum graphics window width */
29 < #define MINHEIGHT       16              /* minimum graphics window height */
28 > #define MINWIDTH        (32*COMCW)      /* minimum graphics window width */
29 > #define MINHEIGHT       MINWIDTH        /* minimum graphics window height */
30  
31   #define BORWIDTH        5               /* border width */
32   #define COMHEIGHT       (COMLH*COMCH)   /* command line height (pixels) */
# Line 37 | Line 38 | static char SCCSid[] = "$SunId$ LBL";
38  
39   #define  ourscreen      DefaultScreen(ourdisplay)
40   #define  ourroot        RootWindow(ourdisplay,ourscreen)
40 #define  ourwhite       WhitePixel(ourdisplay,ourscreen)
41 #define  ourblack       BlackPixel(ourdisplay,ourscreen)
41  
42   #define  levptr(etype)  ((etype *)&currentevent)
43  
44   static XEvent  currentevent;            /* current event */
45  
46   static int  ncolors = 0;                /* color table size */
47 < static int  *pixval = NULL;             /* allocated pixels */
47 > static unsigned long  *pixval = NULL;   /* allocated pixels */
48 > static unsigned long  ourblack=0, ourwhite=1;
49  
50   static Display  *ourdisplay = NULL;     /* our display */
51  
52 + static XVisualInfo  ourvinfo;           /* our visual information */
53 +
54   static Window  gwind = 0;               /* our graphics window */
55  
56   static Cursor  pickcursor = 0;          /* cursor used for picking */
# Line 63 | Line 65 | static int  c_last = 0;                        /* last character in queue *
65  
66   static GC  ourgc = 0;                   /* our graphics context for drawing */
67  
68 < static Colormap ourmap;                 /* our color map */
68 > static Colormap ourmap = 0;             /* our color map */
69  
70 < extern char  *malloc();
70 > extern char  *malloc(), *getcombuf();
71  
72 < int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
72 > static int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
73                  x11_getcur(), x11_comout(), x11_comin(), x11_flush();
74  
75   static struct driver  x11_driver = {
# Line 80 | Line 82 | struct driver *
82   x11_init(name, id)              /* initialize driver */
83   char  *name, *id;
84   {
85 +        extern char  *getenv();
86 +        char  *gv;
87 +        int  nplanes;
88 +        XSetWindowAttributes    ourwinattr;
89          XWMHints  ourxwmhints;
90 <        Pixmap  bmCursorSrc, bmCursorMsk;
90 >        XSizeHints      oursizhints;
91  
92          ourdisplay = XOpenDisplay(NULL);
93          if (ourdisplay == NULL) {
94                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
95                  return(NULL);
96          }
97 <        if (DisplayPlanes(ourdisplay, ourscreen) < 4) {
98 <                stderr_v("not enough colors\n");
99 <                return(NULL);
97 >                                        /* find a usable visual */
98 >        nplanes = DisplayPlanes(ourdisplay, ourscreen);
99 >        if (XMatchVisualInfo(ourdisplay,ourscreen,
100 >                                24,TrueColor,&ourvinfo) ||
101 >                        XMatchVisualInfo(ourdisplay,ourscreen,
102 >                                24,DirectColor,&ourvinfo)) {
103 >                ourblack = 0;
104 >                ourwhite = ourvinfo.red_mask |
105 >                                ourvinfo.green_mask |
106 >                                ourvinfo.blue_mask ;
107 >        } else {
108 >                if (nplanes < 4) {
109 >                        stderr_v("not enough colors\n");
110 >                        return(NULL);
111 >                }
112 >                if (!XMatchVisualInfo(ourdisplay,ourscreen,
113 >                                        nplanes,PseudoColor,&ourvinfo) &&
114 >                                !XMatchVisualInfo(ourdisplay,ourscreen,
115 >                                        nplanes,GrayScale,&ourvinfo)) {
116 >                        stderr_v("unsupported visual type\n");
117 >                        return(NULL);
118 >                }
119 >                ourblack = BlackPixel(ourdisplay,ourscreen);
120 >                ourwhite = WhitePixel(ourdisplay,ourscreen);
121          }
122 <        ourmap = DefaultColormap(ourdisplay,ourscreen);
123 <        make_gmap(GAMMA);                       /* make color map */
124 <        /* create a cursor */
125 <        pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross);
126 <        /* open window */
127 <        gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0,
122 >                                        /* set gamma */
123 >        if ((gv = getenv("GAMMA")) != NULL)
124 >                make_gmap(atof(gv));
125 >        else
126 >                make_gmap(GAMMA);
127 >                                        /* open window */
128 >        ourwinattr.background_pixel = ourblack;
129 >        ourwinattr.border_pixel = ourblack;
130 >                                        /* this is stupid */
131 >        ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
132 >                                ourvinfo.visual, AllocNone);
133 >        gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
134                  DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
135                  DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
136 <                BORWIDTH, ourblack, ourwhite);
136 >                BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
137 >                CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
138          if (gwind == 0) {
139                  stderr_v("cannot create window\n");
140                  return(NULL);
141          }
142 <        XStoreName(ourdisplay, gwind, id);
142 >        XStoreName(ourdisplay, gwind, id);
143 >        /* create a cursor */
144 >        pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross);
145          ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
146 <        ourxwmhints.flags = InputHint;
146 >        ourxwmhints.flags = InputHint|IconPixmapHint;
147          ourxwmhints.input = True;
148 +        ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
149 +                        gwind, x11icon_bits, x11icon_width, x11icon_height);
150          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
151 +        oursizhints.min_width = MINWIDTH;
152 +        oursizhints.min_height = MINHEIGHT+COMHEIGHT;
153 +        oursizhints.flags = PMinSize;
154 +        XSetNormalHints(ourdisplay, gwind, &oursizhints);
155          XSelectInput(ourdisplay, gwind, ExposureMask);
156          XMapWindow(ourdisplay, gwind);
157 <        XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent));
157 >        XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent));
158          gwidth = levptr(XExposeEvent)->width;
159          gheight = levptr(XExposeEvent)->height - COMHEIGHT;
160          x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
# Line 137 | Line 179 | x11_close()                    /* close our display */
179                  xt_close(comline);
180                  comline = NULL;
181          }
140        if (gwind != 0) {
141                XFreeGC(ourdisplay, ourgc);
142                XDestroyWindow(ourdisplay, gwind);
143                gwind = 0;
144                ourgc = 0;
145        }
146        XFreeCursor(ourdisplay, pickcursor);
182          freepixels();
183 +        XFreeGC(ourdisplay, ourgc);
184 +        XDestroyWindow(ourdisplay, gwind);
185 +        gwind = 0;
186 +        ourgc = 0;
187 +        XFreeCursor(ourdisplay, pickcursor);
188          XCloseDisplay(ourdisplay);
189          ourdisplay = NULL;
190   }
# Line 154 | Line 194 | static
194   x11_clear(xres, yres)                   /* clear our display */
195   int  xres, yres;
196   {
197 <        if (xres != gwidth || yres != gheight) {        /* change window */
198 <                if (comline != NULL)
199 <                        xt_close(comline);
197 >                                                /* check limits */
198 >        if (xres < MINWIDTH)
199 >                xres = MINWIDTH;
200 >        if (yres < MINHEIGHT)
201 >                yres = MINHEIGHT;
202 >                                                /* resize window */
203 >        if (xres != gwidth || yres != gheight) {
204                  XSelectInput(ourdisplay, gwind, 0);
205                  XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
162                comline = xt_open(ourdisplay,
163                                DefaultGC(ourdisplay,ourscreen),
164                                gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
165                if (comline == NULL) {
166                        stderr_v("Cannot open command line window\n");
167                        quit(1);
168                }
169                XSelectInput(ourdisplay, comline->w, ExposureMask);
206                  gwidth = xres;
207                  gheight = yres;
208 <                XSync(ourdisplay, 1);           /* discard input */
208 >                XFlush(ourdisplay);
209                  sleep(2);                       /* wait for window manager */
210 +                XSync(ourdisplay, 1);           /* discard input */
211          }
212          XClearWindow(ourdisplay, gwind);
213                                                  /* reinitialize color table */
214 <        if (getpixels() == 0)
215 <                stderr_v("cannot allocate colors\n");
216 <        else
217 <                new_ctab(ncolors);
218 <
214 >        if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale)
215 >                if (getpixels() == 0)
216 >                        stderr_v("cannot allocate colors\n");
217 >                else
218 >                        new_ctab(ncolors);
219 >                                                /* get new command line */
220 >        if (comline != NULL)
221 >                xt_close(comline);
222 >        comline = xt_open(ourdisplay, gwind, 0, gheight,
223 >                        gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN);
224 >        if (comline == NULL) {
225 >                stderr_v("Cannot open command line window\n");
226 >                quit(1);
227 >        }
228 >        XSelectInput(ourdisplay, comline->w, ExposureMask);
229 >                                                /* remove earmuffs */
230          XSelectInput(ourdisplay, gwind,
231                  StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
232   }
# Line 190 | Line 238 | COLOR  col;
238   int  xmin, ymin, xmax, ymax;
239   {
240          extern int  xnewcolr();         /* pixel assignment routine */
241 +        extern unsigned long  true_pixel();
242 +        unsigned long  pixel;
243  
244 <        if (ncolors > 0) {
245 <                XSetForeground(ourdisplay, ourgc,
246 <                                pixval[get_pixel(col, xnewcolr)]);
247 <                XFillRectangle(ourdisplay, gwind,
248 <                        ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
249 <        }
244 >        if (ncolors > 0)
245 >                pixel = pixval[get_pixel(col, xnewcolr)];
246 >        else if (ourvinfo.class == TrueColor || ourvinfo.class == DirectColor)
247 >                pixel = true_pixel(col);
248 >        else
249 >                return;
250 >        XSetForeground(ourdisplay, ourgc, pixel);
251 >        XFillRectangle(ourdisplay, gwind,
252 >                ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
253   }
254  
255  
256   static
257   x11_flush()                     /* flush output */
258   {
259 <        if (ncolors <= 0)       /* output necessary for death */
207 <                XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1);
259 >        XNoOp(ourdisplay);
260          while (XPending(ourdisplay) > 0)
261                  getevent();
262   }
# Line 214 | Line 266 | static
266   x11_comin(inp, prompt)          /* read in a command line */
267   char  *inp, *prompt;
268   {
269 <        int  x11_getc(), x11_comout();
269 >        extern int  x11_getc();
270  
271          if (prompt != NULL)
272                  if (fromcombuf(inp, &x11_driver))
# Line 231 | Line 283 | static
283   x11_comout(out)                 /* output a string to command line */
284   char  *out;
285   {
286 <        if (comline != NULL)
287 <                xt_puts(out, comline);
288 <        XFlush(ourdisplay);
286 >        if (comline == NULL)
287 >                return;
288 >        xt_puts(out, comline);
289 >        if (out[strlen(out)-1] == '\n')
290 >                XFlush(ourdisplay);
291   }
292  
293  
# Line 241 | Line 295 | static
295   x11_errout(msg)                 /* output an error message */
296   char  *msg;
297   {
244        x11_comout(msg);
298          stderr_v(msg);          /* send to stderr also! */
299 +        x11_comout(msg);
300   }
301  
302  
# Line 297 | Line 351 | int  r, g, b;
351   static int
352   getpixels()                             /* get the color map */
353   {
354 <        Visual  *ourvis = DefaultVisual(ourdisplay,ourscreen);
354 >        XColor  thiscolor;
355 >        register int  i, j;
356  
357          if (ncolors > 0)
358                  return(ncolors);
359 <        for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){
360 <                pixval = (int *)malloc(ncolors*sizeof(int));
359 >        if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
360 >                ourmap = DefaultColormap(ourdisplay,ourscreen);
361 >                goto loop;
362 >        }
363 > newmap:
364 >        ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
365 > loop:
366 >        for (ncolors = ourvinfo.colormap_size;
367 >                        ncolors > ourvinfo.colormap_size/3;
368 >                        ncolors = ncolors*.937) {
369 >                pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
370                  if (pixval == NULL)
371 <                        break;
371 >                        return(ncolors = 0);
372                  if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
373                                  pixval,ncolors) != 0)
374 <                        return(ncolors);
374 >                        break;
375                  free((char *)pixval);
376 +                pixval = NULL;
377          }
378 <        return(ncolors = 0);
378 >        if (pixval == NULL) {
379 >                if (ourmap == DefaultColormap(ourdisplay,ourscreen))
380 >                        goto newmap;            /* try it with our map */
381 >                else
382 >                        return(ncolors = 0);    /* failed */
383 >        }
384 >        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
385 >                for (i = 0; i < ncolors; i++) { /* reset black and white */
386 >                        if (pixval[i] != ourblack && pixval[i] != ourwhite)
387 >                                continue;
388 >                        thiscolor.pixel = pixval[i];
389 >                        thiscolor.flags = DoRed|DoGreen|DoBlue;
390 >                        XQueryColor(ourdisplay,
391 >                                        DefaultColormap(ourdisplay,ourscreen),
392 >                                        &thiscolor);
393 >                        XStoreColor(ourdisplay, ourmap, &thiscolor);
394 >                        for (j = i; j+1 < ncolors; j++)
395 >                                pixval[j] = pixval[j+1];
396 >                        ncolors--;
397 >                        i--;
398 >                }
399 >        XSetWindowColormap(ourdisplay, gwind, ourmap);
400 >        return(ncolors);
401   }
402  
403  
# Line 321 | Line 408 | freepixels()                           /* free our pixels */
408                  return;
409          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
410          ncolors = 0;
411 +        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
412 +                XFreeColormap(ourdisplay, ourmap);
413 +        ourmap = 0;
414   }
415  
416  
417 + static unsigned long
418 + true_pixel(col)                 /* return true pixel value for color */
419 + COLOR  col;
420 + {
421 +        unsigned long  rval;
422 +        BYTE  rgb[3];
423 +
424 +        map_color(rgb, col);
425 +        rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
426 +        rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
427 +        rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
428 +        return(rval);
429 + }
430 +
431 +
432   static int
433   x11_getc()                      /* get a command character */
434   {
# Line 348 | Line 453 | getevent()                     /* get next event */
453                  freepixels();
454                  break;
455          case MapNotify:
456 <                if (getpixels() == 0)
457 <                        stderr_v("Cannot allocate colors\n");
458 <                else
459 <                        new_ctab(ncolors);
456 >                if (ourvinfo.class == PseudoColor ||
457 >                                ourvinfo.class == GrayScale)
458 >                        if (getpixels() == 0)
459 >                                stderr_v("Cannot allocate colors\n");
460 >                        else
461 >                                new_ctab(ncolors);
462                  break;
463          case Expose:
464                  fixwindow(levptr(XExposeEvent));
# Line 369 | Line 476 | static
476   getkey(ekey)                            /* get input key */
477   register XKeyPressedEvent  *ekey;
478   {
479 <        c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
479 >        register int  n;
480 >
481 >        n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
482                                  NULL, NULL);
483 <        x11_driver.inpready = c_last-c_first;
483 >        c_last += n;
484 >        x11_driver.inpready += n;
485   }
486  
487  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines