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 1.9 by greg, Fri Mar 2 10:08:01 1990 UTC

# Line 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24  
25   #define GAMMA           2.2             /* exponent for color correction */
26  
27 < #define MINWIDTH        (4*COMCW)       /* minimum graphics window width */
28 < #define MINHEIGHT       16              /* minimum graphics window height */
27 > #define MINWIDTH        (32*COMCW)      /* minimum graphics window width */
28 > #define MINHEIGHT       MINWIDTH        /* minimum graphics window height */
29  
30   #define BORWIDTH        5               /* border width */
31   #define COMHEIGHT       (COMLH*COMCH)   /* command line height (pixels) */
# Line 49 | Line 49 | static int  *pixval = NULL;            /* allocated pixels */
49  
50   static Display  *ourdisplay = NULL;     /* our display */
51  
52 + static Visual  *ourvisual;              /* our visual structure */
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();
71  
# Line 80 | Line 82 | struct driver *
82   x11_init(name, id)              /* initialize driver */
83   char  *name, *id;
84   {
85 +        int  nplanes;
86 +        XVisualInfo  ourvinfo;
87          XWMHints  ourxwmhints;
84        Pixmap  bmCursorSrc, bmCursorMsk;
88  
89          ourdisplay = XOpenDisplay(NULL);
90          if (ourdisplay == NULL) {
91                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
92                  return(NULL);
93          }
94 <        if (DisplayPlanes(ourdisplay, ourscreen) < 4) {
94 >        nplanes = DisplayPlanes(ourdisplay, ourscreen);
95 >        if (nplanes < 4) {
96                  stderr_v("not enough colors\n");
97                  return(NULL);
98 +        } else if (nplanes <= 12) {
99 +                if (!XMatchVisualInfo(ourdisplay,ourscreen,
100 +                                nplanes,PseudoColor,&ourvinfo)) {
101 +                        stderr_v("PseudoColor not supported\n");
102 +                        return(NULL);
103 +                }
104 +        } else if (!XMatchVisualInfo(ourdisplay,ourscreen,
105 +                        nplanes,TrueColor,&ourvinfo)) {
106 +                stderr_v("TrueColor not supported\n");
107 +                return(NULL);
108          }
109 <        ourmap = DefaultColormap(ourdisplay,ourscreen);
110 <        make_gmap(GAMMA);                       /* make color map */
109 >        ourvisual = ourvinfo.visual;
110 >        make_gmap(GAMMA);
111          /* create a cursor */
112          pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross);
113          /* open window */
# Line 137 | Line 151 | x11_close()                    /* close our display */
151                  xt_close(comline);
152                  comline = NULL;
153          }
140        if (gwind != 0) {
141                XFreeGC(ourdisplay, ourgc);
142                XDestroyWindow(ourdisplay, gwind);
143                gwind = 0;
144                ourgc = 0;
145        }
146        XFreeCursor(ourdisplay, pickcursor);
154          freepixels();
155 +        XFreeGC(ourdisplay, ourgc);
156 +        XDestroyWindow(ourdisplay, gwind);
157 +        gwind = 0;
158 +        ourgc = 0;
159 +        XFreeCursor(ourdisplay, pickcursor);
160          XCloseDisplay(ourdisplay);
161          ourdisplay = NULL;
162   }
# Line 169 | Line 181 | int  xres, yres;
181                  XSelectInput(ourdisplay, comline->w, ExposureMask);
182                  gwidth = xres;
183                  gheight = yres;
184 <                XSync(ourdisplay, 1);           /* discard input */
184 >                XFlush(ourdisplay);
185                  sleep(2);                       /* wait for window manager */
186 +                XSync(ourdisplay, 1);           /* discard input */
187          }
188          XClearWindow(ourdisplay, gwind);
189 <                                                /* reinitialize color table */
190 <        if (getpixels() == 0)
191 <                stderr_v("cannot allocate colors\n");
192 <        else
193 <                new_ctab(ncolors);
189 >        if (ourvisual->class == PseudoColor)    /* reinitialize color table */
190 >                if (getpixels() == 0)
191 >                        stderr_v("cannot allocate colors\n");
192 >                else
193 >                        new_ctab(ncolors);
194  
195          XSelectInput(ourdisplay, gwind,
196                  StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
# Line 190 | Line 203 | COLOR  col;
203   int  xmin, ymin, xmax, ymax;
204   {
205          extern int  xnewcolr();         /* pixel assignment routine */
206 +        extern unsigned long  true_pixel();
207 +        unsigned long  pixel;
208  
209 <        if (ncolors > 0) {
210 <                XSetForeground(ourdisplay, ourgc,
211 <                                pixval[get_pixel(col, xnewcolr)]);
212 <                XFillRectangle(ourdisplay, gwind,
213 <                        ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
214 <        }
209 >        if (ncolors > 0)
210 >                pixel = pixval[get_pixel(col, xnewcolr)];
211 >        else if (ourvisual->class == TrueColor)
212 >                pixel = true_pixel(col);
213 >        else
214 >                return;
215 >        XSetForeground(ourdisplay, ourgc, pixel);
216 >        XFillRectangle(ourdisplay, gwind,
217 >                ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
218   }
219  
220  
221   static
222   x11_flush()                     /* flush output */
223   {
224 <        if (ncolors <= 0)       /* output necessary for death */
224 >        if (ncolors <= 0 && ourvisual->class != TrueColor)      /* dummy */
225                  XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1);
226          while (XPending(ourdisplay) > 0)
227                  getevent();
# Line 233 | Line 251 | char  *out;
251   {
252          if (comline != NULL)
253                  xt_puts(out, comline);
236        XFlush(ourdisplay);
254   }
255  
256  
# Line 241 | Line 258 | static
258   x11_errout(msg)                 /* output an error message */
259   char  *msg;
260   {
244        x11_comout(msg);
261          stderr_v(msg);          /* send to stderr also! */
262 +        x11_comout(msg);
263 +        x11_flush();
264   }
265  
266  
# Line 297 | Line 315 | int  r, g, b;
315   static int
316   getpixels()                             /* get the color map */
317   {
300        Visual  *ourvis = DefaultVisual(ourdisplay,ourscreen);
301
318          if (ncolors > 0)
319                  return(ncolors);
320 <        for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){
320 >        if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) {
321 >                ourmap = DefaultColormap(ourdisplay,ourscreen);
322 >                goto loop;
323 >        }
324 > newmap:
325 >        ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone);
326 > loop:
327 >        for (ncolors = ourvisual->map_entries;
328 >                        ncolors > ourvisual->map_entries/3;
329 >                        ncolors = ncolors*.937) {
330                  pixval = (int *)malloc(ncolors*sizeof(int));
331                  if (pixval == NULL)
332 <                        break;
332 >                        return(ncolors = 0);
333                  if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
334                                  pixval,ncolors) != 0)
335 <                        return(ncolors);
335 >                        break;
336                  free((char *)pixval);
337 +                pixval = NULL;
338          }
339 <        return(ncolors = 0);
339 >        if (pixval == NULL) {
340 >                if (ourmap == DefaultColormap(ourdisplay,ourscreen))
341 >                        goto newmap;            /* try it with our map */
342 >                else
343 >                        return(ncolors = 0);    /* failed */
344 >        }
345 >        if (ourmap != DefaultColormap(ourdisplay,ourscreen)) {
346 >                XColor  thiscolor;
347 >                register int  i, j;
348 >                                                /* reset black and white */
349 >                for (i = 0; i < ncolors; i++) {
350 >                        if (pixval[i] != ourblack && pixval[i] != ourwhite)
351 >                                continue;
352 >                        thiscolor.pixel = pixval[i];
353 >                        thiscolor.flags = DoRed|DoGreen|DoBlue;
354 >                        XQueryColor(ourdisplay,
355 >                                        DefaultColormap(ourdisplay,ourscreen),
356 >                                        &thiscolor);
357 >                        XStoreColor(ourdisplay, ourmap, &thiscolor);
358 >                        for (j = i; j+1 < ncolors; j++)
359 >                                pixval[j] = pixval[j+1];
360 >                        ncolors--;
361 >                        i--;
362 >                }
363 >        }
364 >        XSetWindowColormap(ourdisplay, gwind, ourmap);
365 >        return(ncolors);
366   }
367  
368  
# Line 321 | Line 373 | freepixels()                           /* free our pixels */
373                  return;
374          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
375          ncolors = 0;
376 +        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
377 +                XFreeColormap(ourdisplay, ourmap);
378 +        ourmap = 0;
379   }
380  
381  
382 + static unsigned long
383 + true_pixel(col)                 /* return true pixel value for color */
384 + COLOR  col;
385 + {
386 +        unsigned long  rval;
387 +        BYTE  rgb[3];
388 +
389 +        map_color(rgb, col);
390 +        rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask;
391 +        rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask;
392 +        rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask;
393 +        return(rval);
394 + }
395 +
396 +
397   static int
398   x11_getc()                      /* get a command character */
399   {
# Line 348 | Line 418 | getevent()                     /* get next event */
418                  freepixels();
419                  break;
420          case MapNotify:
421 <                if (getpixels() == 0)
422 <                        stderr_v("Cannot allocate colors\n");
423 <                else
424 <                        new_ctab(ncolors);
421 >                if (ourvisual->class == PseudoColor)
422 >                        if (getpixels() == 0)
423 >                                stderr_v("Cannot allocate colors\n");
424 >                        else
425 >                                new_ctab(ncolors);
426                  break;
427          case Expose:
428                  fixwindow(levptr(XExposeEvent));
# Line 369 | Line 440 | static
440   getkey(ekey)                            /* get input key */
441   register XKeyPressedEvent  *ekey;
442   {
443 <        c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
443 >        register int  n;
444 >
445 >        n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
446                                  NULL, NULL);
447 <        x11_driver.inpready = c_last-c_first;
447 >        c_last += n;
448 >        x11_driver.inpready += n;
449   }
450  
451  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines