| 1 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */
|
| 2 |
|
| 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 |
|
| 16 |
HOLO *hdlist[HDMAX+1]; /* global holodeck list */
|
| 17 |
|
| 18 |
int imm_mode = 0; /* bundles are being delivered immediately */
|
| 19 |
|
| 20 |
char *progname; /* global argv[0] */
|
| 21 |
|
| 22 |
#define RDY_SRV 01
|
| 23 |
#define RDY_DEV 02
|
| 24 |
|
| 25 |
|
| 26 |
main(argc, argv)
|
| 27 |
int argc;
|
| 28 |
char *argv[];
|
| 29 |
{
|
| 30 |
int rdy, inp, res = 0, pause = 0;
|
| 31 |
|
| 32 |
progname = argv[0];
|
| 33 |
if (argc != 2)
|
| 34 |
error(USER, "bad command line arguments");
|
| 35 |
/* open our device */
|
| 36 |
dev_open(argv[1]);
|
| 37 |
/* enter main loop */
|
| 38 |
do {
|
| 39 |
rdy = disp_wait();
|
| 40 |
if (rdy & RDY_DEV) { /* get user input */
|
| 41 |
inp = dev_input();
|
| 42 |
if (inp & DEV_NEWVIEW)
|
| 43 |
new_view(&odev.v);
|
| 44 |
if (inp & DEV_SHUTDOWN)
|
| 45 |
serv_request(DR_SHUTDOWN, 0, NULL);
|
| 46 |
if (inp & DEV_REDRAW) {
|
| 47 |
imm_mode = 1; /* preempt updates */
|
| 48 |
beam_sync();
|
| 49 |
}
|
| 50 |
if (inp & DEV_WAIT)
|
| 51 |
pause = 1;
|
| 52 |
if (inp & DEV_RESUME) {
|
| 53 |
serv_request(DR_NOOP, 0, NULL);
|
| 54 |
pause = 0;
|
| 55 |
}
|
| 56 |
}
|
| 57 |
if (rdy & RDY_SRV) { /* get server result */
|
| 58 |
res = serv_result();
|
| 59 |
if (pause && res != DS_SHUTDOWN) {
|
| 60 |
serv_request(DR_ATTEN, 0, NULL);
|
| 61 |
while ((res = serv_result()) != DS_ACKNOW &&
|
| 62 |
res != DS_SHUTDOWN)
|
| 63 |
;
|
| 64 |
}
|
| 65 |
}
|
| 66 |
} while (res != DS_SHUTDOWN);
|
| 67 |
/* all done */
|
| 68 |
quit(0);
|
| 69 |
}
|
| 70 |
|
| 71 |
|
| 72 |
int
|
| 73 |
disp_wait() /* wait for more input */
|
| 74 |
{
|
| 75 |
fd_set readset, errset;
|
| 76 |
int flgs;
|
| 77 |
int n;
|
| 78 |
register int i;
|
| 79 |
/* see if we can avoid select call */
|
| 80 |
if (imm_mode || stdin->_cnt > 0)
|
| 81 |
return(RDY_SRV);
|
| 82 |
if (dev_flush())
|
| 83 |
return(RDY_DEV);
|
| 84 |
/* make the call */
|
| 85 |
FD_ZERO(&readset); FD_ZERO(&errset);
|
| 86 |
FD_SET(0, &readset);
|
| 87 |
FD_SET(0, &errset);
|
| 88 |
FD_SET(odev.ifd, &readset);
|
| 89 |
FD_SET(odev.ifd, &errset);
|
| 90 |
n = odev.ifd + 1;
|
| 91 |
n = select(n, &readset, NULL, &errset, NULL);
|
| 92 |
if (n < 0) {
|
| 93 |
if (errno == EINTR)
|
| 94 |
return(0);
|
| 95 |
error(SYSTEM, "select call failure in disp_wait");
|
| 96 |
}
|
| 97 |
flgs = 0; /* flag what's ready */
|
| 98 |
if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset))
|
| 99 |
flgs |= RDY_SRV;
|
| 100 |
if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset))
|
| 101 |
flgs |= RDY_DEV;
|
| 102 |
return(flgs);
|
| 103 |
}
|
| 104 |
|
| 105 |
|
| 106 |
add_holo(hdg) /* register a new holodeck section */
|
| 107 |
HDGRID *hdg;
|
| 108 |
{
|
| 109 |
VIEW nv;
|
| 110 |
register int hd;
|
| 111 |
|
| 112 |
for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++)
|
| 113 |
;
|
| 114 |
if (hd >= HDMAX)
|
| 115 |
error(INTERNAL, "too many holodeck sections in add_holo");
|
| 116 |
hdlist[hd] = (HOLO *)malloc(sizeof(HOLO));
|
| 117 |
if (hdlist[hd] == NULL)
|
| 118 |
error(SYSTEM, "out of memory in add_holo");
|
| 119 |
bcopy((char *)hdg, (char *)hdlist[hd], sizeof(HDGRID));
|
| 120 |
hdcompgrid(hdlist[hd]);
|
| 121 |
if (hd)
|
| 122 |
return;
|
| 123 |
/* set initial viewpoint */
|
| 124 |
copystruct(&nv, &odev.v);
|
| 125 |
VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5);
|
| 126 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5);
|
| 127 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5);
|
| 128 |
fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]);
|
| 129 |
VCOPY(nv.vup, hdlist[0]->xv[2]);
|
| 130 |
new_view(&nv);
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
disp_bundle(p) /* display a ray bundle */
|
| 135 |
register PACKHEAD *p;
|
| 136 |
{
|
| 137 |
GCOORD gc[2];
|
| 138 |
FVECT ro, rd, wp;
|
| 139 |
double d;
|
| 140 |
register int i;
|
| 141 |
/* get beam coordinates */
|
| 142 |
if (p->hd < 0 | p->hd >= HDMAX || hdlist[p->hd] == NULL)
|
| 143 |
error(INTERNAL, "bad holodeck number in disp_bundle");
|
| 144 |
if (!hdbcoord(gc, hdlist[p->hd], p->bi))
|
| 145 |
error(INTERNAL, "bad beam index in disp_bundle");
|
| 146 |
/* display each ray */
|
| 147 |
for (i = p->nr; i--; ) {
|
| 148 |
hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r);
|
| 149 |
d = hddepth(hdlist[p->hd], packra(p)[i].d);
|
| 150 |
VSUM(wp, ro, rd, d); /* might be behind viewpoint */
|
| 151 |
dev_value(packra(p)[i].v, wp);
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
|
| 156 |
new_view(v) /* change view parameters */
|
| 157 |
VIEW *v;
|
| 158 |
{
|
| 159 |
char *err;
|
| 160 |
|
| 161 |
do {
|
| 162 |
if ((err = setview(v)) != NULL)
|
| 163 |
error(INTERNAL, err);
|
| 164 |
dev_view(v); /* update display driver */
|
| 165 |
dev_flush(); /* update screen */
|
| 166 |
} while (!beam_view(v)); /* update beam list */
|
| 167 |
}
|
| 168 |
|
| 169 |
|
| 170 |
int
|
| 171 |
serv_result() /* get next server result and process it */
|
| 172 |
{
|
| 173 |
static char *buf = NULL;
|
| 174 |
static int bufsiz = 0;
|
| 175 |
MSGHEAD msg;
|
| 176 |
int n;
|
| 177 |
/* read message header */
|
| 178 |
if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1)
|
| 179 |
goto readerr;
|
| 180 |
if (msg.nbytes > 0) { /* get the message body */
|
| 181 |
if (msg.nbytes > bufsiz) {
|
| 182 |
if (buf == NULL)
|
| 183 |
buf = (char *)malloc(bufsiz=msg.nbytes);
|
| 184 |
else
|
| 185 |
buf = (char *)realloc(buf, bufsiz=msg.nbytes);
|
| 186 |
if (buf == NULL)
|
| 187 |
error(SYSTEM, "out of memory in serv_result");
|
| 188 |
}
|
| 189 |
if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes)
|
| 190 |
goto readerr;
|
| 191 |
}
|
| 192 |
switch (msg.type) { /* process results */
|
| 193 |
case DS_BUNDLE:
|
| 194 |
if (msg.nbytes < sizeof(PACKHEAD) ||
|
| 195 |
msg.nbytes != packsiz(((PACKHEAD *)buf)->nr))
|
| 196 |
error(INTERNAL, "bad display packet from server");
|
| 197 |
disp_bundle((PACKHEAD *)buf);
|
| 198 |
break;
|
| 199 |
case DS_ADDHOLO:
|
| 200 |
if (msg.nbytes != sizeof(HDGRID))
|
| 201 |
error(INTERNAL, "bad holodeck record from server");
|
| 202 |
add_holo((HDGRID *)buf);
|
| 203 |
break;
|
| 204 |
case DS_STARTIMM:
|
| 205 |
case DS_ENDIMM:
|
| 206 |
imm_mode = msg.type==DS_STARTIMM;
|
| 207 |
if (msg.type == DS_ENDIMM)
|
| 208 |
dev_flush(); /* update display NOW */
|
| 209 |
/* fall through */
|
| 210 |
case DS_ACKNOW:
|
| 211 |
case DS_SHUTDOWN:
|
| 212 |
if (msg.nbytes) {
|
| 213 |
sprintf(errmsg,
|
| 214 |
"unexpected body with server message %d",
|
| 215 |
msg.type);
|
| 216 |
error(INTERNAL, errmsg);
|
| 217 |
}
|
| 218 |
break;
|
| 219 |
default:
|
| 220 |
error(INTERNAL, "unrecognized result from server process");
|
| 221 |
}
|
| 222 |
return(msg.type); /* return message type */
|
| 223 |
readerr:
|
| 224 |
if (feof(stdin))
|
| 225 |
error(SYSTEM, "server process died");
|
| 226 |
error(SYSTEM, "error reading from server process");
|
| 227 |
}
|
| 228 |
|
| 229 |
|
| 230 |
serv_request(type, nbytes, p) /* send a request to the server process */
|
| 231 |
int type, nbytes;
|
| 232 |
char *p;
|
| 233 |
{
|
| 234 |
MSGHEAD msg;
|
| 235 |
int m;
|
| 236 |
/* get server's attention for big request */
|
| 237 |
if (nbytes >= BIGREQSIZ-sizeof(MSGHEAD)) {
|
| 238 |
serv_request(DR_ATTEN, 0, NULL);
|
| 239 |
while ((m = serv_result()) != DS_ACKNOW)
|
| 240 |
if (m == DS_SHUTDOWN) /* the bugger quit on us */
|
| 241 |
quit(0);
|
| 242 |
}
|
| 243 |
msg.type = type; /* write and flush the message */
|
| 244 |
msg.nbytes = nbytes;
|
| 245 |
fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout);
|
| 246 |
if (nbytes > 0)
|
| 247 |
fwrite(p, 1, nbytes, stdout);
|
| 248 |
if (fflush(stdout) < 0)
|
| 249 |
error(SYSTEM, "write error in serv_request");
|
| 250 |
}
|
| 251 |
|
| 252 |
|
| 253 |
eputs(s) /* put error message to stderr */
|
| 254 |
register char *s;
|
| 255 |
{
|
| 256 |
static int midline = 0;
|
| 257 |
|
| 258 |
if (!*s)
|
| 259 |
return;
|
| 260 |
if (!midline++) { /* prepend line with program name */
|
| 261 |
fputs(progname, stderr);
|
| 262 |
fputs(": ", stderr);
|
| 263 |
}
|
| 264 |
fputs(s, stderr);
|
| 265 |
if (s[strlen(s)-1] == '\n') {
|
| 266 |
fflush(stderr);
|
| 267 |
midline = 0;
|
| 268 |
}
|
| 269 |
}
|
| 270 |
|
| 271 |
|
| 272 |
quit(code) /* clean up and exit */
|
| 273 |
int code;
|
| 274 |
{
|
| 275 |
if (odev.v.type)
|
| 276 |
dev_close();
|
| 277 |
exit(code);
|
| 278 |
}
|