ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_x11.c
Revision: 3.40
Committed: Tue Feb 8 01:03:27 2005 UTC (19 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.39: +2 -2 lines
Log Message:
Eliminated warning about return value in void function

File Contents

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