ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 2.3
Committed: Fri May 29 15:42:49 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +10 -6 lines
Log Message:
fixed text and background color for 24-bit screens

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2     static char SCCSid[] = "$SunId$ LBL";
3     #endif
4    
5     /* Copyright (c) 1989 Regents of the University of California */
6    
7     /*
8 greg 1.4 * x11.c - driver for X-windows version 11.3
9 greg 1.1 *
10 greg 1.4 * Jan 1990
11 greg 1.1 */
12    
13     #include <stdio.h>
14    
15     #include <sys/ioctl.h>
16    
17     #include <X11/Xlib.h>
18 greg 1.4 #include <X11/cursorfont.h>
19     #include <X11/Xutil.h>
20 greg 1.1
21     #include "color.h"
22     #include "driver.h"
23     #include "x11twind.h"
24 greg 1.18 #include "x11icon.h"
25 greg 1.1
26     #define GAMMA 2.2 /* exponent for color correction */
27    
28 greg 1.6 #define MINWIDTH (32*COMCW) /* minimum graphics window width */
29     #define MINHEIGHT MINWIDTH /* minimum graphics window height */
30 greg 1.4
31 greg 1.1 #define BORWIDTH 5 /* border width */
32     #define COMHEIGHT (COMLH*COMCH) /* command line height (pixels) */
33    
34     #define COMFN "8x13" /* command line font name */
35     #define COMLH 3 /* number of command lines */
36     #define COMCW 8 /* approx. character width (pixels) */
37     #define COMCH 14 /* approx. character height (pixels) */
38    
39 greg 1.4 #define ourscreen DefaultScreen(ourdisplay)
40     #define ourroot RootWindow(ourdisplay,ourscreen)
41    
42     #define levptr(etype) ((etype *)&currentevent)
43 greg 1.1
44 greg 1.4 static XEvent currentevent; /* current event */
45 greg 1.1
46     static int ncolors = 0; /* color table size */
47 greg 1.17 static unsigned long *pixval = NULL; /* allocated pixels */
48 greg 2.3 static unsigned long ourblack=0, ourwhite=1;
49 greg 1.1
50     static Display *ourdisplay = NULL; /* our display */
51    
52 greg 2.2 static XVisualInfo ourvinfo; /* our visual information */
53 greg 1.6
54 greg 1.1 static Window gwind = 0; /* our graphics window */
55    
56     static Cursor pickcursor = 0; /* cursor used for picking */
57    
58 greg 1.4 static int gwidth, gheight; /* graphics window size */
59 greg 1.1
60     static TEXTWIND *comline = NULL; /* our command line */
61    
62     static char c_queue[64]; /* input queue */
63     static int c_first = 0; /* first character in queue */
64     static int c_last = 0; /* last character in queue */
65    
66     static GC ourgc = 0; /* our graphics context for drawing */
67    
68 greg 1.6 static Colormap ourmap = 0; /* our color map */
69 greg 1.1
70 greg 1.14 extern char *malloc(), *getcombuf();
71 greg 1.1
72 greg 1.14 static int x11_close(), x11_clear(), x11_paintr(), x11_errout(),
73 greg 1.5 x11_getcur(), x11_comout(), x11_comin(), x11_flush();
74 greg 1.1
75     static struct driver x11_driver = {
76     x11_close, x11_clear, x11_paintr, x11_getcur,
77 greg 1.5 x11_comout, x11_comin, x11_flush, 1.0
78 greg 1.1 };
79    
80    
81     struct driver *
82     x11_init(name, id) /* initialize driver */
83     char *name, *id;
84     {
85 greg 1.6 int nplanes;
86 greg 1.10 XSetWindowAttributes ourwinattr;
87 greg 1.4 XWMHints ourxwmhints;
88 greg 1.12 XSizeHints oursizhints;
89 greg 1.1
90     ourdisplay = XOpenDisplay(NULL);
91     if (ourdisplay == NULL) {
92     stderr_v("cannot open X-windows; DISPLAY variable set?\n");
93     return(NULL);
94     }
95 greg 2.2 /* find a usable visual */
96 greg 1.6 nplanes = DisplayPlanes(ourdisplay, ourscreen);
97 greg 2.2 if ( !XMatchVisualInfo(ourdisplay,ourscreen,
98     24,TrueColor,&ourvinfo) &&
99     !XMatchVisualInfo(ourdisplay,ourscreen,
100 greg 2.3 24,DirectColor,&ourvinfo) ) {
101 greg 2.2 if (nplanes < 4) {
102     stderr_v("not enough colors\n");
103 greg 1.6 return(NULL);
104 greg 2.2 } else if (!XMatchVisualInfo(ourdisplay,ourscreen,
105     nplanes,PseudoColor,&ourvinfo) &&
106     !XMatchVisualInfo(ourdisplay,ourscreen,
107     nplanes,GrayScale,&ourvinfo)) {
108     stderr_v("unsupported visual type\n");
109     return(NULL);
110 greg 1.6 }
111 greg 2.3 ourblack = BlackPixel(ourdisplay,ourscreen);
112     ourwhite = WhitePixel(ourdisplay,ourscreen);
113     } else {
114     ourblack = 0;
115     ourwhite = ~0;
116     }
117 greg 1.6 make_gmap(GAMMA);
118 greg 1.4 /* open window */
119 greg 1.12 ourwinattr.background_pixel = ourblack;
120 greg 1.10 ourwinattr.border_pixel = ourblack;
121 greg 2.2 /* this is a waste! */
122     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
123     ourvinfo.visual, AllocNone);
124 greg 1.10 gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
125 greg 1.4 DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
126     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
127 greg 2.2 BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
128     CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
129 greg 1.4 if (gwind == 0) {
130     stderr_v("cannot create window\n");
131     return(NULL);
132     }
133     XStoreName(ourdisplay, gwind, id);
134 greg 1.10 /* create a cursor */
135     pickcursor = XCreateFontCursor(ourdisplay, XC_diamond_cross);
136 greg 1.4 ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
137 greg 1.18 ourxwmhints.flags = InputHint|IconPixmapHint;
138 greg 1.4 ourxwmhints.input = True;
139 greg 1.18 ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
140     gwind, x11icon_bits, x11icon_width, x11icon_height);
141 greg 1.4 XSetWMHints(ourdisplay, gwind, &ourxwmhints);
142 greg 1.12 oursizhints.min_width = MINWIDTH;
143     oursizhints.min_height = MINHEIGHT+COMHEIGHT;
144     oursizhints.flags = PMinSize;
145     XSetNormalHints(ourdisplay, gwind, &oursizhints);
146 greg 1.4 XSelectInput(ourdisplay, gwind, ExposureMask);
147     XMapWindow(ourdisplay, gwind);
148 greg 1.17 XWindowEvent(ourdisplay, gwind, ExposureMask, levptr(XEvent));
149 greg 1.4 gwidth = levptr(XExposeEvent)->width;
150     gheight = levptr(XExposeEvent)->height - COMHEIGHT;
151     x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
152     x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
153 greg 1.1 x11_driver.inpready = 0;
154     cmdvec = x11_comout; /* set error vectors */
155     if (wrnvec != NULL)
156     wrnvec = x11_errout;
157     return(&x11_driver);
158     }
159    
160    
161     static
162     x11_close() /* close our display */
163     {
164     cmdvec = NULL; /* reset error vectors */
165     if (wrnvec != NULL)
166     wrnvec = stderr_v;
167     if (ourdisplay == NULL)
168     return;
169     if (comline != NULL) {
170     xt_close(comline);
171     comline = NULL;
172     }
173 greg 1.8 freepixels();
174     XFreeGC(ourdisplay, ourgc);
175     XDestroyWindow(ourdisplay, gwind);
176     gwind = 0;
177     ourgc = 0;
178 greg 1.1 XFreeCursor(ourdisplay, pickcursor);
179     XCloseDisplay(ourdisplay);
180     ourdisplay = NULL;
181     }
182    
183    
184     static
185     x11_clear(xres, yres) /* clear our display */
186     int xres, yres;
187     {
188 greg 1.12 /* check limits */
189     if (xres < MINWIDTH)
190     xres = MINWIDTH;
191     if (yres < MINHEIGHT)
192     yres = MINHEIGHT;
193     /* resize window */
194     if (xres != gwidth || yres != gheight) {
195 greg 1.4 XSelectInput(ourdisplay, gwind, 0);
196     XResizeWindow(ourdisplay, gwind, xres, yres+COMHEIGHT);
197 greg 1.1 gwidth = xres;
198     gheight = yres;
199 greg 1.8 XFlush(ourdisplay);
200     sleep(2); /* wait for window manager */
201 greg 1.4 XSync(ourdisplay, 1); /* discard input */
202     }
203 greg 1.13 XClearWindow(ourdisplay, gwind);
204     /* reinitialize color table */
205 greg 2.2 if (ourvinfo.class == PseudoColor || ourvinfo.class == GrayScale)
206 greg 1.13 if (getpixels() == 0)
207     stderr_v("cannot allocate colors\n");
208     else
209     new_ctab(ncolors);
210 greg 1.12 /* get new command line */
211 greg 1.13 if (comline != NULL)
212     xt_close(comline);
213 greg 2.3 comline = xt_open(ourdisplay, gwind, 0, gheight,
214     gwidth, COMHEIGHT, 0, ourblack, ourwhite, COMFN);
215 greg 1.12 if (comline == NULL) {
216     stderr_v("Cannot open command line window\n");
217     quit(1);
218     }
219     XSelectInput(ourdisplay, comline->w, ExposureMask);
220     /* remove earmuffs */
221 greg 1.4 XSelectInput(ourdisplay, gwind,
222     StructureNotifyMask|ExposureMask|KeyPressMask|ButtonPressMask);
223 greg 1.1 }
224    
225    
226     static
227     x11_paintr(col, xmin, ymin, xmax, ymax) /* fill a rectangle */
228     COLOR col;
229     int xmin, ymin, xmax, ymax;
230     {
231     extern int xnewcolr(); /* pixel assignment routine */
232 greg 1.6 extern unsigned long true_pixel();
233     unsigned long pixel;
234 greg 1.1
235 greg 1.6 if (ncolors > 0)
236     pixel = pixval[get_pixel(col, xnewcolr)];
237 greg 2.2 else if (ourvinfo.class == TrueColor || ourvinfo.class == DirectColor)
238 greg 1.6 pixel = true_pixel(col);
239     else
240     return;
241     XSetForeground(ourdisplay, ourgc, pixel);
242     XFillRectangle(ourdisplay, gwind,
243     ourgc, xmin, gheight-ymax, xmax-xmin, ymax-ymin);
244 greg 1.5 }
245    
246    
247     static
248     x11_flush() /* flush output */
249     {
250 greg 1.10 XNoOp(ourdisplay);
251 greg 1.5 while (XPending(ourdisplay) > 0)
252     getevent();
253 greg 1.1 }
254    
255    
256     static
257 greg 1.3 x11_comin(inp, prompt) /* read in a command line */
258     char *inp, *prompt;
259 greg 1.1 {
260 greg 1.14 extern int x11_getc();
261 greg 1.1
262 greg 1.3 if (prompt != NULL)
263 greg 1.4 if (fromcombuf(inp, &x11_driver))
264     return;
265     else
266     xt_puts(prompt, comline);
267 greg 1.1 xt_cursor(comline, TBLKCURS);
268     editline(inp, x11_getc, x11_comout);
269     xt_cursor(comline, TNOCURS);
270     }
271    
272    
273     static
274     x11_comout(out) /* output a string to command line */
275     char *out;
276     {
277 greg 1.11 if (comline == NULL)
278     return;
279     xt_puts(out, comline);
280     if (out[strlen(out)-1] == '\n')
281     XFlush(ourdisplay);
282 greg 1.1 }
283    
284    
285     static
286     x11_errout(msg) /* output an error message */
287     char *msg;
288     {
289 greg 1.9 stderr_v(msg); /* send to stderr also! */
290 greg 1.1 x11_comout(msg);
291     }
292    
293    
294     static int
295     x11_getcur(xp, yp) /* get cursor position */
296     int *xp, *yp;
297     {
298     while (XGrabPointer(ourdisplay, gwind, True, ButtonPressMask,
299     GrabModeAsync, GrabModeAsync, None, pickcursor,
300     CurrentTime) != GrabSuccess)
301     sleep(2);
302    
303     do
304     getevent();
305     while (c_last <= c_first && levptr(XEvent)->type != ButtonPress);
306     *xp = levptr(XButtonPressedEvent)->x;
307     *yp = gheight-1 - levptr(XButtonPressedEvent)->y;
308     XUngrabPointer(ourdisplay, CurrentTime);
309     XFlush(ourdisplay); /* insure release */
310     if (c_last > c_first) /* key pressed */
311     return(x11_getc());
312     /* button pressed */
313     if (levptr(XButtonPressedEvent)->button & Button1)
314     return(MB1);
315     if (levptr(XButtonPressedEvent)->button & Button2)
316     return(MB2);
317     if (levptr(XButtonPressedEvent)->button & Button3)
318     return(MB3);
319     if (levptr(XButtonPressedEvent)->button & (Button4|Button5))
320     return(MB1);
321     return(ABORT);
322     }
323    
324    
325     static
326     xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
327     int ndx;
328     int r, g, b;
329     {
330     XColor xcolor;
331    
332     xcolor.pixel = pixval[ndx];
333     xcolor.red = r << 8;
334     xcolor.green = g << 8;
335     xcolor.blue = b << 8;
336     xcolor.flags = DoRed|DoGreen|DoBlue;
337    
338     XStoreColor(ourdisplay, ourmap, &xcolor);
339     }
340    
341    
342     static int
343     getpixels() /* get the color map */
344     {
345 greg 1.16 XColor thiscolor;
346     register int i, j;
347    
348 greg 1.4 if (ncolors > 0)
349 greg 2.2 return(ncolors);
350     if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
351 greg 1.6 ourmap = DefaultColormap(ourdisplay,ourscreen);
352     goto loop;
353     }
354     newmap:
355 greg 2.2 ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
356 greg 1.6 loop:
357 greg 2.2 for (ncolors = ourvinfo.colormap_size;
358     ncolors > ourvinfo.colormap_size/3;
359 greg 1.6 ncolors = ncolors*.937) {
360 greg 1.17 pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
361 greg 1.1 if (pixval == NULL)
362 greg 1.6 return(ncolors = 0);
363 greg 1.1 if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,
364     pixval,ncolors) != 0)
365 greg 1.6 break;
366 greg 1.1 free((char *)pixval);
367 greg 1.6 pixval = NULL;
368 greg 1.1 }
369 greg 1.6 if (pixval == NULL) {
370     if (ourmap == DefaultColormap(ourdisplay,ourscreen))
371     goto newmap; /* try it with our map */
372     else
373     return(ncolors = 0); /* failed */
374     }
375 greg 1.16 if (ourmap != DefaultColormap(ourdisplay,ourscreen))
376     for (i = 0; i < ncolors; i++) { /* reset black and white */
377 greg 1.6 if (pixval[i] != ourblack && pixval[i] != ourwhite)
378     continue;
379     thiscolor.pixel = pixval[i];
380     thiscolor.flags = DoRed|DoGreen|DoBlue;
381     XQueryColor(ourdisplay,
382     DefaultColormap(ourdisplay,ourscreen),
383     &thiscolor);
384     XStoreColor(ourdisplay, ourmap, &thiscolor);
385     for (j = i; j+1 < ncolors; j++)
386     pixval[j] = pixval[j+1];
387     ncolors--;
388     i--;
389     }
390 greg 1.16 XSetWindowColormap(ourdisplay, gwind, ourmap);
391 greg 1.6 return(ncolors);
392 greg 1.1 }
393    
394    
395     static
396     freepixels() /* free our pixels */
397     {
398     if (ncolors == 0)
399     return;
400     XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
401     ncolors = 0;
402 greg 1.8 if (ourmap != DefaultColormap(ourdisplay,ourscreen))
403 greg 1.6 XFreeColormap(ourdisplay, ourmap);
404     ourmap = 0;
405 greg 1.1 }
406    
407    
408 greg 1.6 static unsigned long
409     true_pixel(col) /* return true pixel value for color */
410     COLOR col;
411     {
412     unsigned long rval;
413     BYTE rgb[3];
414    
415     map_color(rgb, col);
416 greg 2.2 rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
417     rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
418     rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
419 greg 1.6 return(rval);
420     }
421    
422    
423 greg 1.1 static int
424     x11_getc() /* get a command character */
425     {
426     while (c_last <= c_first) {
427     c_first = c_last = 0; /* reset */
428     getevent(); /* wait for key */
429     }
430     x11_driver.inpready--;
431     return(c_queue[c_first++]);
432     }
433    
434    
435     static
436     getevent() /* get next event */
437     {
438     XNextEvent(ourdisplay, levptr(XEvent));
439     switch (levptr(XEvent)->type) {
440 greg 1.4 case ConfigureNotify:
441     resizewindow(levptr(XConfigureEvent));
442     break;
443     case UnmapNotify:
444     freepixels();
445     break;
446     case MapNotify:
447 greg 2.2 if (ourvinfo.class == PseudoColor ||
448     ourvinfo.class == GrayScale)
449 greg 1.6 if (getpixels() == 0)
450     stderr_v("Cannot allocate colors\n");
451     else
452     new_ctab(ncolors);
453 greg 1.4 break;
454     case Expose:
455     fixwindow(levptr(XExposeEvent));
456     break;
457 greg 1.1 case KeyPress:
458     getkey(levptr(XKeyPressedEvent));
459     break;
460     case ButtonPress:
461     break;
462     }
463     }
464    
465    
466     static
467     getkey(ekey) /* get input key */
468     register XKeyPressedEvent *ekey;
469     {
470 greg 1.8 register int n;
471 greg 1.7
472     n = XLookupString(ekey, c_queue+c_last, sizeof(c_queue)-c_last,
473 greg 1.1 NULL, NULL);
474 greg 1.7 c_last += n;
475     x11_driver.inpready += n;
476 greg 1.1 }
477    
478    
479     static
480     fixwindow(eexp) /* repair damage to window */
481     register XExposeEvent *eexp;
482     {
483 greg 1.4 if (eexp->window == gwind) {
484     sprintf(getcombuf(&x11_driver), "repaint %d %d %d %d\n",
485     eexp->x, gheight - eexp->y - eexp->height,
486 greg 1.1 eexp->x + eexp->width, gheight - eexp->y);
487 greg 1.4 } else if (eexp->window == comline->w) {
488     if (eexp->count == 0)
489     xt_redraw(comline);
490     }
491 greg 1.1 }
492 greg 1.4
493    
494     static
495     resizewindow(ersz) /* resize window */
496     register XConfigureEvent *ersz;
497     {
498     if (ersz->width == gwidth && ersz->height-COMHEIGHT == gheight)
499     return;
500    
501     gwidth = ersz->width;
502     gheight = ersz->height-COMHEIGHT;
503     x11_driver.xsiz = gwidth < MINWIDTH ? MINWIDTH : gwidth;
504     x11_driver.ysiz = gheight < MINHEIGHT ? MINHEIGHT : gheight;
505    
506     strcpy(getcombuf(&x11_driver), "new\n");
507     }