ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 2.6
Committed: Wed Jun 24 09:12:50 1992 UTC (31 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +9 -3 lines
Log Message:
added code to get GAMMA from environment

File Contents

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