pull-prototypes-to-head-20020821
[openafs.git] / src / gtx / objects.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Description:
12  *      Implementation of the gator object interface.
13  *
14  *------------------------------------------------------------------------*/
15
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19 RCSID("$Header$");
20
21 #include "gtxobjects.h"         /*Interface for this module*/
22 #include "gtxtextobj.h"         /*Text object interface*/
23 #include "gtxlightobj.h"        /*Light object interface*/
24 #include "gtxobjdict.h"         /*Object dictionary module*/
25 #include <stdio.h>              /*Standard I/O stuff*/
26 #include <errno.h>
27
28
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #else
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 #endif
36 #include <stdlib.h>
37
38 /*
39  * Number of known gator object types.
40  */
41 #define GATOR_NUM_OBJTYPES 3
42
43 static char mn[] = "gator_objects";             /*Module name*/
44 int objects_debug;                              /*Is debugging output on?*/
45
46 int (*on_create[GATOR_NUM_OBJTYPES])();         /*Array of ptrs to creation functions*/
47 struct onodeops objops[GATOR_NUM_OBJTYPES];     /*Per-type op arrays*/
48
49
50 /*--------------------------------------------------------------------------------
51  * gator_objects_init
52  *
53  * Description:
54  *      Initialize the gator object package.
55  *
56  * Arguments:
57  *      struct onode_initparams *params: Initialization parameters.
58  *
59  * Returns:
60  *      0 on success,
61  *      Error value otherwise.
62  *
63  * Environment:
64  *      *** MUST BE THE FIRST ROUTINE CALLED FROM
65  *            THIS PACKAGE ***
66  *
67  * Side Effects:
68  *      
69  *--------------------------------------------------------------------------------*/
70
71 int gator_objects_init(params)
72     struct onode_initparams *params;
73
74 { /*gator_objects_init*/
75
76     static char rn[] = "gator_objects_init";    /*Routine name*/
77     static int initialized = 0;                 /*Have we been called?*/
78     register int code;                          /*Return code*/
79
80     /*
81      * If we've already been called, just return.
82      */
83     if (initialized) {
84       initialized++;
85       if (objects_debug)
86         fprintf(stderr, "[%s:%s] Called more than once!! (%d time[s])\n",
87                 mn, rn, initialized);
88       return(0);
89     }
90
91     /*
92      * Remember our debugging level.
93      */
94     objects_debug = params->i_debug;
95
96     /*
97      * Set up the onode op array, one entry for each known gator object type.
98      */
99     if (objects_debug)
100       fprintf(stderr, "[%s:%s] Setting up objops array\n", mn, rn);
101     objops[GATOR_OBJ_TEXT]   = gator_text_ops;
102     objops[GATOR_OBJ_LIGHT]  = gator_light_ops;
103
104     /*
105       * Initialize the object dictionary.
106       */
107     if (objects_debug)
108       fprintf(stderr, "[%s:%s] Initializing object dictionary\n", mn, rn);
109     code = gator_objdict_init(objects_debug);
110     if (code) {
111         fprintf(stderr, "[%s:%s] Can't initialize object dictionary: error code is %d\n", mn, rn, code);
112         return(code);
113     }
114
115     /*
116       * Initialize the chosen window package.  Remember the base window
117       * is accessible as gator_basegwin.
118       */
119     if (objects_debug) {
120       fprintf(stderr, "[%s:%s] Initializing gator window module for package %d.\n", mn, rn, params->i_gwparams->i_type);
121       fprintf(stderr, "\tWindow init params are: type %d, (%d, %d), width=%d, height=%d, debug=%d\n", params->i_gwparams->i_type, params->i_gwparams->i_x, params->i_gwparams->i_y, params->i_gwparams->i_width, params->i_gwparams->i_height, params->i_gwparams->i_debug);
122     }
123     code = gw_init(params->i_gwparams);
124     if (code) {
125       fprintf(stderr, "[%s:%s] Can't initialize gator windows for package %d; error is: %d\n", mn, rn, params->i_gwparams->i_type, code);
126       return(code);
127     }
128
129     /*
130       * Set up the array of creation functions.
131       */
132     if (objects_debug)
133       fprintf(stderr, "[%s:%s] Initializing gator object creation function array.\n", mn, rn);
134     on_create[GATOR_OBJ_TEXT]   = gator_text_create;
135     on_create[GATOR_OBJ_LIGHT]  = gator_light_create;
136
137     /*
138      * Finally, return the good news.
139      */
140     return(0);
141
142 } /*gator_objects_init*/
143
144 /*--------------------------------------------------------------------------------
145  * gator_objects_create
146  *
147  * Description:
148  *      Create an onode of the given type.
149  *
150  * Arguments:
151  *      struct onode_createparams *params: Ptr to creation params.
152  *
153  * Returns:
154  *      Ptr to newly-created onode if successful,
155  *      Null pointer otherwise.
156  *
157  * Environment:
158  *      Nothing interesting.
159  *
160  * Side Effects:
161  *      As advertised.
162  *--------------------------------------------------------------------------------*/
163
164 struct onode *gator_objects_create(params)
165     struct onode_createparams *params;
166
167 { /*gator_objects_create*/
168
169     static char rn[] = "gator_objects_create";  /*Routine name*/
170     register int code;                          /*Return code*/
171     struct onode *new_onode;                    /*Ptr to new onode*/
172
173     if (objects_debug) {
174       fprintf(stderr, "[%s:%s] Creating onode type %d, named '%s'\n", mn, rn, params->cr_type, params->cr_name);
175       fprintf(stderr, "\tOrigin at (%d, %d)\n", params->cr_x, params->cr_y);
176       fprintf(stderr, "\tWidth=%d, height=%d\n", params->cr_width, params->cr_height);
177       fprintf(stderr, "\tHelpstring='%s'\n", params->cr_helpstring);
178       fprintf(stderr, "\tWindow struct at 0x%x\n", params->cr_window);
179     }
180
181     if (objects_debug)
182       fprintf(stderr, "[%s:%s] Allocating %d bytes for new onode structure\n", mn, rn, sizeof(struct onode));
183     new_onode = (struct onode *)malloc(sizeof(struct onode));
184     if (new_onode == NULL) {
185       fprintf(stderr, "[%s:%s] Can't allocate %d bytes for new onode structure; errno is %d\n", mn, rn, sizeof(struct onode), errno);
186       return(NULL);
187     }
188
189     /*
190       * Fill in the onode fields we can do right away.
191       * **** Don't do anything with cr_helpstring yet - eventually,
192       *      we'll create a scrollable text help object with it ****
193       */
194     if (objects_debug)
195       fprintf(stderr, "[%s:%s] Filling in onode fields\n", mn, rn, sizeof(struct onode));
196     new_onode->o_type     = params->cr_type;
197     strcpy(new_onode->o_name, params->cr_name);
198     new_onode->o_x        = params->cr_x;
199     new_onode->o_y        = params->cr_y;
200     new_onode->o_width    = params->cr_width;
201     new_onode->o_height   = params->cr_height;
202     new_onode->o_changed  = 1;
203     new_onode->o_refcount = 1;
204     new_onode->o_window   = params->cr_window;
205     new_onode->o_op       = &(objops[params->cr_type]);
206     new_onode->o_home     = params->cr_home_obj;
207     new_onode->o_help     = NULL;
208     new_onode->o_nextobj  = NULL;
209     new_onode->o_upobj    = params->cr_parent_obj;
210     new_onode->o_downobj  = NULL;
211
212     /*
213       * Call the proper routine to initialize the private parts of the
214       * given object.
215       */
216     if (objects_debug)
217       fprintf(stderr, "[%s:%s] Calling the creation routine for gator object type %d\n", mn, rn, params->cr_type);
218     code = (on_create[params->cr_type])(new_onode, params);
219     if (code) {
220       if (objects_debug)
221         fprintf(stderr, "[%s:%s] Error %d in creation routine for gator object type %d\n", mn, rn, code, params->cr_type);
222       free(new_onode);
223       return(NULL);
224     }
225
226     /*
227       * Set the links on the parent and previous objects, if so directed.
228       */
229     if (params->cr_prev_obj != NULL) {
230       if (objects_debug)
231         fprintf(stderr, "[%s:%s] Setting o_nextobj pointer in the previous object located at 0x%x (previous value was 0x%x)\n", mn, rn, params->cr_prev_obj, params->cr_prev_obj->o_nextobj);
232       params->cr_prev_obj->o_nextobj = new_onode;
233     }
234     if (params->cr_parent_obj != NULL) {
235       if (objects_debug)
236         fprintf(stderr, "[%s:%s] Setting o_downobj pointer in the parent object located at 0x%x (previous value was 0x%x)\n", mn, rn, params->cr_parent_obj, params->cr_parent_obj->o_downobj);
237       params->cr_parent_obj->o_downobj = new_onode;
238     }
239
240     /*
241       * Return the location of the completely-initialized onode object.
242       */
243     return(new_onode);
244
245 } /*gator_objects_create*/
246
247 /*--------------------------------------------------------------------------------
248  * gator_objects_lookup
249  *
250  * Description:
251  *      
252  *
253  * Arguments:
254  *      char *onode_name: Onode string name to find.
255  *
256  * Returns:
257  *      Ptr to onode matching the given name if one exists,
258  *      Null pointer otherwise.
259  *
260  * Environment:
261  *      Nothing interesting.
262  *
263  * Side Effects:
264  *      As advertised.
265  *--------------------------------------------------------------------------------*/
266
267 struct onode *gator_objects_lookup(onode_name)
268     char *onode_name;
269
270 { /*gator_objects_lookup*/
271
272     static char rn[] = "gator_objects_lookup";  /*Routine name*/
273
274     /*
275      * Life is very simple here - just call the dictionary routine.
276      */
277     if (objects_debug)
278       fprintf(stderr, "[%s:%s] Looking up gator object '%s'\n", mn, rn, onode_name);
279     return(gator_objdict_lookup(onode_name));
280
281 } /*gator_objects_lookup*/