ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
(Generate patch)

Comparing ray/src/rt/x11.c (file contents):
Revision 1.5 by greg, Thu Feb 22 11:46:26 1990 UTC vs.
Revision 2.32 by schorsch, Tue Mar 30 16:13:01 2004 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
4
5 /* Copyright (c) 1989 Regents of the University of California */
6
4   /*
5 < *  x11.c - driver for X-windows version 11.3
9 < *
10 < *     Jan 1990
5 > *  x11.c - driver for X-windows version 11
6   */
7  
8 < #include  <stdio.h>
8 > #include "copyright.h"
9  
10 + #include  "standard.h"
11   #include  <sys/ioctl.h>
12 + #ifdef sparc
13 + #include  <sys/conf.h>
14 + #include  <sys/file.h>
15 + #include  <sys/filio.h>
16 + #endif
17 + #if  !defined(FNDELAY) && defined(O_NONBLOCK)
18 + #define  FNDELAY  O_NONBLOCK
19 + #endif
20  
21   #include  <X11/Xlib.h>
22   #include  <X11/cursorfont.h>
23   #include  <X11/Xutil.h>
24  
25 + #include  "platform.h"
26   #include  "color.h"
27   #include  "driver.h"
28   #include  "x11twind.h"
29 + #include  "x11icon.h"
30  
31 < #define GAMMA           2.2             /* exponent for color correction */
31 > #define GAMMA           2.2             /* default exponent correction */
32  
33 < #define MINWIDTH        (4*COMCW)       /* minimum graphics window width */
34 < #define MINHEIGHT       16              /* minimum graphics window height */
33 > #define MINWIDTH        (32*COMCW)      /* minimum graphics window width */
34 > #define MINHEIGHT       (MINWIDTH/2)    /* minimum graphics window height */
35  
36   #define BORWIDTH        5               /* border width */
37   #define COMHEIGHT       (COMLH*COMCH)   /* command line height (pixels) */
# Line 37 | Line 43 | static char SCCSid[] = "$SunId$ LBL";
43  
44   #define  ourscreen      DefaultScreen(ourdisplay)
45   #define  ourroot        RootWindow(ourdisplay,ourscreen)
40 #define  ourwhite       WhitePixel(ourdisplay,ourscreen)
41 #define  ourblack       BlackPixel(ourdisplay,ourscreen)
46  
47   #define  levptr(etype)  ((etype *)&currentevent)
48  
49   static XEvent  currentevent;            /* current event */
50  
51   static int  ncolors = 0;                /* color table size */
52 < static int  *pixval = NULL;             /* allocated pixels */
52 > static int  mapped = 0;                 /* window is mapped? */
53 > static unsigned long  *pixval = NULL;   /* allocated pixels */
54 > static unsigned long  ourblack=0, ourwhite=1;
55  
56   static Display  *ourdisplay = NULL;     /* our display */
57  
58 + static XVisualInfo  ourvinfo;           /* our visual information */
59 +
60   static Window  gwind = 0;               /* our graphics window */
61  
62   static Cursor  pickcursor = 0;          /* cursor used for picking */
63  
64   static int  gwidth, gheight;            /* graphics window size */
65  
66 + static int  comheight;                  /* desired comline height */
67   static TEXTWIND  *comline = NULL;       /* our command line */
68  
69   static char  c_queue[64];               /* input queue */
# Line 63 | Line 72 | static int  c_last = 0;                        /* last character in queue *
72  
73   static GC  ourgc = 0;                   /* our graphics context for drawing */
74  
75 < static Colormap ourmap;                 /* our color map */
75 > static Colormap ourmap = 0;             /* our color map */
76  
77 < extern char  *malloc();
77 > #define IC_X11          0
78 > #define IC_IOCTL        1
79 > #define IC_READ         2
80  
81 < int  x11_close(), x11_clear(), x11_paintr(), x11_errout(),
71 <                x11_getcur(), x11_comout(), x11_comin(), x11_flush();
81 > static int  inpcheck;                   /* whence to check input */
82  
83 + static void x11_errout(char  *msg);
84 +
85 + static dr_closef_t x11_close;
86 + static dr_clearf_t x11_clear;
87 + static dr_paintrf_t x11_paintr;
88 + static dr_getcurf_t x11_getcur;
89 + static dr_comoutf_t x11_comout;
90 + static dr_cominf_t x11_comin;
91 + static dr_flushf_t x11_flush;
92 +
93 + static dr_cominf_t std_comin;
94 + static dr_comoutf_t std_comout;
95 +
96   static struct driver  x11_driver = {
97          x11_close, x11_clear, x11_paintr, x11_getcur,
98 <        x11_comout, x11_comin, x11_flush, 1.0
98 >        NULL, NULL, x11_flush, 1.0
99   };
100  
101 + static dr_getchf_t x11_getc;
102  
103 < struct driver *
104 < x11_init(name, id)              /* initialize driver */
105 < char  *name, *id;
103 > static void freepixels(void);
104 > static int getpixels(void);
105 > static dr_newcolrf_t xnewcolr;
106 > static unsigned long true_pixel(COLOR  col);
107 > static void getevent(void);
108 > static void getkey(XKeyPressedEvent  *ekey);
109 > static void fixwindow(XExposeEvent  *eexp);
110 > static void resizewindow(XConfigureEvent  *ersz);
111 >
112 > extern dr_initf_t x11_init; /* XXX this should be in a seperate header file */
113 >
114 >
115 > extern struct driver *
116 > x11_init(               /* initialize driver */
117 >        char  *name,
118 >        char  *id
119 > )
120   {
121 +        char  *gv;
122 +        int  nplanes;
123 +        XSetWindowAttributes    ourwinattr;
124          XWMHints  ourxwmhints;
125 <        Pixmap  bmCursorSrc, bmCursorMsk;
126 <
125 >        XSizeHints      oursizhints;
126 >                                        /* open display server */
127          ourdisplay = XOpenDisplay(NULL);
128          if (ourdisplay == NULL) {
129 <                stderr_v("cannot open X-windows; DISPLAY variable set?\n");
129 >                eputs("cannot open X-windows; DISPLAY variable set?\n");
130                  return(NULL);
131          }
132 <        if (DisplayPlanes(ourdisplay, ourscreen) < 4) {
133 <                stderr_v("not enough colors\n");
134 <                return(NULL);
132 >                                        /* find a usable visual */
133 >        nplanes = DisplayPlanes(ourdisplay, ourscreen);
134 >        if (XMatchVisualInfo(ourdisplay,ourscreen,
135 >                                nplanes>12?nplanes:24,TrueColor,&ourvinfo) ||
136 >                        XMatchVisualInfo(ourdisplay,ourscreen,
137 >                                nplanes>12?nplanes:24,DirectColor,&ourvinfo)) {
138 >                ourblack = 0;
139 >                ourwhite = ourvinfo.red_mask |
140 >                                ourvinfo.green_mask |
141 >                                ourvinfo.blue_mask ;
142 >        } else {
143 >                if (nplanes < 4) {
144 >                        eputs("not enough colors\n");
145 >                        return(NULL);
146 >                }
147 >                if (!XMatchVisualInfo(ourdisplay,ourscreen,
148 >                                        nplanes,PseudoColor,&ourvinfo) &&
149 >                                !XMatchVisualInfo(ourdisplay,ourscreen,
150 >                                        nplanes,GrayScale,&ourvinfo)) {
151 >                        eputs("unsupported visual type\n");
152 >                        return(NULL);
153 >                }
154 >                ourblack = BlackPixel(ourdisplay,ourscreen);
155 >                ourwhite = WhitePixel(ourdisplay,ourscreen);
156          }
157 <        ourmap = DefaultColormap(ourdisplay,ourscreen);
158 <        make_gmap(GAMMA);                       /* make color map */
159 <        /* create a cursor */
160 <        pickcursor = XCreateFontCursor (ourdisplay, XC_diamond_cross);
161 <        /* open window */
162 <        gwind = XCreateSimpleWindow(ourdisplay, ourroot, 0, 0,
157 >                                        /* set gamma */
158 >        if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
159 >                        || (gv = getenv("DISPLAY_GAMMA")) != NULL)
160 >                make_gmap(atof(gv));
161 >        else
162 >                make_gmap(GAMMA);
163 >                                        /* X11 command line or no? */
164 >        if (!strcmp(name, "x11"))
165 >                comheight = COMHEIGHT;
166 >        else /* "x11d" */ {
167 >                comheight = 0;
168 > #ifndef  FNDELAY
169 >                eputs("warning: x11d driver not fully functional on this machine\n");
170 > #endif
171 >        }
172 >                                        /* open window */
173 >        ourwinattr.background_pixel = ourblack;
174 >        ourwinattr.border_pixel = ourblack;
175 >                                        /* this is stupid */
176 >        ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
177 >                                ourvinfo.visual, AllocNone);
178 >        gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
179                  DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
180                  DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
181 <                BORWIDTH, ourblack, ourwhite);
181 >                BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
182 >                CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
183          if (gwind == 0) {
184 <                stderr_v("cannot create window\n");
184 >                eputs("cannot create window\n");
185                  return(NULL);
186          }
187 <        XStoreName(ourdisplay, gwind, id);
187 >        XStoreName(ourdisplay, gwind, id);
188 >        /* create a cursor */
189 >        pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross);
190          ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
191 <        ourxwmhints.flags = InputHint;
191 >        ourxwmhints.flags = InputHint|IconPixmapHint;
192          ourxwmhints.input = True;
193 +        ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
194 +                        gwind, x11icon_bits, x11icon_width, x11icon_height);
195          XSetWMHints(ourdisplay, gwind, &ourxwmhints);
196 +        oursizhints.min_width = MINWIDTH;
197 +        oursizhints.min_height = MINHEIGHT+comheight;
198 +        oursizhints.flags = PMinSize;
199 +        XSetNormalHints(ourdisplay, gwind, &oursizhints);
200          XSelectInput(ourdisplay, gwind, ExposureMask);
201          XMapWindow(ourdisplay, gwind);
202 <        XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XExposeEvent));
202 >        XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent));
203          gwidth = levptr(XExposeEvent)->width;
204 <        gheight = levptr(XExposeEvent)->height - COMHEIGHT;
204 >        gheight = levptr(XExposeEvent)->height - comheight;
205          x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
206          x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
207          x11_driver.inpready = 0;
208 <        cmdvec = x11_comout;                    /* set error vectors */
209 <        if (wrnvec != NULL)
210 <                wrnvec = x11_errout;
208 >        mapped = 1;
209 >                                        /* set i/o vectors */
210 >        if (comheight) {
211 >                x11_driver.comin = x11_comin;
212 >                x11_driver.comout = x11_comout;
213 >                erract[COMMAND].pf = x11_comout;
214 >                if (erract[WARNING].pf != NULL)
215 >                        erract[WARNING].pf = x11_errout;
216 >                inpcheck = IC_X11;
217 >        } else {
218 >                x11_driver.comin = std_comin;
219 >                x11_driver.comout = std_comout;
220 >                erract[COMMAND].pf = std_comout;
221 >                inpcheck = IC_IOCTL;
222 >        }
223          return(&x11_driver);
224   }
225  
226  
227 < static
228 < x11_close()                     /* close our display */
227 > static void
228 > x11_close(void)                 /* close our display */
229   {
230 <        cmdvec = NULL;                          /* reset error vectors */
231 <        if (wrnvec != NULL)
232 <                wrnvec = stderr_v;
230 >        erract[COMMAND].pf = NULL;              /* reset error vectors */
231 >        if (erract[WARNING].pf != NULL)
232 >                erract[WARNING].pf = wputs;
233          if (ourdisplay == NULL)
234                  return;
235          if (comline != NULL) {
236                  xt_close(comline);
237                  comline = NULL;
238          }
140        if (gwind != 0) {
141                XFreeGC(ourdisplay, ourgc);
142                XDestroyWindow(ourdisplay, gwind);
143                gwind = 0;
144                ourgc = 0;
145        }
146        XFreeCursor(ourdisplay, pickcursor);
239          freepixels();
240 +        XFreeGC(ourdisplay, ourgc);
241 +        XDestroyWindow(ourdisplay, gwind);
242 +        gwind = 0;
243 +        ourgc = 0;
244 +        XFreeCursor(ourdisplay, pickcursor);
245          XCloseDisplay(ourdisplay);
246          ourdisplay = NULL;
247   }
248  
249  
250 < static
251 < x11_clear(xres, yres)                   /* clear our display */
252 < int  xres, yres;
250 > static void
251 > x11_clear(                      /* clear our display */
252 >        int  xres,
253 >        int  yres
254 > )
255   {
256 <        if (xres != gwidth || yres != gheight) {        /* change window */
257 <                if (comline != NULL)
258 <                        xt_close(comline);
256 >                                                /* check limits */
257 >        if (xres < MINWIDTH)
258 >                xres = MINWIDTH;
259 >        if (yres < MINHEIGHT)
260 >                yres = MINHEIGHT;
261 >                                                /* resize window */
262 >        if (xres != gwidth || yres != gheight) {
263                  XSelectInput(ourdisplay, gwind, 0);
264 <                XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
162 <                comline = xt_open(ourdisplay,
163 <                                DefaultGC(ourdisplay,ourscreen),
164 <                                gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
165 <                if (comline == NULL) {
166 <                        stderr_v("Cannot open command line window\n");
167 <                        quit(1);
168 <                }
169 <                XSelectInput(ourdisplay, comline->w, ExposureMask);
264 >                XResizeWindow(ourdisplay, gwind, xres, yres+comheight);
265                  gwidth = xres;
266                  gheight = yres;
267 <                XSync(ourdisplay, 1);           /* discard input */
267 >                XFlush(ourdisplay);
268                  sleep(2);                       /* wait for window manager */
269 +                XSync(ourdisplay, 1);           /* discard input */
270          }
271          XClearWindow(ourdisplay, gwind);
272                                                  /* reinitialize color table */
273 <        if (getpixels() == 0)
274 <                stderr_v("cannot allocate colors\n");
275 <        else
276 <                new_ctab(ncolors);
277 <
278 <        XSelectInput(ourdisplay, gwind,
273 >        if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale) {
274 >                if (getpixels() == 0)
275 >                        eputs("cannot allocate colors\n");
276 >                else
277 >                        new_ctab(ncolors);
278 >        }
279 >                                                /* get new command line */
280 >        if (comline != NULL)
281 >                xt_close(comline);
282 >        if (comheight) {
283 >                comline = xt_open(ourdisplay, gwind, 0, gheight, gwidth,
284 >                                comheight, 0, ourblack, ourwhite, COMFN);
285 >                if (comline == NULL) {
286 >                        eputs("cannot open command line window\n");
287 >                        quit(1);
288 >                }
289 >                XSelectInput(ourdisplay, comline->w, ExposureMask);
290 >                                                /* remove earmuffs */
291 >                XSelectInput(ourdisplay, gwind,
292                  StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
293 +        } else                                  /* remove earmuffs */
294 +                XSelectInput(ourdisplay, gwind,
295 +                        StructureNotifyMask|ExposureMask|ButtonPressMask);
296   }
297  
298  
299 < static
300 < x11_paintr(col, xmin, ymin, xmax, ymax)         /* fill a rectangle */
301 < COLOR  col;
302 < int  xmin, ymin, xmax, ymax;
299 > static void
300 > x11_paintr(             /* fill a rectangle */
301 >        COLOR  col,
302 >        int  xmin,
303 >        int  ymin,
304 >        int  xmax,
305 >        int  ymax
306 > )
307   {
308 <        extern int  xnewcolr();         /* pixel assignment routine */
308 >        unsigned long  pixel;
309  
310 <        if (ncolors > 0) {
311 <                XSetForeground(ourdisplay, ourgc,
312 <                                pixval[get_pixel(col, xnewcolr)]);
313 <                XFillRectangle(ourdisplay, gwind,
314 <                        ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
315 <        }
310 >        if (!mapped)
311 >                return;
312 >        if (ncolors > 0)
313 >                pixel = pixval[get_pixel(col, xnewcolr)];
314 >        else
315 >                pixel = true_pixel(col);
316 >        XSetForeground(ourdisplay, ourgc, pixel);
317 >        XFillRectangle(ourdisplay, gwind,
318 >                ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
319   }
320  
321  
322 < static
323 < x11_flush()                     /* flush output */
322 > static void
323 > x11_flush(void)                 /* flush output */
324   {
325 <        if (ncolors <= 0)       /* output necessary for death */
326 <                XFillRectangle(ourdisplay, gwind, ourgc, 0, 0, 1 ,1);
327 <        while (XPending(ourdisplay) > 0)
325 >        char    buf[256];
326 >        int     n;
327 >                                                /* check for input */
328 >        XNoOp(ourdisplay);
329 >        n = XPending(ourdisplay);                       /* from X server */
330 >        while (n-- > 0)
331                  getevent();
332 + #ifdef FNDELAY
333 +        if (inpcheck == IC_IOCTL) {                     /* from stdin */
334 + #ifdef FIONREAD
335 +                if (ioctl(fileno(stdin), FIONREAD, &n) < 0) {
336 + #else
337 +                if (1) {
338 + #endif
339 +                        if (fcntl(fileno(stdin), F_SETFL, FNDELAY) < 0) {
340 +                                eputs("cannot change input mode\n");
341 +                                quit(1);
342 +                        }
343 +                        inpcheck = IC_READ;
344 +                } else
345 +                        x11_driver.inpready += n;
346 +        }
347 +        if (inpcheck == IC_READ) {
348 +                n = read(fileno(stdin), buf, sizeof(buf)-1);
349 +                if (n > 0) {
350 +                        buf[n] = '\0';
351 +                        tocombuf(buf, &x11_driver);
352 +                }
353 +        }
354 + #endif
355   }
356  
357  
358 < static
359 < x11_comin(inp, prompt)          /* read in a command line */
360 < char  *inp, *prompt;
358 > static void
359 > x11_comin(              /* read in a command line */
360 >        char  *inp,
361 >        char  *prompt
362 > )
363   {
364 <        int  x11_getc(), x11_comout();
365 <
219 <        if (prompt != NULL)
364 >        if (prompt != NULL) {
365 >                x11_flush();            /* make sure we get everything */
366                  if (fromcombuf(inp, &x11_driver))
367                          return;
368 <                else
369 <                        xt_puts(prompt, comline);
368 >                xt_puts(prompt, comline);
369 >        }
370          xt_cursor(comline, TBLKCURS);
371          editline(inp, x11_getc, x11_comout);
372          xt_cursor(comline, TNOCURS);
373   }
374  
375  
376 < static
377 < x11_comout(out)                 /* output a string to command line */
378 < char  *out;
376 > static void
377 > x11_comout(             /* output a string to command line */
378 >        char  *outp
379 > )
380   {
381 <        if (comline != NULL)
382 <                xt_puts(out, comline);
383 <        XFlush(ourdisplay);
381 >        if (comline == NULL || outp == NULL || !outp[0])
382 >                return;
383 >        xt_puts(outp, comline);
384 >        if (outp[strlen(outp)-1] == '\n')
385 >                XFlush(ourdisplay);
386   }
387  
388  
389 < static
390 < x11_errout(msg)                 /* output an error message */
391 < char  *msg;
389 > static void
390 > x11_errout(                     /* output an error message */
391 >        char  *msg
392 > )
393   {
394 +        eputs(msg);             /* send to stderr also! */
395          x11_comout(msg);
245        stderr_v(msg);          /* send to stderr also! */
396   }
397  
398  
399 + static void
400 + std_comin(              /* read in command line from stdin */
401 +        char  *inp,
402 +        char  *prompt
403 + )
404 + {
405 +        if (prompt != NULL) {
406 +                if (fromcombuf(inp, &x11_driver))
407 +                        return;
408 +                if (!x11_driver.inpready)
409 +                        std_comout(prompt);
410 +        }
411 + #ifdef FNDELAY
412 +        if (inpcheck == IC_READ) {      /* turn off FNDELAY */
413 +                if (fcntl(fileno(stdin), F_SETFL, 0) < 0) {
414 +                        eputs("cannot change input mode\n");
415 +                        quit(1);
416 +                }
417 +                inpcheck = IC_IOCTL;
418 +        }
419 + #endif
420 +        if (gets(inp) == NULL) {
421 +                strcpy(inp, "quit");
422 +                return;
423 +        }
424 +        x11_driver.inpready -= strlen(inp) + 1;
425 +        if (x11_driver.inpready < 0)
426 +                x11_driver.inpready = 0;
427 + }
428 +
429 +
430 + static void
431 + std_comout(             /* write out string to stdout */
432 +        char    *outp
433 + )
434 + {
435 +        fputs(outp, stdout);
436 +        fflush(stdout);
437 + }
438 +
439 +
440   static int
441 < x11_getcur(xp, yp)              /* get cursor position */
442 < int  *xp, *yp;
441 > x11_getcur(             /* get cursor position */
442 >        int  *xp,
443 >        int  *yp
444 > )
445   {
446          while (XGrabPointer(ourdisplay, gwind, True, ButtonPressMask,
447                          GrabModeAsync, GrabModeAsync, None, pickcursor,
# Line 265 | Line 458 | int  *xp, *yp;
458          if (c_last > c_first)                   /* key pressed */
459                  return(x11_getc());
460                                                  /* button pressed */
461 <        if (levptr(XButtonPressedEvent)->button & Button1)
461 >        if (levptr(XButtonPressedEvent)->button == Button1)
462                  return(MB1);
463 <        if (levptr(XButtonPressedEvent)->button & Button2)
463 >        if (levptr(XButtonPressedEvent)->button == Button2)
464                  return(MB2);
465 <        if (levptr(XButtonPressedEvent)->button & Button3)
465 >        if (levptr(XButtonPressedEvent)->button == Button3)
466                  return(MB3);
274        if (levptr(XButtonPressedEvent)->button & (Button4|Button5))
275                return(MB1);
467          return(ABORT);
468   }
469  
470  
471 < static
472 < xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
473 < int  ndx;
474 < int  r, g, b;
471 > static void
472 > xnewcolr(               /* enter a color into hardware table */
473 >        int  ndx,
474 >        int  r,
475 >        int  g,
476 >        int  b
477 > )
478   {
479          XColor  xcolor;
480  
# Line 295 | Line 489 | int  r, g, b;
489  
490  
491   static int
492 < getpixels()                             /* get the color map */
492 > getpixels(void)                         /* get the color map */
493   {
494 <        Visual  *ourvis = DefaultVisual(ourdisplay,ourscreen);
494 >        XColor  thiscolor;
495 >        register int  i, j;
496  
497          if (ncolors > 0)
498                  return(ncolors);
499 <        for (ncolors=(ourvis->map_entries)-3; ncolors>12; ncolors=ncolors*.937){
500 <                pixval = (int *)malloc(ncolors*sizeof(int));
499 >        if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
500 >                ourmap = DefaultColormap(ourdisplay,ourscreen);
501 >                goto loop;
502 >        }
503 > newmap:
504 >        ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
505 > loop:
506 >        for (ncolors = ourvinfo.colormap_size;
507 >                        ncolors > ourvinfo.colormap_size/3;
508 >                        ncolors = ncolors*.937) {
509 >                pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
510                  if (pixval == NULL)
511 +                        return(ncolors = 0);
512 +                if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
513                          break;
514 <                if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
515 <                                pixval,ncolors) != 0)
310 <                        return(ncolors);
311 <                free((char *)pixval);
514 >                free((void *)pixval);
515 >                pixval = NULL;
516          }
517 <        return(ncolors = 0);
517 >        if (pixval == NULL) {
518 >                if (ourmap == DefaultColormap(ourdisplay,ourscreen))
519 >                        goto newmap;            /* try it with our map */
520 >                else
521 >                        return(ncolors = 0);    /* failed */
522 >        }
523 >        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
524 >                for (i = 0; i < ncolors; i++) { /* reset black and white */
525 >                        if (pixval[i] != ourblack && pixval[i] != ourwhite)
526 >                                continue;
527 >                        thiscolor.pixel = pixval[i];
528 >                        thiscolor.flags = DoRed|DoGreen|DoBlue;
529 >                        XQueryColor(ourdisplay,
530 >                                        DefaultColormap(ourdisplay,ourscreen),
531 >                                        &thiscolor);
532 >                        XStoreColor(ourdisplay, ourmap, &thiscolor);
533 >                        for (j = i; j+1 < ncolors; j++)
534 >                                pixval[j] = pixval[j+1];
535 >                        ncolors--;
536 >                        i--;
537 >                }
538 >        XSetWindowColormap(ourdisplay, gwind, ourmap);
539 >        return(ncolors);
540   }
541  
542  
543 < static
544 < freepixels()                            /* free our pixels */
543 > static void
544 > freepixels(void)                                /* free our pixels */
545   {
546          if (ncolors == 0)
547                  return;
548          XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
549 +        free((void *)pixval);
550 +        pixval = NULL;
551          ncolors = 0;
552 +        if (ourmap != DefaultColormap(ourdisplay,ourscreen))
553 +                XFreeColormap(ourdisplay, ourmap);
554 +        ourmap = 0;
555   }
556  
557  
558 + static unsigned long
559 + true_pixel(                     /* return true pixel value for color */
560 +        COLOR  col
561 + )
562 + {
563 +        unsigned long  rval;
564 +        BYTE  rgb[3];
565 +
566 +        map_color(rgb, col);
567 +        rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
568 +        rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
569 +        rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
570 +        return(rval);
571 + }
572 +
573 +
574   static int
575 < x11_getc()                      /* get a command character */
575 > x11_getc(void)                  /* get a command character */
576   {
577          while (c_last <= c_first) {
578                  c_first = c_last = 0;           /* reset */
# Line 336 | Line 583 | x11_getc()                     /* get a command character */
583   }
584  
585  
586 < static
587 < getevent()                      /* get next event */
586 > static void
587 > getevent(void)                  /* get next event */
588   {
589          XNextEvent(ourdisplay, levptr(XEvent));
590          switch (levptr(XEvent)->type) {
# Line 345 | Line 592 | getevent()                     /* get next event */
592                  resizewindow(levptr(XConfigureEvent));
593                  break;
594          case UnmapNotify:
595 +                mapped = 0;
596                  freepixels();
597                  break;
598          case MapNotify:
599 <                if (getpixels() == 0)
600 <                        stderr_v("Cannot allocate colors\n");
601 <                else
602 <                        new_ctab(ncolors);
599 >                if (ourvinfo.class == PseudoColor ||
600 >                                ourvinfo.class == GrayScale) {
601 >                        if (getpixels() == 0)
602 >                                eputs("cannot allocate colors\n");
603 >                        else
604 >                                new_ctab(ncolors);
605 >                }
606 >                mapped = 1;
607                  break;
608          case Expose:
609                  fixwindow(levptr(XExposeEvent));
# Line 365 | Line 617 | getevent()                     /* get next event */
617   }
618  
619  
620 < static
621 < getkey(ekey)                            /* get input key */
622 < register XKeyPressedEvent  *ekey;
620 > static void
621 > getkey(                         /* get input key */
622 >        register XKeyPressedEvent  *ekey
623 > )
624   {
625 <        c_last += XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
625 >        register int  n;
626 >
627 >        n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
628                                  NULL, NULL);
629 <        x11_driver.inpready = c_last-c_first;
629 >        c_last += n;
630 >        x11_driver.inpready += n;
631   }
632  
633  
634 < static
635 < fixwindow(eexp)                         /* repair damage to window */
636 < register XExposeEvent  *eexp;
634 > static void
635 > fixwindow(                              /* repair damage to window */
636 >        register XExposeEvent  *eexp
637 > )
638   {
639 +        char  buf[80];
640 +
641          if (eexp->window == gwind) {
642 <                sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n",
642 >                sprintf(buf, "repaint %d %d %d %d\n",
643                          eexp->x, gheight - eexp->y - eexp->height,
644                          eexp->x + eexp->width, gheight - eexp->y);
645 +                tocombuf(buf, &x11_driver);
646          } else if (eexp->window == comline->w) {
647                  if (eexp->count == 0)
648                          xt_redraw(comline);
# Line 390 | Line 650 | register XExposeEvent  *eexp;
650   }
651  
652  
653 < static
654 < resizewindow(ersz)                      /* resize window */
655 < register XConfigureEvent  *ersz;
653 > static void
654 > resizewindow(                   /* resize window */
655 >        register XConfigureEvent  *ersz
656 > )
657   {
658 <        if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight)
658 >        if (ersz->width == gwidth && ersz->height-comheight == gheight)
659                  return;
660  
661          gwidth = ersz->width;
662 <        gheight = ersz->height-COMHEIGHT;
662 >        gheight = ersz->height-comheight;
663          x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
664          x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
665  
666 <        strcpy(getcombuf(&x11_driver), "new\n");
666 >        tocombuf("new\n", &x11_driver);
667   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines