ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 1.16
Committed: Wed Jul 18 12:25:07 1990 UTC (33 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.15: +15 -7 lines
Log Message:
added debugging for color routines

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