1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* X11 driver for holodeck display. |
6 |
|
* Based on rview driver. |
7 |
|
*/ |
8 |
|
|
9 |
< |
#include "standard.h" |
9 |
> |
#include <stdlib.h> |
10 |
> |
#include <stdio.h> |
11 |
|
#include <X11/Xlib.h> |
12 |
|
#include <X11/cursorfont.h> |
13 |
|
#include <X11/Xutil.h> |
14 |
+ |
#include <time.h> |
15 |
+ |
|
16 |
+ |
#include "platform.h" |
17 |
+ |
#include "rtmath.h" |
18 |
+ |
#include "rterror.h" |
19 |
+ |
#include "plocate.h" |
20 |
+ |
#include "rhdisp.h" |
21 |
|
#include "rhd_qtree.h" |
22 |
< |
#include "x11icon.h" |
22 |
> |
#include "x11icon.h" |
23 |
|
|
24 |
|
#ifndef RAYQLEN |
25 |
|
#define RAYQLEN 50000 /* max. rays to queue before flush */ |
54 |
|
|
55 |
|
struct driver odev; /* global device driver structure */ |
56 |
|
|
57 |
+ |
TMstruct *tmGlobal; /* global tone-mapping structure */ |
58 |
+ |
|
59 |
|
char odev_args[64]; /* command arguments */ |
60 |
|
|
61 |
|
static XEvent currentevent; /* current event */ |
77 |
|
|
78 |
|
static int headlocked = 0; /* lock vertical motion */ |
79 |
|
|
73 |
– |
static int getpixels(), xnewcolr(), freepixels(), resizewindow(), getframe(), |
74 |
– |
getevent(), getkey(), moveview(), getmove(), fixwindow(); |
75 |
– |
static unsigned long true_pixel(); |
80 |
|
|
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 uby8 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 |
|
|
98 |
+ |
|
99 |
|
static int |
100 |
< |
mytmflags() /* figure out tone mapping flags */ |
100 |
> |
mytmflags(void) /* figure out tone mapping flags */ |
101 |
|
{ |
102 |
|
extern char *progname; |
103 |
|
register char *cp, *tail; |
112 |
|
if (cp-tail == 4 && !strncmp(tail, "x11h", 4)) |
113 |
|
return(TM_F_HUMAN|TM_F_NOSTDERR); |
114 |
|
error(USER, "illegal driver name"); |
115 |
+ |
return 0; /* pro forma return */ |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
< |
dev_open(id) /* initialize X11 driver */ |
120 |
< |
char *id; |
119 |
> |
extern void |
120 |
> |
dev_open( /* initialize X11 driver */ |
121 |
> |
char *id |
122 |
> |
) |
123 |
|
{ |
100 |
– |
extern char *getenv(); |
124 |
|
static RGBPRIMS myprims = STDPRIMS; |
125 |
|
char *ev; |
126 |
|
double gamval = GAMMA; |
167 |
|
&myprims[BLU][CIEX],&myprims[BLU][CIEY], |
168 |
|
&myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6) |
169 |
|
dpri = myprims; |
170 |
< |
if (tmInit(mytmflags(), dpri, gamval) == NULL) |
170 |
> |
tmGlobal = tmInit(mytmflags(), dpri, gamval); |
171 |
> |
if (tmGlobal == NULL) |
172 |
|
error(SYSTEM, "not enough memory in dev_open"); |
173 |
|
/* open window */ |
174 |
|
ourwinattr.background_pixel = ourblack; |
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); |
193 |
> |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(ourdisplay, gwind, |
194 |
> |
(char *)x11icon_bits, x11icon_width, x11icon_height); |
195 |
|
XSetWMHints(ourdisplay, gwind, &ourxwmhints); |
196 |
|
oursizhints.min_width = MINWIDTH; |
197 |
|
oursizhints.min_height = MINHEIGHT; |
202 |
|
DisplayWidth(ourdisplay, ourscreen); |
203 |
|
pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / |
204 |
|
DisplayHeight(ourdisplay, ourscreen); |
205 |
< |
copystruct(&odev.v, &stdview); |
205 |
> |
odev.v = stdview; |
206 |
|
odev.v.type = VT_PER; |
207 |
|
/* map the window and get its size */ |
208 |
|
XMapWindow(ourdisplay, gwind); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
< |
dev_close() /* close our display */ |
220 |
> |
extern void |
221 |
> |
dev_close(void) /* close our display */ |
222 |
|
{ |
223 |
|
freepixels(); |
224 |
|
XFreeGC(ourdisplay, ourgc); |
228 |
|
XCloseDisplay(ourdisplay); |
229 |
|
ourdisplay = NULL; |
230 |
|
qtFreeLeaves(); |
231 |
< |
tmDone(NULL); |
231 |
> |
tmDone(tmGlobal); |
232 |
|
odev.v.type = 0; |
233 |
|
odev.hres = odev.vres = 0; |
234 |
|
odev.ifd = -1; |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
< |
|
239 |
< |
dev_clear() /* clear our quadtree */ |
238 |
> |
extern void |
239 |
> |
dev_clear(void) /* clear our quadtree */ |
240 |
|
{ |
241 |
|
qtCompost(100); |
242 |
|
if (ncolors > 0) |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
< |
int |
249 |
< |
dev_view(nv) /* assign new driver view */ |
250 |
< |
VIEW *nv; |
248 |
> |
extern int |
249 |
> |
dev_view( /* assign new driver view */ |
250 |
> |
VIEW *nv |
251 |
> |
) |
252 |
|
{ |
253 |
|
if (nv->type == VT_PAR || /* check view legality */ |
254 |
|
nv->horiz > 160. || nv->vert > 160.) { |
286 |
|
XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); |
287 |
|
dev_input(); /* wait for resize event */ |
288 |
|
} |
289 |
< |
copystruct(&odev.v, nv); |
289 |
> |
odev.v = *nv; |
290 |
|
} |
291 |
|
qtReplant(); |
292 |
|
return(1); |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
< |
dev_section(ofn) /* add octree for geometry rendering */ |
297 |
< |
char *ofn; |
296 |
> |
extern void |
297 |
> |
dev_section( /* add octree for geometry rendering */ |
298 |
> |
char *gfn, |
299 |
> |
char *pfn |
300 |
> |
) |
301 |
|
{ |
302 |
|
/* unimplemented */ |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
< |
dev_auxcom(cmd, args) /* process an auxiliary command */ |
307 |
< |
char *cmd, *args; |
306 |
> |
extern void |
307 |
> |
dev_auxcom( /* process an auxiliary command */ |
308 |
> |
char *cmd, |
309 |
> |
char *args |
310 |
> |
) |
311 |
|
{ |
312 |
|
sprintf(errmsg, "%s: unknown command", cmd); |
313 |
|
error(COMMAND, errmsg); |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
< |
VIEW * |
318 |
< |
dev_auxview(n, hvres) /* return nth auxiliary view */ |
319 |
< |
int n; |
320 |
< |
int hvres[2]; |
317 |
> |
extern VIEW * |
318 |
> |
dev_auxview( /* return nth auxiliary view */ |
319 |
> |
int n, |
320 |
> |
int hvres[2] |
321 |
> |
) |
322 |
|
{ |
323 |
|
if (n) |
324 |
|
return(NULL); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
< |
int |
331 |
< |
dev_input() /* get X11 input */ |
330 |
> |
extern int |
331 |
> |
dev_input(void) /* get X11 input */ |
332 |
|
{ |
333 |
|
inpresflags = 0; |
334 |
|
|
343 |
|
} |
344 |
|
|
345 |
|
|
346 |
< |
dev_paintr(rgb, xmin, ymin, xmax, ymax) /* fill a rectangle */ |
347 |
< |
BYTE rgb[3]; |
348 |
< |
int xmin, ymin, xmax, ymax; |
346 |
> |
extern void |
347 |
> |
dev_paintr( /* fill a rectangle */ |
348 |
> |
uby8 rgb[3], |
349 |
> |
int xmin, |
350 |
> |
int ymin, |
351 |
> |
int xmax, |
352 |
> |
int ymax |
353 |
> |
) |
354 |
|
{ |
355 |
|
unsigned long pixel; |
356 |
|
|
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
< |
int |
370 |
< |
dev_flush() /* flush output */ |
369 |
> |
extern int |
370 |
> |
dev_flush(void) /* flush output */ |
371 |
|
{ |
372 |
|
qtUpdate(); |
373 |
|
rayqleft = RAYQLEN; |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
< |
static |
379 |
< |
xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ |
380 |
< |
int ndx; |
381 |
< |
int r, g, b; |
378 |
> |
static void |
379 |
> |
xnewcolr( /* enter a color into hardware table */ |
380 |
> |
int ndx, |
381 |
> |
int r, |
382 |
> |
int g, |
383 |
> |
int b |
384 |
> |
) |
385 |
|
{ |
386 |
|
XColor xcolor; |
387 |
|
|
396 |
|
|
397 |
|
|
398 |
|
static int |
399 |
< |
getpixels() /* get the color map */ |
399 |
> |
getpixels(void) /* get the color map */ |
400 |
|
{ |
401 |
|
XColor thiscolor; |
402 |
|
register int i, j; |
418 |
|
return(ncolors = 0); |
419 |
|
if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) |
420 |
|
break; |
421 |
< |
free((char *)pixval); |
421 |
> |
free((void *)pixval); |
422 |
|
pixval = NULL; |
423 |
|
} |
424 |
|
if (pixval == NULL) { |
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
< |
static |
451 |
< |
freepixels() /* free our pixels */ |
450 |
> |
static void |
451 |
> |
freepixels(void) /* free our pixels */ |
452 |
|
{ |
453 |
|
if (ncolors == 0) |
454 |
|
return; |
455 |
|
XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); |
456 |
< |
free((char *)pixval); |
456 |
> |
free((void *)pixval); |
457 |
|
pixval = NULL; |
458 |
|
ncolors = 0; |
459 |
|
if (ourmap != DefaultColormap(ourdisplay,ourscreen)) |
463 |
|
|
464 |
|
|
465 |
|
static unsigned long |
466 |
< |
true_pixel(rgb) /* return true pixel value for color */ |
467 |
< |
register BYTE rgb[3]; |
466 |
> |
true_pixel( /* return true pixel value for color */ |
467 |
> |
register uby8 rgb[3] |
468 |
> |
) |
469 |
|
{ |
470 |
|
register unsigned long rval; |
471 |
|
|
476 |
|
} |
477 |
|
|
478 |
|
|
479 |
< |
static |
480 |
< |
getevent() /* get next event */ |
479 |
> |
static void |
480 |
> |
getevent(void) /* get next event */ |
481 |
|
{ |
482 |
|
XNextEvent(ourdisplay, levptr(XEvent)); |
483 |
|
switch (levptr(XEvent)->type) { |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
< |
static |
517 |
< |
ilclip(dp, wp) /* clip world coordinates to device */ |
518 |
< |
int dp[2][2]; |
519 |
< |
FVECT wp[2]; |
516 |
> |
static int |
517 |
> |
ilclip( /* clip world coordinates to device */ |
518 |
> |
int dp[2][2], |
519 |
> |
FVECT wp[2] |
520 |
> |
) |
521 |
|
{ |
522 |
|
static FVECT vmin = {0.,0.,0.}, vmax = {1.-FTINY,1.-FTINY,FHUGE}; |
523 |
|
FVECT wpc[2], ip[2]; |
552 |
|
} |
553 |
|
|
554 |
|
|
555 |
< |
static |
556 |
< |
draw3dline(wp) /* draw 3d line in world coordinates */ |
557 |
< |
FVECT wp[2]; |
555 |
> |
static void |
556 |
> |
draw3dline( /* draw 3d line in world coordinates */ |
557 |
> |
FVECT wp[2] |
558 |
> |
) |
559 |
|
{ |
560 |
|
int dp[2][2]; |
561 |
|
|
567 |
|
} |
568 |
|
|
569 |
|
|
570 |
< |
static |
571 |
< |
draw_grids() /* draw holodeck section grids */ |
570 |
> |
static void |
571 |
> |
draw_grids(void) /* draw holodeck section grids */ |
572 |
|
{ |
573 |
< |
static BYTE gridrgb[3] = {0x0, 0xff, 0xff}; |
573 |
> |
static uby8 gridrgb[3] = {0x0, 0xff, 0xff}; |
574 |
|
unsigned long pixel; |
575 |
|
|
576 |
|
if (ncolors > 0) |
583 |
|
} |
584 |
|
|
585 |
|
|
586 |
< |
static |
587 |
< |
moveview(dx, dy, mov, orb) /* move our view */ |
588 |
< |
int dx, dy, mov, orb; |
586 |
> |
static int |
587 |
> |
moveview( /* move our view */ |
588 |
> |
int dx, |
589 |
> |
int dy, |
590 |
> |
int mov, |
591 |
> |
int orb |
592 |
> |
) |
593 |
|
{ |
594 |
|
VIEW nv; |
595 |
|
FVECT odir, v1; |
596 |
|
double d; |
597 |
|
register int li; |
598 |
|
/* start with old view */ |
599 |
< |
copystruct(&nv, &odev.v); |
599 |
> |
nv = odev.v; |
600 |
|
/* change view direction */ |
601 |
|
if (mov | orb) { |
602 |
|
if ((li = qtFindLeaf(dx, dy)) < 0) |
612 |
|
VSUM(nv.vp, qtL.wp[li], odir, -1.); |
613 |
|
spinvector(nv.vdir, nv.vdir, nv.vup, d); |
614 |
|
} else if (orb) { /* orbit up/down */ |
615 |
< |
fcross(v1, odir, nv.vup); |
616 |
< |
if (normalize(v1) == 0.) |
615 |
> |
if (geodesic(odir, odir, nv.vup, |
616 |
> |
d=MOVDEG*PI/180.*orb, GEOD_RAD) == 0.0) |
617 |
|
return(0); |
570 |
– |
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); |
619 |
> |
geodesic(nv.vdir, nv.vdir, nv.vup, d, GEOD_RAD); |
620 |
|
} else if (mov) { /* move forward/backward */ |
621 |
|
d = MOVPCT/100. * mov; |
622 |
|
VSUM(nv.vp, nv.vp, odir, d); |
634 |
|
} |
635 |
|
|
636 |
|
|
637 |
< |
static |
638 |
< |
getframe(ebut) /* get focus frame */ |
639 |
< |
XButtonPressedEvent *ebut; |
637 |
> |
static void |
638 |
> |
getframe( /* get focus frame */ |
639 |
> |
XButtonPressedEvent *ebut |
640 |
> |
) |
641 |
|
{ |
642 |
|
int startx = ebut->x, starty = ebut->y; |
643 |
< |
int endx, endy; |
644 |
< |
|
643 |
> |
int endx, endy, midx, midy; |
644 |
> |
FVECT v1; |
645 |
> |
int li; |
646 |
> |
/* get mouse drag */ |
647 |
|
XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); |
648 |
|
endx = levptr(XButtonReleasedEvent)->x; |
649 |
|
endy = levptr(XButtonReleasedEvent)->y; |
650 |
< |
if (endx == startx | endy == starty) { |
651 |
< |
XBell(ourdisplay, 0); |
650 |
> |
midx = (startx + endx) >> 1; |
651 |
> |
midy = (starty + endy) >> 1; |
652 |
> |
/* set focus distance */ |
653 |
> |
if ((li = qtFindLeaf(midx, midy)) < 0) |
654 |
> |
return; /* not on window */ |
655 |
> |
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 |
|
return; |
603 |
– |
} |
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", |
666 |
|
} |
667 |
|
|
668 |
|
|
669 |
< |
static |
670 |
< |
getmove(ebut) /* get view change */ |
615 |
< |
XButtonPressedEvent *ebut; |
669 |
> |
static void |
670 |
> |
waitabit(void) /* pause a moment */ |
671 |
|
{ |
672 |
+ |
struct timespec ts; |
673 |
+ |
ts.tv_sec = 0; |
674 |
+ |
ts.tv_nsec = 100000000; |
675 |
+ |
nanosleep(&ts, NULL); |
676 |
+ |
} |
677 |
+ |
|
678 |
+ |
|
679 |
+ |
static void |
680 |
+ |
getmove( /* get view change */ |
681 |
+ |
XButtonPressedEvent *ebut |
682 |
+ |
) |
683 |
+ |
{ |
684 |
|
int movdir = MOVDIR(ebut->button); |
685 |
|
int movorb = MOVORB(ebut->state); |
686 |
|
int oldnodesiz = qtMinNodesiz; |
693 |
|
|
694 |
|
while (!XCheckMaskEvent(ourdisplay, |
695 |
|
ButtonReleaseMask, levptr(XEvent))) { |
696 |
< |
|
696 |
> |
waitabit(); |
697 |
|
if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, |
698 |
|
&rootx, &rooty, &wx, &wy, &statemask)) |
699 |
|
break; /* on another screen */ |
718 |
|
} |
719 |
|
|
720 |
|
|
721 |
< |
static |
722 |
< |
getkey(ekey) /* get input key */ |
723 |
< |
register XKeyPressedEvent *ekey; |
721 |
> |
static void |
722 |
> |
getkey( /* get input key */ |
723 |
> |
register XKeyPressedEvent *ekey |
724 |
> |
) |
725 |
|
{ |
726 |
|
Window rootw, childw; |
727 |
|
int rootx, rooty, wx, wy; |
800 |
|
} |
801 |
|
|
802 |
|
|
803 |
< |
static |
804 |
< |
fixwindow(eexp) /* repair damage to window */ |
805 |
< |
register XExposeEvent *eexp; |
803 |
> |
static void |
804 |
> |
fixwindow( /* repair damage to window */ |
805 |
> |
register XExposeEvent *eexp |
806 |
> |
) |
807 |
|
{ |
808 |
|
if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ |
809 |
|
odev.hres = eexp->width; |
814 |
|
} |
815 |
|
|
816 |
|
|
817 |
< |
static |
818 |
< |
resizewindow(ersz) /* resize window */ |
819 |
< |
register XConfigureEvent *ersz; |
817 |
> |
static void |
818 |
> |
resizewindow( /* resize window */ |
819 |
> |
register XConfigureEvent *ersz |
820 |
> |
) |
821 |
|
{ |
822 |
|
if (ersz->width == odev.hres && ersz->height == odev.vres) |
823 |
|
return; |