ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.8
Committed: Tue Nov 25 11:21:50 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.7: +4 -0 lines
Log Message:
improved display update

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * X11 driver for holodeck display.
9     * Based on rview driver.
10     */
11    
12     #include "standard.h"
13     #include "rhd_qtree.h"
14    
15     #include <X11/Xlib.h>
16     #include <X11/cursorfont.h>
17     #include <X11/Xutil.h>
18    
19     #include "x11icon.h"
20    
21 gregl 3.2 #define CTRL(c) ((c)-'@')
22    
23 gregl 3.1 #define GAMMA 2.2 /* default gamma correction */
24    
25 gregl 3.2 #define MOVPCT 10 /* percent distance to move */
26     #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
27    
28 gregl 3.1 #define MINWIDTH 480 /* minimum graphics window width */
29     #define MINHEIGHT 400 /* minimum graphics window height */
30    
31     #define VIEWDIST 356 /* assumed viewing distance (mm) */
32    
33     #define BORWIDTH 5 /* border width */
34    
35     #define ourscreen DefaultScreen(ourdisplay)
36     #define ourroot RootWindow(ourdisplay,ourscreen)
37     #define ourmask (StructureNotifyMask|ExposureMask|KeyPressMask|\
38 gregl 3.2 ButtonPressMask|ButtonReleaseMask)
39 gregl 3.1
40     #define levptr(etype) ((etype *)&currentevent)
41    
42     struct driver odev; /* global device driver structure */
43    
44     static XEvent currentevent; /* current event */
45    
46     static int ncolors = 0; /* color table size */
47     static int mapped = 0; /* window is mapped? */
48     static unsigned long *pixval = NULL; /* allocated pixels */
49     static unsigned long ourblack=0, ourwhite=1;
50    
51     static Display *ourdisplay = NULL; /* our display */
52    
53     static XVisualInfo ourvinfo; /* our visual information */
54    
55     static Window gwind = 0; /* our graphics window */
56    
57     static GC ourgc = 0; /* our graphics context for drawing */
58    
59     static Colormap ourmap = 0; /* our color map */
60    
61     static double pwidth, pheight; /* pixel dimensions (mm) */
62    
63     static int inpresflags; /* input result flags */
64    
65 gregl 3.2 static int headlocked = 0; /* lock vertical motion */
66 gregl 3.1
67     static int getpixels(), xnewcolr(), freepixels(), resizewindow(),
68 gregl 3.2 getevent(), getkey(), moveview(), getmove(), fixwindow();
69 gregl 3.1 static unsigned long true_pixel();
70    
71    
72     static int
73     mytmflags() /* figure out tone mapping flags */
74     {
75     extern char *progname;
76     register char *cp, *tail;
77     /* find basic name */
78     for (cp = tail = progname; *cp; cp++)
79     if (*cp == '/')
80     tail = cp+1;
81     for (cp = tail; *cp && *cp != '.'; cp++)
82     ;
83     if (cp-tail == 3 && !strncmp(tail, "x11", 3))
84     return(TM_F_CAMERA);
85     if (cp-tail == 4 && !strncmp(tail, "x11h", 4))
86     return(TM_F_HUMAN);
87     error(USER, "illegal driver name");
88     }
89    
90    
91     dev_open(id) /* initialize X11 driver */
92     char *id;
93     {
94     extern char *getenv();
95     char *gv;
96     double gamval = GAMMA;
97     int nplanes;
98     XSetWindowAttributes ourwinattr;
99     XWMHints ourxwmhints;
100     XSizeHints oursizhints;
101 gregl 3.2 /* set quadtree globals */
102     qtDepthEps = 0.02;
103 gregl 3.4 qtMinNodesiz = 2;
104 gregl 3.1 /* open display server */
105     ourdisplay = XOpenDisplay(NULL);
106     if (ourdisplay == NULL)
107     error(USER, "cannot open X-windows; DISPLAY variable set?\n");
108     /* find a usable visual */
109     nplanes = DisplayPlanes(ourdisplay, ourscreen);
110     if (XMatchVisualInfo(ourdisplay,ourscreen,
111     nplanes>12?nplanes:24,TrueColor,&ourvinfo) ||
112     XMatchVisualInfo(ourdisplay,ourscreen,
113     nplanes>12?nplanes:24,DirectColor,&ourvinfo)) {
114     ourblack = 0;
115     ourwhite = ourvinfo.red_mask |
116     ourvinfo.green_mask |
117     ourvinfo.blue_mask ;
118     } else {
119     if (nplanes < 4)
120     error(INTERNAL, "not enough colors\n");
121     if (!XMatchVisualInfo(ourdisplay,ourscreen,
122     nplanes,PseudoColor,&ourvinfo) &&
123     !XMatchVisualInfo(ourdisplay,ourscreen,
124     nplanes,GrayScale,&ourvinfo))
125     error(INTERNAL, "unsupported visual type\n");
126     ourblack = BlackPixel(ourdisplay,ourscreen);
127     ourwhite = WhitePixel(ourdisplay,ourscreen);
128     }
129     /* set gamma and tone mapping */
130     if ((gv = XGetDefault(ourdisplay, "radiance", "gamma")) != NULL
131     || (gv = getenv("DISPLAY_GAMMA")) != NULL)
132     gamval = atof(gv);
133     if (tmInit(mytmflags(), stdprims, gamval) == NULL)
134     error(SYSTEM, "not enough memory in dev_open");
135     /* open window */
136     ourwinattr.background_pixel = ourblack;
137     ourwinattr.border_pixel = ourblack;
138     ourwinattr.event_mask = ourmask;
139     /* this is stupid */
140     ourwinattr.colormap = XCreateColormap(ourdisplay, ourroot,
141     ourvinfo.visual, AllocNone);
142     gwind = XCreateWindow(ourdisplay, ourroot, 0, 0,
143     DisplayWidth(ourdisplay,ourscreen)-2*BORWIDTH,
144     DisplayHeight(ourdisplay,ourscreen)-2*BORWIDTH,
145     BORWIDTH, ourvinfo.depth, InputOutput, ourvinfo.visual,
146     CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &ourwinattr);
147     if (gwind == 0)
148     error(SYSTEM, "cannot create window\n");
149     XStoreName(ourdisplay, gwind, id);
150     /* get graphics context */
151     ourgc = XCreateGC(ourdisplay, gwind, 0, NULL);
152     /* set window manager hints */
153     ourxwmhints.flags = InputHint|IconPixmapHint;
154     ourxwmhints.input = True;
155     ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay,
156     gwind, x11icon_bits, x11icon_width, x11icon_height);
157     XSetWMHints(ourdisplay, gwind, &ourxwmhints);
158     oursizhints.min_width = MINWIDTH;
159     oursizhints.min_height = MINHEIGHT;
160     oursizhints.flags = PMinSize;
161     XSetNormalHints(ourdisplay, gwind, &oursizhints);
162     /* map the window and get its size */
163     XMapWindow(ourdisplay, gwind);
164     dev_input();
165 gregl 3.2 /* allocate our leaf pile */
166     if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) *
167     DisplayHeight(ourdisplay,ourscreen) /
168     (qtMinNodesiz*qtMinNodesiz)))
169     error(SYSTEM, "insufficient memory for leaf storage");
170    
171 gregl 3.1 /* figure out sensible view */
172     pwidth = (double)DisplayWidthMM(ourdisplay, ourscreen) /
173     DisplayWidth(ourdisplay, ourscreen);
174     pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) /
175     DisplayHeight(ourdisplay, ourscreen);
176     copystruct(&odev.v, &stdview);
177     odev.name = id;
178     odev.v.type = VT_PER;
179     odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres);
180     odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres);
181     odev.ifd = ConnectionNumber(ourdisplay);
182     }
183    
184    
185     dev_close() /* close our display */
186     {
187     freepixels();
188     XFreeGC(ourdisplay, ourgc);
189     XDestroyWindow(ourdisplay, gwind);
190     gwind = 0;
191     ourgc = 0;
192     XCloseDisplay(ourdisplay);
193     ourdisplay = NULL;
194     qtFreeLeaves();
195     tmDone(NULL);
196     odev.v.type = 0;
197     odev.hres = odev.vres = 0;
198     odev.ifd = -1;
199     }
200    
201    
202     dev_view(nv) /* assign new driver view */
203     VIEW *nv;
204     {
205     if (nv != &odev.v)
206     copystruct(&odev.v, nv);
207     qtReplant();
208     }
209    
210    
211     int
212     dev_input() /* get X11 input */
213     {
214     inpresflags = 0;
215 gregl 3.5
216 gregl 3.1 do
217     getevent();
218    
219     while (XQLength(ourdisplay) > 0);
220    
221     return(inpresflags);
222     }
223    
224    
225     dev_paintr(rgb, xmin, ymin, xmax, ymax) /* fill a rectangle */
226     BYTE rgb[3];
227     int xmin, ymin, xmax, ymax;
228     {
229     unsigned long pixel;
230    
231     if (!mapped)
232     return;
233     if (ncolors > 0)
234     pixel = pixval[get_pixel(rgb, xnewcolr)];
235     else
236     pixel = true_pixel(rgb);
237     XSetForeground(ourdisplay, ourgc, pixel);
238     XFillRectangle(ourdisplay, gwind,
239     ourgc, xmin, odev.vres-ymax, xmax-xmin, ymax-ymin);
240     }
241    
242    
243     int
244     dev_flush() /* flush output */
245     {
246     qtUpdate();
247     return(XPending(ourdisplay));
248     }
249    
250    
251     static
252     xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
253     int ndx;
254     int r, g, b;
255     {
256     XColor xcolor;
257    
258     xcolor.pixel = pixval[ndx];
259     xcolor.red = r << 8;
260     xcolor.green = g << 8;
261     xcolor.blue = b << 8;
262     xcolor.flags = DoRed|DoGreen|DoBlue;
263    
264     XStoreColor(ourdisplay, ourmap, &xcolor);
265     }
266    
267    
268     static int
269     getpixels() /* get the color map */
270     {
271     XColor thiscolor;
272     register int i, j;
273    
274     if (ncolors > 0)
275     return(ncolors);
276     if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
277     ourmap = DefaultColormap(ourdisplay,ourscreen);
278     goto loop;
279     }
280     newmap:
281     ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
282     loop:
283     for (ncolors = ourvinfo.colormap_size;
284     ncolors > ourvinfo.colormap_size/3;
285     ncolors = ncolors*.937) {
286     pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
287     if (pixval == NULL)
288     return(ncolors = 0);
289     if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
290     break;
291     free((char *)pixval);
292     pixval = NULL;
293     }
294     if (pixval == NULL) {
295     if (ourmap == DefaultColormap(ourdisplay,ourscreen))
296     goto newmap; /* try it with our map */
297     else
298     return(ncolors = 0); /* failed */
299     }
300     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
301     for (i = 0; i < ncolors; i++) { /* reset black and white */
302     if (pixval[i] != ourblack && pixval[i] != ourwhite)
303     continue;
304     thiscolor.pixel = pixval[i];
305     thiscolor.flags = DoRed|DoGreen|DoBlue;
306     XQueryColor(ourdisplay,
307     DefaultColormap(ourdisplay,ourscreen),
308     &thiscolor);
309     XStoreColor(ourdisplay, ourmap, &thiscolor);
310     for (j = i; j+1 < ncolors; j++)
311     pixval[j] = pixval[j+1];
312     ncolors--;
313     i--;
314     }
315     XSetWindowColormap(ourdisplay, gwind, ourmap);
316     return(ncolors);
317     }
318    
319    
320     static
321     freepixels() /* free our pixels */
322     {
323     if (ncolors == 0)
324     return;
325     XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
326     free((char *)pixval);
327     pixval = NULL;
328     ncolors = 0;
329     if (ourmap != DefaultColormap(ourdisplay,ourscreen))
330     XFreeColormap(ourdisplay, ourmap);
331     ourmap = 0;
332     }
333    
334    
335     static unsigned long
336     true_pixel(rgb) /* return true pixel value for color */
337     register BYTE rgb[3];
338     {
339     register unsigned long rval;
340    
341     rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
342     rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
343     rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
344     return(rval);
345     }
346    
347    
348     static
349     getevent() /* get next event */
350     {
351     XNextEvent(ourdisplay, levptr(XEvent));
352     switch (levptr(XEvent)->type) {
353     case ConfigureNotify:
354     resizewindow(levptr(XConfigureEvent));
355     break;
356     case UnmapNotify:
357     mapped = 0;
358     freepixels();
359     break;
360     case MapNotify:
361     if (ourvinfo.class == PseudoColor ||
362     ourvinfo.class == GrayScale) {
363     if (getpixels() == 0)
364     error(SYSTEM, "cannot allocate colors\n");
365     new_ctab(ncolors);
366     }
367     mapped = 1;
368     break;
369     case Expose:
370     fixwindow(levptr(XExposeEvent));
371     break;
372     case KeyPress:
373     getkey(levptr(XKeyPressedEvent));
374     break;
375     case ButtonPress:
376 gregl 3.2 getmove(levptr(XButtonPressedEvent));
377 gregl 3.1 break;
378     }
379     }
380    
381    
382     static
383 gregl 3.2 moveview(dx, dy, move) /* move our view */
384     int dx, dy, move;
385     {
386     VIEW nv;
387     double d;
388 gregl 3.4 register int i, li;
389 gregl 3.2 /* start with old view */
390     copystruct(&nv, &odev.v);
391     /* change view direction */
392     if (move) {
393 gregl 3.4 if ((li = qtFindLeaf(dx, dy)) < 0)
394 gregl 3.2 return(0); /* not on window */
395     for (i = 0; i < 3; i++)
396 gregl 3.4 nv.vdir[i] = qtL.wp[li][i] - nv.vp[i];
397 gregl 3.2 } else {
398     if (viewray(nv.vp, nv.vdir, &odev.v,
399     (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY)
400     return(0); /* outside view */
401     }
402     /* move viewpoint */
403     if (move > 0)
404     for (i = 0; i < 3; i++)
405     nv.vp[i] += MOVPCT/100. * nv.vdir[i];
406     else if (move < 0)
407     for (i = 0; i < 3; i++)
408     nv.vp[i] -= MOVPCT/100. * nv.vdir[i];
409     if (move && headlocked) {
410     d = 0; /* bring head back to same height */
411     for (i = 0; i < 3; i++)
412     d += odev.v.vup[i] * (odev.v.vp[i] - nv.vp[i]);
413     for (i = 0; i < 3; i++)
414     nv.vp[i] += d * odev.v.vup[i];
415     }
416     if (setview(&nv) != NULL)
417     return(0); /* illegal view */
418     dev_view(&nv);
419     inpresflags |= DEV_NEWVIEW;
420     return(1);
421     }
422    
423    
424     static
425     getmove(ebut) /* get view change */
426     XButtonPressedEvent *ebut;
427     {
428     int whichbutton = ebut->button;
429     int oldnodesiz = qtMinNodesiz;
430     Window rootw, childw;
431     int rootx, rooty, wx, wy;
432     unsigned int statemask;
433    
434     qtMinNodesiz = 16; /* for quicker update */
435    
436     do {
437     if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
438     &rootx, &rooty, &wx, &wy, &statemask))
439     break; /* on another screen */
440    
441     if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton)))
442     sleep(1);
443 gregl 3.4 else
444     qtUpdate();
445 gregl 3.2
446     } while (!XCheckMaskEvent(ourdisplay,
447     ButtonReleaseMask, levptr(XEvent)));
448    
449     if (!(inpresflags & DEV_NEWVIEW)) { /* do final motion */
450     whichbutton = levptr(XButtonReleasedEvent)->button;
451     wx = levptr(XButtonReleasedEvent)->x;
452     wy = levptr(XButtonReleasedEvent)->y;
453     moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton));
454 gregl 3.8 dev_flush();
455 gregl 3.2 }
456    
457     qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */
458     }
459    
460    
461     static
462 gregl 3.1 getkey(ekey) /* get input key */
463     register XKeyPressedEvent *ekey;
464     {
465     int n;
466     char buf[8];
467    
468     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
469     if (n != 1)
470     return;
471     switch (buf[0]) {
472     case 'h': /* turn on height motion lock */
473 gregl 3.2 headlocked = 1;
474 gregl 3.1 return;
475     case 'H': /* turn off height motion lock */
476 gregl 3.2 headlocked = 0;
477 gregl 3.1 return;
478 gregl 3.5 case CTRL('S'):
479 gregl 3.2 case 'p': /* pause computation */
480     inpresflags |= DEV_WAIT;
481     return;
482 gregl 3.7 case 'v': /* spit out view */
483     fputs(VIEWSTR, stderr);
484     fprintview(&odev.v, stderr);
485     fputc('\n', stderr);
486     return;
487 gregl 3.5 case CTRL('Q'):
488 gregl 3.2 case '\n':
489 gregl 3.5 case '\r': /* resume computation */
490     inpresflags |= DEV_RESUME;
491 gregl 3.2 return;
492 gregl 3.7 case CTRL('R'): /* redraw screen */
493     if (ncolors > 0)
494     new_ctab(ncolors);
495     qtRedraw(0, 0, odev.hres, odev.vres);
496     return;
497     case CTRL('L'): /* refresh from server */
498 gregl 3.8 if (inpresflags & DEV_REDRAW)
499     return;
500 gregl 3.7 XClearWindow(ourdisplay, gwind);
501 gregl 3.8 XFlush(ourdisplay);
502 gregl 3.6 qtCompost(100); /* unload the old tree */
503 gregl 3.3 if (ncolors > 0)
504     new_ctab(ncolors);
505 gregl 3.6 inpresflags |= DEV_REDRAW; /* resend values from server */
506 gregl 3.3 return;
507 gregl 3.2 case CTRL('D'):
508 gregl 3.1 case 'Q':
509     case 'q': /* quit the program */
510     inpresflags |= DEV_SHUTDOWN;
511     return;
512     default:
513     XBell(ourdisplay, 0);
514     return;
515     }
516     }
517    
518    
519     static
520     fixwindow(eexp) /* repair damage to window */
521     register XExposeEvent *eexp;
522     {
523     if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
524     odev.hres = eexp->width;
525     odev.vres = eexp->height;
526     inpresflags |= DEV_NEWSIZE;
527     }
528     qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
529     eexp->x + eexp->width, odev.vres - eexp->y);
530     }
531    
532    
533     static
534     resizewindow(ersz) /* resize window */
535     register XConfigureEvent *ersz;
536     {
537     if (ersz->width == odev.hres && ersz->height == odev.vres)
538     return;
539    
540     if (odev.hres != 0 && odev.vres != 0) {
541     odev.v.horiz = 2.*180./PI * atan(
542     tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
543     odev.v.vert = 2.*180./PI * atan(
544     tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
545     inpresflags |= DEV_NEWVIEW;
546     }
547     odev.hres = ersz->width;
548     odev.vres = ersz->height;
549    
550     inpresflags |= DEV_NEWSIZE;
551     }