--- ray/src/hd/rhdisp.c 1998/08/26 13:06:09 3.32 +++ ray/src/hd/rhdisp.c 2004/01/01 11:21:55 3.50 @@ -1,25 +1,40 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhdisp.c,v 3.50 2004/01/01 11:21:55 schorsch Exp $"; #endif - /* * Holodeck display process. */ +#include +#include + +#include "rterror.h" #include "rholo.h" #include "rhdisp.h" #include "rhdriver.h" #include "selcall.h" -#include #ifndef VIEWHISTLEN #define VIEWHISTLEN 4 /* number of remembered views */ #endif +#ifndef FSIZDEF +#define FSIZDEF 0.125 /* default focus frame size */ +#endif + +#if defined(freebsd) +#define fbufcnt(f) ((f)->_r) +#elif defined(__GNUC__) +#define fbufcnt(f) ((f)->_IO_read_end - (f)->_IO_read_ptr) +#else +#define fbufcnt(f) ((f)->_cnt) +#endif + HOLO *hdlist[HDMAX+1]; /* global holodeck list */ +char *hdgfn[HDMAX]; /* holodeck section geometry list */ +char *hdpfn[HDMAX]; /* section portal list */ + char cmdlist[DC_NCMDS][8] = DC_INIT; int imm_mode = 0; /* bundles are being delivered immediately */ @@ -33,7 +48,6 @@ char *progname; /* global argv[0] */ FILE *sstdin, *sstdout; /* server's standard input and output */ #ifdef DEBUG -#include extern time_t time(); static time_t tmodesw; static time_t timm, tadd; @@ -44,12 +58,21 @@ static long nimmrays, naddrays; #define RDY_DEV 02 #define RDY_SIN 04 +static int disp_wait(void); +static void add_holo(HDGRID *hdg, char *gfn, char *pfn); +static void disp_bundle(PACKHEAD *p); +static void new_view(register VIEW *v); +static void set_focus(char *args); +static int usr_input(void); +static void printview(void); -main(argc, argv) -int argc; -char *argv[]; + +int +main( + int argc, + char *argv[] +) { - extern int eputs(); int rdy, inp, res = 0, pause = 0; progname = argv[0]; @@ -84,10 +107,14 @@ char *argv[]; inp = dev_input(); if (inp & DFL(DC_SETVIEW)) new_view(&odev.v); + else if (inp & DFL(DC_LASTVIEW)) + new_view(NULL); + if (inp & DFL(DC_REDRAW)) + imm_mode = beam_sync(1) > 0; if (inp & DFL(DC_GETVIEW)) printview(); - if (inp & DFL(DC_LASTVIEW)) - new_view(NULL); + if (inp & DFL(DC_FOCUS)) + set_focus(odev_args); if (inp & DFL(DC_KILL)) { serv_request(DR_KILL, 0, NULL); pause = 0; @@ -104,12 +131,10 @@ char *argv[]; } if (inp & DFL(DC_PAUSE)) pause = 1; - if (inp & DFL(DC_REDRAW)) - imm_mode = beam_sync(1) > 0; if (inp & DFL(DC_QUIT)) serv_request(DR_SHUTDOWN, 0, NULL); } - if (rdy & RDY_SIN) /* user input from sstdin */ + if (rdy & RDY_SIN && !imm_mode) /* user input from sstdin */ switch (usr_input()) { case DC_PAUSE: pause = 1; @@ -135,21 +160,23 @@ char *argv[]; #endif /* all done */ quit(0); + return 0; /* pro forma return */ } -int -disp_wait() /* wait for more input */ +static int +disp_wait(void) /* wait for more input */ { fd_set readset, errset; int flgs; int n; - register int i; /* see if we can avoid select call */ + if (hdlist[0] == NULL) + return(RDY_SRV); /* initialize first */ flgs = 0; /* flag what's ready already */ - if (imm_mode || stdin->_cnt > 0) + if (imm_mode || fbufcnt(stdin) > 0) flgs |= RDY_SRV; - if (sstdin != NULL && sstdin->_cnt > 0) + if (sstdin != NULL && fbufcnt(sstdin) > 0) flgs |= RDY_SIN; if (odev.inpready) flgs |= RDY_DEV; @@ -187,8 +214,12 @@ disp_wait() /* wait for more input */ } -add_holo(hdg) /* register a new holodeck section */ -HDGRID *hdg; +static void +add_holo( /* register a new holodeck section */ + HDGRID *hdg, + char *gfn, + char *pfn +) { VIEW nv; double d; @@ -201,12 +232,14 @@ HDGRID *hdg; hdlist[hd] = (HOLO *)malloc(sizeof(HOLO)); if (hdlist[hd] == NULL) error(SYSTEM, "out of memory in add_holo"); - bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID)); + memcpy((void *)hdlist[hd], (void *)hdg, sizeof(HDGRID)); hdcompgrid(hdlist[hd]); + hdgfn[hd] = savestr(gfn); + hdpfn[hd] = pfn && *pfn ? savestr(pfn) : (char *)NULL; if (hd) return; /* set initial viewpoint */ - copystruct(&nv, &odev.v); + nv = odev.v; VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5); VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5); VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5); @@ -222,15 +255,17 @@ HDGRID *hdg; } -disp_bundle(p) /* display a ray bundle */ -register PACKHEAD *p; +static void +disp_bundle( /* display a ray bundle */ + register PACKHEAD *p +) { GCOORD gc[2]; FVECT ro, rd, wp; double d; register int i; /* get beam coordinates */ - if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL) + if ((p->hd < 0) | (p->hd >= HDMAX) || hdlist[p->hd] == NULL) error(INTERNAL, "bad holodeck number in disp_bundle"); if (!hdbcoord(gc, hdlist[p->hd], p->bi)) error(INTERNAL, "bad beam index in disp_bundle"); @@ -251,13 +286,16 @@ register PACKHEAD *p; } -new_view(v) /* change view parameters */ -register VIEW *v; +static void +new_view( /* change view parameters */ + register VIEW *v +) { static VIEW viewhist[VIEWHISTLEN]; static unsigned nhist; VIEW *dv; int i, res[2]; + int16 *slist; char *err; /* restore previous view? */ if (v == NULL) { @@ -273,35 +311,103 @@ again: error(COMMAND, err); return; } + if (!dev_view(v)) /* notify display driver */ + goto again; if (v->type == VT_PAR) { error(COMMAND, "cannot handle parallel views"); return; } - if (!dev_view(v)) /* notify display driver */ - goto again; - dev_flush(); /* update screen */ - beam_init(0); /* compute new beam set */ - for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) - if (!beam_view(dv, res[0], res[1])) { + beam_init(odev.firstuse); /* compute new beam set */ + for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) { + if ((slist = beam_view(dv, res[0], res[1])) == NULL) { if (!nhist) { error(COMMAND, "invalid starting view"); return; } - copystruct(v, viewhist + ((nhist-1)%VIEWHISTLEN)); - goto again; + *v = *(viewhist + ((nhist-1)%VIEWHISTLEN)); + goto again; /* poss. overloading dev_section()? */ } - beam_sync(0); /* update server */ + DCHECK(*slist < 0, WARNING, "no visible sections in new_view"); + for ( ; *slist >= 0; slist++) + dev_section(hdgfn[*slist], hdpfn[*slist]); + } + dev_section(NULL,NULL); /* end section list */ + dev_flush(); /* update display */ + /* update server */ + imm_mode = beam_sync(odev.firstuse) > 0; /* record new view */ if (v < viewhist || v >= viewhist+VIEWHISTLEN) { - copystruct(viewhist + (nhist%VIEWHISTLEN), v); + *(viewhist + (nhist%VIEWHISTLEN)) = *v; nhist++; } } -int -usr_input() /* get user input and process it */ +static void +set_focus( /* set focus frame */ + char *args +) { + double hcent, vcent, hsiz, vsiz; + VIEW *dv, vwfocus; + int i, res[2]; + + i = sscanf(args, "%lf %lf %lf %lf", &hcent, &vcent, &hsiz, &vsiz); + if (i < 2 || hcent < 0 || hcent > 1 || vcent < 0 || vcent > 1) { + beam_init(0); /* restore view */ + for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) + beam_view(dv, res[0], res[1]); + beam_sync(0); /* update server */ + return; + } + if (i < 4 || hsiz <= hcent || hsiz > 1 || vsiz <= vcent || vsiz > 1) + hsiz = vsiz = FSIZDEF; /* gave center only */ + else { + hsiz -= hcent; hcent += 0.5*hsiz; /* gave min and max */ + vsiz -= vcent; vcent += 0.5*vsiz; + } + beam_init(0); /* add basic views */ + for (i = 0; (dv = dev_auxview(i, res)) != NULL; i++) + beam_view(dv, res[0]>>4, res[1]>>4); + vwfocus = odev.v; /* add focus view */ + switch (odev.v.type) { + case VT_PER: + vwfocus.horiz = 2.*180./PI*atan( + hsiz * tan(PI/180./2.*odev.v.horiz) ); + vwfocus.vert = 2.*180./PI*atan( + vsiz * tan(PI/180./2.*odev.v.vert) ); + break; + case VT_PAR: + case VT_ANG: + vwfocus.horiz = hsiz * odev.v.horiz; + vwfocus.vert = vsiz * odev.v.vert; + break; + case VT_HEM: + vwfocus.horiz = 2.*180./PI*asin( + hsiz * sin(PI/180./2.*odev.v.horiz) ); + vwfocus.vert = 2.*180./PI*asin( + vsiz * sin(PI/180./2.*odev.v.vert) ); + break; + case VT_CYL: + vwfocus.horiz = hsiz * odev.v.horiz; + vwfocus.vert = 2.*180./PI*atan( + vsiz * tan(PI/180./2.*odev.v.vert) ); + break; + default: + error(INTERNAL, "bad view type in set_focus"); + } + vwfocus.hoff = (odev.v.hoff + hcent - 0.5)/hsiz; + vwfocus.voff = (odev.v.voff + vcent - 0.5)/vsiz; + setview(&vwfocus); + beam_view(&vwfocus, (int)(3*odev.hres*hsiz)+100, + (int)(3*odev.vres*vsiz)+100); + beam_sync(0); /* update server */ +} + + +static int +usr_input(void) /* get user input and process it */ +{ VIEW vparams; char cmd[256]; register char *args; @@ -329,7 +435,7 @@ usr_input() /* get user input and process it */ } switch (i) { case DC_SETVIEW: /* set the view */ - copystruct(&vparams, &odev.v); + vparams = odev.v; if (!sscanview(&vparams, args)) error(COMMAND, "missing view options"); else @@ -341,6 +447,9 @@ usr_input() /* get user input and process it */ case DC_LASTVIEW: /* restore previous view */ new_view(NULL); break; + case DC_FOCUS: /* set focus frame */ + set_focus(args); + break; case DC_PAUSE: /* pause the current calculation */ case DC_RESUME: /* resume the calculation */ /* handled in main() */ @@ -368,7 +477,8 @@ usr_input() /* get user input and process it */ } -printview() /* print our current view to server stdout */ +static void +printview(void) /* print our current view to server stdout */ { fputs(VIEWSTR, sstdout); fprintview(&odev.v, sstdout); @@ -377,13 +487,12 @@ printview() /* print our current view to server stdo } -int -serv_result() /* get next server result and process it */ +extern int +serv_result(void) /* get next server result and process it */ { static char *buf = NULL; static int bufsiz = 0; MSGHEAD msg; - int n; /* read message header */ if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1) goto readerr; @@ -392,7 +501,8 @@ serv_result() /* get next server result and process if (buf == NULL) buf = (char *)malloc(bufsiz=msg.nbytes); else - buf = (char *)realloc(buf, bufsiz=msg.nbytes); + buf = (char *)realloc((void *)buf, + bufsiz=msg.nbytes); if (buf == NULL) error(SYSTEM, "out of memory in serv_result"); } @@ -407,9 +517,10 @@ serv_result() /* get next server result and process disp_bundle((PACKHEAD *)buf); break; case DS_ADDHOLO: - if (msg.nbytes != sizeof(HDGRID)) + if (msg.nbytes < sizeof(HDGRID)+2) error(INTERNAL, "bad holodeck record from server"); - add_holo((HDGRID *)buf); + add_holo((HDGRID *)buf, buf+sizeof(HDGRID), + buf+sizeof(HDGRID)+strlen(buf+sizeof(HDGRID))+1); break; case DS_OUTSECT: do_outside = 1; @@ -420,15 +531,16 @@ serv_result() /* get next server result and process break; case DS_STARTIMM: case DS_ENDIMM: + if (!(imm_mode = msg.type==DS_STARTIMM)) + dev_flush(); #ifdef DEBUG - if (imm_mode != (msg.type==DS_STARTIMM)) { + { time_t tnow = time(NULL); - if (imm_mode) timm += tnow - tmodesw; - else tadd += tnow - tmodesw; + if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw; + else timm += tnow - tmodesw; tmodesw = tnow; } #endif - imm_mode = msg.type==DS_STARTIMM; goto noargs; case DS_ACKNOW: case DS_SHUTDOWN: @@ -448,15 +560,22 @@ readerr: if (feof(stdin)) error(SYSTEM, "server process died"); error(SYSTEM, "error reading from server process"); + return -1; } -serv_request(type, nbytes, p) /* send a request to the server process */ -int type, nbytes; -char *p; +extern void +serv_request( /* send a request to the server process */ + int type, + int nbytes, + char *p +) { MSGHEAD msg; int m; + /* consistency checks */ + DCHECK(nbytes < 0 || nbytes > 0 & p == NULL, + CONSISTENCY, "bad buffer handed to serv_request"); /* get server's attention for big request */ if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) { serv_request(DR_ATTEN, 0, NULL); @@ -474,8 +593,10 @@ char *p; } -eputs(s) /* put error message to stderr */ -register char *s; +void +eputs( /* put error message to stderr */ + register char *s +) { static int midline = 0; @@ -493,10 +614,14 @@ register char *s; } -quit(code) /* clean up and exit */ -int code; +void +quit( /* clean up and exit */ + int code +) { + if (code) + exit(code); if (odev.v.type) dev_close(); - exit(code); + exit(0); }