| 1 | gregl | 3.23 | /* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 | gregl | 3.1 |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Holodeck display process. | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include "rholo.h" | 
| 12 |  |  | #include "rhdisp.h" | 
| 13 |  |  | #include "rhdriver.h" | 
| 14 |  |  | #include "selcall.h" | 
| 15 | gregl | 3.10 | #include <ctype.h> | 
| 16 | gregl | 3.1 |  | 
| 17 | gregl | 3.12 | #ifndef VIEWHISTLEN | 
| 18 | gregl | 3.16 | #define VIEWHISTLEN     4       /* number of remembered views */ | 
| 19 | gregl | 3.12 | #endif | 
| 20 |  |  |  | 
| 21 | gregl | 3.1 | HOLO    *hdlist[HDMAX+1];       /* global holodeck list */ | 
| 22 |  |  |  | 
| 23 | gregl | 3.10 | char    cmdlist[DC_NCMDS][8] = DC_INIT; | 
| 24 |  |  |  | 
| 25 | gregl | 3.1 | int     imm_mode = 0;           /* bundles are being delivered immediately */ | 
| 26 |  |  |  | 
| 27 | gregl | 3.20 | int     do_outside = 0;         /* render from outside sections */ | 
| 28 |  |  |  | 
| 29 | gwlarson | 3.25 | double  eyesepdist = 1;         /* eye separation distance */ | 
| 30 |  |  |  | 
| 31 | gregl | 3.1 | char    *progname;              /* global argv[0] */ | 
| 32 |  |  |  | 
| 33 | gregl | 3.10 | FILE    *sstdin, *sstdout;      /* server's standard input and output */ | 
| 34 | gregl | 3.1 |  | 
| 35 | gregl | 3.21 | #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 | gregl | 3.10 | #define RDY_SRV         01 | 
| 44 |  |  | #define RDY_DEV         02 | 
| 45 |  |  | #define RDY_SIN         04 | 
| 46 | gregl | 3.1 |  | 
| 47 | gregl | 3.10 |  | 
| 48 | gregl | 3.1 | main(argc, argv) | 
| 49 |  |  | int     argc; | 
| 50 |  |  | char    *argv[]; | 
| 51 |  |  | { | 
| 52 | gregl | 3.10 | extern int      eputs(); | 
| 53 | gregl | 3.3 | int     rdy, inp, res = 0, pause = 0; | 
| 54 | gregl | 3.1 |  | 
| 55 |  |  | progname = argv[0]; | 
| 56 | gregl | 3.10 | if (argc < 3) | 
| 57 | gregl | 3.1 | error(USER, "bad command line arguments"); | 
| 58 |  |  | /* open our device */ | 
| 59 |  |  | dev_open(argv[1]); | 
| 60 | gregl | 3.10 | /* open server process i/o */ | 
| 61 |  |  | sstdout = fdopen(atoi(argv[2]), "w"); | 
| 62 |  |  | if (argc < 4 || (inp = atoi(argv[3])) < 0) | 
| 63 |  |  | sstdin = NULL; | 
| 64 |  |  | else | 
| 65 |  |  | sstdin = fdopen(inp, "r"); | 
| 66 |  |  | /* set command error vector */ | 
| 67 |  |  | erract[COMMAND].pf = eputs; | 
| 68 | gregl | 3.21 | #ifdef DEBUG | 
| 69 |  |  | tmodesw = time(NULL); | 
| 70 |  |  | #endif | 
| 71 | gregl | 3.1 | /* enter main loop */ | 
| 72 |  |  | do { | 
| 73 |  |  | rdy = disp_wait(); | 
| 74 | gregl | 3.19 | 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 | gregl | 3.10 | if (rdy & RDY_DEV) {            /* user input from driver */ | 
| 84 | gregl | 3.3 | inp = dev_input(); | 
| 85 | gregl | 3.13 | if (inp & DFL(DC_SETVIEW)) | 
| 86 |  |  | new_view(&odev.v); | 
| 87 |  |  | if (inp & DFL(DC_GETVIEW)) | 
| 88 | gregl | 3.10 | printview(); | 
| 89 | gregl | 3.13 | if (inp & DFL(DC_LASTVIEW)) | 
| 90 | gregl | 3.12 | new_view(NULL); | 
| 91 | gregl | 3.13 | if (inp & DFL(DC_RESUME)) { | 
| 92 | gregl | 3.3 | serv_request(DR_NOOP, 0, NULL); | 
| 93 |  |  | pause = 0; | 
| 94 |  |  | } | 
| 95 | gregl | 3.15 | if (inp & DFL(DC_PAUSE)) | 
| 96 |  |  | pause = 1; | 
| 97 | gregl | 3.13 | if (inp & DFL(DC_REDRAW)) | 
| 98 | gwlarson | 3.24 | imm_mode = beam_sync(1) > 0; | 
| 99 | gregl | 3.15 | if (inp & DFL(DC_KILL)) { | 
| 100 | gregl | 3.14 | serv_request(DR_KILL, 0, NULL); | 
| 101 | gregl | 3.15 | pause = 0; | 
| 102 |  |  | } | 
| 103 | gregl | 3.14 | if (inp & DFL(DC_CLOBBER)) | 
| 104 |  |  | serv_request(DR_CLOBBER, 0, NULL); | 
| 105 | gregl | 3.15 | if (inp & DFL(DC_RESTART)) { | 
| 106 | gregl | 3.14 | serv_request(DR_RESTART, 0, NULL); | 
| 107 | gregl | 3.15 | pause = 0; | 
| 108 |  |  | } | 
| 109 | gregl | 3.13 | if (inp & DFL(DC_QUIT)) | 
| 110 |  |  | serv_request(DR_SHUTDOWN, 0, NULL); | 
| 111 | gregl | 3.3 | } | 
| 112 | gregl | 3.10 | if (rdy & RDY_SIN)              /* user input from sstdin */ | 
| 113 |  |  | switch (usr_input()) { | 
| 114 |  |  | case DC_PAUSE: | 
| 115 |  |  | pause = 1; | 
| 116 |  |  | break; | 
| 117 |  |  | case DC_RESUME: | 
| 118 |  |  | serv_request(DR_NOOP, 0, NULL); | 
| 119 | gregl | 3.15 | /* fall through */ | 
| 120 |  |  | case DC_KILL: | 
| 121 |  |  | case DC_RESTART: | 
| 122 | gregl | 3.10 | pause = 0; | 
| 123 |  |  | break; | 
| 124 |  |  | } | 
| 125 | gregl | 3.1 | } while (res != DS_SHUTDOWN); | 
| 126 | gregl | 3.21 | #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 | gregl | 3.1 | /* all done */ | 
| 137 |  |  | quit(0); | 
| 138 |  |  | } | 
| 139 |  |  |  | 
| 140 |  |  |  | 
| 141 |  |  | int | 
| 142 |  |  | disp_wait()                     /* wait for more input */ | 
| 143 |  |  | { | 
| 144 |  |  | fd_set  readset, errset; | 
| 145 |  |  | int     flgs; | 
| 146 |  |  | int     n; | 
| 147 |  |  | register int    i; | 
| 148 |  |  | /* see if we can avoid select call */ | 
| 149 | gwlarson | 3.30 | flgs = 0;               /* flag what's ready already */ | 
| 150 | gregl | 3.1 | if (imm_mode || stdin->_cnt > 0) | 
| 151 | gwlarson | 3.30 | flgs |= RDY_SRV; | 
| 152 | gregl | 3.10 | if (sstdin != NULL && sstdin->_cnt > 0) | 
| 153 | gwlarson | 3.30 | 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 | gregl | 3.4 | return(RDY_DEV); | 
| 160 | gwlarson | 3.30 | /* if nothing, we need to call select */ | 
| 161 | gregl | 3.1 | FD_ZERO(&readset); FD_ZERO(&errset); | 
| 162 |  |  | FD_SET(0, &readset); | 
| 163 |  |  | FD_SET(0, &errset); | 
| 164 |  |  | FD_SET(odev.ifd, &readset); | 
| 165 |  |  | FD_SET(odev.ifd, &errset); | 
| 166 | gregl | 3.10 | n = odev.ifd+1; | 
| 167 |  |  | if (sstdin != NULL) { | 
| 168 |  |  | FD_SET(fileno(sstdin), &readset); | 
| 169 | gregl | 3.23 | FD_SET(fileno(sstdin), &errset); | 
| 170 | gregl | 3.10 | if (fileno(sstdin) >= n) | 
| 171 |  |  | n = fileno(sstdin) + 1; | 
| 172 |  |  | } | 
| 173 | gregl | 3.1 | n = select(n, &readset, NULL, &errset, NULL); | 
| 174 |  |  | if (n < 0) { | 
| 175 |  |  | if (errno == EINTR) | 
| 176 |  |  | return(0); | 
| 177 |  |  | error(SYSTEM, "select call failure in disp_wait"); | 
| 178 |  |  | } | 
| 179 |  |  | if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset)) | 
| 180 | gregl | 3.4 | flgs |= RDY_SRV; | 
| 181 | gregl | 3.1 | if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset)) | 
| 182 | gregl | 3.4 | flgs |= RDY_DEV; | 
| 183 | gregl | 3.23 | if (sstdin != NULL && (FD_ISSET(fileno(sstdin), &readset) || | 
| 184 |  |  | FD_ISSET(fileno(sstdin), &errset))) | 
| 185 | gregl | 3.10 | flgs |= RDY_SIN; | 
| 186 | gregl | 3.1 | return(flgs); | 
| 187 |  |  | } | 
| 188 |  |  |  | 
| 189 |  |  |  | 
| 190 |  |  | add_holo(hdg)                   /* register a new holodeck section */ | 
| 191 |  |  | HDGRID  *hdg; | 
| 192 |  |  | { | 
| 193 |  |  | VIEW    nv; | 
| 194 | gregl | 3.20 | double  d; | 
| 195 | gregl | 3.1 | register int    hd; | 
| 196 |  |  |  | 
| 197 |  |  | for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++) | 
| 198 |  |  | ; | 
| 199 |  |  | if (hd >= HDMAX) | 
| 200 |  |  | error(INTERNAL, "too many holodeck sections in add_holo"); | 
| 201 |  |  | hdlist[hd] = (HOLO *)malloc(sizeof(HOLO)); | 
| 202 |  |  | if (hdlist[hd] == NULL) | 
| 203 |  |  | error(SYSTEM, "out of memory in add_holo"); | 
| 204 |  |  | bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID)); | 
| 205 |  |  | hdcompgrid(hdlist[hd]); | 
| 206 |  |  | if (hd) | 
| 207 |  |  | return; | 
| 208 |  |  | /* set initial viewpoint */ | 
| 209 |  |  | copystruct(&nv, &odev.v); | 
| 210 |  |  | VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5); | 
| 211 |  |  | VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5); | 
| 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 | gregl | 3.20 | 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 | gregl | 3.1 | new_view(&nv); | 
| 222 |  |  | } | 
| 223 |  |  |  | 
| 224 |  |  |  | 
| 225 |  |  | disp_bundle(p)                  /* display a ray bundle */ | 
| 226 |  |  | register PACKHEAD       *p; | 
| 227 |  |  | { | 
| 228 |  |  | GCOORD  gc[2]; | 
| 229 |  |  | FVECT   ro, rd, wp; | 
| 230 |  |  | double  d; | 
| 231 |  |  | register int    i; | 
| 232 |  |  | /* get beam coordinates */ | 
| 233 |  |  | if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL) | 
| 234 |  |  | error(INTERNAL, "bad holodeck number in disp_bundle"); | 
| 235 |  |  | if (!hdbcoord(gc, hdlist[p->hd], p->bi)) | 
| 236 |  |  | error(INTERNAL, "bad beam index in disp_bundle"); | 
| 237 |  |  | /* display each ray */ | 
| 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 | gwlarson | 3.29 | 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 | gregl | 3.1 | } | 
| 247 | gregl | 3.21 | #ifdef DEBUG | 
| 248 |  |  | if (imm_mode) nimmrays += p->nr; | 
| 249 |  |  | else naddrays += p->nr; | 
| 250 |  |  | #endif | 
| 251 | gregl | 3.1 | } | 
| 252 |  |  |  | 
| 253 |  |  |  | 
| 254 |  |  | new_view(v)                     /* change view parameters */ | 
| 255 | gregl | 3.12 | register VIEW   *v; | 
| 256 | gregl | 3.1 | { | 
| 257 | gregl | 3.12 | static VIEW     viewhist[VIEWHISTLEN]; | 
| 258 |  |  | static unsigned nhist; | 
| 259 | gwlarson | 3.24 | VIEW    *dv; | 
| 260 |  |  | int     i, res[2]; | 
| 261 | gregl | 3.1 | char    *err; | 
| 262 | gregl | 3.12 | /* restore previous view? */ | 
| 263 |  |  | if (v == NULL) { | 
| 264 | gregl | 3.16 | 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 | gregl | 3.12 | v = viewhist + ((nhist-1)%VIEWHISTLEN); | 
| 270 | gregl | 3.17 | } else | 
| 271 |  |  | again: | 
| 272 |  |  | if ((err = setview(v)) != NULL) { | 
| 273 | gregl | 3.12 | error(COMMAND, err); | 
| 274 |  |  | return; | 
| 275 |  |  | } | 
| 276 |  |  | if (v->type == VT_PAR) { | 
| 277 |  |  | error(COMMAND, "cannot handle parallel views"); | 
| 278 |  |  | return; | 
| 279 |  |  | } | 
| 280 | gwlarson | 3.24 | if (!dev_view(v))       /* notify display driver */ | 
| 281 | gregl | 3.12 | goto again; | 
| 282 |  |  | dev_flush();            /* update screen */ | 
| 283 | gwlarson | 3.27 | beam_init(0);           /* compute new beam set */ | 
| 284 | gwlarson | 3.24 | 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 | gregl | 3.12 | /* record new view */ | 
| 295 |  |  | if (v < viewhist || v >= viewhist+VIEWHISTLEN) { | 
| 296 |  |  | copystruct(viewhist + (nhist%VIEWHISTLEN), v); | 
| 297 |  |  | nhist++; | 
| 298 |  |  | } | 
| 299 | gregl | 3.10 | } | 
| 300 |  |  |  | 
| 301 |  |  |  | 
| 302 |  |  | int | 
| 303 |  |  | usr_input()                     /* get user input and process it */ | 
| 304 |  |  | { | 
| 305 |  |  | VIEW    vparams; | 
| 306 | gregl | 3.23 | char    cmd[256]; | 
| 307 | gregl | 3.10 | register char   *args; | 
| 308 | gregl | 3.11 | register int    i; | 
| 309 | gregl | 3.10 |  | 
| 310 | gregl | 3.23 | if (fgets(cmd, sizeof(cmd), sstdin) == NULL) { | 
| 311 |  |  | fclose(sstdin); | 
| 312 |  |  | sstdin = NULL; | 
| 313 | gwlarson | 3.26 | return(-1); | 
| 314 | gregl | 3.23 | } | 
| 315 | gwlarson | 3.31 | if (*cmd == '\n') | 
| 316 | gwlarson | 3.26 | return(DC_RESUME); | 
| 317 | gregl | 3.10 | for (args = cmd; *args && !isspace(*args); args++) | 
| 318 |  |  | ; | 
| 319 |  |  | while (isspace(*args)) | 
| 320 |  |  | *args++ = '\0'; | 
| 321 | gregl | 3.11 | 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 | gregl | 3.10 | break; | 
| 326 | gregl | 3.11 | if (i >= DC_NCMDS) { | 
| 327 | gwlarson | 3.26 | dev_auxcom(cmd, args); | 
| 328 | gregl | 3.10 | return(-1); | 
| 329 |  |  | } | 
| 330 | gregl | 3.11 | switch (i) { | 
| 331 | gregl | 3.10 | case DC_SETVIEW:                /* set the view */ | 
| 332 |  |  | copystruct(&vparams, &odev.v); | 
| 333 |  |  | if (!sscanview(&vparams, args)) | 
| 334 |  |  | error(COMMAND, "missing view options"); | 
| 335 |  |  | else | 
| 336 |  |  | new_view(&vparams); | 
| 337 |  |  | break; | 
| 338 |  |  | case DC_GETVIEW:                /* print the current view */ | 
| 339 |  |  | printview(); | 
| 340 | gregl | 3.12 | break; | 
| 341 |  |  | case DC_LASTVIEW:               /* restore previous view */ | 
| 342 |  |  | new_view(NULL); | 
| 343 | gregl | 3.10 | break; | 
| 344 |  |  | case DC_PAUSE:                  /* pause the current calculation */ | 
| 345 |  |  | case DC_RESUME:                 /* resume the calculation */ | 
| 346 |  |  | /* handled in main() */ | 
| 347 | gregl | 3.13 | break; | 
| 348 |  |  | case DC_REDRAW:                 /* redraw from server */ | 
| 349 | gwlarson | 3.24 | imm_mode = beam_sync(1) > 0; | 
| 350 | gregl | 3.22 | dev_clear(); | 
| 351 | gregl | 3.14 | break; | 
| 352 |  |  | case DC_KILL:                   /* kill rtrace process(es) */ | 
| 353 |  |  | serv_request(DR_KILL, 0, NULL); | 
| 354 |  |  | break; | 
| 355 |  |  | case DC_CLOBBER:                /* clobber holodeck */ | 
| 356 |  |  | serv_request(DR_CLOBBER, 0, NULL); | 
| 357 |  |  | break; | 
| 358 |  |  | case DC_RESTART:                /* restart rtrace */ | 
| 359 |  |  | serv_request(DR_RESTART, 0, NULL); | 
| 360 | gregl | 3.10 | break; | 
| 361 |  |  | case DC_QUIT:                   /* quit request */ | 
| 362 |  |  | serv_request(DR_SHUTDOWN, 0, NULL); | 
| 363 |  |  | break; | 
| 364 |  |  | default: | 
| 365 |  |  | error(CONSISTENCY, "bad command id in usr_input"); | 
| 366 |  |  | } | 
| 367 | gregl | 3.11 | return(i); | 
| 368 | gregl | 3.10 | } | 
| 369 |  |  |  | 
| 370 |  |  |  | 
| 371 |  |  | printview()                     /* print our current view to server stdout */ | 
| 372 |  |  | { | 
| 373 |  |  | fputs(VIEWSTR, sstdout); | 
| 374 |  |  | fprintview(&odev.v, sstdout); | 
| 375 |  |  | fputc('\n', sstdout); | 
| 376 |  |  | fflush(sstdout); | 
| 377 | gregl | 3.1 | } | 
| 378 |  |  |  | 
| 379 |  |  |  | 
| 380 |  |  | int | 
| 381 |  |  | serv_result()                   /* get next server result and process it */ | 
| 382 |  |  | { | 
| 383 |  |  | static char     *buf = NULL; | 
| 384 |  |  | static int      bufsiz = 0; | 
| 385 |  |  | MSGHEAD msg; | 
| 386 |  |  | int     n; | 
| 387 |  |  | /* read message header */ | 
| 388 |  |  | if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1) | 
| 389 |  |  | goto readerr; | 
| 390 |  |  | if (msg.nbytes > 0) {           /* get the message body */ | 
| 391 |  |  | if (msg.nbytes > bufsiz) { | 
| 392 |  |  | if (buf == NULL) | 
| 393 |  |  | buf = (char *)malloc(bufsiz=msg.nbytes); | 
| 394 |  |  | else | 
| 395 |  |  | buf = (char *)realloc(buf, bufsiz=msg.nbytes); | 
| 396 |  |  | if (buf == NULL) | 
| 397 |  |  | error(SYSTEM, "out of memory in serv_result"); | 
| 398 |  |  | } | 
| 399 |  |  | if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes) | 
| 400 |  |  | goto readerr; | 
| 401 |  |  | } | 
| 402 |  |  | switch (msg.type) {             /* process results */ | 
| 403 |  |  | case DS_BUNDLE: | 
| 404 |  |  | if (msg.nbytes < sizeof(PACKHEAD) || | 
| 405 |  |  | msg.nbytes != packsiz(((PACKHEAD *)buf)->nr)) | 
| 406 |  |  | error(INTERNAL, "bad display packet from server"); | 
| 407 |  |  | disp_bundle((PACKHEAD *)buf); | 
| 408 |  |  | break; | 
| 409 |  |  | case DS_ADDHOLO: | 
| 410 |  |  | if (msg.nbytes != sizeof(HDGRID)) | 
| 411 |  |  | error(INTERNAL, "bad holodeck record from server"); | 
| 412 |  |  | add_holo((HDGRID *)buf); | 
| 413 | gregl | 3.20 | break; | 
| 414 |  |  | case DS_OUTSECT: | 
| 415 |  |  | do_outside = 1; | 
| 416 | gwlarson | 3.28 | goto noargs; | 
| 417 | gwlarson | 3.25 | case DS_EYESEP: | 
| 418 |  |  | if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY) | 
| 419 |  |  | error(INTERNAL, "bad eye separation from server"); | 
| 420 | gregl | 3.1 | break; | 
| 421 |  |  | case DS_STARTIMM: | 
| 422 |  |  | case DS_ENDIMM: | 
| 423 | gregl | 3.21 | #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 | gregl | 3.1 | imm_mode = msg.type==DS_STARTIMM; | 
| 432 | gwlarson | 3.28 | goto noargs; | 
| 433 | gregl | 3.1 | case DS_ACKNOW: | 
| 434 |  |  | case DS_SHUTDOWN: | 
| 435 | gwlarson | 3.28 | goto noargs; | 
| 436 | gregl | 3.1 | default: | 
| 437 |  |  | error(INTERNAL, "unrecognized result from server process"); | 
| 438 |  |  | } | 
| 439 |  |  | return(msg.type);               /* return message type */ | 
| 440 | gwlarson | 3.28 | 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 | gregl | 3.1 | readerr: | 
| 448 |  |  | if (feof(stdin)) | 
| 449 |  |  | error(SYSTEM, "server process died"); | 
| 450 |  |  | error(SYSTEM, "error reading from server process"); | 
| 451 |  |  | } | 
| 452 |  |  |  | 
| 453 |  |  |  | 
| 454 |  |  | serv_request(type, nbytes, p)   /* send a request to the server process */ | 
| 455 |  |  | int     type, nbytes; | 
| 456 |  |  | char    *p; | 
| 457 |  |  | { | 
| 458 |  |  | MSGHEAD msg; | 
| 459 |  |  | int     m; | 
| 460 |  |  | /* get server's attention for big request */ | 
| 461 |  |  | if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) { | 
| 462 |  |  | serv_request(DR_ATTEN, 0, NULL); | 
| 463 |  |  | while ((m = serv_result()) != DS_ACKNOW) | 
| 464 |  |  | if (m == DS_SHUTDOWN)   /* the bugger quit on us */ | 
| 465 |  |  | quit(0); | 
| 466 |  |  | } | 
| 467 |  |  | msg.type = type;        /* write and flush the message */ | 
| 468 |  |  | msg.nbytes = nbytes; | 
| 469 |  |  | fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout); | 
| 470 |  |  | if (nbytes > 0) | 
| 471 |  |  | fwrite(p, 1, nbytes, stdout); | 
| 472 |  |  | if (fflush(stdout) < 0) | 
| 473 |  |  | error(SYSTEM, "write error in serv_request"); | 
| 474 | gregl | 3.2 | } | 
| 475 |  |  |  | 
| 476 |  |  |  | 
| 477 |  |  | eputs(s)                        /* put error message to stderr */ | 
| 478 |  |  | register char  *s; | 
| 479 |  |  | { | 
| 480 |  |  | static int  midline = 0; | 
| 481 |  |  |  | 
| 482 |  |  | if (!*s) | 
| 483 |  |  | return; | 
| 484 |  |  | if (!midline++) {       /* prepend line with program name */ | 
| 485 |  |  | fputs(progname, stderr); | 
| 486 |  |  | fputs(": ", stderr); | 
| 487 |  |  | } | 
| 488 |  |  | fputs(s, stderr); | 
| 489 |  |  | if (s[strlen(s)-1] == '\n') { | 
| 490 |  |  | fflush(stderr); | 
| 491 |  |  | midline = 0; | 
| 492 |  |  | } | 
| 493 | gregl | 3.1 | } | 
| 494 |  |  |  | 
| 495 |  |  |  | 
| 496 |  |  | quit(code)                      /* clean up and exit */ | 
| 497 |  |  | int     code; | 
| 498 |  |  | { | 
| 499 |  |  | if (odev.v.type) | 
| 500 |  |  | dev_close(); | 
| 501 |  |  | exit(code); | 
| 502 |  |  | } |