--- ray/src/hd/rhd_x11.c 1998/01/06 13:07:57 3.24 +++ ray/src/hd/rhd_x11.c 2005/01/21 00:52:59 3.39 @@ -1,22 +1,25 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhd_x11.c,v 3.39 2005/01/21 00:52:59 greg Exp $"; #endif - /* * X11 driver for holodeck display. * Based on rview driver. */ -#include "standard.h" -#include "rhd_qtree.h" - +#include +#include #include #include #include +#include -#include "x11icon.h" +#include "platform.h" +#include "rtmath.h" +#include "rterror.h" +#include "plocate.h" +#include "rhdisp.h" +#include "rhd_qtree.h" +#include "x11icon.h" #ifndef RAYQLEN #define RAYQLEN 50000 /* max. rays to queue before flush */ @@ -28,6 +31,8 @@ static char SCCSid[] = "$SunId$ SGI"; #define GAMMA 2.2 /* default gamma correction */ +#define FRAMESTATE(s) (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) + #define MOVPCT 7 /* percent distance to move /frame */ #define MOVDIR(b) ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) #define MOVDEG (-5) /* degrees to orbit CW/down /frame */ @@ -49,6 +54,10 @@ static char SCCSid[] = "$SunId$ SGI"; struct driver odev; /* global device driver structure */ +TMstruct *tmGlobal; /* global tone-mapping structure */ + +char odev_args[64]; /* command arguments */ + static XEvent currentevent; /* current event */ static int ncolors = 0; /* color table size */ @@ -68,13 +77,27 @@ static int inpresflags; /* input result flags */ static int headlocked = 0; /* lock vertical motion */ -static int getpixels(), xnewcolr(), freepixels(), resizewindow(), - getevent(), getkey(), moveview(), getmove(), fixwindow(); -static unsigned long true_pixel(); +static int mytmflags(void); +static void xnewcolr(int ndx, int r, int g, int b); +static int getpixels(void); +static void freepixels(void); +static unsigned long true_pixel(register BYTE rgb[3]); +static void getevent(void); +static int ilclip(int dp[2][2], FVECT wp[2]); +static void draw3dline(FVECT wp[2]); +static void draw_grids(void); +static int moveview(int dx, int dy, int mov, int orb); +static void getframe(XButtonPressedEvent *ebut); +static void waitabit(void); +static void getmove(XButtonPressedEvent *ebut); +static void getkey(register XKeyPressedEvent *ekey); +static void fixwindow(register XExposeEvent *eexp); +static void resizewindow(register XConfigureEvent *ersz); + static int -mytmflags() /* figure out tone mapping flags */ +mytmflags(void) /* figure out tone mapping flags */ { extern char *progname; register char *cp, *tail; @@ -89,13 +112,15 @@ mytmflags() /* figure out tone mapping flags */ if (cp-tail == 4 && !strncmp(tail, "x11h", 4)) return(TM_F_HUMAN|TM_F_NOSTDERR); error(USER, "illegal driver name"); + return 0; /* pro forma return */ } -dev_open(id) /* initialize X11 driver */ -char *id; +extern void +dev_open( /* initialize X11 driver */ + char *id +) { - extern char *getenv(); static RGBPRIMS myprims = STDPRIMS; char *ev; double gamval = GAMMA; @@ -142,7 +167,8 @@ char *id; &myprims[BLU][CIEX],&myprims[BLU][CIEY], &myprims[WHT][CIEX],&myprims[WHT][CIEY]) >= 6) dpri = myprims; - if (tmInit(mytmflags(), dpri, gamval) == NULL) + tmGlobal = tmInit(mytmflags(), dpri, gamval); + if (tmGlobal == NULL) error(SYSTEM, "not enough memory in dev_open"); /* open window */ ourwinattr.background_pixel = ourblack; @@ -176,7 +202,7 @@ char *id; DisplayWidth(ourdisplay, ourscreen); pheight = (double)DisplayHeightMM(ourdisplay, ourscreen) / DisplayHeight(ourdisplay, ourscreen); - copystruct(&odev.v, &stdview); + odev.v = stdview; odev.v.type = VT_PER; /* map the window and get its size */ XMapWindow(ourdisplay, gwind); @@ -191,7 +217,8 @@ char *id; } -dev_close() /* close our display */ +extern void +dev_close(void) /* close our display */ { freepixels(); XFreeGC(ourdisplay, ourgc); @@ -201,15 +228,15 @@ dev_close() /* close our display */ XCloseDisplay(ourdisplay); ourdisplay = NULL; qtFreeLeaves(); - tmDone(NULL); + tmDone(tmGlobal); odev.v.type = 0; odev.hres = odev.vres = 0; odev.ifd = -1; } - -dev_clear() /* clear our quadtree */ +extern void +dev_clear(void) /* clear our quadtree */ { qtCompost(100); if (ncolors > 0) @@ -218,9 +245,10 @@ dev_clear() /* clear our quadtree */ } -int -dev_view(nv) /* assign new driver view */ -VIEW *nv; +extern int +dev_view( /* assign new driver view */ + VIEW *nv +) { if (nv->type == VT_PAR || /* check view legality */ nv->horiz > 160. || nv->vert > 160.) { @@ -258,30 +286,70 @@ VIEW *nv; XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); dev_input(); /* wait for resize event */ } - copystruct(&odev.v, nv); + odev.v = *nv; } qtReplant(); return(1); } -int -dev_input() /* get X11 input */ +extern void +dev_section( /* add octree for geometry rendering */ + char *ofn +) { + /* unimplemented */ +} + + +extern void +dev_auxcom( /* process an auxiliary command */ + char *cmd, + char *args +) +{ + sprintf(errmsg, "%s: unknown command", cmd); + error(COMMAND, errmsg); +} + + +extern VIEW * +dev_auxview( /* return nth auxiliary view */ + int n, + int hvres[2] +) +{ + if (n) + return(NULL); + hvres[0] = odev.hres; hvres[1] = odev.vres; + return(&odev.v); +} + + +extern int +dev_input(void) /* get X11 input */ +{ inpresflags = 0; do getevent(); - while (XQLength(ourdisplay) > 0); + while (XPending(ourdisplay) > 0); + odev.inpready = 0; + return(inpresflags); } -dev_paintr(rgb, xmin, ymin, xmax, ymax) /* fill a rectangle */ -BYTE rgb[3]; -int xmin, ymin, xmax, ymax; +extern void +dev_paintr( /* fill a rectangle */ + BYTE rgb[3], + int xmin, + int ymin, + int xmax, + int ymax +) { unsigned long pixel; @@ -297,19 +365,22 @@ int xmin, ymin, xmax, ymax; } -int -dev_flush() /* flush output */ +extern int +dev_flush(void) /* flush output */ { qtUpdate(); rayqleft = RAYQLEN; - return(XPending(ourdisplay)); + return(odev.inpready = XPending(ourdisplay)); } -static -xnewcolr(ndx, r, g, b) /* enter a color into hardware table */ -int ndx; -int r, g, b; +static void +xnewcolr( /* enter a color into hardware table */ + int ndx, + int r, + int g, + int b +) { XColor xcolor; @@ -324,7 +395,7 @@ int r, g, b; static int -getpixels() /* get the color map */ +getpixels(void) /* get the color map */ { XColor thiscolor; register int i, j; @@ -346,7 +417,7 @@ loop: return(ncolors = 0); if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) break; - free((char *)pixval); + free((void *)pixval); pixval = NULL; } if (pixval == NULL) { @@ -375,13 +446,13 @@ loop: } -static -freepixels() /* free our pixels */ +static void +freepixels(void) /* free our pixels */ { if (ncolors == 0) return; XFreeColors(ourdisplay,ourmap,pixval,ncolors,0L); - free((char *)pixval); + free((void *)pixval); pixval = NULL; ncolors = 0; if (ourmap != DefaultColormap(ourdisplay,ourscreen)) @@ -391,8 +462,9 @@ freepixels() /* free our pixels */ static unsigned long -true_pixel(rgb) /* return true pixel value for color */ -register BYTE rgb[3]; +true_pixel( /* return true pixel value for color */ + register BYTE rgb[3] +) { register unsigned long rval; @@ -403,8 +475,8 @@ register BYTE rgb[3]; } -static -getevent() /* get next event */ +static void +getevent(void) /* get next event */ { XNextEvent(ourdisplay, levptr(XEvent)); switch (levptr(XEvent)->type) { @@ -431,22 +503,44 @@ getevent() /* get next event */ getkey(levptr(XKeyPressedEvent)); break; case ButtonPress: - getmove(levptr(XButtonPressedEvent)); + if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) + getframe(levptr(XButtonPressedEvent)); + else + getmove(levptr(XButtonPressedEvent)); break; } } -static -ilclip(dp, wp) /* clip world coordinates to device */ -int dp[2][2]; -FVECT wp[2]; +static int +ilclip( /* clip world coordinates to device */ + int dp[2][2], + FVECT wp[2] +) { - static FVECT vmin = {0.,0.,0.}, vmax = {1.,1.,FHUGE}; - FVECT ip[2]; - /* not exactly right, but who cares? */ - viewloc(ip[0], &odev.v, wp[0]); - viewloc(ip[1], &odev.v, wp[1]); + static FVECT vmin = {0.,0.,0.}, vmax = {1.-FTINY,1.-FTINY,FHUGE}; + FVECT wpc[2], ip[2]; + double d, d0, d1; + /* check for points behind view */ + d = DOT(odev.v.vp, odev.v.vdir); + d0 = DOT(wp[0], odev.v.vdir) - d; + d1 = DOT(wp[1], odev.v.vdir) - d; + /* work on copy of world points */ + if (d0 <= d1) { + VCOPY(wpc[0], wp[0]); VCOPY(wpc[1], wp[1]); + } else { + d = d0; d0 = d1; d1 = d; + VCOPY(wpc[1], wp[0]); VCOPY(wpc[0], wp[1]); + } + if (d0 <= FTINY) { + if (d1 <= FTINY) return(0); + VSUB(wpc[0], wpc[0], wpc[1]); + d = .99*d1/(d1-d0); + VSUM(wpc[0], wpc[1], wpc[0], d); + } + /* get view coordinates and clip to window */ + viewloc(ip[0], &odev.v, wpc[0]); + viewloc(ip[1], &odev.v, wpc[1]); if (!clip(ip[0], ip[1], vmin, vmax)) return(0); dp[0][0] = ip[0][0]*odev.hres; @@ -457,9 +551,10 @@ FVECT wp[2]; } -static -draw3dline(wp) /* draw 3d line in world coordinates */ -FVECT wp[2]; +static void +draw3dline( /* draw 3d line in world coordinates */ + FVECT wp[2] +) { int dp[2][2]; @@ -471,14 +566,12 @@ FVECT wp[2]; } -static -draw_grids() /* draw holodeck section grids */ +static void +draw_grids(void) /* draw holodeck section grids */ { static BYTE gridrgb[3] = {0x0, 0xff, 0xff}; unsigned long pixel; - if (!mapped) - return; if (ncolors > 0) pixel = pixval[get_pixel(gridrgb, xnewcolr)]; else @@ -489,16 +582,20 @@ draw_grids() /* draw holodeck section grids */ } -static -moveview(dx, dy, mov, orb) /* move our view */ -int dx, dy, mov, orb; +static int +moveview( /* move our view */ + int dx, + int dy, + int mov, + int orb +) { VIEW nv; FVECT odir, v1; double d; register int li; /* start with old view */ - copystruct(&nv, &odev.v); + nv = odev.v; /* change view direction */ if (mov | orb) { if ((li = qtFindLeaf(dx, dy)) < 0) @@ -537,10 +634,53 @@ int dx, dy, mov, orb; } -static -getmove(ebut) /* get view change */ -XButtonPressedEvent *ebut; +static void +getframe( /* get focus frame */ + XButtonPressedEvent *ebut +) { + int startx = ebut->x, starty = ebut->y; + int endx, endy, midx, midy; + FVECT v1; + int li; + /* get mouse drag */ + XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); + endx = levptr(XButtonReleasedEvent)->x; + endy = levptr(XButtonReleasedEvent)->y; + midx = (startx + endx) >> 1; + midy = (starty + endy) >> 1; + /* set focus distance */ + if ((li = qtFindLeaf(midx, midy)) < 0) + return(0); /* not on window */ + VCOPY(v1, qtL.wp[li]); + odev.v.vdist = sqrt(dist2(odev.v.vp, v1)); + /* set frame for rendering */ + if ((endx == startx) | (endy == starty)) + return; + if (endx < startx) {register int c = endx; endx = startx; startx = c;} + if (endy < starty) {register int c = endy; endy = starty; starty = c;} + sprintf(odev_args, "%.3f %.3f %.3f %.3f", + (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres, + (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres); + inpresflags |= DFL(DC_FOCUS); +} + + +static void +waitabit(void) /* pause a moment */ +{ + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 100000000; + nanosleep(&ts, NULL); +} + + +static void +getmove( /* get view change */ + XButtonPressedEvent *ebut +) +{ int movdir = MOVDIR(ebut->button); int movorb = MOVORB(ebut->state); int oldnodesiz = qtMinNodesiz; @@ -553,7 +693,7 @@ XButtonPressedEvent *ebut; while (!XCheckMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent))) { - + waitabit(); if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, &rootx, &rooty, &wx, &wy, &statemask)) break; /* on another screen */ @@ -578,10 +718,14 @@ XButtonPressedEvent *ebut; } -static -getkey(ekey) /* get input key */ -register XKeyPressedEvent *ekey; +static void +getkey( /* get input key */ + register XKeyPressedEvent *ekey +) { + Window rootw, childw; + int rootx, rooty, wx, wy; + unsigned int statemask; int n; char buf[8]; @@ -598,6 +742,18 @@ register XKeyPressedEvent *ekey; case 'l': /* retrieve last view */ inpresflags |= DFL(DC_LASTVIEW); return; + case 'f': /* frame view position */ + if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, + &rootx, &rooty, &wx, &wy, &statemask)) + return; /* on another screen */ + sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres, + 1.-(wy+.5)/odev.vres); + inpresflags |= DFL(DC_FOCUS); + return; + case 'F': /* unfocus */ + odev_args[0] = '\0'; + inpresflags |= DFL(DC_FOCUS); + return; case 'p': /* pause computation */ inpresflags |= DFL(DC_PAUSE); return; @@ -644,9 +800,10 @@ register XKeyPressedEvent *ekey; } -static -fixwindow(eexp) /* repair damage to window */ -register XExposeEvent *eexp; +static void +fixwindow( /* repair damage to window */ + register XExposeEvent *eexp +) { if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ odev.hres = eexp->width; @@ -657,9 +814,10 @@ register XExposeEvent *eexp; } -static -resizewindow(ersz) /* resize window */ -register XConfigureEvent *ersz; +static void +resizewindow( /* resize window */ + register XConfigureEvent *ersz +) { if (ersz->width == odev.hres && ersz->height == odev.vres) return;