1 |
/* Copyright (c) 1998 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 <X11/Xlib.h> |
14 |
#include <X11/cursorfont.h> |
15 |
#include <X11/Xutil.h> |
16 |
#include "rhd_qtree.h" |
17 |
#include "x11icon.h" |
18 |
|
19 |
#ifndef RAYQLEN |
20 |
#define RAYQLEN 50000 /* max. rays to queue before flush */ |
21 |
#endif |
22 |
|
23 |
#ifndef FEQ |
24 |
#define FEQ(a,b) ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) |
25 |
#endif |
26 |
|
27 |
#define GAMMA 2.2 /* default gamma correction */ |
28 |
|
29 |
#define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) |
30 |
|
31 |
#define MOVPCT 7 /* percent distance to move /frame */ |
32 |
#define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) |
33 |
#define MOVDEG (-5) /* degrees to orbit CW/down /frame */ |
34 |
#define MOVORB(s) ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) |
35 |
|
36 |
#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 |
ButtonPressMask|ButtonReleaseMask) |
47 |
|
48 |
#define levptr(etype) ((etype *)¤tevent) |
49 |
|
50 |
struct driver odev; /* global device driver structure */ |
51 |
|
52 |
char odev_args[64]; /* command arguments */ |
53 |
|
54 |
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 |
static int headlocked = 0; /* lock vertical motion */ |
72 |
|
73 |
static int getpixels(), xnewcolr(), freepixels(), resizewindow(), getframe(), |
74 |
getevent(), getkey(), moveview(), getmove(), fixwindow(); |
75 |
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 |
return(TM_F_CAMERA|TM_F_NOSTDERR); |
91 |
if (cp-tail == 4 && !strncmp(tail, "x11h", 4)) |
92 |
return(TM_F_HUMAN|TM_F_NOSTDERR); |
93 |
error(USER, "illegal driver name"); |
94 |
} |
95 |
|
96 |
|
97 |
dev_open(id) /* initialize X11 driver */ |
98 |
char *id; |
99 |
{ |
100 |
extern char *getenv(); |
101 |
static RGBPRIMS myprims = STDPRIMS; |
102 |
char *ev; |
103 |
double gamval = GAMMA; |
104 |
RGBPRIMP dpri = stdprims; |
105 |
int nplanes; |
106 |
XSetWindowAttributes ourwinattr; |
107 |
XWMHints ourxwmhints; |
108 |
XSizeHints oursizhints; |
109 |
/* set quadtree globals */ |
110 |
qtMinNodesiz = 2; |
111 |
/* 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 |
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 |
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 |
/* 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 |
/* map the window and get its size */ |
184 |
XMapWindow(ourdisplay, gwind); |
185 |
dev_input(); /* sets size and view angles */ |
186 |
/* allocate our leaf pile */ |
187 |
if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) * |
188 |
DisplayHeight(ourdisplay,ourscreen) * 3 / |
189 |
(qtMinNodesiz*qtMinNodesiz*2))) |
190 |
error(SYSTEM, "insufficient memory for leaf storage"); |
191 |
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 |
|
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 |
int |
224 |
dev_view(nv) /* assign new driver view */ |
225 |
VIEW *nv; |
226 |
{ |
227 |
if (nv->type == VT_PAR || /* check view legality */ |
228 |
nv->horiz > 160. || nv->vert > 160.) { |
229 |
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 |
if (nv != &odev.v) { |
241 |
if (!FEQ(nv->horiz,odev.v.horiz) || /* resize window? */ |
242 |
!FEQ(nv->vert,odev.v.vert)) { |
243 |
int dw = DisplayWidth(ourdisplay,ourscreen); |
244 |
int dh = DisplayHeight(ourdisplay,ourscreen); |
245 |
|
246 |
dw -= 25; /* for window frame */ |
247 |
dh -= 50; |
248 |
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 |
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 |
XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); |
261 |
dev_input(); /* wait for resize event */ |
262 |
} |
263 |
copystruct(&odev.v, nv); |
264 |
} |
265 |
qtReplant(); |
266 |
return(1); |
267 |
} |
268 |
|
269 |
|
270 |
dev_section(ofn) /* add octree for geometry rendering */ |
271 |
char *ofn; |
272 |
{ |
273 |
/* unimplemented */ |
274 |
} |
275 |
|
276 |
|
277 |
dev_auxcom(cmd, args) /* process an auxiliary command */ |
278 |
char *cmd, *args; |
279 |
{ |
280 |
sprintf(errmsg, "%s: unknown command", cmd); |
281 |
error(COMMAND, errmsg); |
282 |
} |
283 |
|
284 |
|
285 |
VIEW * |
286 |
dev_auxview(n, hvres) /* return nth auxiliary view */ |
287 |
int n; |
288 |
int hvres[2]; |
289 |
{ |
290 |
if (n) |
291 |
return(NULL); |
292 |
hvres[0] = odev.hres; hvres[1] = odev.vres; |
293 |
return(&odev.v); |
294 |
} |
295 |
|
296 |
|
297 |
int |
298 |
dev_input() /* get X11 input */ |
299 |
{ |
300 |
inpresflags = 0; |
301 |
|
302 |
do |
303 |
getevent(); |
304 |
|
305 |
while (XPending(ourdisplay) > 0); |
306 |
|
307 |
odev.inpready = 0; |
308 |
|
309 |
return(inpresflags); |
310 |
} |
311 |
|
312 |
|
313 |
dev_paintr(rgb, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
314 |
BYTE rgb[3]; |
315 |
int xmin, ymin, xmax, ymax; |
316 |
{ |
317 |
unsigned long pixel; |
318 |
|
319 |
if (!mapped) |
320 |
return; |
321 |
if (ncolors > 0) |
322 |
pixel = pixval[get_pixel(rgb, xnewcolr)]; |
323 |
else |
324 |
pixel = true_pixel(rgb); |
325 |
XSetForeground(ourdisplay, ourgc, pixel); |
326 |
XFillRectangle(ourdisplay, gwind, |
327 |
ourgc, xmin, odev.vres-ymax, xmax-xmin, ymax-ymin); |
328 |
} |
329 |
|
330 |
|
331 |
int |
332 |
dev_flush() /* flush output */ |
333 |
{ |
334 |
qtUpdate(); |
335 |
rayqleft = RAYQLEN; |
336 |
return(odev.inpready = XPending(ourdisplay)); |
337 |
} |
338 |
|
339 |
|
340 |
static |
341 |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
342 |
int ndx; |
343 |
int r, g, b; |
344 |
{ |
345 |
XColor xcolor; |
346 |
|
347 |
xcolor.pixel = pixval[ndx]; |
348 |
xcolor.red = r << 8; |
349 |
xcolor.green = g << 8; |
350 |
xcolor.blue = b << 8; |
351 |
xcolor.flags = DoRed|DoGreen|DoBlue; |
352 |
|
353 |
XStoreColor(ourdisplay, ourmap, &xcolor); |
354 |
} |
355 |
|
356 |
|
357 |
static int |
358 |
getpixels() /* get the color map */ |
359 |
{ |
360 |
XColor thiscolor; |
361 |
register int i, j; |
362 |
|
363 |
if (ncolors > 0) |
364 |
return(ncolors); |
365 |
if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) { |
366 |
ourmap = DefaultColormap(ourdisplay,ourscreen); |
367 |
goto loop; |
368 |
} |
369 |
newmap: |
370 |
ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone); |
371 |
loop: |
372 |
for (ncolors = ourvinfo.colormap_size; |
373 |
ncolors > ourvinfo.colormap_size/3; |
374 |
ncolors = ncolors*.937) { |
375 |
pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long)); |
376 |
if (pixval == NULL) |
377 |
return(ncolors = 0); |
378 |
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) |
379 |
break; |
380 |
free((char *)pixval); |
381 |
pixval = NULL; |
382 |
} |
383 |
if (pixval == NULL) { |
384 |
if (ourmap == DefaultColormap(ourdisplay,ourscreen)) |
385 |
goto newmap; /* try it with our map */ |
386 |
else |
387 |
return(ncolors = 0); /* failed */ |
388 |
} |
389 |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
390 |
for (i = 0; i < ncolors; i++) { /* reset black and white */ |
391 |
if (pixval[i] != ourblack && pixval[i] != ourwhite) |
392 |
continue; |
393 |
thiscolor.pixel = pixval[i]; |
394 |
thiscolor.flags = DoRed|DoGreen|DoBlue; |
395 |
XQueryColor(ourdisplay, |
396 |
DefaultColormap(ourdisplay,ourscreen), |
397 |
&thiscolor); |
398 |
XStoreColor(ourdisplay, ourmap, &thiscolor); |
399 |
for (j = i; j+1 < ncolors; j++) |
400 |
pixval[j] = pixval[j+1]; |
401 |
ncolors--; |
402 |
i--; |
403 |
} |
404 |
XSetWindowColormap(ourdisplay, gwind, ourmap); |
405 |
return(ncolors); |
406 |
} |
407 |
|
408 |
|
409 |
static |
410 |
freepixels() /* free our pixels */ |
411 |
{ |
412 |
if (ncolors == 0) |
413 |
return; |
414 |
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
415 |
free((char *)pixval); |
416 |
pixval = NULL; |
417 |
ncolors = 0; |
418 |
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
419 |
XFreeColormap(ourdisplay, ourmap); |
420 |
ourmap = 0; |
421 |
} |
422 |
|
423 |
|
424 |
static unsigned long |
425 |
true_pixel(rgb) /* return true pixel value for color */ |
426 |
register BYTE rgb[3]; |
427 |
{ |
428 |
register unsigned long rval; |
429 |
|
430 |
rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask; |
431 |
rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask; |
432 |
rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask; |
433 |
return(rval); |
434 |
} |
435 |
|
436 |
|
437 |
static |
438 |
getevent() /* get next event */ |
439 |
{ |
440 |
XNextEvent(ourdisplay, levptr(XEvent)); |
441 |
switch (levptr(XEvent)->type) { |
442 |
case ConfigureNotify: |
443 |
resizewindow(levptr(XConfigureEvent)); |
444 |
break; |
445 |
case UnmapNotify: |
446 |
mapped = 0; |
447 |
freepixels(); |
448 |
break; |
449 |
case MapNotify: |
450 |
if (ourvinfo.class == PseudoColor || |
451 |
ourvinfo.class == GrayScale) { |
452 |
if (getpixels() == 0) |
453 |
error(SYSTEM, "cannot allocate colors\n"); |
454 |
new_ctab(ncolors); |
455 |
} |
456 |
mapped = 1; |
457 |
break; |
458 |
case Expose: |
459 |
fixwindow(levptr(XExposeEvent)); |
460 |
break; |
461 |
case KeyPress: |
462 |
getkey(levptr(XKeyPressedEvent)); |
463 |
break; |
464 |
case ButtonPress: |
465 |
if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) |
466 |
getframe(levptr(XButtonPressedEvent)); |
467 |
else |
468 |
getmove(levptr(XButtonPressedEvent)); |
469 |
break; |
470 |
} |
471 |
} |
472 |
|
473 |
|
474 |
static |
475 |
ilclip(dp, wp) /* clip world coordinates to device */ |
476 |
int dp[2][2]; |
477 |
FVECT wp[2]; |
478 |
{ |
479 |
static FVECT vmin = {0.,0.,0.}, vmax = {1.-FTINY,1.-FTINY,FHUGE}; |
480 |
FVECT wpc[2], ip[2]; |
481 |
double d, d0, d1; |
482 |
/* check for points behind view */ |
483 |
d = DOT(odev.v.vp, odev.v.vdir); |
484 |
d0 = DOT(wp[0], odev.v.vdir) - d; |
485 |
d1 = DOT(wp[1], odev.v.vdir) - d; |
486 |
/* work on copy of world points */ |
487 |
if (d0 <= d1) { |
488 |
VCOPY(wpc[0], wp[0]); VCOPY(wpc[1], wp[1]); |
489 |
} else { |
490 |
d = d0; d0 = d1; d1 = d; |
491 |
VCOPY(wpc[1], wp[0]); VCOPY(wpc[0], wp[1]); |
492 |
} |
493 |
if (d0 <= FTINY) { |
494 |
if (d1 <= FTINY) return(0); |
495 |
VSUB(wpc[0], wpc[0], wpc[1]); |
496 |
d = .99*d1/(d1-d0); |
497 |
VSUM(wpc[0], wpc[1], wpc[0], d); |
498 |
} |
499 |
/* get view coordinates and clip to window */ |
500 |
viewloc(ip[0], &odev.v, wpc[0]); |
501 |
viewloc(ip[1], &odev.v, wpc[1]); |
502 |
if (!clip(ip[0], ip[1], vmin, vmax)) |
503 |
return(0); |
504 |
dp[0][0] = ip[0][0]*odev.hres; |
505 |
dp[0][1] = ip[0][1]*odev.vres; |
506 |
dp[1][0] = ip[1][0]*odev.hres; |
507 |
dp[1][1] = ip[1][1]*odev.vres; |
508 |
return(1); |
509 |
} |
510 |
|
511 |
|
512 |
static |
513 |
draw3dline(wp) /* draw 3d line in world coordinates */ |
514 |
FVECT wp[2]; |
515 |
{ |
516 |
int dp[2][2]; |
517 |
|
518 |
if (!ilclip(dp, wp)) |
519 |
return; |
520 |
XDrawLine(ourdisplay, gwind, ourgc, |
521 |
dp[0][0], odev.vres-1 - dp[0][1], |
522 |
dp[1][0], odev.vres-1 - dp[1][1]); |
523 |
} |
524 |
|
525 |
|
526 |
static |
527 |
draw_grids() /* draw holodeck section grids */ |
528 |
{ |
529 |
static BYTE gridrgb[3] = {0x0, 0xff, 0xff}; |
530 |
unsigned long pixel; |
531 |
|
532 |
if (ncolors > 0) |
533 |
pixel = pixval[get_pixel(gridrgb, xnewcolr)]; |
534 |
else |
535 |
pixel = true_pixel(gridrgb); |
536 |
XSetForeground(ourdisplay, ourgc, pixel); |
537 |
/* draw each grid line */ |
538 |
gridlines(draw3dline); |
539 |
} |
540 |
|
541 |
|
542 |
static |
543 |
moveview(dx, dy, mov, orb) /* move our view */ |
544 |
int dx, dy, mov, orb; |
545 |
{ |
546 |
VIEW nv; |
547 |
FVECT odir, v1; |
548 |
double d; |
549 |
register int li; |
550 |
/* start with old view */ |
551 |
copystruct(&nv, &odev.v); |
552 |
/* change view direction */ |
553 |
if (mov | orb) { |
554 |
if ((li = qtFindLeaf(dx, dy)) < 0) |
555 |
return(0); /* not on window */ |
556 |
VSUM(odir, qtL.wp[li], nv.vp, -1.); |
557 |
} else { |
558 |
if (viewray(nv.vp, nv.vdir, &odev.v, |
559 |
(dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) |
560 |
return(0); /* outside view */ |
561 |
} |
562 |
if (orb && mov) { /* orbit left/right */ |
563 |
spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); |
564 |
VSUM(nv.vp, qtL.wp[li], odir, -1.); |
565 |
spinvector(nv.vdir, nv.vdir, nv.vup, d); |
566 |
} else if (orb) { /* orbit up/down */ |
567 |
fcross(v1, odir, nv.vup); |
568 |
if (normalize(v1) == 0.) |
569 |
return(0); |
570 |
spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); |
571 |
VSUM(nv.vp, qtL.wp[li], odir, -1.); |
572 |
spinvector(nv.vdir, nv.vdir, v1, d); |
573 |
} else if (mov) { /* move forward/backward */ |
574 |
d = MOVPCT/100. * mov; |
575 |
VSUM(nv.vp, nv.vp, odir, d); |
576 |
} |
577 |
if (!mov ^ !orb && headlocked) { /* restore head height */ |
578 |
VSUM(v1, odev.v.vp, nv.vp, -1.); |
579 |
d = DOT(v1, odev.v.vup); |
580 |
VSUM(nv.vp, nv.vp, odev.v.vup, d); |
581 |
} |
582 |
if (setview(&nv) != NULL) |
583 |
return(0); /* illegal view */ |
584 |
dev_view(&nv); |
585 |
inpresflags |= DFL(DC_SETVIEW); |
586 |
return(1); |
587 |
} |
588 |
|
589 |
|
590 |
static |
591 |
getframe(ebut) /* get focus frame */ |
592 |
XButtonPressedEvent *ebut; |
593 |
{ |
594 |
int startx = ebut->x, starty = ebut->y; |
595 |
int endx, endy; |
596 |
|
597 |
XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); |
598 |
endx = levptr(XButtonReleasedEvent)->x; |
599 |
endy = levptr(XButtonReleasedEvent)->y; |
600 |
if (endx == startx | endy == starty) { |
601 |
XBell(ourdisplay, 0); |
602 |
return; |
603 |
} |
604 |
if (endx < startx) {register int c = endx; endx = startx; startx = c;} |
605 |
if (endy < starty) {register int c = endy; endy = starty; starty = c;} |
606 |
sprintf(odev_args, "%.3f %.3f %.3f %.3f", |
607 |
(startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres, |
608 |
(endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres); |
609 |
inpresflags |= DFL(DC_FOCUS); |
610 |
} |
611 |
|
612 |
|
613 |
static |
614 |
getmove(ebut) /* get view change */ |
615 |
XButtonPressedEvent *ebut; |
616 |
{ |
617 |
int movdir = MOVDIR(ebut->button); |
618 |
int movorb = MOVORB(ebut->state); |
619 |
int oldnodesiz = qtMinNodesiz; |
620 |
Window rootw, childw; |
621 |
int rootx, rooty, wx, wy; |
622 |
unsigned int statemask; |
623 |
|
624 |
qtMinNodesiz = 16; /* for quicker update */ |
625 |
XNoOp(ourdisplay); |
626 |
|
627 |
while (!XCheckMaskEvent(ourdisplay, |
628 |
ButtonReleaseMask, levptr(XEvent))) { |
629 |
|
630 |
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
631 |
&rootx, &rooty, &wx, &wy, &statemask)) |
632 |
break; /* on another screen */ |
633 |
|
634 |
if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { |
635 |
sleep(1); |
636 |
continue; |
637 |
} |
638 |
XClearWindow(ourdisplay, gwind); |
639 |
qtUpdate(); |
640 |
draw_grids(); |
641 |
} |
642 |
if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ |
643 |
movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); |
644 |
wx = levptr(XButtonReleasedEvent)->x; |
645 |
wy = levptr(XButtonReleasedEvent)->y; |
646 |
moveview(wx, odev.vres-1-wy, movdir, movorb); |
647 |
} |
648 |
dev_flush(); |
649 |
|
650 |
qtMinNodesiz = oldnodesiz; /* restore quadtree resolution */ |
651 |
} |
652 |
|
653 |
|
654 |
static |
655 |
getkey(ekey) /* get input key */ |
656 |
register XKeyPressedEvent *ekey; |
657 |
{ |
658 |
Window rootw, childw; |
659 |
int rootx, rooty, wx, wy; |
660 |
unsigned int statemask; |
661 |
int n; |
662 |
char buf[8]; |
663 |
|
664 |
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
665 |
if (n != 1) |
666 |
return; |
667 |
switch (buf[0]) { |
668 |
case 'h': /* turn on height motion lock */ |
669 |
headlocked = 1; |
670 |
return; |
671 |
case 'H': /* turn off height motion lock */ |
672 |
headlocked = 0; |
673 |
return; |
674 |
case 'l': /* retrieve last view */ |
675 |
inpresflags |= DFL(DC_LASTVIEW); |
676 |
return; |
677 |
case 'f': /* frame view position */ |
678 |
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
679 |
&rootx, &rooty, &wx, &wy, &statemask)) |
680 |
return; /* on another screen */ |
681 |
sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres, |
682 |
1.-(wy+.5)/odev.vres); |
683 |
inpresflags |= DFL(DC_FOCUS); |
684 |
return; |
685 |
case 'F': /* unfocus */ |
686 |
odev_args[0] = '\0'; |
687 |
inpresflags |= DFL(DC_FOCUS); |
688 |
return; |
689 |
case 'p': /* pause computation */ |
690 |
inpresflags |= DFL(DC_PAUSE); |
691 |
return; |
692 |
case 'v': /* spit out view */ |
693 |
inpresflags |= DFL(DC_GETVIEW); |
694 |
return; |
695 |
case '\n': |
696 |
case '\r': /* resume computation */ |
697 |
inpresflags |= DFL(DC_RESUME); |
698 |
return; |
699 |
case CTRL('R'): /* redraw screen */ |
700 |
if (ncolors > 0) |
701 |
new_ctab(ncolors); |
702 |
qtRedraw(0, 0, odev.hres, odev.vres); |
703 |
return; |
704 |
case CTRL('L'): /* refresh from server */ |
705 |
if (inpresflags & DFL(DC_REDRAW)) |
706 |
return; |
707 |
XClearWindow(ourdisplay, gwind); |
708 |
draw_grids(); |
709 |
XFlush(ourdisplay); |
710 |
qtCompost(100); /* unload the old tree */ |
711 |
if (ncolors > 0) |
712 |
new_ctab(ncolors); |
713 |
inpresflags |= DFL(DC_REDRAW); /* resend values from server */ |
714 |
rayqleft = 0; /* hold off update */ |
715 |
return; |
716 |
case 'K': /* kill rtrace process(es) */ |
717 |
inpresflags |= DFL(DC_KILL); |
718 |
break; |
719 |
case 'R': /* restart rtrace */ |
720 |
inpresflags |= DFL(DC_RESTART); |
721 |
break; |
722 |
case 'C': /* clobber holodeck */ |
723 |
inpresflags |= DFL(DC_CLOBBER); |
724 |
break; |
725 |
case 'q': /* quit the program */ |
726 |
inpresflags |= DFL(DC_QUIT); |
727 |
return; |
728 |
default: |
729 |
XBell(ourdisplay, 0); |
730 |
return; |
731 |
} |
732 |
} |
733 |
|
734 |
|
735 |
static |
736 |
fixwindow(eexp) /* repair damage to window */ |
737 |
register XExposeEvent *eexp; |
738 |
{ |
739 |
if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ |
740 |
odev.hres = eexp->width; |
741 |
odev.vres = eexp->height; |
742 |
} |
743 |
qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height, |
744 |
eexp->x + eexp->width, odev.vres - eexp->y); |
745 |
} |
746 |
|
747 |
|
748 |
static |
749 |
resizewindow(ersz) /* resize window */ |
750 |
register XConfigureEvent *ersz; |
751 |
{ |
752 |
if (ersz->width == odev.hres && ersz->height == odev.vres) |
753 |
return; |
754 |
|
755 |
odev.hres = ersz->width; |
756 |
odev.vres = ersz->height; |
757 |
|
758 |
odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); |
759 |
odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); |
760 |
|
761 |
inpresflags |= DFL(DC_SETVIEW); |
762 |
} |