| 1 | 
gregl | 
3.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 communication | 
| 9 | 
  | 
  | 
 */ | 
| 10 | 
  | 
  | 
 | 
| 11 | 
  | 
  | 
#include "rholo.h" | 
| 12 | 
  | 
  | 
#include "rhdisp.h" | 
| 13 | 
gregl | 
3.2 | 
#include <sys/uio.h> | 
| 14 | 
gregl | 
3.1 | 
 | 
| 15 | 
gregl | 
3.2 | 
#ifndef HDSUF | 
| 16 | 
  | 
  | 
#define HDSUF   ".hdisp" | 
| 17 | 
  | 
  | 
#endif | 
| 18 | 
gregl | 
3.1 | 
 | 
| 19 | 
gregl | 
3.2 | 
static int      inp_flags; | 
| 20 | 
  | 
  | 
static int      dpd[3]; | 
| 21 | 
gregl | 
3.5 | 
static FILE     *dpout; | 
| 22 | 
gregl | 
3.2 | 
 | 
| 23 | 
  | 
  | 
 | 
| 24 | 
  | 
  | 
disp_open(dname)                /* open the named display driver */ | 
| 25 | 
gregl | 
3.1 | 
char    *dname; | 
| 26 | 
  | 
  | 
{ | 
| 27 | 
gregl | 
3.5 | 
        char    dpath[128], *com[3]; | 
| 28 | 
gregl | 
3.2 | 
        int     i; | 
| 29 | 
  | 
  | 
 | 
| 30 | 
  | 
  | 
#ifdef DEVPATH | 
| 31 | 
  | 
  | 
        sprintf(dpath, "%s/%s%s", DEVPATH, dname, HDSUF); | 
| 32 | 
  | 
  | 
#else | 
| 33 | 
  | 
  | 
        sprintf(dpath, "dev/%s%s", dname, HDSUF); | 
| 34 | 
  | 
  | 
#endif | 
| 35 | 
gregl | 
3.5 | 
        com[0] = dpath; com[1] = froot; com[2] = NULL; | 
| 36 | 
gregl | 
3.6 | 
        i = open_process(dpd, com); | 
| 37 | 
  | 
  | 
        if (i <= 0) | 
| 38 | 
gregl | 
3.2 | 
                error(USER, "cannot start display process"); | 
| 39 | 
gregl | 
3.5 | 
        if ((dpout = fdopen(dup(dpd[1]), "w")) == NULL) | 
| 40 | 
  | 
  | 
                error(SYSTEM, "cannot associate FILE with display pipe"); | 
| 41 | 
gregl | 
3.2 | 
        inp_flags = 0; | 
| 42 | 
  | 
  | 
                                /* write out hologram grids */ | 
| 43 | 
  | 
  | 
        for (i = 0; hdlist[i] != NULL; i++) | 
| 44 | 
  | 
  | 
                disp_result(DS_ADDHOLO, sizeof(HDGRID), (char *)hdlist[i]); | 
| 45 | 
gregl | 
3.5 | 
        disp_flush(); | 
| 46 | 
gregl | 
3.1 | 
} | 
| 47 | 
  | 
  | 
 | 
| 48 | 
  | 
  | 
 | 
| 49 | 
gregl | 
3.2 | 
disp_packet(p)                  /* display a packet */ | 
| 50 | 
gregl | 
3.4 | 
register PACKHEAD       *p; | 
| 51 | 
gregl | 
3.2 | 
{ | 
| 52 | 
gregl | 
3.4 | 
        disp_result(DS_BUNDLE, packsiz(p->nr), (char *)p); | 
| 53 | 
gregl | 
3.2 | 
} | 
| 54 | 
  | 
  | 
 | 
| 55 | 
  | 
  | 
 | 
| 56 | 
gregl | 
3.1 | 
disp_check(block)               /* check display process */ | 
| 57 | 
  | 
  | 
int     block; | 
| 58 | 
  | 
  | 
{ | 
| 59 | 
gregl | 
3.2 | 
        MSGHEAD msg; | 
| 60 | 
  | 
  | 
        int     n; | 
| 61 | 
  | 
  | 
        char    *buf = NULL; | 
| 62 | 
  | 
  | 
 | 
| 63 | 
gregl | 
3.6 | 
        if (dpout == NULL) | 
| 64 | 
gregl | 
3.2 | 
                return(-1); | 
| 65 | 
gregl | 
3.5 | 
                                        /* flush display output */ | 
| 66 | 
  | 
  | 
        disp_flush(); | 
| 67 | 
gregl | 
3.3 | 
                                        /* check read blocking */ | 
| 68 | 
gregl | 
3.2 | 
        if (block != (inp_flags == 0)) { | 
| 69 | 
  | 
  | 
                inp_flags = block ? 0 : FNONBLK; | 
| 70 | 
  | 
  | 
                if (fcntl(dpd[0], F_SETFL, inp_flags) < 0) | 
| 71 | 
  | 
  | 
                        goto fcntlerr; | 
| 72 | 
  | 
  | 
        } | 
| 73 | 
  | 
  | 
                                        /* read message header */ | 
| 74 | 
  | 
  | 
        n = read(dpd[0], (char *)&msg, sizeof(MSGHEAD)); | 
| 75 | 
  | 
  | 
        if (n != sizeof(MSGHEAD)) { | 
| 76 | 
  | 
  | 
                if (n >= 0) | 
| 77 | 
  | 
  | 
                        error(USER, "display process died"); | 
| 78 | 
  | 
  | 
                if (errno != EAGAIN & errno != EINTR) | 
| 79 | 
  | 
  | 
                        goto readerr; | 
| 80 | 
gregl | 
3.3 | 
                return(2);              /* acceptable failure */ | 
| 81 | 
gregl | 
3.2 | 
        } | 
| 82 | 
  | 
  | 
        if (msg.nbytes) {               /* get the message body */ | 
| 83 | 
  | 
  | 
                buf = (char *)malloc(msg.nbytes); | 
| 84 | 
  | 
  | 
                if (buf == NULL) | 
| 85 | 
  | 
  | 
                        error(SYSTEM, "out of memory in disp_check"); | 
| 86 | 
  | 
  | 
                if (fcntl(dpd[0], F_SETFL, inp_flags=0) < 0) | 
| 87 | 
  | 
  | 
                        goto fcntlerr; | 
| 88 | 
  | 
  | 
                if (readbuf(dpd[0], buf, msg.nbytes) != msg.nbytes) | 
| 89 | 
  | 
  | 
                        goto readerr; | 
| 90 | 
  | 
  | 
        } | 
| 91 | 
  | 
  | 
        switch (msg.type) {             /* take appropriate action */ | 
| 92 | 
  | 
  | 
        case DR_BUNDLE: | 
| 93 | 
  | 
  | 
                if (msg.nbytes != sizeof(PACKHEAD)) | 
| 94 | 
  | 
  | 
                        error(INTERNAL, "bad DR_BUNDLE from display process"); | 
| 95 | 
  | 
  | 
                bundle_set(BS_ADD, (PACKHEAD *)buf, 1); | 
| 96 | 
  | 
  | 
                break; | 
| 97 | 
  | 
  | 
        case DR_NEWSET: | 
| 98 | 
  | 
  | 
                if (msg.nbytes % sizeof(PACKHEAD)) | 
| 99 | 
  | 
  | 
                        error(INTERNAL, "bad DR_NEWSET from display process"); | 
| 100 | 
  | 
  | 
                disp_result(DS_STARTIMM, 0, NULL); | 
| 101 | 
  | 
  | 
                bundle_set(BS_NEW, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); | 
| 102 | 
  | 
  | 
                disp_result(DS_ENDIMM, 0, NULL); | 
| 103 | 
gregl | 
3.5 | 
                disp_flush(); | 
| 104 | 
gregl | 
3.2 | 
                break; | 
| 105 | 
  | 
  | 
        case DR_ADDSET: | 
| 106 | 
  | 
  | 
                if (msg.nbytes % sizeof(PACKHEAD)) | 
| 107 | 
  | 
  | 
                        error(INTERNAL, "bad DR_ADDSET from display process"); | 
| 108 | 
  | 
  | 
                disp_result(DS_STARTIMM, 0, NULL); | 
| 109 | 
  | 
  | 
                bundle_set(BS_ADD, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); | 
| 110 | 
  | 
  | 
                disp_result(DS_ENDIMM, 0, NULL); | 
| 111 | 
gregl | 
3.5 | 
                disp_flush(); | 
| 112 | 
gregl | 
3.2 | 
                break; | 
| 113 | 
  | 
  | 
        case DR_DELSET: | 
| 114 | 
  | 
  | 
                if (msg.nbytes % sizeof(PACKHEAD)) | 
| 115 | 
  | 
  | 
                        error(INTERNAL, "bad DR_DELSET from display process"); | 
| 116 | 
  | 
  | 
                bundle_set(BS_DEL, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); | 
| 117 | 
  | 
  | 
                break; | 
| 118 | 
  | 
  | 
        case DR_ATTEN: | 
| 119 | 
  | 
  | 
                if (msg.nbytes) | 
| 120 | 
  | 
  | 
                        error(INTERNAL, "bad DR_ATTEN from display process"); | 
| 121 | 
  | 
  | 
                                        /* send acknowledgement */ | 
| 122 | 
  | 
  | 
                disp_result(DS_ACKNOW, 0, NULL); | 
| 123 | 
gregl | 
3.3 | 
                return(disp_check(1));  /* block on following request */ | 
| 124 | 
gregl | 
3.2 | 
        case DR_SHUTDOWN: | 
| 125 | 
  | 
  | 
                if (msg.nbytes) | 
| 126 | 
  | 
  | 
                        error(INTERNAL, "bad DR_SHUTDOWN from display process"); | 
| 127 | 
  | 
  | 
                return(0);              /* zero return signals shutdown */ | 
| 128 | 
  | 
  | 
        default: | 
| 129 | 
  | 
  | 
                error(INTERNAL, "unrecognized request from display process"); | 
| 130 | 
  | 
  | 
        } | 
| 131 | 
gregl | 
3.3 | 
        if (msg.nbytes)                 /* clean up */ | 
| 132 | 
gregl | 
3.2 | 
                free(buf); | 
| 133 | 
  | 
  | 
        return(1);                      /* normal return value */ | 
| 134 | 
  | 
  | 
fcntlerr: | 
| 135 | 
  | 
  | 
        error(SYSTEM, "cannot change display blocking mode"); | 
| 136 | 
  | 
  | 
readerr: | 
| 137 | 
  | 
  | 
        error(SYSTEM, "error reading from display process"); | 
| 138 | 
gregl | 
3.1 | 
} | 
| 139 | 
  | 
  | 
 | 
| 140 | 
  | 
  | 
 | 
| 141 | 
gregl | 
3.2 | 
int | 
| 142 | 
  | 
  | 
disp_close()                    /* close our display process */ | 
| 143 | 
gregl | 
3.1 | 
{ | 
| 144 | 
gregl | 
3.2 | 
        int     rval; | 
| 145 | 
  | 
  | 
 | 
| 146 | 
gregl | 
3.6 | 
        if (dpout == NULL) | 
| 147 | 
gregl | 
3.2 | 
                return(-1); | 
| 148 | 
  | 
  | 
        disp_result(DS_SHUTDOWN, 0, NULL); | 
| 149 | 
gregl | 
3.5 | 
        fclose(dpout); | 
| 150 | 
  | 
  | 
        dpout = NULL; | 
| 151 | 
gregl | 
3.2 | 
        return(close_process(dpd)); | 
| 152 | 
  | 
  | 
} | 
| 153 | 
  | 
  | 
 | 
| 154 | 
  | 
  | 
 | 
| 155 | 
gregl | 
3.5 | 
disp_result(type, nbytes, p)    /* queue result message to display process */ | 
| 156 | 
gregl | 
3.2 | 
int     type, nbytes; | 
| 157 | 
  | 
  | 
char    *p; | 
| 158 | 
  | 
  | 
{ | 
| 159 | 
  | 
  | 
        MSGHEAD msg; | 
| 160 | 
  | 
  | 
 | 
| 161 | 
gregl | 
3.6 | 
        if (dpout == NULL) | 
| 162 | 
  | 
  | 
                return; | 
| 163 | 
gregl | 
3.2 | 
        msg.type = type; | 
| 164 | 
  | 
  | 
        msg.nbytes = nbytes; | 
| 165 | 
gregl | 
3.5 | 
        fwrite((char *)&msg, sizeof(MSGHEAD), 1, dpout); | 
| 166 | 
  | 
  | 
        if (nbytes > 0) | 
| 167 | 
  | 
  | 
                fwrite(p, 1, nbytes, dpout); | 
| 168 | 
  | 
  | 
} | 
| 169 | 
  | 
  | 
 | 
| 170 | 
  | 
  | 
 | 
| 171 | 
  | 
  | 
disp_flush()                    /* flush output to display */ | 
| 172 | 
  | 
  | 
{ | 
| 173 | 
  | 
  | 
        if (fflush(dpout) < 0) | 
| 174 | 
  | 
  | 
                error(SYSTEM, "error writing to the display process"); | 
| 175 | 
gregl | 
3.1 | 
} |