ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.13
Committed: Thu Dec 11 09:37:43 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.12: +62 -24 lines
Log Message:
added command for returning to previous view(s)
added check to dev_view() for illegal views
fixed bug in X11 driver on window resizing

File Contents

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