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.11 by greg, Thu Mar 8 09:47:23 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();
71  
72   int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
73 <                x11_getcur(), x11_comout(), x11_comin();
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  
90          ourdisplay = XOpenDisplay(NULL);
91          if (ourdisplay == NULL) {
92                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
93                  return(NULL);
94          }
95 <        if (DisplayPlanes(ourdisplay, DefaultScreen(ourdisplay)) < 4) {
95 >        nplanes = DisplayPlanes(ourdisplay, ourscreen);
96 >        if (nplanes < 4) {
97                  stderr_v("not enough colors\n");
98                  return(NULL);
99 +        } else if (nplanes <= 12) {
100 +                if (!XMatchVisualInfo(ourdisplay,ourscreen,
101 +                                nplanes,PseudoColor,&ourvinfo)) {
102 +                        stderr_v("PseudoColor not supported\n");
103 +                        return(NULL);
104 +                }
105 +        } else if (!XMatchVisualInfo(ourdisplay,ourscreen,
106 +                        nplanes,TrueColor,&ourvinfo)) {
107 +                stderr_v("TrueColor not supported\n");
108 +                return(NULL);
109          }
110 <        ourmap = DefaultColormap(ourdisplay,DefaultScreen(ourdisplay));
111 <        make_gmap(GAMMA);                       /* make color map */
112 <        /*
113 <        bmCursorSrc = XCreateBitmapFromData(ourdisplay,
114 <                        gwind, bcross_bits,
115 <                        bcross_width, bcross_height);
116 <        bmCursorMsk = XCreateBitmapFromData(ourdisplay,
117 <                        gwind, bcross_mask_bits,
118 <                        bcross_width, bcross_height);
119 <
120 <        pickcursor = XCreatePixmapCursor(ourdisplay,
121 <                        bmCursorSrc, bmCursorMsk,
122 <                        BlackPixel(ourdisplay,
123 <                        DefaultScreen(ourdisplay)),
124 <                        WhitePixel(ourdisplay,
110 <                        DefaultScreen(ourdisplay)),
111 <                        bcross_x_hot, bcross_y_hot);
112 <        XFreePixmap(ourdisplay, bmCursorSrc);
113 <        XFreePixmap(ourdisplay, bmCursorMsk);
114 <        */
110 >        ourvisual = ourvinfo.visual;
111 >        make_gmap(GAMMA);
112 >        /* open window */
113 >        ourwinattr.background_pixel = ourwhite;
114 >        ourwinattr.border_pixel = ourblack;
115 >        gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
116 >                DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
117 >                DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
118 >                BORWIDTH, nplanes, InputOutput, ourvisual,
119 >                CWBackPixel|CWBorderPixel, &ourwinattr);
120 >        if (gwind == 0) {
121 >                stderr_v("cannot create window\n");
122 >                return(NULL);
123 >        }
124 >        XStoreName(ourdisplay, gwind, id);
125          /* create a cursor */
126 <        pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross);
127 <        /*  new */
128 <        clientname = id;
129 <        x11_driver.xsiz = DisplayWidth(ourdisplay,DefaultScreen(ourdisplay))
130 <                        - 2*BORWIDTH;
131 <        x11_driver.ysiz = DisplayHeight(ourdisplay,DefaultScreen(ourdisplay))
132 <                        - (COMHEIGHT + 2*BORWIDTH);
126 >        pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross);
127 >        ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
128 >        ourxwmhints.flags = InputHint;
129 >        ourxwmhints.input = True;
130 >        XSetWMHints(ourdisplay, gwind, &ourxwmhints);
131 >        XSelectInput(ourdisplay, gwind, ExposureMask);
132 >        XMapWindow(ourdisplay, gwind);
133 >        XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent));
134 >        gwidth = levptr(XExposeEvent)->width;
135 >        gheight = levptr(XExposeEvent)->height - COMHEIGHT;
136 >        x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
137 >        x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
138          x11_driver.inpready = 0;
139          cmdvec = x11_comout;                    /* set error vectors */
140          if (wrnvec != NULL)
# Line 140 | Line 155 | x11_close()                    /* close our display */
155                  xt_close(comline);
156                  comline = NULL;
157          }
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);
158          freepixels();
159 +        XFreeGC(ourdisplay, ourgc);
160 +        XDestroyWindow(ourdisplay, gwind);
161 +        gwind = 0;
162 +        ourgc = 0;
163 +        XFreeCursor(ourdisplay, pickcursor);
164          XCloseDisplay(ourdisplay);
165          ourdisplay = NULL;
166   }
# Line 158 | Line 170 | static
170   x11_clear(xres, yres)                   /* clear our display */
171   int  xres, yres;
172   {
161        XWMHints  ourxwmhints;
162        XSetWindowAttributes ourwindowattr;
163
173          if (xres != gwidth || yres != gheight) {        /* change window */
174                  if (comline != NULL)
175                          xt_close(comline);
176 <                if (gwind == 0) {                       /* new window */
177 <                        ourwindowattr.backing_store = Always;
169 <                        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);
176 >                XSelectInput(ourdisplay, gwind, 0);
177 >                XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
178                  comline = xt_open(ourdisplay,
179 <                                DefaultGC(ourdisplay,DefaultScreen(ourdisplay)),
179 >                                DefaultGC(ourdisplay,ourscreen),
180                                  gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
181 <                if (comline == NULL)
182 <                        goto fail;
181 >                if (comline == NULL) {
182 >                        stderr_v("Cannot open command line window\n");
183 >                        quit(1);
184 >                }
185 >                XSelectInput(ourdisplay, comline->w, ExposureMask);
186                  gwidth = xres;
187                  gheight = yres;
188 <        } else                                          /* just clear */
189 <                XClearWindow(ourdisplay, gwind);
190 <                                                /* reinitialize color table */
191 <        if (ncolors == 0 && getpixels() == 0)
192 <                stderr_v("cannot allocate colors\n");
193 <        else
194 <                new_ctab(ncolors);
195 <        return;
196 < fail:
197 <        stderr_v("Failure opening window in x11_clear\n");
198 <        quit(1);
188 >                XFlush(ourdisplay);
189 >                sleep(2);                       /* wait for window manager */
190 >                XSync(ourdisplay, 1);           /* discard input */
191 >        }
192 >        XClearWindow(ourdisplay, gwind);
193 >        if (ourvisual->class == PseudoColor)    /* reinitialize color table */
194 >                if (getpixels() == 0)
195 >                        stderr_v("cannot allocate colors\n");
196 >                else
197 >                        new_ctab(ncolors);
198 >
199 >        XSelectInput(ourdisplay, gwind,
200 >                StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
201   }
202  
203  
# Line 215 | Line 206 | x11_paintr(col, xmin, ymin, xmax, ymax)                /* fill a rec
206   COLOR  col;
207   int  xmin, ymin, xmax, ymax;
208   {
218        extern long  nrays;             /* global ray count */
209          extern int  xnewcolr();         /* pixel assignment routine */
210 <        static long  lastflush = 0;     /* ray count at last flush */
210 >        extern unsigned long  true_pixel();
211 >        unsigned long  pixel;
212  
213 <        if (ncolors > 0) {
214 <                XSetForeground(ourdisplay, ourgc,
215 <                                pixval[get_pixel(col, xnewcolr)]);
216 <                XFillRectangle(ourdisplay, gwind,
217 <                        ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
218 <        }
219 <        if (nrays - lastflush >= WFLUSH) {
220 <                if (ncolors <= 0)       /* output necessary for death */
221 <                        XFillRectangle(ourdisplay, gwind,
231 <                                        ourgc, 0, 0, 1 ,1);
232 <                checkinp();
233 <                lastflush = nrays;
234 <        }
213 >        if (ncolors > 0)
214 >                pixel = pixval[get_pixel(col, xnewcolr)];
215 >        else if (ourvisual->class == TrueColor)
216 >                pixel = true_pixel(col);
217 >        else
218 >                return;
219 >        XSetForeground(ourdisplay, ourgc, pixel);
220 >        XFillRectangle(ourdisplay, gwind,
221 >                ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
222   }
223  
224  
225   static
226 + x11_flush()                     /* flush output */
227 + {
228 +        XNoOp(ourdisplay);
229 +        while (XPending(ourdisplay) > 0)
230 +                getevent();
231 + }
232 +
233 +
234 + static
235   x11_comin(inp, prompt)          /* read in a command line */
236   char  *inp, *prompt;
237   {
238          int  x11_getc(), x11_comout();
239  
240          if (prompt != NULL)
241 <                xt_puts(prompt, comline);
241 >                if (fromcombuf(inp, &x11_driver))
242 >                        return;
243 >                else
244 >                        xt_puts(prompt, comline);
245          xt_cursor(comline, TBLKCURS);
246          editline(inp, x11_getc, x11_comout);
247          xt_cursor(comline, TNOCURS);
# Line 253 | Line 252 | static
252   x11_comout(out)                 /* output a string to command line */
253   char  *out;
254   {
255 <        if (comline != NULL)
256 <                xt_puts(out, comline);
257 <        XFlush(ourdisplay);
255 >        if (comline == NULL)
256 >                return;
257 >        xt_puts(out, comline);
258 >        if (out[strlen(out)-1] == '\n')
259 >                XFlush(ourdisplay);
260   }
261  
262  
# Line 263 | Line 264 | static
264   x11_errout(msg)                 /* output an error message */
265   char  *msg;
266   {
266        x11_comout(msg);
267          stderr_v(msg);          /* send to stderr also! */
268 +        x11_comout(msg);
269   }
270  
271  
# Line 319 | Line 320 | int  r, g, b;
320   static int
321   getpixels()                             /* get the color map */
322   {
323 <        Visual  *ourvis = DefaultVisual(ourdisplay,DefaultScreen(ourdisplay));
324 <
325 <        freepixels();
326 <        for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){
323 >        if (ncolors > 0)
324 >                return(ncolors);
325 >        if (ourvisual == DefaultVisual(ourdisplay,ourscreen)) {
326 >                ourmap = DefaultColormap(ourdisplay,ourscreen);
327 >                goto loop;
328 >        }
329 > newmap:
330 >        ourmap = XCreateColormap(ourdisplay,gwind,ourvisual,AllocNone);
331 > loop:
332 >        for (ncolors = ourvisual->map_entries;
333 >                        ncolors > ourvisual->map_entries/3;
334 >                        ncolors = ncolors*.937) {
335                  pixval = (int *)malloc(ncolors*sizeof(int));
336                  if (pixval == NULL)
337 <                        break;
337 >                        return(ncolors = 0);
338                  if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
339                                  pixval,ncolors) != 0)
340 <                        return(ncolors);
340 >                        break;
341                  free((char *)pixval);
342 +                pixval = NULL;
343          }
344 <        return(ncolors = 0);
344 >        if (pixval == NULL) {
345 >                if (ourmap == DefaultColormap(ourdisplay,ourscreen))
346 >                        goto newmap;            /* try it with our map */
347 >                else
348 >                        return(ncolors = 0);    /* failed */
349 >        }
350 >        if (ourmap != DefaultColormap(ourdisplay,ourscreen)) {
351 >                XColor  thiscolor;
352 >                register int  i, j;
353 >                                                /* reset black and white */
354 >                for (i = 0; i < ncolors; i++) {
355 >                        if (pixval[i] != ourblack && pixval[i] != ourwhite)
356 >                                continue;
357 >                        thiscolor.pixel = pixval[i];
358 >                        thiscolor.flags = DoRed|DoGreen|DoBlue;
359 >                        XQueryColor(ourdisplay,
360 >                                        DefaultColormap(ourdisplay,ourscreen),
361 >                                        &thiscolor);
362 >                        XStoreColor(ourdisplay, ourmap, &thiscolor);
363 >                        for (j = i; j+1 < ncolors; j++)
364 >                                pixval[j] = pixval[j+1];
365 >                        ncolors--;
366 >                        i--;
367 >                }
368 >        }
369 >        XSetWindowColormap(ourdisplay, gwind, ourmap);
370 >        return(ncolors);
371   }
372  
373  
# Line 342 | Line 378 | freepixels()                           /* free our pixels */
378                  return;
379          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
380          ncolors = 0;
381 +        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
382 +                XFreeColormap(ourdisplay, ourmap);
383 +        ourmap = 0;
384   }
385  
386  
387 + static unsigned long
388 + true_pixel(col)                 /* return true pixel value for color */
389 + COLOR  col;
390 + {
391 +        unsigned long  rval;
392 +        BYTE  rgb[3];
393 +
394 +        map_color(rgb, col);
395 +        rval = ourvisual->red_mask*rgb[RED]/255 & ourvisual->red_mask;
396 +        rval |= ourvisual->green_mask*rgb[GRN]/255 & ourvisual->green_mask;
397 +        rval |= ourvisual->blue_mask*rgb[BLU]/255 & ourvisual->blue_mask;
398 +        return(rval);
399 + }
400 +
401 +
402   static int
403   x11_getc()                      /* get a command character */
404   {
# Line 362 | Line 416 | getevent()                     /* get next event */
416   {
417          XNextEvent(ourdisplay, levptr(XEvent));
418          switch (levptr(XEvent)->type) {
419 +        case ConfigureNotify:
420 +                resizewindow(levptr(XConfigureEvent));
421 +                break;
422 +        case UnmapNotify:
423 +                freepixels();
424 +                break;
425 +        case MapNotify:
426 +                if (ourvisual->class == PseudoColor)
427 +                        if (getpixels() == 0)
428 +                                stderr_v("Cannot allocate colors\n");
429 +                        else
430 +                                new_ctab(ncolors);
431 +                break;
432 +        case Expose:
433 +                fixwindow(levptr(XExposeEvent));
434 +                break;
435          case KeyPress:
436                  getkey(levptr(XKeyPressedEvent));
437                  break;
# Line 375 | Line 445 | static
445   getkey(ekey)                            /* get input key */
446   register XKeyPressedEvent  *ekey;
447   {
448 <        c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
448 >        register int  n;
449 >
450 >        n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
451                                  NULL, NULL);
452 <        x11_driver.inpready = c_last-c_first;
452 >        c_last += n;
453 >        x11_driver.inpready += n;
454   }
455  
456  
384 #ifdef notdef
457   static
458   fixwindow(eexp)                         /* repair damage to window */
459   register XExposeEvent  *eexp;
460   {
461 <        if (eexp->subwindow == 0)
462 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
461 >        if (eexp->window == gwind) {
462 >                sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n",
463 >                        eexp->x, gheight - eexp->y - eexp->height,
464                          eexp->x + eexp->width, gheight - eexp->y);
465 <        else if (eexp->subwindow == comline->w)
466 <                xt_redraw(comline);
465 >        } else if (eexp->window == comline->w) {
466 >                if (eexp->count == 0)
467 >                        xt_redraw(comline);
468 >        }
469   }
470 < #endif
470 >
471 >
472 > static
473 > resizewindow(ersz)                      /* resize window */
474 > register XConfigureEvent  *ersz;
475 > {
476 >        if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight)
477 >                return;
478 >
479 >        gwidth = ersz->width;
480 >        gheight = ersz->height-COMHEIGHT;
481 >        x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
482 >        x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
483 >
484 >        strcpy(getcombuf(&x11_driver), "new\n");
485 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines