ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 1.7
Committed: Fri Feb 23 14:50:38 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +5 -2 lines
Log Message:
fixed handling of inpready

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