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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines