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.3 by greg, Tue Jan 30 11:37:54 1990 UTC vs.
Revision 1.14 by greg, Mon Mar 12 11:08:48 1990 UTC

# Line 5 | Line 5 | static char SCCSid[] = "$SunId$ LBL";
5   /* Copyright (c) 1989 Regents of the University of California */
6  
7   /*
8 < *  x11.c - driver for X-windows version 10.4
8 > *  x11.c - driver for X-windows version 11.3
9   *
10 < *     1989
10 > *     Jan 1990
11   */
12  
13   #include  <stdio.h>
# Line 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   #include  <sys/ioctl.h>
16  
17   #include  <X11/Xlib.h>
18 < #include <X11/cursorfont.h>
19 < #include <X11/Xutil.h>
18 > #include  <X11/cursorfont.h>
19 > #include  <X11/Xutil.h>
20  
21   #include  "color.h"
22   #include  "driver.h"
# Line 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24  
25   #define GAMMA           2.2             /* exponent for color correction */
26  
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) */
32  
# Line 32 | Line 35 | static char SCCSid[] = "$SunId$ LBL";
35   #define COMCW           8               /* approx. character width (pixels) */
36   #define COMCH           14              /* approx. character height (pixels) */
37  
38 < #ifndef WFLUSH
39 < #define WFLUSH          30              /* flush after this many rays */
40 < #endif
38 > #define  ourscreen      DefaultScreen(ourdisplay)
39 > #define  ourroot        RootWindow(ourdisplay,ourscreen)
40 > #define  ourwhite       WhitePixel(ourdisplay,ourscreen)
41 > #define  ourblack       BlackPixel(ourdisplay,ourscreen)
42  
43 < #define  checkinp()     while (XPending(ourdisplay) > 0) getevent()
43 > #define  levptr(etype)  ((etype *)&currentevent)
44  
45 < #define  levptr(etype)  ((etype *)&thisevent)
45 > static XEvent  currentevent;            /* current event */
46  
43 static char  *clientname;               /* calling client's name */
44
45 static XEvent  thisevent;               /* current event */
46
47   static int  ncolors = 0;                /* color table size */
48   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 */
57  
58 < static int  gwidth = 0;                 /* graphics window width */
57 < static int  gheight = 0;                /* graphics window height */
58 > static int  gwidth, gheight;            /* graphics window size */
59  
60   static TEXTWIND  *comline = NULL;       /* our command line */
61  
# Line 64 | 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(),
73 <                x11_getcur(), x11_comout(), x11_comin();
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 = {
76          x11_close, x11_clear, x11_paintr, x11_getcur,
77 <        x11_comout, x11_comin, 1.0
77 >        x11_comout, x11_comin, x11_flush, 1.0
78   };
79  
80  
# Line 81 | Line 82 | struct driver *
82   x11_init(name, id)              /* initialize driver */
83   char  *name, *id;
84   {
85 <        Pixmap  bmCursorSrc, bmCursorMsk;
85 >        int  nplanes;
86 >        XVisualInfo  ourvinfo;
87 >        XSetWindowAttributes    ourwinattr;
88 >        XWMHints  ourxwmhints;
89 >        XSizeHints      oursizhints;
90  
91          ourdisplay = XOpenDisplay(NULL);
92          if (ourdisplay == NULL) {
93                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
94                  return(NULL);
95          }
96 <        if (DisplayPlanes(ourdisplay, DefaultScreen(ourdisplay)) < 4) {
96 >        nplanes = DisplayPlanes(ourdisplay, ourscreen);
97 >        if (nplanes < 4) {
98                  stderr_v("not enough colors\n");
99                  return(NULL);
100 +        } else if (nplanes <= 12) {
101 +                if (!XMatchVisualInfo(ourdisplay,ourscreen,
102 +                                nplanes,PseudoColor,&ourvinfo)) {
103 +                        stderr_v("PseudoColor not supported\n");
104 +                        return(NULL);
105 +                }
106 +        } else if (!XMatchVisualInfo(ourdisplay,ourscreen,
107 +                        nplanes,TrueColor,&ourvinfo)) {
108 +                stderr_v("TrueColor not supported\n");
109 +                return(NULL);
110          }
111 <        ourmap = DefaultColormap(ourdisplay,DefaultScreen(ourdisplay));
112 <        make_gmap(GAMMA);                       /* make color map */
113 <        /*
114 <        bmCursorSrc = XCreateBitmapFromData(ourdisplay,
115 <                        gwind, bcross_bits,
116 <                        bcross_width, bcross_height);
117 <        bmCursorMsk = XCreateBitmapFromData(ourdisplay,
118 <                        gwind, bcross_mask_bits,
119 <                        bcross_width, bcross_height);
120 <
121 <        pickcursor = XCreatePixmapCursor(ourdisplay,
122 <                        bmCursorSrc, bmCursorMsk,
123 <                        BlackPixel(ourdisplay,
124 <                        DefaultScreen(ourdisplay)),
125 <                        WhitePixel(ourdisplay,
110 <                        DefaultScreen(ourdisplay)),
111 <                        bcross_x_hot, bcross_y_hot);
112 <        XFreePixmap(ourdisplay, bmCursorSrc);
113 <        XFreePixmap(ourdisplay, bmCursorMsk);
114 <        */
111 >        ourvisual = ourvinfo.visual;
112 >        make_gmap(GAMMA);
113 >        /* open window */
114 >        ourwinattr.background_pixel = ourblack;
115 >        ourwinattr.border_pixel = ourblack;
116 >        gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
117 >                DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
118 >                DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
119 >                BORWIDTH, nplanes, InputOutput, ourvisual,
120 >                CWBackPixel|CWBorderPixel, &ourwinattr);
121 >        if (gwind == 0) {
122 >                stderr_v("cannot create window\n");
123 >                return(NULL);
124 >        }
125 >        XStoreName(ourdisplay, gwind, id);
126          /* create a cursor */
127 <        pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross);
128 <        /*  new */
129 <        clientname = id;
130 <        x11_driver.xsiz = DisplayWidth(ourdisplay,DefaultScreen(ourdisplay))
131 <                        - 2*BORWIDTH;
132 <        x11_driver.ysiz = DisplayHeight(ourdisplay,DefaultScreen(ourdisplay))
133 <                        - (COMHEIGHT + 2*BORWIDTH);
127 >        pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross);
128 >        ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
129 >        ourxwmhints.flags = InputHint;
130 >        ourxwmhints.input = True;
131 >        XSetWMHints(ourdisplay, gwind, &ourxwmhints);
132 >        oursizhints.min_width = MINWIDTH;
133 >        oursizhints.min_height = MINHEIGHT+COMHEIGHT;
134 >        oursizhints.flags = PMinSize;
135 >        XSetNormalHints(ourdisplay, gwind, &oursizhints);
136 >        XSelectInput(ourdisplay, gwind, ExposureMask);
137 >        XMapWindow(ourdisplay, gwind);
138 >        XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent));
139 >        gwidth = levptr(XExposeEvent)->width;
140 >        gheight = levptr(XExposeEvent)->height - COMHEIGHT;
141 >        x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
142 >        x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
143          x11_driver.inpready = 0;
144          cmdvec = x11_comout;                    /* set error vectors */
145          if (wrnvec != NULL)
# Line 140 | Line 160 | x11_close()                    /* close our display */
160                  xt_close(comline);
161                  comline = NULL;
162          }
143        if (gwind != 0) {
144                XFreeGC(ourdisplay, ourgc);
145                XDestroyWindow(ourdisplay, gwind);
146                gwind = 0;
147                gwidth = gheight = 0;
148                ourgc = 0;
149        }
150        XFreeCursor(ourdisplay, pickcursor);
163          freepixels();
164 +        XFreeGC(ourdisplay, ourgc);
165 +        XDestroyWindow(ourdisplay, gwind);
166 +        gwind = 0;
167 +        ourgc = 0;
168 +        XFreeCursor(ourdisplay, pickcursor);
169          XCloseDisplay(ourdisplay);
170          ourdisplay = NULL;
171   }
# Line 158 | Line 175 | static
175   x11_clear(xres, yres)                   /* clear our display */
176   int  xres, yres;
177   {
178 <        XWMHints  ourxwmhints;
179 <        XSetWindowAttributes ourwindowattr;
180 <
181 <        if (xres != gwidth || yres != gheight) {        /* change window */
182 <                if (comline != NULL)
183 <                        xt_close(comline);
184 <                if (gwind == 0) {                       /* new window */
185 <                        ourwindowattr.backing_store = Always;
186 <                        ourwindowattr.background_pixel =
170 <                        WhitePixel(ourdisplay, DefaultScreen(ourdisplay));
171 <                        ourwindowattr.border_pixel =
172 <                        BlackPixel(ourdisplay, DefaultScreen(ourdisplay));
173 <                        gwind = XCreateWindow(ourdisplay,
174 <                                RootWindow(ourdisplay,
175 <                                DefaultScreen(ourdisplay)),
176 <                                0, 0, xres, yres+COMHEIGHT, BORWIDTH,
177 <                                0, InputOutput, CopyFromParent,
178 <                                CWBackingStore|CWBackPixel|CWBorderPixel,
179 <                                &ourwindowattr);
180 <                        if (gwind == 0)
181 <                                goto fail;
182 <                        XStoreName(ourdisplay, gwind, clientname);
183 <                        ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
184 <                        ourxwmhints.flags = InputHint;
185 <                        ourxwmhints.input = True;
186 <                        XSetWMHints(ourdisplay, gwind, &ourxwmhints);
187 <                        XSelectInput(ourdisplay, gwind,
188 <                                     KeyPressMask|ButtonPressMask);
189 <                        XMapWindow(ourdisplay, gwind);
190 <                } else                                  /* resize window */
191 <                        XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
192 <                comline = xt_open(ourdisplay,
193 <                                DefaultGC(ourdisplay,DefaultScreen(ourdisplay)),
194 <                                gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
195 <                if (comline == NULL)
196 <                        goto fail;
178 >                                                /* check limits */
179 >        if (xres < MINWIDTH)
180 >                xres = MINWIDTH;
181 >        if (yres < MINHEIGHT)
182 >                yres = MINHEIGHT;
183 >                                                /* resize window */
184 >        if (xres != gwidth || yres != gheight) {
185 >                XSelectInput(ourdisplay, gwind, 0);
186 >                XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
187                  gwidth = xres;
188                  gheight = yres;
189 <        } else                                          /* just clear */
190 <                XClearWindow(ourdisplay, gwind);
189 >                XFlush(ourdisplay);
190 >                sleep(2);                       /* wait for window manager */
191 >                XSync(ourdisplay, 1);           /* discard input */
192 >        }
193 >        XClearWindow(ourdisplay, gwind);
194                                                  /* reinitialize color table */
195 <        if (ncolors == 0 && getpixels() == 0)
196 <                stderr_v("cannot allocate colors\n");
197 <        else
198 <                new_ctab(ncolors);
199 <        return;
200 < fail:
201 <        stderr_v("Failure opening window in x11_clear\n");
202 <        quit(1);
195 >        if (ourvisual->class == PseudoColor)
196 >                if (getpixels() == 0)
197 >                        stderr_v("cannot allocate colors\n");
198 >                else
199 >                        new_ctab(ncolors);
200 >                                                /* get new command line */
201 >        if (comline != NULL)
202 >                xt_close(comline);
203 >        comline = xt_open(ourdisplay,
204 >                        DefaultGC(ourdisplay,ourscreen),
205 >                        gwind, 0, gheight, gwidth, COMHEIGHT, 0, COMFN);
206 >        if (comline == NULL) {
207 >                stderr_v("Cannot open command line window\n");
208 >                quit(1);
209 >        }
210 >        XSelectInput(ourdisplay, comline->w, ExposureMask);
211 >                                                /* remove earmuffs */
212 >        XSelectInput(ourdisplay, gwind,
213 >                StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
214   }
215  
216  
# Line 215 | Line 219 | x11_paintr(col, xmin, ymin, xmax, ymax)                /* fill a rec
219   COLOR  col;
220   int  xmin, ymin, xmax, ymax;
221   {
218        extern long  nrays;             /* global ray count */
222          extern int  xnewcolr();         /* pixel assignment routine */
223 <        static long  lastflush = 0;     /* ray count at last flush */
223 >        extern unsigned long  true_pixel();
224 >        unsigned long  pixel;
225  
226 <        if (ncolors > 0) {
227 <                XSetForeground(ourdisplay, ourgc,
228 <                                pixval[get_pixel(col, xnewcolr)]);
229 <                XFillRectangle(ourdisplay, gwind,
230 <                        ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
231 <        }
232 <        if (nrays - lastflush >= WFLUSH) {
233 <                if (ncolors <= 0)       /* output necessary for death */
234 <                        XFillRectangle(ourdisplay, gwind,
231 <                                        ourgc, 0, 0, 1 ,1);
232 <                checkinp();
233 <                lastflush = nrays;
234 <        }
226 >        if (ncolors > 0)
227 >                pixel = pixval[get_pixel(col, xnewcolr)];
228 >        else if (ourvisual->class == TrueColor)
229 >                pixel = true_pixel(col);
230 >        else
231 >                return;
232 >        XSetForeground(ourdisplay, ourgc, pixel);
233 >        XFillRectangle(ourdisplay, gwind,
234 >                ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
235   }
236  
237  
238   static
239 + x11_flush()                     /* flush output */
240 + {
241 +        XNoOp(ourdisplay);
242 +        while (XPending(ourdisplay) > 0)
243 +                getevent();
244 + }
245 +
246 +
247 + static
248   x11_comin(inp, prompt)          /* read in a command line */
249   char  *inp, *prompt;
250   {
251 <        int  x11_getc(), x11_comout();
251 >        extern int  x11_getc();
252  
253          if (prompt != NULL)
254 <                xt_puts(prompt, comline);
254 >                if (fromcombuf(inp, &x11_driver))
255 >                        return;
256 >                else
257 >                        xt_puts(prompt, comline);
258          xt_cursor(comline, TBLKCURS);
259          editline(inp, x11_getc, x11_comout);
260          xt_cursor(comline, TNOCURS);
# Line 253 | Line 265 | static
265   x11_comout(out)                 /* output a string to command line */
266   char  *out;
267   {
268 <        if (comline != NULL)
269 <                xt_puts(out, comline);
270 <        XFlush(ourdisplay);
268 >        if (comline == NULL)
269 >                return;
270 >        xt_puts(out, comline);
271 >        if (out[strlen(out)-1] == '\n')
272 >                XFlush(ourdisplay);
273   }
274  
275  
# Line 263 | Line 277 | static
277   x11_errout(msg)                 /* output an error message */
278   char  *msg;
279   {
266        x11_comout(msg);
280          stderr_v(msg);          /* send to stderr also! */
281 +        x11_comout(msg);
282   }
283  
284  
# Line 319 | Line 333 | int  r, g, b;
333   static int
334   getpixels()                             /* get the color map */
335   {
336 <        Visual  *ourvis = DefaultVisual(ourdisplay,DefaultScreen(ourdisplay));
337 <
338 <        freepixels();
339 <        for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){
336 >        if (ncolors > 0)
337 >                return(ncolors);
338 >        if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) {
339 >                ourmap = DefaultColormap(ourdisplay,ourscreen);
340 >                goto loop;
341 >        }
342 > newmap:
343 >        ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone);
344 > loop:
345 >        for (ncolors = ourvisual->map_entries;
346 >                        ncolors > ourvisual->map_entries/3;
347 >                        ncolors = ncolors*.937) {
348                  pixval = (int *)malloc(ncolors*sizeof(int));
349                  if (pixval == NULL)
350 <                        break;
350 >                        return(ncolors = 0);
351                  if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
352                                  pixval,ncolors) != 0)
353 <                        return(ncolors);
353 >                        break;
354                  free((char *)pixval);
355 +                pixval = NULL;
356          }
357 <        return(ncolors = 0);
357 >        if (pixval == NULL) {
358 >                if (ourmap == DefaultColormap(ourdisplay,ourscreen))
359 >                        goto newmap;            /* try it with our map */
360 >                else
361 >                        return(ncolors = 0);    /* failed */
362 >        }
363 >        if (ourmap != DefaultColormap(ourdisplay,ourscreen)) {
364 >                XColor  thiscolor;
365 >                register int  i, j;
366 >                                                /* reset black and white */
367 >                for (i = 0; i < ncolors; i++) {
368 >                        if (pixval[i] != ourblack && pixval[i] != ourwhite)
369 >                                continue;
370 >                        thiscolor.pixel = pixval[i];
371 >                        thiscolor.flags = DoRed|DoGreen|DoBlue;
372 >                        XQueryColor(ourdisplay,
373 >                                        DefaultColormap(ourdisplay,ourscreen),
374 >                                        &thiscolor);
375 >                        XStoreColor(ourdisplay, ourmap, &thiscolor);
376 >                        for (j = i; j+1 < ncolors; j++)
377 >                                pixval[j] = pixval[j+1];
378 >                        ncolors--;
379 >                        i--;
380 >                }
381 >        }
382 >        XSetWindowColormap(ourdisplay, gwind, ourmap);
383 >        return(ncolors);
384   }
385  
386  
# Line 342 | Line 391 | freepixels()                           /* free our pixels */
391                  return;
392          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
393          ncolors = 0;
394 +        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
395 +                XFreeColormap(ourdisplay, ourmap);
396 +        ourmap = 0;
397   }
398  
399  
400 + static unsigned long
401 + true_pixel(col)                 /* return true pixel value for color */
402 + COLOR  col;
403 + {
404 +        unsigned long  rval;
405 +        BYTE  rgb[3];
406 +
407 +        map_color(rgb, col);
408 +        rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask;
409 +        rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask;
410 +        rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask;
411 +        return(rval);
412 + }
413 +
414 +
415   static int
416   x11_getc()                      /* get a command character */
417   {
# Line 362 | Line 429 | getevent()                     /* get next event */
429   {
430          XNextEvent(ourdisplay, levptr(XEvent));
431          switch (levptr(XEvent)->type) {
432 +        case ConfigureNotify:
433 +                resizewindow(levptr(XConfigureEvent));
434 +                break;
435 +        case UnmapNotify:
436 +                freepixels();
437 +                break;
438 +        case MapNotify:
439 +                if (ourvisual->class == PseudoColor)
440 +                        if (getpixels() == 0)
441 +                                stderr_v("Cannot allocate colors\n");
442 +                        else
443 +                                new_ctab(ncolors);
444 +                break;
445 +        case Expose:
446 +                fixwindow(levptr(XExposeEvent));
447 +                break;
448          case KeyPress:
449                  getkey(levptr(XKeyPressedEvent));
450                  break;
# Line 375 | Line 458 | static
458   getkey(ekey)                            /* get input key */
459   register XKeyPressedEvent  *ekey;
460   {
461 <        c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
461 >        register int  n;
462 >
463 >        n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
464                                  NULL, NULL);
465 <        x11_driver.inpready = c_last-c_first;
465 >        c_last += n;
466 >        x11_driver.inpready += n;
467   }
468  
469  
384 #ifdef notdef
470   static
471   fixwindow(eexp)                         /* repair damage to window */
472   register XExposeEvent  *eexp;
473   {
474 <        if (eexp->subwindow == 0)
475 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
474 >        if (eexp->window == gwind) {
475 >                sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n",
476 >                        eexp->x, gheight - eexp->y - eexp->height,
477                          eexp->x + eexp->width, gheight - eexp->y);
478 <        else if (eexp->subwindow == comline->w)
479 <                xt_redraw(comline);
478 >        } else if (eexp->window == comline->w) {
479 >                if (eexp->count == 0)
480 >                        xt_redraw(comline);
481 >        }
482   }
483 < #endif
483 >
484 >
485 > static
486 > resizewindow(ersz)                      /* resize window */
487 > register XConfigureEvent  *ersz;
488 > {
489 >        if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight)
490 >                return;
491 >
492 >        gwidth = ersz->width;
493 >        gheight = ersz->height-COMHEIGHT;
494 >        x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
495 >        x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
496 >
497 >        strcpy(getcombuf(&x11_driver), "new\n");
498 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines