| 1 | – | /* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 | – |  | 
| 1 |  | #ifndef lint | 
| 2 | < | static char SCCSid[] = "$SunId$ SGI"; | 
| 2 | > | static const char       RCSid[] = "$Id$"; | 
| 3 |  | #endif | 
| 6 | – |  | 
| 4 |  | /* | 
| 5 |  | * Holodeck display process. | 
| 6 |  | */ | 
| 7 |  |  | 
| 8 | + | #include <string.h> | 
| 9 | + | #include <ctype.h> | 
| 10 | + |  | 
| 11 |  | #include "rholo.h" | 
| 12 |  | #include "rhdisp.h" | 
| 13 |  | #include "rhdriver.h" | 
| 14 |  | #include "selcall.h" | 
| 15 | – | #include <ctype.h> | 
| 15 |  |  | 
| 16 |  | #ifndef VIEWHISTLEN | 
| 17 |  | #define VIEWHISTLEN     4       /* number of remembered views */ | 
| 18 |  | #endif | 
| 19 |  |  | 
| 20 | + | #ifndef FSIZDEF | 
| 21 | + | #define FSIZDEF         0.125   /* default focus frame size */ | 
| 22 | + | #endif | 
| 23 | + |  | 
| 24 | + | #if defined(freebsd) | 
| 25 | + | #define fbufcnt(f)      ((f)->_r) | 
| 26 | + | #elif defined(__GNUC__) | 
| 27 | + | #define fbufcnt(f)      ((f)->_IO_read_end - (f)->_IO_read_ptr) | 
| 28 | + | #else | 
| 29 | + | #define fbufcnt(f)      ((f)->_cnt) | 
| 30 | + | #endif | 
| 31 | + |  | 
| 32 |  | HOLO    *hdlist[HDMAX+1];       /* global holodeck list */ | 
| 33 |  |  | 
| 34 | + | char    *hdgfn[HDMAX];          /* holodeck section geometry list */ | 
| 35 | + | char    *hdpfn[HDMAX];          /* section portal list */ | 
| 36 | + |  | 
| 37 |  | char    cmdlist[DC_NCMDS][8] = DC_INIT; | 
| 38 |  |  | 
| 39 |  | int     imm_mode = 0;           /* bundles are being delivered immediately */ | 
| 47 |  | FILE    *sstdin, *sstdout;      /* server's standard input and output */ | 
| 48 |  |  | 
| 49 |  | #ifdef DEBUG | 
| 36 | – | #include <sys/types.h> | 
| 50 |  | extern time_t   time(); | 
| 51 |  | static time_t   tmodesw; | 
| 52 |  | static time_t   timm, tadd; | 
| 62 |  | int     argc; | 
| 63 |  | char    *argv[]; | 
| 64 |  | { | 
| 52 | – | extern int      eputs(); | 
| 65 |  | int     rdy, inp, res = 0, pause = 0; | 
| 66 |  |  | 
| 67 |  | progname = argv[0]; | 
| 96 |  | inp = dev_input(); | 
| 97 |  | if (inp & DFL(DC_SETVIEW)) | 
| 98 |  | new_view(&odev.v); | 
| 99 | + | else if (inp & DFL(DC_LASTVIEW)) | 
| 100 | + | new_view(NULL); | 
| 101 | + | if (inp & DFL(DC_REDRAW)) | 
| 102 | + | imm_mode = beam_sync(1) > 0; | 
| 103 |  | if (inp & DFL(DC_GETVIEW)) | 
| 104 |  | printview(); | 
| 105 | < | if (inp & DFL(DC_LASTVIEW)) | 
| 106 | < | new_view(NULL); | 
| 105 | > | if (inp & DFL(DC_FOCUS)) | 
| 106 | > | set_focus(odev_args); | 
| 107 |  | if (inp & DFL(DC_KILL)) { | 
| 108 |  | serv_request(DR_KILL, 0, NULL); | 
| 109 |  | pause = 0; | 
| 120 |  | } | 
| 121 |  | if (inp & DFL(DC_PAUSE)) | 
| 122 |  | pause = 1; | 
| 107 | – | if (inp & DFL(DC_REDRAW)) | 
| 108 | – | imm_mode = beam_sync(1) > 0; | 
| 123 |  | if (inp & DFL(DC_QUIT)) | 
| 124 |  | serv_request(DR_SHUTDOWN, 0, NULL); | 
| 125 |  | } | 
| 126 | < | if (rdy & RDY_SIN)              /* user input from sstdin */ | 
| 126 | > | if (rdy & RDY_SIN && !imm_mode) /* user input from sstdin */ | 
| 127 |  | switch (usr_input()) { | 
| 128 |  | case DC_PAUSE: | 
| 129 |  | pause = 1; | 
| 160 |  | int     n; | 
| 161 |  | register int    i; | 
| 162 |  | /* see if we can avoid select call */ | 
| 163 | + | if (hdlist[0] == NULL) | 
| 164 | + | return(RDY_SRV);        /* initialize first */ | 
| 165 |  | flgs = 0;               /* flag what's ready already */ | 
| 166 | < | if (imm_mode || stdin->_cnt > 0) | 
| 166 | > | if (imm_mode || fbufcnt(stdin) > 0) | 
| 167 |  | flgs |= RDY_SRV; | 
| 168 | < | if (sstdin != NULL && sstdin->_cnt > 0) | 
| 168 | > | if (sstdin != NULL && fbufcnt(sstdin) > 0) | 
| 169 |  | flgs |= RDY_SIN; | 
| 170 |  | if (odev.inpready) | 
| 171 |  | flgs |= RDY_DEV; | 
| 203 |  | } | 
| 204 |  |  | 
| 205 |  |  | 
| 206 | < | add_holo(hdg)                   /* register a new holodeck section */ | 
| 206 | > | add_holo(hdg, gfn, pfn)         /* register a new holodeck section */ | 
| 207 |  | HDGRID  *hdg; | 
| 208 | + | char    *gfn, *pfn; | 
| 209 |  | { | 
| 210 |  | VIEW    nv; | 
| 211 |  | double  d; | 
| 218 |  | hdlist[hd] = (HOLO *)malloc(sizeof(HOLO)); | 
| 219 |  | if (hdlist[hd] == NULL) | 
| 220 |  | error(SYSTEM, "out of memory in add_holo"); | 
| 221 | < | bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID)); | 
| 221 | > | memcpy((void *)hdlist[hd], (void *)hdg, sizeof(HDGRID)); | 
| 222 |  | hdcompgrid(hdlist[hd]); | 
| 223 | + | hdgfn[hd] = savestr(gfn); | 
| 224 | + | hdpfn[hd] = pfn && *pfn ? savestr(pfn) : (char *)NULL; | 
| 225 |  | if (hd) | 
| 226 |  | return; | 
| 227 |  | /* set initial viewpoint */ | 
| 277 |  | static unsigned nhist; | 
| 278 |  | VIEW    *dv; | 
| 279 |  | int     i, res[2]; | 
| 280 | + | int16   *slist; | 
| 281 |  | char    *err; | 
| 282 |  | /* restore previous view? */ | 
| 283 |  | if (v == NULL) { | 
| 293 |  | error(COMMAND, err); | 
| 294 |  | return; | 
| 295 |  | } | 
| 296 | + | if (!dev_view(v))       /* notify display driver */ | 
| 297 | + | goto again; | 
| 298 |  | if (v->type == VT_PAR) { | 
| 299 |  | error(COMMAND, "cannot handle parallel views"); | 
| 300 |  | return; | 
| 301 |  | } | 
| 302 | < | if (!dev_view(v))       /* notify display driver */ | 
| 303 | < | goto again; | 
| 304 | < | dev_flush();            /* update screen */ | 
| 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])) { | 
| 302 | > | beam_init(odev.firstuse);       /* compute new beam set */ | 
| 303 | > | for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) { | 
| 304 | > | if ((slist = beam_view(dv, res[0], res[1])) == NULL) { | 
| 305 |  | if (!nhist) { | 
| 306 |  | error(COMMAND, "invalid starting view"); | 
| 307 |  | return; | 
| 308 |  | } | 
| 309 |  | copystruct(v, viewhist + ((nhist-1)%VIEWHISTLEN)); | 
| 310 | < | goto again; | 
| 310 | > | goto again;     /* poss. overloading dev_section()? */ | 
| 311 |  | } | 
| 312 | < | beam_sync(0);           /* update server */ | 
| 312 | > | DCHECK(*slist < 0, WARNING, "no visible sections in new_view"); | 
| 313 | > | for ( ; *slist >= 0; slist++) | 
| 314 | > | dev_section(hdgfn[*slist], hdpfn[*slist]); | 
| 315 | > | } | 
| 316 | > | dev_section(NULL,NULL); /* end section list */ | 
| 317 | > | dev_flush();            /* update display */ | 
| 318 | > | /* update server */ | 
| 319 | > | imm_mode = beam_sync(odev.firstuse) > 0; | 
| 320 |  | /* record new view */ | 
| 321 |  | if (v < viewhist || v >= viewhist+VIEWHISTLEN) { | 
| 322 |  | copystruct(viewhist + (nhist%VIEWHISTLEN), v); | 
| 325 |  | } | 
| 326 |  |  | 
| 327 |  |  | 
| 328 | + | set_focus(args)                 /* set focus frame */ | 
| 329 | + | char    *args; | 
| 330 | + | { | 
| 331 | + | double  hcent, vcent, hsiz, vsiz; | 
| 332 | + | VIEW    *dv, vwfocus; | 
| 333 | + | int     i, res[2]; | 
| 334 | + |  | 
| 335 | + | i = sscanf(args, "%lf %lf %lf %lf", &hcent, &vcent, &hsiz, &vsiz); | 
| 336 | + | if (i < 2 || hcent < 0 || hcent > 1 || vcent < 0 || vcent > 1) { | 
| 337 | + | beam_init(0);                           /* restore view */ | 
| 338 | + | for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) | 
| 339 | + | beam_view(dv, res[0], res[1]); | 
| 340 | + | beam_sync(0);                           /* update server */ | 
| 341 | + | return; | 
| 342 | + | } | 
| 343 | + | if (i < 4 || hsiz <= hcent || hsiz > 1 || vsiz <= vcent || vsiz > 1) | 
| 344 | + | hsiz = vsiz = FSIZDEF;                  /* gave center only */ | 
| 345 | + | else { | 
| 346 | + | hsiz -= hcent; hcent += 0.5*hsiz;       /* gave min and max */ | 
| 347 | + | vsiz -= vcent; vcent += 0.5*vsiz; | 
| 348 | + | } | 
| 349 | + | beam_init(0);                                   /* add basic views */ | 
| 350 | + | for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) | 
| 351 | + | beam_view(dv, res[0]>>4, res[1]>>4); | 
| 352 | + | copystruct(&vwfocus, &odev.v);                  /* add focus view */ | 
| 353 | + | switch (odev.v.type) { | 
| 354 | + | case VT_PER: | 
| 355 | + | vwfocus.horiz = 2.*180./PI*atan( | 
| 356 | + | hsiz * tan(PI/180./2.*odev.v.horiz) ); | 
| 357 | + | vwfocus.vert = 2.*180./PI*atan( | 
| 358 | + | vsiz * tan(PI/180./2.*odev.v.vert) ); | 
| 359 | + | break; | 
| 360 | + | case VT_PAR: | 
| 361 | + | case VT_ANG: | 
| 362 | + | vwfocus.horiz = hsiz * odev.v.horiz; | 
| 363 | + | vwfocus.vert = vsiz * odev.v.vert; | 
| 364 | + | break; | 
| 365 | + | case VT_HEM: | 
| 366 | + | vwfocus.horiz = 2.*180./PI*asin( | 
| 367 | + | hsiz * sin(PI/180./2.*odev.v.horiz) ); | 
| 368 | + | vwfocus.vert = 2.*180./PI*asin( | 
| 369 | + | vsiz * sin(PI/180./2.*odev.v.vert) ); | 
| 370 | + | break; | 
| 371 | + | case VT_CYL: | 
| 372 | + | vwfocus.horiz = hsiz * odev.v.horiz; | 
| 373 | + | vwfocus.vert = 2.*180./PI*atan( | 
| 374 | + | vsiz * tan(PI/180./2.*odev.v.vert) ); | 
| 375 | + | break; | 
| 376 | + | default: | 
| 377 | + | error(INTERNAL, "bad view type in set_focus"); | 
| 378 | + | } | 
| 379 | + | vwfocus.hoff = (odev.v.hoff + hcent - 0.5)/hsiz; | 
| 380 | + | vwfocus.voff = (odev.v.voff + vcent - 0.5)/vsiz; | 
| 381 | + | setview(&vwfocus); | 
| 382 | + | beam_view(&vwfocus, (int)(3*odev.hres*hsiz)+100, | 
| 383 | + | (int)(3*odev.vres*vsiz)+100); | 
| 384 | + | beam_sync(0);                                   /* update server */ | 
| 385 | + | } | 
| 386 | + |  | 
| 387 | + |  | 
| 388 |  | int | 
| 389 |  | usr_input()                     /* get user input and process it */ | 
| 390 |  | { | 
| 427 |  | case DC_LASTVIEW:               /* restore previous view */ | 
| 428 |  | new_view(NULL); | 
| 429 |  | break; | 
| 430 | + | case DC_FOCUS:                  /* set focus frame */ | 
| 431 | + | set_focus(args); | 
| 432 | + | break; | 
| 433 |  | case DC_PAUSE:                  /* pause the current calculation */ | 
| 434 |  | case DC_RESUME:                 /* resume the calculation */ | 
| 435 |  | /* handled in main() */ | 
| 481 |  | if (buf == NULL) | 
| 482 |  | buf = (char *)malloc(bufsiz=msg.nbytes); | 
| 483 |  | else | 
| 484 | < | buf = (char *)realloc(buf, bufsiz=msg.nbytes); | 
| 484 | > | buf = (char *)realloc((void *)buf, | 
| 485 | > | bufsiz=msg.nbytes); | 
| 486 |  | if (buf == NULL) | 
| 487 |  | error(SYSTEM, "out of memory in serv_result"); | 
| 488 |  | } | 
| 497 |  | disp_bundle((PACKHEAD *)buf); | 
| 498 |  | break; | 
| 499 |  | case DS_ADDHOLO: | 
| 500 | < | if (msg.nbytes != sizeof(HDGRID)) | 
| 500 | > | if (msg.nbytes < sizeof(HDGRID)+2) | 
| 501 |  | error(INTERNAL, "bad holodeck record from server"); | 
| 502 | < | add_holo((HDGRID *)buf); | 
| 502 | > | add_holo((HDGRID *)buf, buf+sizeof(HDGRID), | 
| 503 | > | buf+sizeof(HDGRID)+strlen(buf+sizeof(HDGRID))+1); | 
| 504 |  | break; | 
| 505 |  | case DS_OUTSECT: | 
| 506 |  | do_outside = 1; | 
| 511 |  | break; | 
| 512 |  | case DS_STARTIMM: | 
| 513 |  | case DS_ENDIMM: | 
| 514 | + | if (!(imm_mode = msg.type==DS_STARTIMM)) | 
| 515 | + | dev_flush(); | 
| 516 |  | #ifdef DEBUG | 
| 517 | < | if (imm_mode != (msg.type==DS_STARTIMM)) { | 
| 517 | > | { | 
| 518 |  | time_t  tnow = time(NULL); | 
| 519 | < | if (imm_mode) timm += tnow - tmodesw; | 
| 520 | < | else tadd += tnow - tmodesw; | 
| 519 | > | if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw; | 
| 520 | > | else timm += tnow - tmodesw; | 
| 521 |  | tmodesw = tnow; | 
| 522 |  | } | 
| 523 |  | #endif | 
| 431 | – | imm_mode = msg.type==DS_STARTIMM; | 
| 524 |  | goto noargs; | 
| 525 |  | case DS_ACKNOW: | 
| 526 |  | case DS_SHUTDOWN: | 
| 550 |  | MSGHEAD msg; | 
| 551 |  | int     m; | 
| 552 |  | /* consistency checks */ | 
| 553 | < | #ifdef DEBUG | 
| 554 | < | if (nbytes < 0 || nbytes > 0 & p == NULL) | 
| 463 | < | error(CONSISTENCY, "bad buffer handed to serv_request"); | 
| 464 | < | #endif | 
| 553 | > | DCHECK(nbytes < 0 || nbytes > 0 & p == NULL, | 
| 554 | > | CONSISTENCY, "bad buffer handed to serv_request"); | 
| 555 |  | /* get server's attention for big request */ | 
| 556 |  | if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) { | 
| 557 |  | serv_request(DR_ATTEN, 0, NULL); | 
| 569 |  | } | 
| 570 |  |  | 
| 571 |  |  | 
| 572 | + | void | 
| 573 |  | eputs(s)                        /* put error message to stderr */ | 
| 574 |  | register char  *s; | 
| 575 |  | { | 
| 589 |  | } | 
| 590 |  |  | 
| 591 |  |  | 
| 592 | + | void | 
| 593 |  | quit(code)                      /* clean up and exit */ | 
| 594 |  | int     code; | 
| 595 |  | { | 
| 596 | + | if (code) | 
| 597 | + | exit(code); | 
| 598 |  | if (odev.v.type) | 
| 599 |  | dev_close(); | 
| 600 | < | exit(code); | 
| 600 | > | exit(0); | 
| 601 |  | } |