| 1 | < | /* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 1 | > | /* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 |  |  | 
| 3 |  | #ifndef lint | 
| 4 |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 18 |  | #define VIEWHISTLEN     4       /* number of remembered views */ | 
| 19 |  | #endif | 
| 20 |  |  | 
| 21 | + | #ifndef FSIZDEF | 
| 22 | + | #define FSIZDEF         0.125   /* default focus frame size */ | 
| 23 | + | #endif | 
| 24 | + |  | 
| 25 |  | HOLO    *hdlist[HDMAX+1];       /* global holodeck list */ | 
| 26 |  |  | 
| 27 |  | char    cmdlist[DC_NCMDS][8] = DC_INIT; | 
| 30 |  |  | 
| 31 |  | int     do_outside = 0;         /* render from outside sections */ | 
| 32 |  |  | 
| 33 | + | double  eyesepdist = 1;         /* eye separation distance */ | 
| 34 | + |  | 
| 35 |  | char    *progname;              /* global argv[0] */ | 
| 36 |  |  | 
| 37 |  | FILE    *sstdin, *sstdout;      /* server's standard input and output */ | 
| 38 |  |  | 
| 39 | + | #ifdef DEBUG | 
| 40 | + | #include <sys/types.h> | 
| 41 | + | extern time_t   time(); | 
| 42 | + | static time_t   tmodesw; | 
| 43 | + | static time_t   timm, tadd; | 
| 44 | + | static long     nimmrays, naddrays; | 
| 45 | + | #endif | 
| 46 | + |  | 
| 47 |  | #define RDY_SRV         01 | 
| 48 |  | #define RDY_DEV         02 | 
| 49 |  | #define RDY_SIN         04 | 
| 69 |  | sstdin = fdopen(inp, "r"); | 
| 70 |  | /* set command error vector */ | 
| 71 |  | erract[COMMAND].pf = eputs; | 
| 72 | + | #ifdef DEBUG | 
| 73 | + | tmodesw = time(NULL); | 
| 74 | + | #endif | 
| 75 |  | /* enter main loop */ | 
| 76 |  | do { | 
| 77 |  | rdy = disp_wait(); | 
| 92 |  | printview(); | 
| 93 |  | if (inp & DFL(DC_LASTVIEW)) | 
| 94 |  | new_view(NULL); | 
| 95 | < | if (inp & DFL(DC_RESUME)) { | 
| 96 | < | serv_request(DR_NOOP, 0, NULL); | 
| 80 | < | pause = 0; | 
| 81 | < | } | 
| 82 | < | if (inp & DFL(DC_PAUSE)) | 
| 83 | < | pause = 1; | 
| 84 | < | if (inp & DFL(DC_REDRAW)) | 
| 85 | < | imm_mode = beam_sync() > 0; | 
| 95 | > | if (inp & DFL(DC_FOCUS)) | 
| 96 | > | set_focus(odev_args); | 
| 97 |  | if (inp & DFL(DC_KILL)) { | 
| 98 |  | serv_request(DR_KILL, 0, NULL); | 
| 99 |  | pause = 0; | 
| 104 |  | serv_request(DR_RESTART, 0, NULL); | 
| 105 |  | pause = 0; | 
| 106 |  | } | 
| 107 | + | if (inp & DFL(DC_RESUME)) { | 
| 108 | + | serv_request(DR_NOOP, 0, NULL); | 
| 109 | + | pause = 0; | 
| 110 | + | } | 
| 111 | + | if (inp & DFL(DC_PAUSE)) | 
| 112 | + | pause = 1; | 
| 113 | + | if (inp & DFL(DC_REDRAW)) | 
| 114 | + | imm_mode = beam_sync(1) > 0; | 
| 115 |  | if (inp & DFL(DC_QUIT)) | 
| 116 |  | serv_request(DR_SHUTDOWN, 0, NULL); | 
| 117 |  | } | 
| 118 | < | if (rdy & RDY_SIN)              /* user input from sstdin */ | 
| 118 | > | if (rdy & RDY_SIN && !imm_mode) /* user input from sstdin */ | 
| 119 |  | switch (usr_input()) { | 
| 120 |  | case DC_PAUSE: | 
| 121 |  | pause = 1; | 
| 129 |  | break; | 
| 130 |  | } | 
| 131 |  | } while (res != DS_SHUTDOWN); | 
| 132 | + | #ifdef DEBUG | 
| 133 | + | if (timm && nimmrays) | 
| 134 | + | fprintf(stderr, | 
| 135 | + | "%s: %.1f rays recalled/second (%ld rays total)\n", | 
| 136 | + | progname, (double)nimmrays/timm, nimmrays); | 
| 137 | + | if (tadd && naddrays) | 
| 138 | + | fprintf(stderr, | 
| 139 | + | "%s: %.1f rays calculated/second (%ld rays total)\n", | 
| 140 | + | progname, (double)naddrays/tadd, naddrays); | 
| 141 | + | #endif | 
| 142 |  | /* all done */ | 
| 143 |  | quit(0); | 
| 144 |  | } | 
| 152 |  | int     n; | 
| 153 |  | register int    i; | 
| 154 |  | /* see if we can avoid select call */ | 
| 155 | + | flgs = 0;               /* flag what's ready already */ | 
| 156 |  | if (imm_mode || stdin->_cnt > 0) | 
| 157 | < | return(RDY_SRV); | 
| 157 | > | flgs |= RDY_SRV; | 
| 158 |  | if (sstdin != NULL && sstdin->_cnt > 0) | 
| 159 | < | return(RDY_SIN); | 
| 160 | < | if (dev_flush()) | 
| 159 | > | flgs |= RDY_SIN; | 
| 160 | > | if (odev.inpready) | 
| 161 | > | flgs |= RDY_DEV; | 
| 162 | > | if (flgs)               /* got something? */ | 
| 163 | > | return(flgs); | 
| 164 | > | if (dev_flush())        /* else flush output & check keyboard+mouse */ | 
| 165 |  | return(RDY_DEV); | 
| 166 | < | /* make the call */ | 
| 166 | > | /* if nothing, we need to call select */ | 
| 167 |  | FD_ZERO(&readset); FD_ZERO(&errset); | 
| 168 |  | FD_SET(0, &readset); | 
| 169 |  | FD_SET(0, &errset); | 
| 172 |  | n = odev.ifd+1; | 
| 173 |  | if (sstdin != NULL) { | 
| 174 |  | FD_SET(fileno(sstdin), &readset); | 
| 175 | + | FD_SET(fileno(sstdin), &errset); | 
| 176 |  | if (fileno(sstdin) >= n) | 
| 177 |  | n = fileno(sstdin) + 1; | 
| 178 |  | } | 
| 182 |  | return(0); | 
| 183 |  | error(SYSTEM, "select call failure in disp_wait"); | 
| 184 |  | } | 
| 150 | – | flgs = 0;               /* flag what's ready */ | 
| 185 |  | if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset)) | 
| 186 |  | flgs |= RDY_SRV; | 
| 187 |  | if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset)) | 
| 188 |  | flgs |= RDY_DEV; | 
| 189 | < | if (sstdin != NULL && FD_ISSET(fileno(sstdin), &readset)) | 
| 189 | > | if (sstdin != NULL && (FD_ISSET(fileno(sstdin), &readset) || | 
| 190 | > | FD_ISSET(fileno(sstdin), &errset))) | 
| 191 |  | flgs |= RDY_SIN; | 
| 192 |  | return(flgs); | 
| 193 |  | } | 
| 244 |  | for (i = p->nr; i--; ) { | 
| 245 |  | hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r); | 
| 246 |  | d = hddepth(hdlist[p->hd], packra(p)[i].d); | 
| 247 | < | VSUM(wp, ro, rd, d);            /* might be behind viewpoint */ | 
| 248 | < | dev_value(packra(p)[i].v, wp, rd); | 
| 247 | > | if (d < .99*FHUGE) { | 
| 248 | > | VSUM(wp, ro, rd, d);    /* might be behind viewpoint */ | 
| 249 | > | dev_value(packra(p)[i].v, rd, wp); | 
| 250 | > | } else | 
| 251 | > | dev_value(packra(p)[i].v, rd, NULL); | 
| 252 |  | } | 
| 253 | + | #ifdef DEBUG | 
| 254 | + | if (imm_mode) nimmrays += p->nr; | 
| 255 | + | else naddrays += p->nr; | 
| 256 | + | #endif | 
| 257 |  | } | 
| 258 |  |  | 
| 259 |  |  | 
| 262 |  | { | 
| 263 |  | static VIEW     viewhist[VIEWHISTLEN]; | 
| 264 |  | static unsigned nhist; | 
| 265 | + | VIEW    *dv; | 
| 266 | + | int     i, res[2]; | 
| 267 |  | char    *err; | 
| 268 |  | /* restore previous view? */ | 
| 269 |  | if (v == NULL) { | 
| 283 |  | error(COMMAND, "cannot handle parallel views"); | 
| 284 |  | return; | 
| 285 |  | } | 
| 286 | < | if (!dev_view(v))       /* update display driver */ | 
| 286 | > | if (!dev_view(v))       /* notify display driver */ | 
| 287 |  | goto again; | 
| 288 |  | dev_flush();            /* update screen */ | 
| 289 | < | if (!beam_view(v))      /* update beam list */ | 
| 290 | < | goto again; | 
| 289 | > | beam_init(0);           /* compute new beam set */ | 
| 290 | > | for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) | 
| 291 | > | if (!beam_view(dv, res[0], res[1])) { | 
| 292 | > | if (!nhist) { | 
| 293 | > | error(COMMAND, "invalid starting view"); | 
| 294 | > | return; | 
| 295 | > | } | 
| 296 | > | copystruct(v, viewhist + ((nhist-1)%VIEWHISTLEN)); | 
| 297 | > | goto again; | 
| 298 | > | } | 
| 299 | > | /* update server */ | 
| 300 | > | imm_mode = beam_sync(0) > 0; | 
| 301 |  | /* record new view */ | 
| 302 |  | if (v < viewhist || v >= viewhist+VIEWHISTLEN) { | 
| 303 |  | copystruct(viewhist + (nhist%VIEWHISTLEN), v); | 
| 306 |  | } | 
| 307 |  |  | 
| 308 |  |  | 
| 309 | + | set_focus(args)                 /* set focus frame */ | 
| 310 | + | char    *args; | 
| 311 | + | { | 
| 312 | + | double  hcent, vcent, hsiz, vsiz; | 
| 313 | + | VIEW    *dv, vwfocus; | 
| 314 | + | int     i, res[2]; | 
| 315 | + |  | 
| 316 | + | i = sscanf(args, "%lf %lf %lf %lf", &hcent, &vcent, &hsiz, &vsiz); | 
| 317 | + | if (i < 2 || hcent < 0 || hcent > 1 || vcent < 0 || vcent > 1) { | 
| 318 | + | beam_init(0);                           /* restore view */ | 
| 319 | + | for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) | 
| 320 | + | beam_view(dv, res[0], res[1]); | 
| 321 | + | beam_sync(0);                           /* update server */ | 
| 322 | + | return; | 
| 323 | + | } | 
| 324 | + | if (i < 4 || hsiz <= hcent || hsiz > 1 || vsiz <= vcent || vsiz > 1) | 
| 325 | + | hsiz = vsiz = FSIZDEF;                  /* gave center only */ | 
| 326 | + | else { | 
| 327 | + | hsiz -= hcent; hcent += 0.5*hsiz;       /* gave min and max */ | 
| 328 | + | vsiz -= vcent; vcent += 0.5*vsiz; | 
| 329 | + | } | 
| 330 | + | beam_init(0);                                   /* add basic views */ | 
| 331 | + | for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) | 
| 332 | + | beam_view(dv, res[0]>>4, res[1]>>4); | 
| 333 | + | copystruct(&vwfocus, &odev.v);                  /* add focus view */ | 
| 334 | + | switch (odev.v.type) { | 
| 335 | + | case VT_PER: | 
| 336 | + | vwfocus.horiz = 2.*180./PI*atan( | 
| 337 | + | hsiz * tan(PI/180./2.*odev.v.horiz) ); | 
| 338 | + | vwfocus.vert = 2.*180./PI*atan( | 
| 339 | + | vsiz * tan(PI/180./2.*odev.v.vert) ); | 
| 340 | + | break; | 
| 341 | + | case VT_PAR: | 
| 342 | + | case VT_ANG: | 
| 343 | + | vwfocus.horiz = hsiz * odev.v.horiz; | 
| 344 | + | vwfocus.vert = vsiz * odev.v.vert; | 
| 345 | + | break; | 
| 346 | + | case VT_HEM: | 
| 347 | + | vwfocus.horiz = 2.*180./PI*asin( | 
| 348 | + | hsiz * sin(PI/180./2.*odev.v.horiz) ); | 
| 349 | + | vwfocus.vert = 2.*180./PI*asin( | 
| 350 | + | vsiz * sin(PI/180./2.*odev.v.vert) ); | 
| 351 | + | break; | 
| 352 | + | case VT_CYL: | 
| 353 | + | vwfocus.horiz = hsiz * odev.v.horiz; | 
| 354 | + | vwfocus.vert = 2.*180./PI*atan( | 
| 355 | + | vsiz * tan(PI/180./2.*odev.v.vert) ); | 
| 356 | + | break; | 
| 357 | + | default: | 
| 358 | + | error(INTERNAL, "bad view type in set_focus"); | 
| 359 | + | } | 
| 360 | + | vwfocus.hoff = (odev.v.hoff + hcent - 0.5)/hsiz; | 
| 361 | + | vwfocus.voff = (odev.v.voff + vcent - 0.5)/vsiz; | 
| 362 | + | setview(&vwfocus); | 
| 363 | + | beam_view(&vwfocus, (int)(3*odev.hres*hsiz)+100, | 
| 364 | + | (int)(3*odev.vres*vsiz)+100); | 
| 365 | + | beam_sync(0);                                   /* update server */ | 
| 366 | + | } | 
| 367 | + |  | 
| 368 | + |  | 
| 369 |  | int | 
| 370 |  | usr_input()                     /* get user input and process it */ | 
| 371 |  | { | 
| 372 |  | VIEW    vparams; | 
| 373 | < | char    cmd[128]; | 
| 373 | > | char    cmd[256]; | 
| 374 |  | register char   *args; | 
| 375 |  | register int    i; | 
| 376 |  |  | 
| 377 | < | if (fgets(cmd, sizeof(cmd), sstdin) == NULL) | 
| 378 | < | return(DC_QUIT); | 
| 377 | > | if (fgets(cmd, sizeof(cmd), sstdin) == NULL) { | 
| 378 | > | fclose(sstdin); | 
| 379 | > | sstdin = NULL; | 
| 380 | > | return(-1); | 
| 381 | > | } | 
| 382 | > | if (*cmd == '\n') | 
| 383 | > | return(DC_RESUME); | 
| 384 |  | for (args = cmd; *args && !isspace(*args); args++) | 
| 385 |  | ; | 
| 386 |  | while (isspace(*args)) | 
| 387 |  | *args++ = '\0'; | 
| 269 | – | if (!*cmd) | 
| 270 | – | return(DC_RESUME); | 
| 388 |  | if (*args && args[i=strlen(args)-1] == '\n') | 
| 389 |  | args[i] = '\0'; | 
| 390 |  | for (i = 0; i < DC_NCMDS; i++) | 
| 391 |  | if (!strcmp(cmd, cmdlist[i])) | 
| 392 |  | break; | 
| 393 |  | if (i >= DC_NCMDS) { | 
| 394 | < | sprintf(errmsg, "unknown command: %s", cmd); | 
| 278 | < | error(COMMAND, errmsg); | 
| 394 | > | dev_auxcom(cmd, args); | 
| 395 |  | return(-1); | 
| 396 |  | } | 
| 397 |  | switch (i) { | 
| 408 |  | case DC_LASTVIEW:               /* restore previous view */ | 
| 409 |  | new_view(NULL); | 
| 410 |  | break; | 
| 411 | + | case DC_FOCUS:                  /* set focus frame */ | 
| 412 | + | set_focus(args); | 
| 413 | + | break; | 
| 414 |  | case DC_PAUSE:                  /* pause the current calculation */ | 
| 415 |  | case DC_RESUME:                 /* resume the calculation */ | 
| 416 |  | /* handled in main() */ | 
| 417 |  | break; | 
| 418 |  | case DC_REDRAW:                 /* redraw from server */ | 
| 419 | < | imm_mode = beam_sync() > 0; | 
| 419 | > | imm_mode = beam_sync(1) > 0; | 
| 420 | > | dev_clear(); | 
| 421 |  | break; | 
| 422 |  | case DC_KILL:                   /* kill rtrace process(es) */ | 
| 423 |  | serv_request(DR_KILL, 0, NULL); | 
| 483 |  | break; | 
| 484 |  | case DS_OUTSECT: | 
| 485 |  | do_outside = 1; | 
| 486 | + | goto noargs; | 
| 487 | + | case DS_EYESEP: | 
| 488 | + | if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY) | 
| 489 | + | error(INTERNAL, "bad eye separation from server"); | 
| 490 |  | break; | 
| 491 |  | case DS_STARTIMM: | 
| 492 |  | case DS_ENDIMM: | 
| 493 | < | imm_mode = msg.type==DS_STARTIMM; | 
| 494 | < | /* fall through */ | 
| 493 | > | #ifdef DEBUG | 
| 494 | > | { | 
| 495 | > | time_t  tnow = time(NULL); | 
| 496 | > | if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw; | 
| 497 | > | else timm += tnow - tmodesw; | 
| 498 | > | tmodesw = tnow; | 
| 499 | > | } | 
| 500 | > | #endif | 
| 501 | > | if (!(imm_mode = msg.type==DS_STARTIMM)) | 
| 502 | > | dev_flush(); | 
| 503 | > | goto noargs; | 
| 504 |  | case DS_ACKNOW: | 
| 505 |  | case DS_SHUTDOWN: | 
| 506 | < | if (msg.nbytes) { | 
| 374 | < | sprintf(errmsg, | 
| 375 | < | "unexpected body with server message %d", | 
| 376 | < | msg.type); | 
| 377 | < | error(INTERNAL, errmsg); | 
| 378 | < | } | 
| 379 | < | break; | 
| 506 | > | goto noargs; | 
| 507 |  | default: | 
| 508 |  | error(INTERNAL, "unrecognized result from server process"); | 
| 509 |  | } | 
| 510 |  | return(msg.type);               /* return message type */ | 
| 511 | + | noargs: | 
| 512 | + | if (msg.nbytes) { | 
| 513 | + | sprintf(errmsg, "unexpected body with server message %d", | 
| 514 | + | msg.type); | 
| 515 | + | error(INTERNAL, errmsg); | 
| 516 | + | } | 
| 517 | + | return(msg.type); | 
| 518 |  | readerr: | 
| 519 |  | if (feof(stdin)) | 
| 520 |  | error(SYSTEM, "server process died"); | 
| 528 |  | { | 
| 529 |  | MSGHEAD msg; | 
| 530 |  | int     m; | 
| 531 | + | /* consistency checks */ | 
| 532 | + | #ifdef DEBUG | 
| 533 | + | if (nbytes < 0 || nbytes > 0 & p == NULL) | 
| 534 | + | error(CONSISTENCY, "bad buffer handed to serv_request"); | 
| 535 | + | #endif | 
| 536 |  | /* get server's attention for big request */ | 
| 537 |  | if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) { | 
| 538 |  | serv_request(DR_ATTEN, 0, NULL); |