ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11dbg.c
Revision: 3.2
Committed: Mon Jul 21 22:30:18 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1, rad3R6
Changes since 3.1: +4 -4 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

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