| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: rhdisp.c,v 3.48 2003/07/21 22:30:18 schorsch Exp $";
|
| 3 |
#endif
|
| 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 |
|
| 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 */
|
| 40 |
|
| 41 |
int do_outside = 0; /* render from outside sections */
|
| 42 |
|
| 43 |
double eyesepdist = 1; /* eye separation distance */
|
| 44 |
|
| 45 |
char *progname; /* global argv[0] */
|
| 46 |
|
| 47 |
FILE *sstdin, *sstdout; /* server's standard input and output */
|
| 48 |
|
| 49 |
#ifdef DEBUG
|
| 50 |
extern time_t time();
|
| 51 |
static time_t tmodesw;
|
| 52 |
static time_t timm, tadd;
|
| 53 |
static long nimmrays, naddrays;
|
| 54 |
#endif
|
| 55 |
|
| 56 |
#define RDY_SRV 01
|
| 57 |
#define RDY_DEV 02
|
| 58 |
#define RDY_SIN 04
|
| 59 |
|
| 60 |
|
| 61 |
main(argc, argv)
|
| 62 |
int argc;
|
| 63 |
char *argv[];
|
| 64 |
{
|
| 65 |
int rdy, inp, res = 0, pause = 0;
|
| 66 |
|
| 67 |
progname = argv[0];
|
| 68 |
if (argc < 3)
|
| 69 |
error(USER, "bad command line arguments");
|
| 70 |
/* open our device */
|
| 71 |
dev_open(argv[1]);
|
| 72 |
/* open server process i/o */
|
| 73 |
sstdout = fdopen(atoi(argv[2]), "w");
|
| 74 |
if (argc < 4 || (inp = atoi(argv[3])) < 0)
|
| 75 |
sstdin = NULL;
|
| 76 |
else
|
| 77 |
sstdin = fdopen(inp, "r");
|
| 78 |
/* set command error vector */
|
| 79 |
erract[COMMAND].pf = eputs;
|
| 80 |
#ifdef DEBUG
|
| 81 |
tmodesw = time(NULL);
|
| 82 |
#endif
|
| 83 |
/* enter main loop */
|
| 84 |
do {
|
| 85 |
rdy = disp_wait();
|
| 86 |
if (rdy & RDY_SRV) { /* process server result */
|
| 87 |
res = serv_result();
|
| 88 |
if (pause && res != DS_SHUTDOWN) {
|
| 89 |
serv_request(DR_ATTEN, 0, NULL);
|
| 90 |
while ((res = serv_result()) != DS_ACKNOW &&
|
| 91 |
res != DS_SHUTDOWN)
|
| 92 |
;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
if (rdy & RDY_DEV) { /* user input from driver */
|
| 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_FOCUS))
|
| 106 |
set_focus(odev_args);
|
| 107 |
if (inp & DFL(DC_KILL)) {
|
| 108 |
serv_request(DR_KILL, 0, NULL);
|
| 109 |
pause = 0;
|
| 110 |
}
|
| 111 |
if (inp & DFL(DC_CLOBBER))
|
| 112 |
serv_request(DR_CLOBBER, 0, NULL);
|
| 113 |
if (inp & DFL(DC_RESTART)) {
|
| 114 |
serv_request(DR_RESTART, 0, NULL);
|
| 115 |
pause = 0;
|
| 116 |
}
|
| 117 |
if (inp & DFL(DC_RESUME)) {
|
| 118 |
serv_request(DR_NOOP, 0, NULL);
|
| 119 |
pause = 0;
|
| 120 |
}
|
| 121 |
if (inp & DFL(DC_PAUSE))
|
| 122 |
pause = 1;
|
| 123 |
if (inp & DFL(DC_QUIT))
|
| 124 |
serv_request(DR_SHUTDOWN, 0, NULL);
|
| 125 |
}
|
| 126 |
if (rdy & RDY_SIN && !imm_mode) /* user input from sstdin */
|
| 127 |
switch (usr_input()) {
|
| 128 |
case DC_PAUSE:
|
| 129 |
pause = 1;
|
| 130 |
break;
|
| 131 |
case DC_RESUME:
|
| 132 |
serv_request(DR_NOOP, 0, NULL);
|
| 133 |
/* fall through */
|
| 134 |
case DC_KILL:
|
| 135 |
case DC_RESTART:
|
| 136 |
pause = 0;
|
| 137 |
break;
|
| 138 |
}
|
| 139 |
} while (res != DS_SHUTDOWN);
|
| 140 |
#ifdef DEBUG
|
| 141 |
if (timm && nimmrays)
|
| 142 |
fprintf(stderr,
|
| 143 |
"%s: %.1f rays recalled/second (%ld rays total)\n",
|
| 144 |
progname, (double)nimmrays/timm, nimmrays);
|
| 145 |
if (tadd && naddrays)
|
| 146 |
fprintf(stderr,
|
| 147 |
"%s: %.1f rays calculated/second (%ld rays total)\n",
|
| 148 |
progname, (double)naddrays/tadd, naddrays);
|
| 149 |
#endif
|
| 150 |
/* all done */
|
| 151 |
quit(0);
|
| 152 |
}
|
| 153 |
|
| 154 |
|
| 155 |
int
|
| 156 |
disp_wait() /* wait for more input */
|
| 157 |
{
|
| 158 |
fd_set readset, errset;
|
| 159 |
int flgs;
|
| 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 || fbufcnt(stdin) > 0)
|
| 167 |
flgs |= RDY_SRV;
|
| 168 |
if (sstdin != NULL && fbufcnt(sstdin) > 0)
|
| 169 |
flgs |= RDY_SIN;
|
| 170 |
if (odev.inpready)
|
| 171 |
flgs |= RDY_DEV;
|
| 172 |
if (flgs) /* got something? */
|
| 173 |
return(flgs);
|
| 174 |
if (dev_flush()) /* else flush output & check keyboard+mouse */
|
| 175 |
return(RDY_DEV);
|
| 176 |
/* if nothing, we need to call select */
|
| 177 |
FD_ZERO(&readset); FD_ZERO(&errset);
|
| 178 |
FD_SET(0, &readset);
|
| 179 |
FD_SET(0, &errset);
|
| 180 |
FD_SET(odev.ifd, &readset);
|
| 181 |
FD_SET(odev.ifd, &errset);
|
| 182 |
n = odev.ifd+1;
|
| 183 |
if (sstdin != NULL) {
|
| 184 |
FD_SET(fileno(sstdin), &readset);
|
| 185 |
FD_SET(fileno(sstdin), &errset);
|
| 186 |
if (fileno(sstdin) >= n)
|
| 187 |
n = fileno(sstdin) + 1;
|
| 188 |
}
|
| 189 |
n = select(n, &readset, NULL, &errset, NULL);
|
| 190 |
if (n < 0) {
|
| 191 |
if (errno == EINTR)
|
| 192 |
return(0);
|
| 193 |
error(SYSTEM, "select call failure in disp_wait");
|
| 194 |
}
|
| 195 |
if (FD_ISSET(0, &readset) || FD_ISSET(0, &errset))
|
| 196 |
flgs |= RDY_SRV;
|
| 197 |
if (FD_ISSET(odev.ifd, &readset) || FD_ISSET(odev.ifd, &errset))
|
| 198 |
flgs |= RDY_DEV;
|
| 199 |
if (sstdin != NULL && (FD_ISSET(fileno(sstdin), &readset) ||
|
| 200 |
FD_ISSET(fileno(sstdin), &errset)))
|
| 201 |
flgs |= RDY_SIN;
|
| 202 |
return(flgs);
|
| 203 |
}
|
| 204 |
|
| 205 |
|
| 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;
|
| 212 |
register int hd;
|
| 213 |
|
| 214 |
for (hd = 0; hd < HDMAX && hdlist[hd] != NULL; hd++)
|
| 215 |
;
|
| 216 |
if (hd >= HDMAX)
|
| 217 |
error(INTERNAL, "too many holodeck sections in add_holo");
|
| 218 |
hdlist[hd] = (HOLO *)malloc(sizeof(HOLO));
|
| 219 |
if (hdlist[hd] == NULL)
|
| 220 |
error(SYSTEM, "out of memory in add_holo");
|
| 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 */
|
| 228 |
nv = odev.v;
|
| 229 |
VSUM(nv.vp, hdlist[0]->orig, hdlist[0]->xv[0], 0.5);
|
| 230 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[1], 0.5);
|
| 231 |
VSUM(nv.vp, nv.vp, hdlist[0]->xv[2], 0.5);
|
| 232 |
fcross(nv.vdir, hdlist[0]->xv[1], hdlist[0]->xv[2]);
|
| 233 |
VCOPY(nv.vup, hdlist[0]->xv[2]);
|
| 234 |
if (do_outside) {
|
| 235 |
normalize(nv.vdir);
|
| 236 |
d = VLEN(hdlist[0]->xv[1]);
|
| 237 |
d += VLEN(hdlist[0]->xv[2]);
|
| 238 |
VSUM(nv.vp, nv.vp, nv.vdir, -d);
|
| 239 |
}
|
| 240 |
new_view(&nv);
|
| 241 |
}
|
| 242 |
|
| 243 |
|
| 244 |
disp_bundle(p) /* display a ray bundle */
|
| 245 |
register PACKHEAD *p;
|
| 246 |
{
|
| 247 |
GCOORD gc[2];
|
| 248 |
FVECT ro, rd, wp;
|
| 249 |
double d;
|
| 250 |
register int i;
|
| 251 |
/* get beam coordinates */
|
| 252 |
if ((p->hd < 0) | (p->hd >= HDMAX) || hdlist[p->hd] == NULL)
|
| 253 |
error(INTERNAL, "bad holodeck number in disp_bundle");
|
| 254 |
if (!hdbcoord(gc, hdlist[p->hd], p->bi))
|
| 255 |
error(INTERNAL, "bad beam index in disp_bundle");
|
| 256 |
/* display each ray */
|
| 257 |
for (i = p->nr; i--; ) {
|
| 258 |
hdray(ro, rd, hdlist[p->hd], gc, packra(p)[i].r);
|
| 259 |
d = hddepth(hdlist[p->hd], packra(p)[i].d);
|
| 260 |
if (d < .99*FHUGE) {
|
| 261 |
VSUM(wp, ro, rd, d); /* might be behind viewpoint */
|
| 262 |
dev_value(packra(p)[i].v, rd, wp);
|
| 263 |
} else
|
| 264 |
dev_value(packra(p)[i].v, rd, NULL);
|
| 265 |
}
|
| 266 |
#ifdef DEBUG
|
| 267 |
if (imm_mode) nimmrays += p->nr;
|
| 268 |
else naddrays += p->nr;
|
| 269 |
#endif
|
| 270 |
}
|
| 271 |
|
| 272 |
|
| 273 |
new_view(v) /* change view parameters */
|
| 274 |
register VIEW *v;
|
| 275 |
{
|
| 276 |
static VIEW viewhist[VIEWHISTLEN];
|
| 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) {
|
| 284 |
if (nhist > 1) /* get one before last setting */
|
| 285 |
nhist--;
|
| 286 |
else /* else go to end of list */
|
| 287 |
while (nhist < VIEWHISTLEN && viewhist[nhist].type)
|
| 288 |
nhist++;
|
| 289 |
v = viewhist + ((nhist-1)%VIEWHISTLEN);
|
| 290 |
} else
|
| 291 |
again:
|
| 292 |
if ((err = setview(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 |
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 |
*v = *(viewhist + ((nhist-1)%VIEWHISTLEN));
|
| 310 |
goto again; /* poss. overloading dev_section()? */
|
| 311 |
}
|
| 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 |
*(viewhist + (nhist%VIEWHISTLEN)) = *v;
|
| 323 |
nhist++;
|
| 324 |
}
|
| 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 |
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 |
{
|
| 391 |
VIEW vparams;
|
| 392 |
char cmd[256];
|
| 393 |
register char *args;
|
| 394 |
register int i;
|
| 395 |
|
| 396 |
if (fgets(cmd, sizeof(cmd), sstdin) == NULL) {
|
| 397 |
fclose(sstdin);
|
| 398 |
sstdin = NULL;
|
| 399 |
return(-1);
|
| 400 |
}
|
| 401 |
if (*cmd == '\n')
|
| 402 |
return(DC_RESUME);
|
| 403 |
for (args = cmd; *args && !isspace(*args); args++)
|
| 404 |
;
|
| 405 |
while (isspace(*args))
|
| 406 |
*args++ = '\0';
|
| 407 |
if (*args && args[i=strlen(args)-1] == '\n')
|
| 408 |
args[i] = '\0';
|
| 409 |
for (i = 0; i < DC_NCMDS; i++)
|
| 410 |
if (!strcmp(cmd, cmdlist[i]))
|
| 411 |
break;
|
| 412 |
if (i >= DC_NCMDS) {
|
| 413 |
dev_auxcom(cmd, args);
|
| 414 |
return(-1);
|
| 415 |
}
|
| 416 |
switch (i) {
|
| 417 |
case DC_SETVIEW: /* set the view */
|
| 418 |
vparams = odev.v;
|
| 419 |
if (!sscanview(&vparams, args))
|
| 420 |
error(COMMAND, "missing view options");
|
| 421 |
else
|
| 422 |
new_view(&vparams);
|
| 423 |
break;
|
| 424 |
case DC_GETVIEW: /* print the current view */
|
| 425 |
printview();
|
| 426 |
break;
|
| 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() */
|
| 436 |
break;
|
| 437 |
case DC_REDRAW: /* redraw from server */
|
| 438 |
imm_mode = beam_sync(1) > 0;
|
| 439 |
dev_clear();
|
| 440 |
break;
|
| 441 |
case DC_KILL: /* kill rtrace process(es) */
|
| 442 |
serv_request(DR_KILL, 0, NULL);
|
| 443 |
break;
|
| 444 |
case DC_CLOBBER: /* clobber holodeck */
|
| 445 |
serv_request(DR_CLOBBER, 0, NULL);
|
| 446 |
break;
|
| 447 |
case DC_RESTART: /* restart rtrace */
|
| 448 |
serv_request(DR_RESTART, 0, NULL);
|
| 449 |
break;
|
| 450 |
case DC_QUIT: /* quit request */
|
| 451 |
serv_request(DR_SHUTDOWN, 0, NULL);
|
| 452 |
break;
|
| 453 |
default:
|
| 454 |
error(CONSISTENCY, "bad command id in usr_input");
|
| 455 |
}
|
| 456 |
return(i);
|
| 457 |
}
|
| 458 |
|
| 459 |
|
| 460 |
printview() /* print our current view to server stdout */
|
| 461 |
{
|
| 462 |
fputs(VIEWSTR, sstdout);
|
| 463 |
fprintview(&odev.v, sstdout);
|
| 464 |
fputc('\n', sstdout);
|
| 465 |
fflush(sstdout);
|
| 466 |
}
|
| 467 |
|
| 468 |
|
| 469 |
int
|
| 470 |
serv_result() /* get next server result and process it */
|
| 471 |
{
|
| 472 |
static char *buf = NULL;
|
| 473 |
static int bufsiz = 0;
|
| 474 |
MSGHEAD msg;
|
| 475 |
int n;
|
| 476 |
/* read message header */
|
| 477 |
if (fread((char *)&msg, sizeof(MSGHEAD), 1, stdin) != 1)
|
| 478 |
goto readerr;
|
| 479 |
if (msg.nbytes > 0) { /* get the message body */
|
| 480 |
if (msg.nbytes > bufsiz) {
|
| 481 |
if (buf == NULL)
|
| 482 |
buf = (char *)malloc(bufsiz=msg.nbytes);
|
| 483 |
else
|
| 484 |
buf = (char *)realloc((void *)buf,
|
| 485 |
bufsiz=msg.nbytes);
|
| 486 |
if (buf == NULL)
|
| 487 |
error(SYSTEM, "out of memory in serv_result");
|
| 488 |
}
|
| 489 |
if (fread(buf, 1, msg.nbytes, stdin) != msg.nbytes)
|
| 490 |
goto readerr;
|
| 491 |
}
|
| 492 |
switch (msg.type) { /* process results */
|
| 493 |
case DS_BUNDLE:
|
| 494 |
if (msg.nbytes < sizeof(PACKHEAD) ||
|
| 495 |
msg.nbytes != packsiz(((PACKHEAD *)buf)->nr))
|
| 496 |
error(INTERNAL, "bad display packet from server");
|
| 497 |
disp_bundle((PACKHEAD *)buf);
|
| 498 |
break;
|
| 499 |
case DS_ADDHOLO:
|
| 500 |
if (msg.nbytes < sizeof(HDGRID)+2)
|
| 501 |
error(INTERNAL, "bad holodeck record from server");
|
| 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;
|
| 507 |
goto noargs;
|
| 508 |
case DS_EYESEP:
|
| 509 |
if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY)
|
| 510 |
error(INTERNAL, "bad eye separation from server");
|
| 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 |
{
|
| 518 |
time_t tnow = time(NULL);
|
| 519 |
if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw;
|
| 520 |
else timm += tnow - tmodesw;
|
| 521 |
tmodesw = tnow;
|
| 522 |
}
|
| 523 |
#endif
|
| 524 |
goto noargs;
|
| 525 |
case DS_ACKNOW:
|
| 526 |
case DS_SHUTDOWN:
|
| 527 |
goto noargs;
|
| 528 |
default:
|
| 529 |
error(INTERNAL, "unrecognized result from server process");
|
| 530 |
}
|
| 531 |
return(msg.type); /* return message type */
|
| 532 |
noargs:
|
| 533 |
if (msg.nbytes) {
|
| 534 |
sprintf(errmsg, "unexpected body with server message %d",
|
| 535 |
msg.type);
|
| 536 |
error(INTERNAL, errmsg);
|
| 537 |
}
|
| 538 |
return(msg.type);
|
| 539 |
readerr:
|
| 540 |
if (feof(stdin))
|
| 541 |
error(SYSTEM, "server process died");
|
| 542 |
error(SYSTEM, "error reading from server process");
|
| 543 |
}
|
| 544 |
|
| 545 |
|
| 546 |
serv_request(type, nbytes, p) /* send a request to the server process */
|
| 547 |
int type, nbytes;
|
| 548 |
char *p;
|
| 549 |
{
|
| 550 |
MSGHEAD msg;
|
| 551 |
int m;
|
| 552 |
/* consistency checks */
|
| 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);
|
| 558 |
while ((m = serv_result()) != DS_ACKNOW)
|
| 559 |
if (m == DS_SHUTDOWN) /* the bugger quit on us */
|
| 560 |
quit(0);
|
| 561 |
}
|
| 562 |
msg.type = type; /* write and flush the message */
|
| 563 |
msg.nbytes = nbytes;
|
| 564 |
fwrite((char *)&msg, sizeof(MSGHEAD), 1, stdout);
|
| 565 |
if (nbytes > 0)
|
| 566 |
fwrite(p, 1, nbytes, stdout);
|
| 567 |
if (fflush(stdout) < 0)
|
| 568 |
error(SYSTEM, "write error in serv_request");
|
| 569 |
}
|
| 570 |
|
| 571 |
|
| 572 |
void
|
| 573 |
eputs(s) /* put error message to stderr */
|
| 574 |
register char *s;
|
| 575 |
{
|
| 576 |
static int midline = 0;
|
| 577 |
|
| 578 |
if (!*s)
|
| 579 |
return;
|
| 580 |
if (!midline++) { /* prepend line with program name */
|
| 581 |
fputs(progname, stderr);
|
| 582 |
fputs(": ", stderr);
|
| 583 |
}
|
| 584 |
fputs(s, stderr);
|
| 585 |
if (s[strlen(s)-1] == '\n') {
|
| 586 |
fflush(stderr);
|
| 587 |
midline = 0;
|
| 588 |
}
|
| 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(0);
|
| 601 |
}
|