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.7 by greg, Fri Feb 23 14:50:38 1990 UTC vs.
Revision 2.7 by greg, Thu Jun 25 13:32:47 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        (32*COMCW)      /* minimum graphics window width */
29   #define MINHEIGHT       MINWIDTH        /* minimum graphics window height */
# 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 Visual  *ourvisual;              /* our visual structure */
52 > static XVisualInfo  ourvinfo;           /* our visual information */
53  
54   static Window  gwind = 0;               /* our graphics window */
55  
# Line 67 | Line 67 | static GC  ourgc = 0;                  /* our graphics context for dr
67  
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 82 | 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 <        XVisualInfo  ourvinfo;
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 +                                        /* find a usable visual */
98          nplanes = DisplayPlanes(ourdisplay, ourscreen);
99 <        if (nplanes < 4) {
100 <                stderr_v("not enough colors\n");
101 <                return(NULL);
102 <        } else if (nplanes <= 12) {
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 <                        stderr_v("PseudoColor not supported\n");
113 >                                        nplanes,PseudoColor,&ourvinfo) &&
114 >                                !XMatchVisualInfo(ourdisplay,ourscreen,
115 >                                        nplanes,GrayScale,&ourvinfo)) {
116 >                        stderr_v("unsupported visual type\n");
117                          return(NULL);
118                  }
119 <        } else if (!XMatchVisualInfo(ourdisplay,ourscreen,
120 <                        nplanes,TrueColor,&ourvinfo)) {
107 <                stderr_v("TrueColor not supported\n");
108 <                return(NULL);
119 >                ourblack = BlackPixel(ourdisplay,ourscreen);
120 >                ourwhite = WhitePixel(ourdisplay,ourscreen);
121          }
122 <        ourvisual = ourvinfo.visual;
123 <        make_gmap(GAMMA);
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 152 | Line 179 | x11_close()                    /* close our display */
179                  xt_close(comline);
180                  comline = NULL;
181          }
182 <        if (gwind != 0) {
183 <                freepixels();
184 <                XFreeGC(ourdisplay, ourgc);
185 <                XDestroyWindow(ourdisplay, gwind);
186 <                gwind = 0;
160 <                ourgc = 0;
161 <        }
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;
# Line 169 | 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);
177                comline = xt_open(ourdisplay,
178                                DefaultGC(ourdisplay,ourscreen),
179                                gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
180                if (comline == NULL) {
181                        stderr_v("Cannot open command line window\n");
182                        quit(1);
183                }
184                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 <        if (ourvisual->class == PseudoColor)    /* reinitialize color table */
213 >                                                /* reinitialize color table */
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 <
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 210 | Line 243 | int  xmin, ymin, xmax, ymax;
243  
244          if (ncolors > 0)
245                  pixel = pixval[get_pixel(col, xnewcolr)];
246 <        else if (ourvisual->class == TrueColor)
246 >        else if (ourvinfo.class == TrueColor || ourvinfo.class == DirectColor)
247                  pixel = true_pixel(col);
248          else
249                  return;
# Line 223 | Line 256 | int  xmin, ymin, xmax, ymax;
256   static
257   x11_flush()                     /* flush output */
258   {
259 <        if (ncolors <= 0)       /* output necessary for death */
227 <                XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1);
259 >        XNoOp(ourdisplay);
260          while (XPending(ourdisplay) > 0)
261                  getevent();
262   }
# Line 234 | 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 251 | 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 261 | Line 295 | static
295   x11_errout(msg)                 /* output an error message */
296   char  *msg;
297   {
264        x11_comout(msg);
298          stderr_v(msg);          /* send to stderr also! */
299 +        x11_comout(msg);
300   }
301  
302  
# Line 317 | Line 351 | int  r, g, b;
351   static int
352   getpixels()                             /* get the color map */
353   {
354 +        XColor  thiscolor;
355 +        register int  i, j;
356 +
357          if (ncolors > 0)
358                  return(ncolors);
359 <        if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) {
359 >        if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
360                  ourmap = DefaultColormap(ourdisplay,ourscreen);
361                  goto loop;
362          }
363   newmap:
364 <        ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone);
364 >        ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
365   loop:
366 <        for (ncolors = ourvisual->map_entries;
367 <                        ncolors > ourvisual->map_entries/3;
366 >        for (ncolors = ourvinfo.colormap_size;
367 >                        ncolors > ourvinfo.colormap_size/3;
368                          ncolors = ncolors*.937) {
369 <                pixval = (int *)malloc(ncolors*sizeof(int));
369 >                pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
370                  if (pixval == NULL)
371                          return(ncolors = 0);
372                  if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
# Line 344 | Line 381 | loop:
381                  else
382                          return(ncolors = 0);    /* failed */
383          }
384 <        if (ourmap != DefaultColormap(ourdisplay,ourscreen)) {
385 <                XColor  thiscolor;
349 <                register int  i, j;
350 <                                                /* reset black and white */
351 <                for (i = 0; i < ncolors; i++) {
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];
# Line 362 | Line 396 | loop:
396                          ncolors--;
397                          i--;
398                  }
365        }
399          XSetWindowColormap(ourdisplay, gwind, ourmap);
400          return(ncolors);
401   }
# Line 374 | Line 407 | freepixels()                           /* free our pixels */
407          if (ncolors == 0)
408                  return;
409          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
410 +        free((char *)pixval);
411 +        pixval = NULL;
412          ncolors = 0;
413 <        if (ourmap != 0 && ourmap != DefaultColormap(ourdisplay,ourscreen))
413 >        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
414                  XFreeColormap(ourdisplay, ourmap);
415          ourmap = 0;
416   }
# Line 389 | Line 424 | COLOR  col;
424          BYTE  rgb[3];
425  
426          map_color(rgb, col);
427 <        rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask;
428 <        rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask;
429 <        rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask;
427 >        rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
428 >        rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
429 >        rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
430          return(rval);
431   }
432  
# Line 420 | Line 455 | getevent()                     /* get next event */
455                  freepixels();
456                  break;
457          case MapNotify:
458 <                if (ourvisual->class == PseudoColor)
458 >                if (ourvinfo.class == PseudoColor ||
459 >                                ourvinfo.class == GrayScale)
460                          if (getpixels() == 0)
461                                  stderr_v("Cannot allocate colors\n");
462                          else
# Line 442 | Line 478 | static
478   getkey(ekey)                            /* get input key */
479   register XKeyPressedEvent  *ekey;
480   {
481 <        int  n;
481 >        register int  n;
482  
483          n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
484                                  NULL, NULL);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines