ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 1.9
Committed: Fri Mar 2 10:08:01 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.8: +2 -1 lines
Log Message:
improved flushing action

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