ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo4.c
Revision: 3.10
Committed: Mon Dec 1 16:35:35 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.9: +3 -1 lines
Log Message:
improved error recovery when display process dies

File Contents

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