ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/ogl/gdio.c
Revision: 3.1
Committed: Tue Apr 22 11:16:46 1997 UTC (28 years ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 greg 3.1 /* Copyright (c) 1997 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Handle data stream to and from GL display process.
9     */
10    
11     #include "standard.h"
12     #include <string.h>
13     #include "gldisp.h"
14    
15    
16     #define GD_TYPELEN 1 /* a type is 1 byte in length */
17    
18     /* argument types */
19     #define GD_TY_END 0 /* argument list terminator */
20     #define GD_TY_NAM 1 /* 8-byte (max.) identifier */
21     #define GD_TY_INT 2 /* 4-byte integer */
22     #define GD_TY_FLT 3 /* 4-byte IEEE float */
23     #define GD_TY_DBL 4 /* 8-byte IEEE double */
24     #define GD_TY_CLR 5 /* 4-byte RGBE color value */
25     #define GD_TY_ARR 6 /* 5-byte array prefix */
26     #define GD_TY_STR 7 /* nul-terminated string */
27     #define GD_TY_ARG 8 /* 1-byte argument list prefix */
28     #define GD_TY_ERR 9 /* 1-byte error code */
29    
30     #define GD_NTYPES 10 /* number of argument types */
31    
32     /* argument lengths */
33     #define GD_ARGLEN {0,8,4,4,8,4,5,-1,1,1}
34    
35     #define GD_MAXID 8 /* maximum id. length (must be 8) */
36    
37     /*
38     * A request consists of an argument list, the first of
39     * which is always the request name as an 8-char (max.) string.
40     * The types of the following arguments must match the required
41     * arguments of the display request, or an error will result.
42     *
43     * Only functions return values, and they return them as a argument
44     * list on the client's receiving connection. It is up to the client
45     * program to keep track of its function calls and which values correspond
46     * to which request functions.
47     *
48     * An error is indicated with a special GD_TY_ERR code on the receiving
49     * connection, and usually indicates something fatal.
50     */
51    
52     /* error codes */
53     #define GD_ER_UNRECOG 0 /* unrecognized request */
54     #define GD_ER_ARGTYPE 1 /* argument type mismatch */
55     #define GD_ER_ARGMISS 2 /* argument(s) missing */
56    
57     #define GD_NERRS 3 /* number of errors */
58    
59     /* request argument */
60     typedef struct {
61     BYTE typ; /* argument type */
62     BYTE atyp; /* array subtype if typ==GD_TY_ARR */
63     union {
64     char n[GD_MAXID]; /* 8-char (max.) id. */
65     int4 n1, n2; /* used for ID comparison */
66     int4 i; /* 4-byte integer */
67     float f; /* 4-byte IEEE float */
68     double d; /* 8-byte IEEE double */
69     COLR c; /* RGBE */
70     struct array {
71     int4 l; /* length */
72     MEM_PTR p; /* values */
73     } a; /* array */
74     char *s; /* nul-terminated string */
75     } v; /* argument value */
76     } GDarg;
77    
78     /* a request and its arguments */
79     typedef struct gdreq {
80     struct gdreq *next; /* next request in list */
81     short argc; /* number of arguments */
82     GDarg argv[1]; /* argument list (expandable) */
83     } GDrequest;
84    
85     GDrequest *gdProTab[GD_HSIZ]; /* registered prototypes */
86    
87    
88     gdRegProto(r) /* register a request prototype */
89     register GDrequest *r;
90     {
91     register int4 hval;
92    
93     hval = gdHash(r->argv);
94     r->next = gdProTab[hval];
95     gdProTab[hval] = r;
96     }
97    
98    
99     GDrequest *
100     gdReqAlloc(nm, ac) /* allocate a request and its arguments */
101     char *nm;
102     int ac;
103     {
104     register GDrequest *nr;
105    
106     if (ac < 1)
107     return(NULL);
108     nr = (GDrequest *)malloc(sizeof(GDrequest)+(ac-1)*sizeof(GDarg));
109     if (nr == NULL)
110     return(NULL);
111     nr->next = NULL;
112     nr->argc = ac;
113     if (nm != NULL)
114     (void)strncmp(rn->argv[0].v.n, nm, GD_MAXID);
115     return(nr);
116     }
117    
118    
119     char *
120     gdStrAlloc(a, str) /* allocate and save a string value */
121     register GDarg *a;
122     char *str;
123     {
124     a->typ = GD_TY_STR;
125     a->v.s = (char *)malloc(strlen(str)+1);
126     if (a->v.s == NULL)
127     return(NULL);
128     return(strcpy(a->v.s, str));
129     }
130    
131    
132     MEM_PTR
133     gdArrAlloc(a, typ, len, arr) /* allocate and assign an array */
134     register GDarg *a;
135     int typ, len;
136     MEM_PTR arr;
137     {
138     static short esiz[GD_NTYPES] = GD_ELELEN;
139    
140     if (esiz[typ] <= 0)
141     return(NULL);
142     a->v.a.p = (MEM_PTR)malloc(len*esiz[typ]);
143     if (a->v.a.p == NULL)
144     return(NULL);
145     a->typ = GD_TY_ARR;
146     a->atyp = typ;
147     a->v.a.l = len;
148     if (arr != NULL)
149     (void)memcpy((char *)a->v.a.p, (char *)arr, len*esiz[typ]);
150     return(a->v.a.p);
151     }
152    
153    
154     gdDoneArg(a) /* free any argument data */
155     register GDarg *a;
156     {
157     register int j;
158     /* free allocated arrays */
159     switch (a->typ) {
160     case GD_TY_ARR: /* array of... */
161     if (a->atyp == GD_TY_ARR) { /* arrays */
162     for (j = a->v.a.l; j--; )
163     gdDoneArg((GDarg *)a->v.a.p + j);
164     } else if (a->atyp == GD_TY_STR) { /* strings */
165     for (j = r->v.a.l; j--; )
166     gdFree((char **)r->v.a.p + j);
167     }
168     gdFree(r->argv[i].v.a.p);
169     break;
170     case GD_TY_STR: /* string value */
171     gdFree(a->v.s);
172     break;
173     }
174     }
175    
176    
177     gdFreeReq(r) /* free a request */
178     register GDrequest *r;
179     {
180     register int i;
181     /* free any argument data */
182     for (i = r->argc; i--; )
183     gdDoneArg(r->argv + i);
184     gdFree(r); /* free basic structure */
185     }