ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.2
Committed: Thu Nov 20 18:04:28 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.1: +105 -14 lines
Log Message:
added pause function
added view move function
changed leaf allocation

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 #define CTRL(c) ((c)-'@')
22
23 #define GAMMA 2.2 /* default gamma correction */
24
25 #define MOVPCT 10 /* percent distance to move */
26 #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1)
27
28 #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 ButtonPressMask|ButtonReleaseMask)
39
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 static int headlocked = 0; /* lock vertical motion */
66
67 static int getpixels(), xnewcolr(), freepixels(), resizewindow(),
68 getevent(), getkey(), moveview(), getmove(), fixwindow();
69 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 /* set quadtree globals */
102 qtDepthEps = 0.02;
103 qtMinNodesiz = 1;
104 /* 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 /* 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 /* 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 do
216 getevent();
217
218 while (XQLength(ourdisplay) > 0);
219
220 return(inpresflags);
221 }
222
223
224 dev_paintr(rgb, xmin, ymin, xmax, ymax) /* fill a rectangle */
225 BYTE rgb[3];
226 int xmin, ymin, xmax, ymax;
227 {
228 unsigned long pixel;
229
230 if (!mapped)
231 return;
232 if (ncolors > 0)
233 pixel = pixval[get_pixel(rgb, xnewcolr)];
234 else
235 pixel = true_pixel(rgb);
236 XSetForeground(ourdisplay, ourgc, pixel);
237 XFillRectangle(ourdisplay, gwind,
238 ourgc, xmin, odev.vres-ymax, xmax-xmin, ymax-ymin);
239 }
240
241
242 int
243 dev_flush() /* flush output */
244 {
245 qtUpdate();
246 return(XPending(ourdisplay));
247 }
248
249
250 static
251 xnewcolr(ndx, r, g, b) /* enter a color into hardware table */
252 int ndx;
253 int r, g, b;
254 {
255 XColor xcolor;
256
257 xcolor.pixel = pixval[ndx];
258 xcolor.red = r << 8;
259 xcolor.green = g << 8;
260 xcolor.blue = b << 8;
261 xcolor.flags = DoRed|DoGreen|DoBlue;
262
263 XStoreColor(ourdisplay, ourmap, &xcolor);
264 }
265
266
267 static int
268 getpixels() /* get the color map */
269 {
270 XColor thiscolor;
271 register int i, j;
272
273 if (ncolors > 0)
274 return(ncolors);
275 if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) {
276 ourmap = DefaultColormap(ourdisplay,ourscreen);
277 goto loop;
278 }
279 newmap:
280 ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone);
281 loop:
282 for (ncolors = ourvinfo.colormap_size;
283 ncolors > ourvinfo.colormap_size/3;
284 ncolors = ncolors*.937) {
285 pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long));
286 if (pixval == NULL)
287 return(ncolors = 0);
288 if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors))
289 break;
290 free((char *)pixval);
291 pixval = NULL;
292 }
293 if (pixval == NULL) {
294 if (ourmap == DefaultColormap(ourdisplay,ourscreen))
295 goto newmap; /* try it with our map */
296 else
297 return(ncolors = 0); /* failed */
298 }
299 if (ourmap != DefaultColormap(ourdisplay,ourscreen))
300 for (i = 0; i < ncolors; i++) { /* reset black and white */
301 if (pixval[i] != ourblack && pixval[i] != ourwhite)
302 continue;
303 thiscolor.pixel = pixval[i];
304 thiscolor.flags = DoRed|DoGreen|DoBlue;
305 XQueryColor(ourdisplay,
306 DefaultColormap(ourdisplay,ourscreen),
307 &thiscolor);
308 XStoreColor(ourdisplay, ourmap, &thiscolor);
309 for (j = i; j+1 < ncolors; j++)
310 pixval[j] = pixval[j+1];
311 ncolors--;
312 i--;
313 }
314 XSetWindowColormap(ourdisplay, gwind, ourmap);
315 return(ncolors);
316 }
317
318
319 static
320 freepixels() /* free our pixels */
321 {
322 if (ncolors == 0)
323 return;
324 XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L);
325 free((char *)pixval);
326 pixval = NULL;
327 ncolors = 0;
328 if (ourmap != DefaultColormap(ourdisplay,ourscreen))
329 XFreeColormap(ourdisplay, ourmap);
330 ourmap = 0;
331 }
332
333
334 static unsigned long
335 true_pixel(rgb) /* return true pixel value for color */
336 register BYTE rgb[3];
337 {
338 register unsigned long rval;
339
340 rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask;
341 rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask;
342 rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask;
343 return(rval);
344 }
345
346
347 static
348 getevent() /* get next event */
349 {
350 XNextEvent(ourdisplay, levptr(XEvent));
351 switch (levptr(XEvent)->type) {
352 case ConfigureNotify:
353 resizewindow(levptr(XConfigureEvent));
354 break;
355 case UnmapNotify:
356 mapped = 0;
357 freepixels();
358 break;
359 case MapNotify:
360 if (ourvinfo.class == PseudoColor ||
361 ourvinfo.class == GrayScale) {
362 if (getpixels() == 0)
363 error(SYSTEM, "cannot allocate colors\n");
364 new_ctab(ncolors);
365 }
366 mapped = 1;
367 break;
368 case Expose:
369 fixwindow(levptr(XExposeEvent));
370 break;
371 case KeyPress:
372 getkey(levptr(XKeyPressedEvent));
373 break;
374 case ButtonPress:
375 getmove(levptr(XButtonPressedEvent));
376 break;
377 }
378 }
379
380
381 static
382 moveview(dx, dy, move) /* move our view */
383 int dx, dy, move;
384 {
385 VIEW nv;
386 double d;
387 register int i;
388 /* start with old view */
389 copystruct(&nv, &odev.v);
390 /* change view direction */
391 if (move) {
392 register RLEAF *lp;
393 if ((lp = qtFindLeaf(dx, dy)) == NULL)
394 return(0); /* not on window */
395 for (i = 0; i < 3; i++)
396 nv.vdir[i] = lp->wp[i] - nv.vp[i];
397 } 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
444 } while (!XCheckMaskEvent(ourdisplay,
445 ButtonReleaseMask, levptr(XEvent)));
446
447 if (!(inpresflags & DEV_NEWVIEW)) { /* do final motion */
448 whichbutton = levptr(XButtonReleasedEvent)->button;
449 wx = levptr(XButtonReleasedEvent)->x;
450 wy = levptr(XButtonReleasedEvent)->y;
451 moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton));
452 }
453
454 qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */
455 }
456
457
458 static
459 getkey(ekey) /* get input key */
460 register XKeyPressedEvent *ekey;
461 {
462 int n;
463 char buf[8];
464
465 n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
466 if (n != 1)
467 return;
468 switch (buf[0]) {
469 case 'h': /* turn on height motion lock */
470 headlocked = 1;
471 return;
472 case 'H': /* turn off height motion lock */
473 headlocked = 0;
474 return;
475 case CTRL('Z'):
476 case 'p': /* pause computation */
477 inpresflags |= DEV_WAIT;
478 return;
479 case '\n':
480 case '\r': /* release */
481 return;
482 case CTRL('D'):
483 case 'Q':
484 case 'q': /* quit the program */
485 inpresflags |= DEV_SHUTDOWN;
486 return;
487 default:
488 XBell(ourdisplay, 0);
489 return;
490 }
491 }
492
493
494 static
495 fixwindow(eexp) /* repair damage to window */
496 register XExposeEvent *eexp;
497 {
498 if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
499 odev.hres = eexp->width;
500 odev.vres = eexp->height;
501 inpresflags |= DEV_NEWSIZE;
502 }
503 qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
504 eexp->x + eexp->width, odev.vres - eexp->y);
505 }
506
507
508 static
509 resizewindow(ersz) /* resize window */
510 register XConfigureEvent *ersz;
511 {
512 if (ersz->width == odev.hres && ersz->height == odev.vres)
513 return;
514
515 if (odev.hres != 0 && odev.vres != 0) {
516 odev.v.horiz = 2.*180./PI * atan(
517 tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
518 odev.v.vert = 2.*180./PI * atan(
519 tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
520 inpresflags |= DEV_NEWVIEW;
521 }
522 odev.hres = ersz->width;
523 odev.vres = ersz->height;
524
525 inpresflags |= DEV_NEWSIZE;
526 }