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