ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 1.10
Committed: Fri Mar 2 15:05:26 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +9 -6 lines
Log Message:
changed call to XCreateWindow to support different visuals

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