ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 2.32
Committed: Tue Mar 30 16:13:01 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad3R6, rad3R6P1, rad3R8
Changes since 2.31: +86 -47 lines
Log Message:
Continued ANSIfication. There are only bits and pieces left now.

File Contents

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