ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.5
Committed: Fri Nov 21 15:11:43 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.4: +5 -2 lines
Log Message:
stopped pause function from resuming upon iconification and
interfering with full view updates

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 = 2;
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
216 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 getmove(levptr(XButtonPressedEvent));
377 break;
378 }
379 }
380
381
382 static
383 moveview(dx, dy, move) /* move our view */
384 int dx, dy, move;
385 {
386 VIEW nv;
387 double d;
388 register int i, li;
389 /* start with old view */
390 copystruct(&nv, &odev.v);
391 /* change view direction */
392 if (move) {
393 if ((li = qtFindLeaf(dx, dy)) < 0)
394 return(0); /* not on window */
395 for (i = 0; i < 3; i++)
396 nv.vdir[i] = qtL.wp[li][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 qtCompost(50);
436
437 do {
438 if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw,
439 &rootx, &rooty, &wx, &wy, &statemask))
440 break; /* on another screen */
441
442 if (!moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton)))
443 sleep(1);
444 else
445 qtUpdate();
446
447 } while (!XCheckMaskEvent(ourdisplay,
448 ButtonReleaseMask, levptr(XEvent)));
449
450 if (!(inpresflags & DEV_NEWVIEW)) { /* do final motion */
451 whichbutton = levptr(XButtonReleasedEvent)->button;
452 wx = levptr(XButtonReleasedEvent)->x;
453 wy = levptr(XButtonReleasedEvent)->y;
454 moveview(wx, odev.vres-1-wy, MOVDIR(whichbutton));
455 }
456
457 qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */
458 }
459
460
461 static
462 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 headlocked = 1;
474 return;
475 case 'H': /* turn off height motion lock */
476 headlocked = 0;
477 return;
478 case CTRL('S'):
479 case 'p': /* pause computation */
480 inpresflags |= DEV_WAIT;
481 return;
482 case CTRL('Q'):
483 case '\n':
484 case '\r': /* resume computation */
485 inpresflags |= DEV_RESUME;
486 return;
487 case CTRL('R'): /* redraw */
488 if (ncolors > 0)
489 new_ctab(ncolors);
490 qtRedraw(0, 0, odev.hres, odev.vres);
491 return;
492 case CTRL('D'):
493 case 'Q':
494 case 'q': /* quit the program */
495 inpresflags |= DEV_SHUTDOWN;
496 return;
497 default:
498 XBell(ourdisplay, 0);
499 return;
500 }
501 }
502
503
504 static
505 fixwindow(eexp) /* repair damage to window */
506 register XExposeEvent *eexp;
507 {
508 if (odev.hres == 0 || odev.vres == 0) { /* first exposure */
509 odev.hres = eexp->width;
510 odev.vres = eexp->height;
511 inpresflags |= DEV_NEWSIZE;
512 }
513 qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height,
514 eexp->x + eexp->width, odev.vres - eexp->y);
515 }
516
517
518 static
519 resizewindow(ersz) /* resize window */
520 register XConfigureEvent *ersz;
521 {
522 if (ersz->width == odev.hres && ersz->height == odev.vres)
523 return;
524
525 if (odev.hres != 0 && odev.vres != 0) {
526 odev.v.horiz = 2.*180./PI * atan(
527 tan(PI/180./2.*odev.v.horiz) * ersz->width/odev.hres );
528 odev.v.vert = 2.*180./PI * atan(
529 tan(PI/180./2.*odev.v.vert) * ersz->height/odev.vres );
530 inpresflags |= DEV_NEWVIEW;
531 }
532 odev.hres = ersz->width;
533 odev.vres = ersz->height;
534
535 inpresflags |= DEV_NEWSIZE;
536 }