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 2.19 by greg, Mon Oct 3 11:01:23 1994 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 10.4
8 > *  x11.c - driver for X-windows version 11
9   *
10 < *     1989
10 > *     Jan 1990
11   */
12  
13   #include  <stdio.h>
14 <
14 > #include  <math.h>
15   #include  <sys/ioctl.h>
16 + #include  <fcntl.h>
17  
18   #include  <X11/Xlib.h>
19 < #include <X11/cursorfont.h>
20 < #include <X11/Xutil.h>
19 > #include  <X11/cursorfont.h>
20 > #include  <X11/Xutil.h>
21  
22   #include  "color.h"
23   #include  "driver.h"
24   #include  "x11twind.h"
25 + #include  "x11icon.h"
26  
27 < #define GAMMA           2.2             /* exponent for color correction */
27 > #define GAMMA           2.2             /* default exponent correction */
28  
29 + #define MINWIDTH        (32*COMCW)      /* minimum graphics window width */
30 + #define MINHEIGHT       (MINWIDTH/2)    /* minimum graphics window height */
31 +
32   #define BORWIDTH        5               /* border width */
33   #define COMHEIGHT       (COMLH*COMCH)   /* command line height (pixels) */
34  
# Line 32 | Line 37 | static char SCCSid[] = "$SunId$ LBL";
37   #define COMCW           8               /* approx. character width (pixels) */
38   #define COMCH           14              /* approx. character height (pixels) */
39  
40 < #ifndef WFLUSH
41 < #define WFLUSH          30              /* flush after this many rays */
37 < #endif
40 > #define  ourscreen      DefaultScreen(ourdisplay)
41 > #define  ourroot        RootWindow(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 */
48 > static int  mapped = 0;                 /* window is mapped? */
49 > static unsigned long  *pixval = NULL;   /* allocated pixels */
50 > static unsigned long  ourblack=0, ourwhite=1;
51  
52   static Display  *ourdisplay = NULL;     /* our display */
53  
54 + static XVisualInfo  ourvinfo;           /* our visual information */
55 +
56   static Window  gwind = 0;               /* our graphics window */
57  
58   static Cursor  pickcursor = 0;          /* cursor used for picking */
59  
60 < static int  gwidth = 0;                 /* graphics window width */
57 < static int  gheight = 0;                /* graphics window height */
60 > static int  gwidth, gheight;            /* graphics window size */
61  
62 + static int  comheight;                  /* desired comline height */
63   static TEXTWIND  *comline = NULL;       /* our command line */
64  
65   static char  c_queue[64];               /* input queue */
# Line 64 | Line 68 | static int  c_last = 0;                        /* last character in queue *
68  
69   static GC  ourgc = 0;                   /* our graphics context for drawing */
70  
71 < static Colormap ourmap;                 /* our color map */
71 > static Colormap ourmap = 0;             /* our color map */
72  
73 < extern char  *malloc();
73 > #define IC_X11          0
74 > #define IC_IOCTL        1
75 > #define IC_READ         2
76  
77 < int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
72 <                x11_getcur(), x11_comout(), x11_comin();
77 > static int  inpcheck;                   /* whence to check input */
78  
79 + extern char  *malloc(), *getcombuf();
80 +
81 + static int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
82 +                x11_getcur(), x11_comout(), x11_comin(), x11_flush();
83 +
84 + static int  std_comin(), std_comout();
85 +
86   static struct driver  x11_driver = {
87          x11_close, x11_clear, x11_paintr, x11_getcur,
88 <        x11_comout, x11_comin, 1.0
88 >        NULL, NULL, x11_flush, 1.0
89   };
90  
91 + static int  getpixels(), xnewcolr(), freepixels(), resizewindow(),
92 +                getevent(), getkey(), fixwindow(), x11_getc();
93 + static unsigned long  true_pixel();
94  
95 +
96   struct driver *
97   x11_init(name, id)              /* initialize driver */
98   char  *name, *id;
99   {
100 <        Pixmap  bmCursorSrc, bmCursorMsk;
101 <
100 >        extern char  *getenv();
101 >        char  *gv;
102 >        int  nplanes;
103 >        XSetWindowAttributes    ourwinattr;
104 >        XWMHints  ourxwmhints;
105 >        XSizeHints      oursizhints;
106 >                                        /* open display server */
107          ourdisplay = XOpenDisplay(NULL);
108          if (ourdisplay == NULL) {
109                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
110                  return(NULL);
111          }
112 <        if (DisplayPlanes(ourdisplay, DefaultScreen(ourdisplay)) < 4) {
113 <                stderr_v("not enough colors\n");
112 >                                        /* find a usable visual */
113 >        nplanes = DisplayPlanes(ourdisplay, ourscreen);
114 >        if (XMatchVisualInfo(ourdisplay,ourscreen,
115 >                                24,TrueColor,&ourvinfo) ||
116 >                        XMatchVisualInfo(ourdisplay,ourscreen,
117 >                                24,DirectColor,&ourvinfo)) {
118 >                ourblack = 0;
119 >                ourwhite = ourvinfo.red_mask |
120 >                                ourvinfo.green_mask |
121 >                                ourvinfo.blue_mask ;
122 >        } else {
123 >                if (nplanes < 4) {
124 >                        stderr_v("not enough colors\n");
125 >                        return(NULL);
126 >                }
127 >                if (!XMatchVisualInfo(ourdisplay,ourscreen,
128 >                                        nplanes,PseudoColor,&ourvinfo) &&
129 >                                !XMatchVisualInfo(ourdisplay,ourscreen,
130 >                                        nplanes,GrayScale,&ourvinfo)) {
131 >                        stderr_v("unsupported visual type\n");
132 >                        return(NULL);
133 >                }
134 >                ourblack = BlackPixel(ourdisplay,ourscreen);
135 >                ourwhite = WhitePixel(ourdisplay,ourscreen);
136 >        }
137 >                                        /* set gamma */
138 >        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
139 >                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
140 >                make_gmap(atof(gv));
141 >        else
142 >                make_gmap(GAMMA);
143 >                                        /* X11 command line or no? */
144 >        if (!strcmp(name, "x11"))
145 >                comheight = COMHEIGHT;
146 >        else /* "x11d" */
147 >                comheight = 0;
148 >                                        /* open window */
149 >        ourwinattr.background_pixel = ourblack;
150 >        ourwinattr.border_pixel = ourblack;
151 >                                        /* this is stupid */
152 >        ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
153 >                                ourvinfo.visual, AllocNone);
154 >        gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
155 >                DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
156 >                DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
157 >                BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
158 >                CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
159 >        if (gwind == 0) {
160 >                stderr_v("cannot create window\n");
161                  return(NULL);
162          }
163 <        ourmap = DefaultColormap(ourdisplay,DefaultScreen(ourdisplay));
96 <        make_gmap(GAMMA);                       /* make color map */
97 <        /*
98 <        bmCursorSrc = XCreateBitmapFromData(ourdisplay,
99 <                        gwind, bcross_bits,
100 <                        bcross_width, bcross_height);
101 <        bmCursorMsk = XCreateBitmapFromData(ourdisplay,
102 <                        gwind, bcross_mask_bits,
103 <                        bcross_width, bcross_height);
104 <
105 <        pickcursor = XCreatePixmapCursor(ourdisplay,
106 <                        bmCursorSrc, bmCursorMsk,
107 <                        BlackPixel(ourdisplay,
108 <                        DefaultScreen(ourdisplay)),
109 <                        WhitePixel(ourdisplay,
110 <                        DefaultScreen(ourdisplay)),
111 <                        bcross_x_hot, bcross_y_hot);
112 <        XFreePixmap(ourdisplay, bmCursorSrc);
113 <        XFreePixmap(ourdisplay, bmCursorMsk);
114 <        */
163 >        XStoreName(ourdisplay, gwind, id);
164          /* create a cursor */
165 <        pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross);
166 <        /*  new */
167 <        clientname = id;
168 <        x11_driver.xsiz = DisplayWidth(ourdisplay,DefaultScreen(ourdisplay))
169 <                        - 2*BORWIDTH;
170 <        x11_driver.ysiz = DisplayHeight(ourdisplay,DefaultScreen(ourdisplay))
171 <                        - (COMHEIGHT + 2*BORWIDTH);
165 >        pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross);
166 >        ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
167 >        ourxwmhints.flags = InputHint|IconPixmapHint;
168 >        ourxwmhints.input = True;
169 >        ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
170 >                        gwind, x11icon_bits, x11icon_width, x11icon_height);
171 >        XSetWMHints(ourdisplay, gwind, &ourxwmhints);
172 >        oursizhints.min_width = MINWIDTH;
173 >        oursizhints.min_height = MINHEIGHT+comheight;
174 >        oursizhints.flags = PMinSize;
175 >        XSetNormalHints(ourdisplay, gwind, &oursizhints);
176 >        XSelectInput(ourdisplay, gwind, ExposureMask);
177 >        XMapWindow(ourdisplay, gwind);
178 >        XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent));
179 >        gwidth = levptr(XExposeEvent)->width;
180 >        gheight = levptr(XExposeEvent)->height - comheight;
181 >        x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
182 >        x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
183          x11_driver.inpready = 0;
184 <        cmdvec = x11_comout;                    /* set error vectors */
185 <        if (wrnvec != NULL)
186 <                wrnvec = x11_errout;
184 >        mapped = 1;
185 >                                        /* set i/o vectors */
186 >        if (comheight) {
187 >                x11_driver.comin = x11_comin;
188 >                x11_driver.comout = x11_comout;
189 >                cmdvec = x11_comout;
190 >                if (wrnvec != NULL)
191 >                        wrnvec = x11_errout;
192 >                inpcheck = IC_X11;
193 >        } else {
194 >                x11_driver.comin = std_comin;
195 >                x11_driver.comout = std_comout;
196 >                cmdvec = std_comout;
197 >                inpcheck = IC_IOCTL;
198 >        }
199          return(&x11_driver);
200   }
201  
# Line 140 | Line 212 | x11_close()                    /* close our display */
212                  xt_close(comline);
213                  comline = NULL;
214          }
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);
215          freepixels();
216 +        XFreeGC(ourdisplay, ourgc);
217 +        XDestroyWindow(ourdisplay, gwind);
218 +        gwind = 0;
219 +        ourgc = 0;
220 +        XFreeCursor(ourdisplay, pickcursor);
221          XCloseDisplay(ourdisplay);
222          ourdisplay = NULL;
223   }
# Line 158 | Line 227 | static
227   x11_clear(xres, yres)                   /* clear our display */
228   int  xres, yres;
229   {
230 <        XWMHints  ourxwmhints;
231 <        XSetWindowAttributes ourwindowattr;
232 <
233 <        if (xres != gwidth || yres != gheight) {        /* change window */
234 <                if (comline != NULL)
235 <                        xt_close(comline);
236 <                if (gwind == 0) {                       /* new window */
237 <                        ourwindowattr.backing_store = Always;
238 <                        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;
230 >                                                /* check limits */
231 >        if (xres < MINWIDTH)
232 >                xres = MINWIDTH;
233 >        if (yres < MINHEIGHT)
234 >                yres = MINHEIGHT;
235 >                                                /* resize window */
236 >        if (xres != gwidth || yres != gheight) {
237 >                XSelectInput(ourdisplay, gwind, 0);
238 >                XResizeWindow(ourdisplay, gwind, xres, yres+comheight);
239                  gwidth = xres;
240                  gheight = yres;
241 <        } else                                          /* just clear */
242 <                XClearWindow(ourdisplay, gwind);
241 >                XFlush(ourdisplay);
242 >                sleep(2);                       /* wait for window manager */
243 >                XSync(ourdisplay, 1);           /* discard input */
244 >        }
245 >        XClearWindow(ourdisplay, gwind);
246                                                  /* reinitialize color table */
247 <        if (ncolors == 0 && getpixels() == 0)
248 <                stderr_v("cannot allocate colors\n");
249 <        else
250 <                new_ctab(ncolors);
251 <        return;
252 < fail:
253 <        stderr_v("Failure opening window in x11_clear\n");
254 <        quit(1);
247 >        if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale)
248 >                if (getpixels() == 0)
249 >                        stderr_v("cannot allocate colors\n");
250 >                else
251 >                        new_ctab(ncolors);
252 >                                                /* get new command line */
253 >        if (comline != NULL)
254 >                xt_close(comline);
255 >        if (comheight) {
256 >                comline = xt_open(ourdisplay, gwind, 0, gheight, gwidth,
257 >                                comheight, 0, ourblack, ourwhite, COMFN);
258 >                if (comline == NULL) {
259 >                        stderr_v("Cannot open command line window\n");
260 >                        quit(1);
261 >                }
262 >                XSelectInput(ourdisplay, comline->w, ExposureMask);
263 >                                                /* remove earmuffs */
264 >                XSelectInput(ourdisplay, gwind,
265 >                StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
266 >        } else                                  /* remove earmuffs */
267 >                XSelectInput(ourdisplay, gwind,
268 >                        StructureNotifyMask|ExposureMask|ButtonPressMask);
269   }
270  
271  
# Line 215 | Line 274 | x11_paintr(col, xmin, ymin, xmax, ymax)                /* fill a rec
274   COLOR  col;
275   int  xmin, ymin, xmax, ymax;
276   {
277 <        extern long  nrays;             /* global ray count */
219 <        extern int  xnewcolr();         /* pixel assignment routine */
220 <        static long  lastflush = 0;     /* ray count at last flush */
277 >        unsigned long  pixel;
278  
279 <        if (ncolors > 0) {
280 <                XSetForeground(ourdisplay, ourgc,
281 <                                pixval[get_pixel(col, xnewcolr)]);
282 <                XFillRectangle(ourdisplay, gwind,
283 <                        ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
279 >        if (!mapped)
280 >                return;
281 >        if (ncolors > 0)
282 >                pixel = pixval[get_pixel(col, xnewcolr)];
283 >        else
284 >                pixel = true_pixel(col);
285 >        XSetForeground(ourdisplay, ourgc, pixel);
286 >        XFillRectangle(ourdisplay, gwind,
287 >                ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
288 > }
289 >
290 >
291 > static
292 > x11_flush()                     /* flush output */
293 > {
294 >        char    buf[256];
295 >        int     n;
296 >                                                /* check for input */
297 >        XNoOp(ourdisplay);
298 >        n = XPending(ourdisplay);                       /* from X server */
299 >        while (n-- > 0)
300 >                getevent();
301 >        if (inpcheck == IC_IOCTL) {                     /* from stdin */
302 >                if (ioctl(fileno(stdin), FIONREAD, &n) < 0) {
303 >                        if (fcntl(fileno(stdin), F_SETFL, FNDELAY) < 0) {
304 >                                stderr_v("Cannot change input mode\n");
305 >                                quit(1);
306 >                        }
307 >                        inpcheck = IC_READ;
308 >                } else
309 >                        x11_driver.inpready += n;
310          }
311 <        if (nrays - lastflush >= WFLUSH) {
312 <                if (ncolors <= 0)       /* output necessary for death */
313 <                        XFillRectangle(ourdisplay, gwind,
314 <                                        ourgc, 0, 0, 1 ,1);
315 <                checkinp();
316 <                lastflush = nrays;
311 >        if (inpcheck == IC_READ) {
312 >                n = read(fileno(stdin), buf, sizeof(buf)-1);
313 >                if (n > 0) {
314 >                        buf[n] = '\0';
315 >                        strcpy(getcombuf(&x11_driver), buf);
316 >                }
317          }
318   }
319  
# Line 239 | Line 322 | static
322   x11_comin(inp, prompt)          /* read in a command line */
323   char  *inp, *prompt;
324   {
325 <        int  x11_getc(), x11_comout();
326 <
327 <        if (prompt != NULL)
325 >        if (prompt != NULL) {
326 >                x11_flush();            /* make sure we get everything */
327 >                if (fromcombuf(inp, &x11_driver))
328 >                        return;
329                  xt_puts(prompt, comline);
330 +        }
331          xt_cursor(comline, TBLKCURS);
332          editline(inp, x11_getc, x11_comout);
333          xt_cursor(comline, TNOCURS);
# Line 250 | Line 335 | char  *inp, *prompt;
335  
336  
337   static
338 < x11_comout(out)                 /* output a string to command line */
339 < char  *out;
338 > x11_comout(outp)                /* output a string to command line */
339 > char  *outp;
340   {
341 <        if (comline != NULL)
342 <                xt_puts(out, comline);
343 <        XFlush(ourdisplay);
341 >        if (comline == NULL)
342 >                return;
343 >        xt_puts(outp, comline);
344 >        if (outp[strlen(outp)-1] == '\n')
345 >                XFlush(ourdisplay);
346   }
347  
348  
# Line 263 | Line 350 | static
350   x11_errout(msg)                 /* output an error message */
351   char  *msg;
352   {
266        x11_comout(msg);
353          stderr_v(msg);          /* send to stderr also! */
354 +        x11_comout(msg);
355   }
356  
357  
358 + static
359 + std_comin(inp, prompt)          /* read in command line from stdin */
360 + char  *inp, *prompt;
361 + {
362 +        extern char     *gets();
363 +
364 +        if (prompt != NULL) {
365 +                if (fromcombuf(inp, &x11_driver))
366 +                        return;
367 +                if (!x11_driver.inpready)
368 +                        fputs(prompt, stdout);
369 +        }
370 +        if (gets(inp) == NULL) {
371 +                strcpy(inp, "quit");
372 +                return;
373 +        }
374 +        x11_driver.inpready -= strlen(inp) + 1;
375 +        if (x11_driver.inpready < 0)
376 +                x11_driver.inpready = 0;
377 + }
378 +
379 +
380 + static
381 + std_comout(outp)                /* write out string to stdout */
382 + char    *outp;
383 + {
384 +        fputs(outp, stdout);
385 + }
386 +
387 +
388   static int
389   x11_getcur(xp, yp)              /* get cursor position */
390   int  *xp, *yp;
# Line 287 | Line 404 | int  *xp, *yp;
404          if (c_last > c_first)                   /* key pressed */
405                  return(x11_getc());
406                                                  /* button pressed */
407 <        if (levptr(XButtonPressedEvent)->button & Button1)
407 >        if (levptr(XButtonPressedEvent)->button == Button1)
408                  return(MB1);
409 <        if (levptr(XButtonPressedEvent)->button & Button2)
409 >        if (levptr(XButtonPressedEvent)->button == Button2)
410                  return(MB2);
411 <        if (levptr(XButtonPressedEvent)->button & Button3)
411 >        if (levptr(XButtonPressedEvent)->button == Button3)
412                  return(MB3);
296        if (levptr(XButtonPressedEvent)->button & (Button4|Button5))
297                return(MB1);
413          return(ABORT);
414   }
415  
# Line 319 | Line 434 | int  r, g, b;
434   static int
435   getpixels()                             /* get the color map */
436   {
437 <        Visual  *ourvis = DefaultVisual(ourdisplay,DefaultScreen(ourdisplay));
437 >        XColor  thiscolor;
438 >        register int  i, j;
439  
440 <        freepixels();
441 <        for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){
442 <                pixval = (int *)malloc(ncolors*sizeof(int));
440 >        if (ncolors > 0)
441 >                return(ncolors);
442 >        if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
443 >                ourmap = DefaultColormap(ourdisplay,ourscreen);
444 >                goto loop;
445 >        }
446 > newmap:
447 >        ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
448 > loop:
449 >        for (ncolors = ourvinfo.colormap_size;
450 >                        ncolors > ourvinfo.colormap_size/3;
451 >                        ncolors = ncolors*.937) {
452 >                pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
453                  if (pixval == NULL)
454 +                        return(ncolors = 0);
455 +                if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
456                          break;
329                if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
330                                pixval,ncolors) != 0)
331                        return(ncolors);
457                  free((char *)pixval);
458 +                pixval = NULL;
459          }
460 <        return(ncolors = 0);
460 >        if (pixval == NULL) {
461 >                if (ourmap == DefaultColormap(ourdisplay,ourscreen))
462 >                        goto newmap;            /* try it with our map */
463 >                else
464 >                        return(ncolors = 0);    /* failed */
465 >        }
466 >        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
467 >                for (i = 0; i < ncolors; i++) { /* reset black and white */
468 >                        if (pixval[i] != ourblack && pixval[i] != ourwhite)
469 >                                continue;
470 >                        thiscolor.pixel = pixval[i];
471 >                        thiscolor.flags = DoRed|DoGreen|DoBlue;
472 >                        XQueryColor(ourdisplay,
473 >                                        DefaultColormap(ourdisplay,ourscreen),
474 >                                        &thiscolor);
475 >                        XStoreColor(ourdisplay, ourmap, &thiscolor);
476 >                        for (j = i; j+1 < ncolors; j++)
477 >                                pixval[j] = pixval[j+1];
478 >                        ncolors--;
479 >                        i--;
480 >                }
481 >        XSetWindowColormap(ourdisplay, gwind, ourmap);
482 >        return(ncolors);
483   }
484  
485  
# Line 341 | Line 489 | freepixels()                           /* free our pixels */
489          if (ncolors == 0)
490                  return;
491          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
492 +        free((char *)pixval);
493 +        pixval = NULL;
494          ncolors = 0;
495 +        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
496 +                XFreeColormap(ourdisplay, ourmap);
497 +        ourmap = 0;
498   }
499  
500  
501 + static unsigned long
502 + true_pixel(col)                 /* return true pixel value for color */
503 + COLOR  col;
504 + {
505 +        unsigned long  rval;
506 +        BYTE  rgb[3];
507 +
508 +        map_color(rgb, col);
509 +        rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
510 +        rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
511 +        rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
512 +        return(rval);
513 + }
514 +
515 +
516   static int
517   x11_getc()                      /* get a command character */
518   {
# Line 362 | Line 530 | getevent()                     /* get next event */
530   {
531          XNextEvent(ourdisplay, levptr(XEvent));
532          switch (levptr(XEvent)->type) {
533 +        case ConfigureNotify:
534 +                resizewindow(levptr(XConfigureEvent));
535 +                break;
536 +        case UnmapNotify:
537 +                mapped = 0;
538 +                freepixels();
539 +                break;
540 +        case MapNotify:
541 +                if (ourvinfo.class == PseudoColor ||
542 +                                ourvinfo.class == GrayScale)
543 +                        if (getpixels() == 0)
544 +                                stderr_v("Cannot allocate colors\n");
545 +                        else
546 +                                new_ctab(ncolors);
547 +                mapped = 1;
548 +                break;
549 +        case Expose:
550 +                fixwindow(levptr(XExposeEvent));
551 +                break;
552          case KeyPress:
553                  getkey(levptr(XKeyPressedEvent));
554                  break;
# Line 375 | Line 562 | static
562   getkey(ekey)                            /* get input key */
563   register XKeyPressedEvent  *ekey;
564   {
565 <        c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
565 >        register int  n;
566 >
567 >        n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
568                                  NULL, NULL);
569 <        x11_driver.inpready = c_last-c_first;
569 >        c_last += n;
570 >        x11_driver.inpready += n;
571   }
572  
573  
384 #ifdef notdef
574   static
575   fixwindow(eexp)                         /* repair damage to window */
576   register XExposeEvent  *eexp;
577   {
578 <        if (eexp->subwindow == 0)
579 <                repaint(eexp->x, gheight - eexp->y - eexp->height,
578 >        if (eexp->window == gwind) {
579 >                sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n",
580 >                        eexp->x, gheight - eexp->y - eexp->height,
581                          eexp->x + eexp->width, gheight - eexp->y);
582 <        else if (eexp->subwindow == comline->w)
583 <                xt_redraw(comline);
582 >        } else if (eexp->window == comline->w) {
583 >                if (eexp->count == 0)
584 >                        xt_redraw(comline);
585 >        }
586   }
587 < #endif
587 >
588 >
589 > static
590 > resizewindow(ersz)                      /* resize window */
591 > register XConfigureEvent  *ersz;
592 > {
593 >        if (ersz->width == gwidth && ersz->height-comheight == gheight)
594 >                return;
595 >
596 >        gwidth = ersz->width;
597 >        gheight = ersz->height-comheight;
598 >        x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
599 >        x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
600 >
601 >        strcpy(getcombuf(&x11_driver), "new\n");
602 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines