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