ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11.c
Revision: 1.12
Committed: Thu Mar 8 10:09:58 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +29 -14 lines
Log Message:
put limit on minimum window resize

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