| 1 | 
< | 
/* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 1 | 
> | 
/* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 | 
  | 
 | 
| 3 | 
  | 
#ifndef lint | 
| 4 | 
  | 
static char SCCSid[] = "$SunId$ SGI"; | 
| 15 | 
  | 
#include <ctype.h> | 
| 16 | 
  | 
 | 
| 17 | 
  | 
#ifndef VIEWHISTLEN | 
| 18 | 
< | 
#define VIEWHISTLEN     2       /* number of remembered views */ | 
| 18 | 
> | 
#define VIEWHISTLEN     4       /* number of remembered views */ | 
| 19 | 
  | 
#endif | 
| 20 | 
  | 
 | 
| 21 | 
  | 
HOLO    *hdlist[HDMAX+1];       /* global holodeck list */ | 
| 24 | 
  | 
 | 
| 25 | 
  | 
int     imm_mode = 0;           /* bundles are being delivered immediately */ | 
| 26 | 
  | 
 | 
| 27 | 
+ | 
int     do_outside = 0;         /* render from outside sections */ | 
| 28 | 
+ | 
 | 
| 29 | 
+ | 
double  eyesepdist = 1;         /* eye separation distance */ | 
| 30 | 
+ | 
 | 
| 31 | 
  | 
char    *progname;              /* global argv[0] */ | 
| 32 | 
  | 
 | 
| 33 | 
  | 
FILE    *sstdin, *sstdout;      /* server's standard input and output */ | 
| 34 | 
  | 
 | 
| 35 | 
+ | 
#ifdef DEBUG | 
| 36 | 
+ | 
#include <sys/types.h> | 
| 37 | 
+ | 
extern time_t   time(); | 
| 38 | 
+ | 
static time_t   tmodesw; | 
| 39 | 
+ | 
static time_t   timm, tadd; | 
| 40 | 
+ | 
static long     nimmrays, naddrays; | 
| 41 | 
+ | 
#endif | 
| 42 | 
+ | 
 | 
| 43 | 
  | 
#define RDY_SRV         01 | 
| 44 | 
  | 
#define RDY_DEV         02 | 
| 45 | 
  | 
#define RDY_SIN         04 | 
| 65 | 
  | 
                sstdin = fdopen(inp, "r"); | 
| 66 | 
  | 
                                        /* set command error vector */ | 
| 67 | 
  | 
        erract[COMMAND].pf = eputs; | 
| 68 | 
+ | 
#ifdef DEBUG | 
| 69 | 
+ | 
        tmodesw = time(NULL); | 
| 70 | 
+ | 
#endif | 
| 71 | 
  | 
                                        /* enter main loop */ | 
| 72 | 
  | 
        do { | 
| 73 | 
  | 
                rdy = disp_wait(); | 
| 74 | 
+ | 
                if (rdy & RDY_SRV) {            /* process server result */ | 
| 75 | 
+ | 
                        res = serv_result(); | 
| 76 | 
+ | 
                        if (pause && res != DS_SHUTDOWN) { | 
| 77 | 
+ | 
                                serv_request(DR_ATTEN, 0, NULL); | 
| 78 | 
+ | 
                                while ((res = serv_result()) != DS_ACKNOW && | 
| 79 | 
+ | 
                                                res != DS_SHUTDOWN) | 
| 80 | 
+ | 
                                        ; | 
| 81 | 
+ | 
                        } | 
| 82 | 
+ | 
                } | 
| 83 | 
  | 
                if (rdy & RDY_DEV) {            /* user input from driver */ | 
| 84 | 
  | 
                        inp = dev_input(); | 
| 85 | 
  | 
                        if (inp & DFL(DC_SETVIEW)) | 
| 95 | 
  | 
                        if (inp & DFL(DC_PAUSE)) | 
| 96 | 
  | 
                                pause = 1; | 
| 97 | 
  | 
                        if (inp & DFL(DC_REDRAW)) | 
| 98 | 
< | 
                                imm_mode = beam_sync() > 0; | 
| 98 | 
> | 
                                imm_mode = beam_sync(1) > 0; | 
| 99 | 
  | 
                        if (inp & DFL(DC_KILL)) { | 
| 100 | 
  | 
                                serv_request(DR_KILL, 0, NULL); | 
| 101 | 
  | 
                                pause = 0; | 
| 122 | 
  | 
                                pause = 0; | 
| 123 | 
  | 
                                break; | 
| 124 | 
  | 
                        } | 
| 101 | 
– | 
                if (rdy & RDY_SRV) {            /* process server result */ | 
| 102 | 
– | 
                        res = serv_result(); | 
| 103 | 
– | 
                        if (pause && res != DS_SHUTDOWN) { | 
| 104 | 
– | 
                                serv_request(DR_ATTEN, 0, NULL); | 
| 105 | 
– | 
                                while ((res = serv_result()) != DS_ACKNOW && | 
| 106 | 
– | 
                                                res != DS_SHUTDOWN) | 
| 107 | 
– | 
                                        ; | 
| 108 | 
– | 
                        } | 
| 109 | 
– | 
                } | 
| 125 | 
  | 
        } while (res != DS_SHUTDOWN); | 
| 126 | 
+ | 
#ifdef DEBUG | 
| 127 | 
+ | 
        if (timm && nimmrays) | 
| 128 | 
+ | 
                fprintf(stderr, | 
| 129 | 
+ | 
                        "%s: %.1f rays recalled/second (%ld rays total)\n", | 
| 130 | 
+ | 
                                progname, (double)nimmrays/timm, nimmrays); | 
| 131 | 
+ | 
        if (tadd && naddrays) | 
| 132 | 
+ | 
                fprintf(stderr, | 
| 133 | 
+ | 
                        "%s: %.1f rays calculated/second (%ld rays total)\n", | 
| 134 | 
+ | 
                                progname, (double)naddrays/tadd, naddrays); | 
| 135 | 
+ | 
#endif | 
| 136 | 
  | 
                                        /* all done */ | 
| 137 | 
  | 
        quit(0); | 
| 138 | 
  | 
} | 
| 146 | 
  | 
        int     n; | 
| 147 | 
  | 
        register int    i; | 
| 148 | 
  | 
                                /* see if we can avoid select call */ | 
| 149 | 
+ | 
        flgs = 0;               /* flag what's ready already */ | 
| 150 | 
  | 
        if (imm_mode || stdin->_cnt > 0) | 
| 151 | 
< | 
                return(RDY_SRV); | 
| 151 | 
> | 
                flgs |= RDY_SRV; | 
| 152 | 
  | 
        if (sstdin != NULL && sstdin->_cnt > 0) | 
| 153 | 
< | 
                return(RDY_SIN); | 
| 154 | 
< | 
        if (dev_flush()) | 
| 153 | 
> | 
                flgs |= RDY_SIN; | 
| 154 | 
> | 
        if (odev.inpready) | 
| 155 | 
> | 
                flgs |= RDY_DEV; | 
| 156 | 
> | 
        if (flgs)               /* got something? */ | 
| 157 | 
> | 
                return(flgs); | 
| 158 | 
> | 
        if (dev_flush())        /* else flush output & check keyboard+mouse */ | 
| 159 | 
  | 
                return(RDY_DEV); | 
| 160 | 
< | 
                                /* make the call */ | 
| 160 | 
> | 
                                /* if nothing, we need to call select */ | 
| 161 | 
  | 
        FD_ZERO(&readset); FD_ZERO(&errset); | 
| 162 | 
  | 
        FD_SET(0, &readset); | 
| 163 | 
  | 
        FD_SET(0, &errset); | 
| 166 | 
  | 
        n = odev.ifd+1; | 
| 167 | 
  | 
        if (sstdin != NULL) { | 
| 168 | 
  | 
                FD_SET(fileno(sstdin), &readset); | 
| 169 | 
+ | 
                FD_SET(fileno(sstdin), &errset); | 
| 170 | 
  | 
                if (fileno(sstdin) >= n) | 
| 171 | 
  | 
                        n = fileno(sstdin) + 1; | 
| 172 | 
  | 
        } | 
| 176 | 
  | 
                        return(0); | 
| 177 | 
  | 
                error(SYSTEM, "select call failure in disp_wait"); | 
| 178 | 
  | 
        } | 
| 148 | 
– | 
        flgs = 0;               /* flag what's ready */ | 
| 179 | 
  | 
        if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset)) | 
| 180 | 
  | 
                flgs |= RDY_SRV; | 
| 181 | 
  | 
        if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset)) | 
| 182 | 
  | 
                flgs |= RDY_DEV; | 
| 183 | 
< | 
        if (sstdin != NULL && FD_ISSET(fileno(sstdin), &readset)) | 
| 183 | 
> | 
        if (sstdin != NULL && (FD_ISSET(fileno(sstdin), &readset) || | 
| 184 | 
> | 
                                FD_ISSET(fileno(sstdin), &errset))) | 
| 185 | 
  | 
                flgs |= RDY_SIN; | 
| 186 | 
  | 
        return(flgs); | 
| 187 | 
  | 
} | 
| 191 | 
  | 
HDGRID  *hdg; | 
| 192 | 
  | 
{ | 
| 193 | 
  | 
        VIEW    nv; | 
| 194 | 
+ | 
        double  d; | 
| 195 | 
  | 
        register int    hd; | 
| 196 | 
  | 
 | 
| 197 | 
  | 
        for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++) | 
| 212 | 
  | 
        VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5); | 
| 213 | 
  | 
        fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]); | 
| 214 | 
  | 
        VCOPY(nv.vup, hdlist[0]->xv[2]); | 
| 215 | 
+ | 
        if (do_outside) { | 
| 216 | 
+ | 
                normalize(nv.vdir); | 
| 217 | 
+ | 
                d = VLEN(hdlist[0]->xv[1]); | 
| 218 | 
+ | 
                d += VLEN(hdlist[0]->xv[2]); | 
| 219 | 
+ | 
                VSUM(nv.vp, nv.vp, nv.vdir, -d); | 
| 220 | 
+ | 
        } | 
| 221 | 
  | 
        new_view(&nv); | 
| 222 | 
  | 
} | 
| 223 | 
  | 
 | 
| 238 | 
  | 
        for (i = p->nr; i--; ) { | 
| 239 | 
  | 
                hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r); | 
| 240 | 
  | 
                d = hddepth(hdlist[p->hd], packra(p)[i].d); | 
| 241 | 
< | 
                VSUM(wp, ro, rd, d);            /* might be behind viewpoint */ | 
| 242 | 
< | 
                dev_value(packra(p)[i].v, wp, rd); | 
| 241 | 
> | 
                if (d < .99*FHUGE) { | 
| 242 | 
> | 
                        VSUM(wp, ro, rd, d);    /* might be behind viewpoint */ | 
| 243 | 
> | 
                        dev_value(packra(p)[i].v, rd, wp); | 
| 244 | 
> | 
                } else | 
| 245 | 
> | 
                        dev_value(packra(p)[i].v, rd, NULL); | 
| 246 | 
  | 
        } | 
| 247 | 
+ | 
#ifdef DEBUG | 
| 248 | 
+ | 
        if (imm_mode) nimmrays += p->nr; | 
| 249 | 
+ | 
        else naddrays += p->nr; | 
| 250 | 
+ | 
#endif | 
| 251 | 
  | 
} | 
| 252 | 
  | 
 | 
| 253 | 
  | 
 | 
| 256 | 
  | 
{ | 
| 257 | 
  | 
        static VIEW     viewhist[VIEWHISTLEN]; | 
| 258 | 
  | 
        static unsigned nhist; | 
| 259 | 
+ | 
        VIEW    *dv; | 
| 260 | 
+ | 
        int     i, res[2]; | 
| 261 | 
  | 
        char    *err; | 
| 262 | 
  | 
                                /* restore previous view? */ | 
| 263 | 
  | 
        if (v == NULL) { | 
| 264 | 
< | 
                if (nhist < 2) { | 
| 265 | 
< | 
                        error(COMMAND, "no previous view"); | 
| 266 | 
< | 
                        return; | 
| 267 | 
< | 
                } | 
| 268 | 
< | 
                nhist--;        /* get one before last setting */ | 
| 264 | 
> | 
                if (nhist > 1)          /* get one before last setting */ | 
| 265 | 
> | 
                        nhist--; | 
| 266 | 
> | 
                else                    /* else go to end of list */ | 
| 267 | 
> | 
                        while (nhist < VIEWHISTLEN && viewhist[nhist].type) | 
| 268 | 
> | 
                                nhist++; | 
| 269 | 
  | 
                v = viewhist + ((nhist-1)%VIEWHISTLEN); | 
| 270 | 
< | 
        } else if ((err = setview(v)) != NULL) { | 
| 270 | 
> | 
        } else | 
| 271 | 
> | 
again: | 
| 272 | 
> | 
        if ((err = setview(v)) != NULL) { | 
| 273 | 
  | 
                error(COMMAND, err); | 
| 274 | 
  | 
                return; | 
| 275 | 
  | 
        } | 
| 227 | 
– | 
again: | 
| 276 | 
  | 
        if (v->type == VT_PAR) { | 
| 277 | 
  | 
                error(COMMAND, "cannot handle parallel views"); | 
| 278 | 
  | 
                return; | 
| 279 | 
  | 
        } | 
| 280 | 
< | 
        if (!dev_view(v))       /* update display driver */ | 
| 280 | 
> | 
        if (!dev_view(v))       /* notify display driver */ | 
| 281 | 
  | 
                goto again; | 
| 282 | 
  | 
        dev_flush();            /* update screen */ | 
| 283 | 
< | 
        if (!beam_view(v))      /* update beam list */ | 
| 284 | 
< | 
                goto again; | 
| 283 | 
> | 
        beam_init(0);           /* compute new beam set */ | 
| 284 | 
> | 
        for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) | 
| 285 | 
> | 
                if (!beam_view(dv, res[0], res[1])) { | 
| 286 | 
> | 
                        if (!nhist) { | 
| 287 | 
> | 
                                error(COMMAND, "invalid starting view"); | 
| 288 | 
> | 
                                return; | 
| 289 | 
> | 
                        } | 
| 290 | 
> | 
                        copystruct(v, viewhist + ((nhist-1)%VIEWHISTLEN)); | 
| 291 | 
> | 
                        goto again; | 
| 292 | 
> | 
                } | 
| 293 | 
> | 
        beam_sync(0);           /* update server */ | 
| 294 | 
  | 
                                /* record new view */ | 
| 295 | 
  | 
        if (v < viewhist || v >= viewhist+VIEWHISTLEN) { | 
| 296 | 
  | 
                copystruct(viewhist + (nhist%VIEWHISTLEN), v); | 
| 303 | 
  | 
usr_input()                     /* get user input and process it */ | 
| 304 | 
  | 
{ | 
| 305 | 
  | 
        VIEW    vparams; | 
| 306 | 
< | 
        char    cmd[128]; | 
| 306 | 
> | 
        char    cmd[256]; | 
| 307 | 
  | 
        register char   *args; | 
| 308 | 
  | 
        register int    i; | 
| 309 | 
  | 
 | 
| 310 | 
< | 
        if (fgets(cmd, sizeof(cmd), sstdin) == NULL) | 
| 311 | 
< | 
                return(DC_QUIT); | 
| 310 | 
> | 
        if (fgets(cmd, sizeof(cmd), sstdin) == NULL) { | 
| 311 | 
> | 
                fclose(sstdin); | 
| 312 | 
> | 
                sstdin = NULL; | 
| 313 | 
> | 
                return(-1); | 
| 314 | 
> | 
        } | 
| 315 | 
> | 
        if (!*cmd) | 
| 316 | 
> | 
                return(DC_RESUME); | 
| 317 | 
  | 
        for (args = cmd; *args && !isspace(*args); args++) | 
| 318 | 
  | 
                ; | 
| 319 | 
  | 
        while (isspace(*args)) | 
| 320 | 
  | 
                *args++ = '\0'; | 
| 259 | 
– | 
        if (!*cmd) | 
| 260 | 
– | 
                return(DC_RESUME); | 
| 321 | 
  | 
        if (*args && args[i=strlen(args)-1] == '\n') | 
| 322 | 
  | 
                args[i] = '\0'; | 
| 323 | 
  | 
        for (i = 0; i < DC_NCMDS; i++) | 
| 324 | 
  | 
                if (!strcmp(cmd, cmdlist[i])) | 
| 325 | 
  | 
                        break; | 
| 326 | 
  | 
        if (i >= DC_NCMDS) { | 
| 327 | 
< | 
                sprintf(errmsg, "unknown command: %s", cmd); | 
| 268 | 
< | 
                error(COMMAND, errmsg); | 
| 327 | 
> | 
                dev_auxcom(cmd, args); | 
| 328 | 
  | 
                return(-1); | 
| 329 | 
  | 
        } | 
| 330 | 
  | 
        switch (i) { | 
| 346 | 
  | 
                /* handled in main() */ | 
| 347 | 
  | 
                break; | 
| 348 | 
  | 
        case DC_REDRAW:                 /* redraw from server */ | 
| 349 | 
< | 
                imm_mode = beam_sync() > 0; | 
| 349 | 
> | 
                imm_mode = beam_sync(1) > 0; | 
| 350 | 
> | 
                dev_clear(); | 
| 351 | 
  | 
                break; | 
| 352 | 
  | 
        case DC_KILL:                   /* kill rtrace process(es) */ | 
| 353 | 
  | 
                serv_request(DR_KILL, 0, NULL); | 
| 411 | 
  | 
                        error(INTERNAL, "bad holodeck record from server"); | 
| 412 | 
  | 
                add_holo((HDGRID *)buf); | 
| 413 | 
  | 
                break; | 
| 414 | 
+ | 
        case DS_OUTSECT: | 
| 415 | 
+ | 
                do_outside = 1; | 
| 416 | 
+ | 
                goto noargs; | 
| 417 | 
+ | 
        case DS_EYESEP: | 
| 418 | 
+ | 
                if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY) | 
| 419 | 
+ | 
                        error(INTERNAL, "bad eye separation from server"); | 
| 420 | 
+ | 
                break; | 
| 421 | 
  | 
        case DS_STARTIMM: | 
| 422 | 
  | 
        case DS_ENDIMM: | 
| 423 | 
+ | 
#ifdef DEBUG | 
| 424 | 
+ | 
                if (imm_mode != (msg.type==DS_STARTIMM)) { | 
| 425 | 
+ | 
                        time_t  tnow = time(NULL); | 
| 426 | 
+ | 
                        if (imm_mode) timm += tnow - tmodesw; | 
| 427 | 
+ | 
                        else tadd += tnow - tmodesw; | 
| 428 | 
+ | 
                        tmodesw = tnow; | 
| 429 | 
+ | 
                } | 
| 430 | 
+ | 
#endif | 
| 431 | 
  | 
                imm_mode = msg.type==DS_STARTIMM; | 
| 432 | 
< | 
                if (msg.type == DS_ENDIMM) | 
| 358 | 
< | 
                        dev_flush();            /* update display NOW */ | 
| 359 | 
< | 
                /* fall through */ | 
| 432 | 
> | 
                goto noargs; | 
| 433 | 
  | 
        case DS_ACKNOW: | 
| 434 | 
  | 
        case DS_SHUTDOWN: | 
| 435 | 
< | 
                if (msg.nbytes) { | 
| 363 | 
< | 
                        sprintf(errmsg, | 
| 364 | 
< | 
                                "unexpected body with server message %d", | 
| 365 | 
< | 
                                        msg.type); | 
| 366 | 
< | 
                        error(INTERNAL, errmsg); | 
| 367 | 
< | 
                } | 
| 368 | 
< | 
                break; | 
| 435 | 
> | 
                goto noargs; | 
| 436 | 
  | 
        default: | 
| 437 | 
  | 
                error(INTERNAL, "unrecognized result from server process"); | 
| 438 | 
  | 
        } | 
| 439 | 
  | 
        return(msg.type);               /* return message type */ | 
| 440 | 
+ | 
noargs: | 
| 441 | 
+ | 
        if (msg.nbytes) { | 
| 442 | 
+ | 
                sprintf(errmsg, "unexpected body with server message %d", | 
| 443 | 
+ | 
                                msg.type); | 
| 444 | 
+ | 
                error(INTERNAL, errmsg); | 
| 445 | 
+ | 
        } | 
| 446 | 
+ | 
        return(msg.type); | 
| 447 | 
  | 
readerr: | 
| 448 | 
  | 
        if (feof(stdin)) | 
| 449 | 
  | 
                error(SYSTEM, "server process died"); |