| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: rhd_x11.c,v 3.42 2006/06/07 17:52:04 schorsch Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | * X11 driver for holodeck display. | 
| 6 | * Based on rview driver. | 
| 7 | */ | 
| 8 |  | 
| 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" | 
| 23 |  | 
| 24 | #ifndef RAYQLEN | 
| 25 | #define RAYQLEN         50000           /* max. rays to queue before flush */ | 
| 26 | #endif | 
| 27 |  | 
| 28 | #ifndef FEQ | 
| 29 | #define FEQ(a,b)        ((a)-(b) <= FTINY && (a)-(b) >= -FTINY) | 
| 30 | #endif | 
| 31 |  | 
| 32 | #define GAMMA           2.2             /* default gamma correction */ | 
| 33 |  | 
| 34 | #define FRAMESTATE(s)   (((s)&(ShiftMask|ControlMask))==(ShiftMask|ControlMask)) | 
| 35 |  | 
| 36 | #define MOVPCT          7               /* percent distance to move /frame */ | 
| 37 | #define MOVDIR(b)       ((b)==Button1 ? 1 : (b)==Button2 ? 0 : -1) | 
| 38 | #define MOVDEG          (-5)            /* degrees to orbit CW/down /frame */ | 
| 39 | #define MOVORB(s)       ((s)&ShiftMask ? 1 : (s)&ControlMask ? -1 : 0) | 
| 40 |  | 
| 41 | #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 | ButtonPressMask|ButtonReleaseMask) | 
| 52 |  | 
| 53 | #define  levptr(etype)  ((etype *)¤tevent) | 
| 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 */ | 
| 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 | static int      headlocked = 0;         /* lock vertical motion */ | 
| 79 |  | 
| 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(void)                 /* figure out tone mapping flags */ | 
| 101 | { | 
| 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 | return(TM_F_CAMERA|TM_F_NOSTDERR); | 
| 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 | extern void | 
| 120 | dev_open(                       /* initialize X11 driver */ | 
| 121 | char  *id | 
| 122 | ) | 
| 123 | { | 
| 124 | static RGBPRIMS myprims = STDPRIMS; | 
| 125 | char  *ev; | 
| 126 | double  gamval = GAMMA; | 
| 127 | RGBPRIMP        dpri = stdprims; | 
| 128 | int  nplanes; | 
| 129 | XSetWindowAttributes    ourwinattr; | 
| 130 | XWMHints  ourxwmhints; | 
| 131 | XSizeHints      oursizhints; | 
| 132 | /* set quadtree globals */ | 
| 133 | qtMinNodesiz = 2; | 
| 134 | /* 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 | 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 | 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; | 
| 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, gwind, | 
| 194 | (char *)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 | /* 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 | odev.v = stdview; | 
| 206 | odev.v.type = VT_PER; | 
| 207 | /* map the window and get its size */ | 
| 208 | XMapWindow(ourdisplay, gwind); | 
| 209 | dev_input();                    /* sets size and view angles */ | 
| 210 | /* allocate our leaf pile */ | 
| 211 | if (!qtAllocLeaves(DisplayWidth(ourdisplay,ourscreen) * | 
| 212 | DisplayHeight(ourdisplay,ourscreen) * 3 / | 
| 213 | (qtMinNodesiz*qtMinNodesiz*2))) | 
| 214 | error(SYSTEM, "insufficient memory for leaf storage"); | 
| 215 | odev.name = id; | 
| 216 | odev.ifd = ConnectionNumber(ourdisplay); | 
| 217 | } | 
| 218 |  | 
| 219 |  | 
| 220 | extern void | 
| 221 | dev_close(void)                 /* close our display */ | 
| 222 | { | 
| 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 | tmDone(tmGlobal); | 
| 232 | odev.v.type = 0; | 
| 233 | odev.hres = odev.vres = 0; | 
| 234 | odev.ifd = -1; | 
| 235 | } | 
| 236 |  | 
| 237 |  | 
| 238 | extern void | 
| 239 | dev_clear(void)                 /* clear our quadtree */ | 
| 240 | { | 
| 241 | qtCompost(100); | 
| 242 | if (ncolors > 0) | 
| 243 | new_ctab(ncolors); | 
| 244 | rayqleft = 0;                   /* hold off update */ | 
| 245 | } | 
| 246 |  | 
| 247 |  | 
| 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.) { | 
| 255 | 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 | if (nv != &odev.v) { | 
| 267 | if (!FEQ(nv->horiz,odev.v.horiz) ||     /* resize window? */ | 
| 268 | !FEQ(nv->vert,odev.v.vert)) { | 
| 269 | int     dw = DisplayWidth(ourdisplay,ourscreen); | 
| 270 | int     dh = DisplayHeight(ourdisplay,ourscreen); | 
| 271 |  | 
| 272 | dw -= 25;       /* for window frame */ | 
| 273 | dh -= 50; | 
| 274 | 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 | 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 | XResizeWindow(ourdisplay, gwind, odev.hres, odev.vres); | 
| 287 | dev_input();    /* wait for resize event */ | 
| 288 | } | 
| 289 | odev.v = *nv; | 
| 290 | } | 
| 291 | qtReplant(); | 
| 292 | return(1); | 
| 293 | } | 
| 294 |  | 
| 295 |  | 
| 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 | 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 | extern VIEW * | 
| 318 | dev_auxview(            /* return nth auxiliary view */ | 
| 319 | int     n, | 
| 320 | int     hvres[2] | 
| 321 | ) | 
| 322 | { | 
| 323 | if (n) | 
| 324 | return(NULL); | 
| 325 | hvres[0] = odev.hres; hvres[1] = odev.vres; | 
| 326 | return(&odev.v); | 
| 327 | } | 
| 328 |  | 
| 329 |  | 
| 330 | extern int | 
| 331 | dev_input(void)                 /* get X11 input */ | 
| 332 | { | 
| 333 | inpresflags = 0; | 
| 334 |  | 
| 335 | do | 
| 336 | getevent(); | 
| 337 |  | 
| 338 | while (XPending(ourdisplay) > 0); | 
| 339 |  | 
| 340 | odev.inpready = 0; | 
| 341 |  | 
| 342 | return(inpresflags); | 
| 343 | } | 
| 344 |  | 
| 345 |  | 
| 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 |  | 
| 357 | if (!mapped) | 
| 358 | return; | 
| 359 | if (ncolors > 0) | 
| 360 | pixel = pixval[get_pixel(rgb, xnewcolr)]; | 
| 361 | else | 
| 362 | pixel = true_pixel(rgb); | 
| 363 | XSetForeground(ourdisplay, ourgc, pixel); | 
| 364 | XFillRectangle(ourdisplay, gwind, | 
| 365 | ourgc, xmin, odev.vres-ymax, xmax-xmin, ymax-ymin); | 
| 366 | } | 
| 367 |  | 
| 368 |  | 
| 369 | extern int | 
| 370 | dev_flush(void)                 /* flush output */ | 
| 371 | { | 
| 372 | qtUpdate(); | 
| 373 | rayqleft = RAYQLEN; | 
| 374 | return(odev.inpready = XPending(ourdisplay)); | 
| 375 | } | 
| 376 |  | 
| 377 |  | 
| 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 |  | 
| 388 | xcolor.pixel = pixval[ndx]; | 
| 389 | xcolor.red = r << 8; | 
| 390 | xcolor.green = g << 8; | 
| 391 | xcolor.blue = b << 8; | 
| 392 | xcolor.flags = DoRed|DoGreen|DoBlue; | 
| 393 |  | 
| 394 | XStoreColor(ourdisplay, ourmap, &xcolor); | 
| 395 | } | 
| 396 |  | 
| 397 |  | 
| 398 | static int | 
| 399 | getpixels(void)                         /* get the color map */ | 
| 400 | { | 
| 401 | XColor  thiscolor; | 
| 402 | register int  i, j; | 
| 403 |  | 
| 404 | if (ncolors > 0) | 
| 405 | return(ncolors); | 
| 406 | if (ourvinfo.visual == DefaultVisual(ourdisplay,ourscreen)) { | 
| 407 | ourmap = DefaultColormap(ourdisplay,ourscreen); | 
| 408 | goto loop; | 
| 409 | } | 
| 410 | newmap: | 
| 411 | ourmap = XCreateColormap(ourdisplay,gwind,ourvinfo.visual,AllocNone); | 
| 412 | loop: | 
| 413 | for (ncolors = ourvinfo.colormap_size; | 
| 414 | ncolors > ourvinfo.colormap_size/3; | 
| 415 | ncolors = ncolors*.937) { | 
| 416 | pixval = (unsigned long *)malloc(ncolors*sizeof(unsigned long)); | 
| 417 | if (pixval == NULL) | 
| 418 | return(ncolors = 0); | 
| 419 | if (XAllocColorCells(ourdisplay,ourmap,0,NULL,0,pixval,ncolors)) | 
| 420 | break; | 
| 421 | free((void *)pixval); | 
| 422 | pixval = NULL; | 
| 423 | } | 
| 424 | if (pixval == NULL) { | 
| 425 | if (ourmap == DefaultColormap(ourdisplay,ourscreen)) | 
| 426 | goto newmap;            /* try it with our map */ | 
| 427 | else | 
| 428 | return(ncolors = 0);    /* failed */ | 
| 429 | } | 
| 430 | if (ourmap != DefaultColormap(ourdisplay,ourscreen)) | 
| 431 | for (i = 0; i < ncolors; i++) { /* reset black and white */ | 
| 432 | if (pixval[i] != ourblack && pixval[i] != ourwhite) | 
| 433 | continue; | 
| 434 | thiscolor.pixel = pixval[i]; | 
| 435 | thiscolor.flags = DoRed|DoGreen|DoBlue; | 
| 436 | XQueryColor(ourdisplay, | 
| 437 | DefaultColormap(ourdisplay,ourscreen), | 
| 438 | &thiscolor); | 
| 439 | XStoreColor(ourdisplay, ourmap, &thiscolor); | 
| 440 | for (j = i; j+1 < ncolors; j++) | 
| 441 | pixval[j] = pixval[j+1]; | 
| 442 | ncolors--; | 
| 443 | i--; | 
| 444 | } | 
| 445 | XSetWindowColormap(ourdisplay, gwind, ourmap); | 
| 446 | return(ncolors); | 
| 447 | } | 
| 448 |  | 
| 449 |  | 
| 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((void *)pixval); | 
| 457 | pixval = NULL; | 
| 458 | ncolors = 0; | 
| 459 | if (ourmap != DefaultColormap(ourdisplay,ourscreen)) | 
| 460 | XFreeColormap(ourdisplay, ourmap); | 
| 461 | ourmap = 0; | 
| 462 | } | 
| 463 |  | 
| 464 |  | 
| 465 | static unsigned long | 
| 466 | true_pixel(                     /* return true pixel value for color */ | 
| 467 | register uby8   rgb[3] | 
| 468 | ) | 
| 469 | { | 
| 470 | register unsigned long  rval; | 
| 471 |  | 
| 472 | rval = ourvinfo.red_mask*rgb[RED]/255 & ourvinfo.red_mask; | 
| 473 | rval |= ourvinfo.green_mask*rgb[GRN]/255 & ourvinfo.green_mask; | 
| 474 | rval |= ourvinfo.blue_mask*rgb[BLU]/255 & ourvinfo.blue_mask; | 
| 475 | return(rval); | 
| 476 | } | 
| 477 |  | 
| 478 |  | 
| 479 | static void | 
| 480 | getevent(void)                  /* get next event */ | 
| 481 | { | 
| 482 | XNextEvent(ourdisplay, levptr(XEvent)); | 
| 483 | switch (levptr(XEvent)->type) { | 
| 484 | case ConfigureNotify: | 
| 485 | resizewindow(levptr(XConfigureEvent)); | 
| 486 | break; | 
| 487 | case UnmapNotify: | 
| 488 | mapped = 0; | 
| 489 | freepixels(); | 
| 490 | break; | 
| 491 | case MapNotify: | 
| 492 | if (ourvinfo.class == PseudoColor || | 
| 493 | ourvinfo.class == GrayScale) { | 
| 494 | if (getpixels() == 0) | 
| 495 | error(SYSTEM, "cannot allocate colors\n"); | 
| 496 | new_ctab(ncolors); | 
| 497 | } | 
| 498 | mapped = 1; | 
| 499 | break; | 
| 500 | case Expose: | 
| 501 | fixwindow(levptr(XExposeEvent)); | 
| 502 | break; | 
| 503 | case KeyPress: | 
| 504 | getkey(levptr(XKeyPressedEvent)); | 
| 505 | break; | 
| 506 | case ButtonPress: | 
| 507 | if (FRAMESTATE(levptr(XButtonPressedEvent)->state)) | 
| 508 | getframe(levptr(XButtonPressedEvent)); | 
| 509 | else | 
| 510 | getmove(levptr(XButtonPressedEvent)); | 
| 511 | break; | 
| 512 | } | 
| 513 | } | 
| 514 |  | 
| 515 |  | 
| 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]; | 
| 524 | double  d, d0, d1; | 
| 525 | /* check for points behind view */ | 
| 526 | d = DOT(odev.v.vp, odev.v.vdir); | 
| 527 | d0 = DOT(wp[0], odev.v.vdir) - d; | 
| 528 | d1 = DOT(wp[1], odev.v.vdir) - d; | 
| 529 | /* work on copy of world points */ | 
| 530 | if (d0 <= d1) { | 
| 531 | VCOPY(wpc[0], wp[0]); VCOPY(wpc[1], wp[1]); | 
| 532 | } else { | 
| 533 | d = d0; d0 = d1; d1 = d; | 
| 534 | VCOPY(wpc[1], wp[0]); VCOPY(wpc[0], wp[1]); | 
| 535 | } | 
| 536 | if (d0 <= FTINY) { | 
| 537 | if (d1 <= FTINY) return(0); | 
| 538 | VSUB(wpc[0], wpc[0], wpc[1]); | 
| 539 | d = .99*d1/(d1-d0); | 
| 540 | VSUM(wpc[0], wpc[1], wpc[0], d); | 
| 541 | } | 
| 542 | /* get view coordinates and clip to window */ | 
| 543 | viewloc(ip[0], &odev.v, wpc[0]); | 
| 544 | viewloc(ip[1], &odev.v, wpc[1]); | 
| 545 | if (!clip(ip[0], ip[1], vmin, vmax)) | 
| 546 | return(0); | 
| 547 | dp[0][0] = ip[0][0]*odev.hres; | 
| 548 | dp[0][1] = ip[0][1]*odev.vres; | 
| 549 | dp[1][0] = ip[1][0]*odev.hres; | 
| 550 | dp[1][1] = ip[1][1]*odev.vres; | 
| 551 | return(1); | 
| 552 | } | 
| 553 |  | 
| 554 |  | 
| 555 | static void | 
| 556 | draw3dline(                     /* draw 3d line in world coordinates */ | 
| 557 | FVECT   wp[2] | 
| 558 | ) | 
| 559 | { | 
| 560 | int     dp[2][2]; | 
| 561 |  | 
| 562 | if (!ilclip(dp, wp)) | 
| 563 | return; | 
| 564 | XDrawLine(ourdisplay, gwind, ourgc, | 
| 565 | dp[0][0], odev.vres-1 - dp[0][1], | 
| 566 | dp[1][0], odev.vres-1 - dp[1][1]); | 
| 567 | } | 
| 568 |  | 
| 569 |  | 
| 570 | static void | 
| 571 | draw_grids(void)                        /* draw holodeck section grids */ | 
| 572 | { | 
| 573 | static uby8     gridrgb[3] = {0x0, 0xff, 0xff}; | 
| 574 | unsigned long  pixel; | 
| 575 |  | 
| 576 | if (ncolors > 0) | 
| 577 | pixel = pixval[get_pixel(gridrgb, xnewcolr)]; | 
| 578 | else | 
| 579 | pixel = true_pixel(gridrgb); | 
| 580 | XSetForeground(ourdisplay, ourgc, pixel); | 
| 581 | /* draw each grid line */ | 
| 582 | gridlines(draw3dline); | 
| 583 | } | 
| 584 |  | 
| 585 |  | 
| 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 | nv = odev.v; | 
| 600 | /* change view direction */ | 
| 601 | if (mov | orb) { | 
| 602 | if ((li = qtFindLeaf(dx, dy)) < 0) | 
| 603 | return(0);      /* not on window */ | 
| 604 | VSUM(odir, qtL.wp[li], nv.vp, -1.); | 
| 605 | } else { | 
| 606 | if (viewray(nv.vp, nv.vdir, &odev.v, | 
| 607 | (dx+.5)/odev.hres, (dy+.5)/odev.vres) < -FTINY) | 
| 608 | return(0);      /* outside view */ | 
| 609 | } | 
| 610 | if (orb && mov) {               /* orbit left/right */ | 
| 611 | spinvector(odir, odir, nv.vup, d=MOVDEG*PI/180.*mov); | 
| 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.) | 
| 617 | return(0); | 
| 618 | spinvector(odir, odir, v1, d=MOVDEG*PI/180.*orb); | 
| 619 | VSUM(nv.vp, qtL.wp[li], odir, -1.); | 
| 620 | spinvector(nv.vdir, nv.vdir, v1, d); | 
| 621 | } else if (mov) {               /* move forward/backward */ | 
| 622 | d = MOVPCT/100. * mov; | 
| 623 | VSUM(nv.vp, nv.vp, odir, d); | 
| 624 | } | 
| 625 | if (!mov ^ !orb && headlocked) {        /* restore head height */ | 
| 626 | VSUM(v1, odev.v.vp, nv.vp, -1.); | 
| 627 | d = DOT(v1, odev.v.vup); | 
| 628 | VSUM(nv.vp, nv.vp, odev.v.vup, d); | 
| 629 | } | 
| 630 | if (setview(&nv) != NULL) | 
| 631 | return(0);      /* illegal view */ | 
| 632 | dev_view(&nv); | 
| 633 | inpresflags |= DFL(DC_SETVIEW); | 
| 634 | return(1); | 
| 635 | } | 
| 636 |  | 
| 637 |  | 
| 638 | static void | 
| 639 | getframe(                               /* get focus frame */ | 
| 640 | XButtonPressedEvent     *ebut | 
| 641 | ) | 
| 642 | { | 
| 643 | int     startx = ebut->x, starty = ebut->y; | 
| 644 | int     endx, endy, midx, midy; | 
| 645 | FVECT   v1; | 
| 646 | int     li; | 
| 647 | /* get mouse drag */ | 
| 648 | XMaskEvent(ourdisplay, ButtonReleaseMask, levptr(XEvent)); | 
| 649 | endx = levptr(XButtonReleasedEvent)->x; | 
| 650 | endy = levptr(XButtonReleasedEvent)->y; | 
| 651 | midx = (startx + endx) >> 1; | 
| 652 | midy = (starty + endy) >> 1; | 
| 653 | /* set focus distance */ | 
| 654 | if ((li = qtFindLeaf(midx, midy)) < 0) | 
| 655 | return;                 /* not on window */ | 
| 656 | VCOPY(v1, qtL.wp[li]); | 
| 657 | odev.v.vdist = sqrt(dist2(odev.v.vp, v1)); | 
| 658 | /* set frame for rendering */ | 
| 659 | if ((endx == startx) | (endy == starty)) | 
| 660 | return; | 
| 661 | if (endx < startx) {register int c = endx; endx = startx; startx = c;} | 
| 662 | if (endy < starty) {register int c = endy; endy = starty; starty = c;} | 
| 663 | sprintf(odev_args, "%.3f %.3f %.3f %.3f", | 
| 664 | (startx+.5)/odev.hres, 1.-(endy+.5)/odev.vres, | 
| 665 | (endx+.5)/odev.hres, 1.-(starty+.5)/odev.vres); | 
| 666 | inpresflags |= DFL(DC_FOCUS); | 
| 667 | } | 
| 668 |  | 
| 669 |  | 
| 670 | static void | 
| 671 | waitabit(void)                          /* pause a moment */ | 
| 672 | { | 
| 673 | struct timespec ts; | 
| 674 | ts.tv_sec = 0; | 
| 675 | ts.tv_nsec = 100000000; | 
| 676 | nanosleep(&ts, NULL); | 
| 677 | } | 
| 678 |  | 
| 679 |  | 
| 680 | static void | 
| 681 | getmove(                                /* get view change */ | 
| 682 | XButtonPressedEvent     *ebut | 
| 683 | ) | 
| 684 | { | 
| 685 | int     movdir = MOVDIR(ebut->button); | 
| 686 | int     movorb = MOVORB(ebut->state); | 
| 687 | int     oldnodesiz = qtMinNodesiz; | 
| 688 | Window  rootw, childw; | 
| 689 | int     rootx, rooty, wx, wy; | 
| 690 | unsigned int    statemask; | 
| 691 |  | 
| 692 | qtMinNodesiz = 16;              /* for quicker update */ | 
| 693 | XNoOp(ourdisplay); | 
| 694 |  | 
| 695 | while (!XCheckMaskEvent(ourdisplay, | 
| 696 | ButtonReleaseMask, levptr(XEvent))) { | 
| 697 | waitabit(); | 
| 698 | if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, | 
| 699 | &rootx, &rooty, &wx, &wy, &statemask)) | 
| 700 | break;          /* on another screen */ | 
| 701 |  | 
| 702 | if (!moveview(wx, odev.vres-1-wy, movdir, movorb)) { | 
| 703 | sleep(1); | 
| 704 | continue; | 
| 705 | } | 
| 706 | XClearWindow(ourdisplay, gwind); | 
| 707 | qtUpdate(); | 
| 708 | draw_grids(); | 
| 709 | } | 
| 710 | if (!(inpresflags & DFL(DC_SETVIEW))) { /* do final motion */ | 
| 711 | movdir = MOVDIR(levptr(XButtonReleasedEvent)->button); | 
| 712 | wx = levptr(XButtonReleasedEvent)->x; | 
| 713 | wy = levptr(XButtonReleasedEvent)->y; | 
| 714 | moveview(wx, odev.vres-1-wy, movdir, movorb); | 
| 715 | } | 
| 716 | dev_flush(); | 
| 717 |  | 
| 718 | qtMinNodesiz = oldnodesiz;      /* restore quadtree resolution */ | 
| 719 | } | 
| 720 |  | 
| 721 |  | 
| 722 | static void | 
| 723 | getkey(                         /* get input key */ | 
| 724 | register XKeyPressedEvent  *ekey | 
| 725 | ) | 
| 726 | { | 
| 727 | Window  rootw, childw; | 
| 728 | int     rootx, rooty, wx, wy; | 
| 729 | unsigned int    statemask; | 
| 730 | int  n; | 
| 731 | char    buf[8]; | 
| 732 |  | 
| 733 | n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); | 
| 734 | if (n != 1) | 
| 735 | return; | 
| 736 | switch (buf[0]) { | 
| 737 | case 'h':                       /* turn on height motion lock */ | 
| 738 | headlocked = 1; | 
| 739 | return; | 
| 740 | case 'H':                       /* turn off height motion lock */ | 
| 741 | headlocked = 0; | 
| 742 | return; | 
| 743 | case 'l':                       /* retrieve last view */ | 
| 744 | inpresflags |= DFL(DC_LASTVIEW); | 
| 745 | return; | 
| 746 | case 'f':                       /* frame view position */ | 
| 747 | if (!XQueryPointer(ourdisplay, gwind, &rootw, &childw, | 
| 748 | &rootx, &rooty, &wx, &wy, &statemask)) | 
| 749 | return;         /* on another screen */ | 
| 750 | sprintf(odev_args, "%.4f %.4f", (wx+.5)/odev.hres, | 
| 751 | 1.-(wy+.5)/odev.vres); | 
| 752 | inpresflags |= DFL(DC_FOCUS); | 
| 753 | return; | 
| 754 | case 'F':                       /* unfocus */ | 
| 755 | odev_args[0] = '\0'; | 
| 756 | inpresflags |= DFL(DC_FOCUS); | 
| 757 | return; | 
| 758 | case 'p':                       /* pause computation */ | 
| 759 | inpresflags |= DFL(DC_PAUSE); | 
| 760 | return; | 
| 761 | case 'v':                       /* spit out view */ | 
| 762 | inpresflags |= DFL(DC_GETVIEW); | 
| 763 | return; | 
| 764 | case '\n': | 
| 765 | case '\r':                      /* resume computation */ | 
| 766 | inpresflags |= DFL(DC_RESUME); | 
| 767 | return; | 
| 768 | case CTRL('R'):                 /* redraw screen */ | 
| 769 | if (ncolors > 0) | 
| 770 | new_ctab(ncolors); | 
| 771 | qtRedraw(0, 0, odev.hres, odev.vres); | 
| 772 | return; | 
| 773 | case CTRL('L'):                 /* refresh from server */ | 
| 774 | if (inpresflags & DFL(DC_REDRAW)) | 
| 775 | return; | 
| 776 | XClearWindow(ourdisplay, gwind); | 
| 777 | draw_grids(); | 
| 778 | XFlush(ourdisplay); | 
| 779 | qtCompost(100);                 /* unload the old tree */ | 
| 780 | if (ncolors > 0) | 
| 781 | new_ctab(ncolors); | 
| 782 | inpresflags |= DFL(DC_REDRAW);  /* resend values from server */ | 
| 783 | rayqleft = 0;                   /* hold off update */ | 
| 784 | return; | 
| 785 | case 'K':                       /* kill rtrace process(es) */ | 
| 786 | inpresflags |= DFL(DC_KILL); | 
| 787 | break; | 
| 788 | case 'R':                       /* restart rtrace */ | 
| 789 | inpresflags |= DFL(DC_RESTART); | 
| 790 | break; | 
| 791 | case 'C':                       /* clobber holodeck */ | 
| 792 | inpresflags |= DFL(DC_CLOBBER); | 
| 793 | break; | 
| 794 | case 'q':                       /* quit the program */ | 
| 795 | inpresflags |= DFL(DC_QUIT); | 
| 796 | return; | 
| 797 | default: | 
| 798 | XBell(ourdisplay, 0); | 
| 799 | return; | 
| 800 | } | 
| 801 | } | 
| 802 |  | 
| 803 |  | 
| 804 | static void | 
| 805 | fixwindow(                              /* repair damage to window */ | 
| 806 | register XExposeEvent  *eexp | 
| 807 | ) | 
| 808 | { | 
| 809 | if (odev.hres == 0 || odev.vres == 0) { /* first exposure */ | 
| 810 | odev.hres = eexp->width; | 
| 811 | odev.vres = eexp->height; | 
| 812 | } | 
| 813 | qtRedraw(eexp->x, odev.vres - eexp->y - eexp->height, | 
| 814 | eexp->x + eexp->width, odev.vres - eexp->y); | 
| 815 | } | 
| 816 |  | 
| 817 |  | 
| 818 | static void | 
| 819 | resizewindow(                   /* resize window */ | 
| 820 | register XConfigureEvent  *ersz | 
| 821 | ) | 
| 822 | { | 
| 823 | if (ersz->width == odev.hres && ersz->height == odev.vres) | 
| 824 | return; | 
| 825 |  | 
| 826 | odev.hres = ersz->width; | 
| 827 | odev.vres = ersz->height; | 
| 828 |  | 
| 829 | odev.v.horiz = 2.*180./PI * atan(0.5/VIEWDIST*pwidth*odev.hres); | 
| 830 | odev.v.vert = 2.*180./PI * atan(0.5/VIEWDIST*pheight*odev.vres); | 
| 831 |  | 
| 832 | inpresflags |= DFL(DC_SETVIEW); | 
| 833 | } |