ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.30
Committed: Thu Dec 10 10:45:54 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.29: +47 -2 lines
Log Message:
created "frame" command to focus calculation

File Contents

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