4feaf1a330576f28ddb79df8404238a4e8833c68
[openafs.git] / src / gtx / lightobject.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 light object.
13  *
14  *------------------------------------------------------------------------*/
15
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19 RCSID
20     ("$Header$");
21
22 #include "gtxlightobj.h"        /*Interface for this module */
23 #include <stdio.h>              /*Standard I/O stuff */
24 #include <errno.h>
25
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #else
29 #ifdef HAVE_STRINGS_H
30 #include <strings.h>
31 #endif
32 #endif
33 #include <stdlib.h>
34
35
36 /*Externally-advertised array of light onode operations*/
37 struct onodeops gator_light_ops = {
38     gator_light_destroy,
39     gator_light_display,
40     gator_light_release
41 };
42
43 static char mn[] = "gator_lightobject"; /*Module name */
44
45 /*------------------------------------------------------------------------
46  * gator_light_create
47  *
48  * Description:
49  *      Create a gator light object.
50  *
51  * Arguments:
52  *      struct onode *light_onp : Ptr to the light onode to fill out.
53  *      struct onode_createparams *params : Generic ptr to creation
54  *          parameters.
55  *
56  * Returns:
57  *      Zero if successful,
58  *      Error value otherwise.
59  *
60  * Environment:
61  *      The base onode fields have already been set.  Lights are turned
62  *      off at creation.
63  *
64  * Side Effects:
65  *      Creates and initializes the light private data area, including
66  *      a window string-drawing parameter structure.  These areas are
67  *      garbage-collected upon failure.
68  *------------------------------------------------------------------------*/
69
70 int
71 gator_light_create(light_onp, params)
72      struct onode *light_onp;
73      struct onode_createparams *params;
74
75 {                               /*gator_light_create */
76
77     static char rn[] = "gator_light_create";    /*Routine name */
78     struct gator_light_crparams *light_params;  /*My specific creation params */
79     struct gator_lightobj *light_data;  /*Ptr to private data */
80     struct gwin_strparams *light_strparams;     /*Light label params */
81
82     light_params = (struct gator_light_crparams *)params;
83     if (objects_debug) {
84         fprintf(stderr, "[%s:%s] Private data passed to light object:\n", mn,
85                 rn);
86         fprintf(stderr,
87                 "\tAppearance: %d, flashfreq: %d, label at offset (%d, %d): '%s'\n",
88                 light_params->appearance, light_params->flashfreq,
89                 light_params->label_x, light_params->label_y,
90                 light_params->label);
91     }
92
93     /*
94      * Allocate the private data area, including the lower-level
95      * structure, then fill it in.
96      */
97     light_data =
98         (struct gator_lightobj *)malloc(sizeof(struct gator_lightobj));
99     if (light_data == (struct gator_lightobj *)0) {
100         fprintf(stderr,
101                 "[%s:%s] Can't allocate %d bytes for light object private data region, errno is %d\n",
102                 mn, rn, sizeof(struct gator_lightobj), errno);
103         return (errno);
104     }
105
106     light_strparams =
107         (struct gwin_strparams *)malloc(sizeof(struct gwin_strparams));
108     if (light_strparams == (struct gwin_strparams *)0) {
109         fprintf(stderr,
110                 "[%s:%s] Can't allocate %d bytes for light object label in private data region, errno is %d\n",
111                 mn, rn, sizeof(struct gwin_strparams), errno);
112         free(light_data);
113         return (errno);
114     }
115
116     /*
117      * Now that we have the private structures allocated, set them up.
118      */
119     light_data->setting = 0;
120     light_data->appearance = light_params->appearance;
121     light_data->flashfreq = light_params->flashfreq;
122     light_data->lasttoggletime = 0;
123     strcpy(light_data->label, light_params->label);
124
125     light_strparams->x = light_onp->o_x + light_params->label_x;
126     light_strparams->y = light_onp->o_y + light_params->label_y;
127     light_strparams->s = light_data->label;
128     light_strparams->highlight = 0;
129     light_data->llrock = (int *)light_strparams;
130
131     /*
132      * Attach the private data to the onode, then return the happy news.
133      */
134     light_onp->o_data = (int *)light_data;
135     return (0);
136
137 }                               /*gator_light_create */
138
139 /*------------------------------------------------------------------------
140  * gator_light_destroy
141  *
142  * Description:
143  *      Destroy a gator light object.
144  *
145  * Arguments:
146  *      struct onode *onp : Ptr to the light onode to delete.
147  *
148  * Returns:
149  *      0: Success
150  *      Error value otherwise.
151  *
152  * Environment:
153  *      Nothing interesting.
154  *
155  * Side Effects:
156  *      As advertised.
157  *------------------------------------------------------------------------*/
158
159 int
160 gator_light_destroy(onp)
161      struct onode *onp;
162
163 {                               /*gator_light_destroy */
164
165     /*
166      * For now, this is a no-op.
167      */
168     return (0);
169
170 }                               /*gator_light_destroy */
171
172 /*------------------------------------------------------------------------
173  * gator_light_display
174  *
175  * Description:
176  *      Display/redraw a gator light object.
177  *
178  * Arguments:
179  *      struct onode *onp: Ptr to the light onode to display.
180  *
181  * Returns:
182  *      0: Success
183  *      Error value otherwise.
184  *
185  * Environment:
186  *      Light objects have a pointer to string-drawing params in the
187  *      lower-level rock, with the proper highlighting set according
188  *      to whether the light is on or off, so we just have to draw
189  *      that string to get the proper effect.
190  *
191  * Side Effects:
192  *      As advertised.
193  *------------------------------------------------------------------------*/
194
195 int
196 gator_light_display(onp)
197      struct onode *onp;
198
199 {                               /*gator_light_display */
200
201     static char rn[] = "gator_light_display";   /*Routine name */
202     struct gator_lightobj *light_data;  /*Ptr to light obj data */
203     struct gwin_strparams *label_strparams;     /*String-drawing params */
204
205     /*
206      * Draw the label, with proper highlighting depending on whether
207      * the light is on.
208      */
209     light_data = (struct gator_lightobj *)(onp->o_data);
210     label_strparams = (struct gwin_strparams *)(light_data->llrock);
211     if (objects_debug)
212         fprintf(stderr, "[%s:%s] Printing out light label '%s' at (%d, %d)\n",
213                 mn, rn, label_strparams->s, label_strparams->x,
214                 label_strparams->y);
215     WOP_DRAWSTRING(onp->o_window, label_strparams);
216     return (0);
217
218 }                               /*gator_light_display */
219
220 /*------------------------------------------------------------------------
221  * gator_light_release
222  *
223  * Description:
224  *      Drop the refcount on a gator light object.
225  *
226  * Arguments:
227  *      struct onode *onp : Ptr to the onode whose refcount is
228  *                                   to be dropped.
229  *
230  * Returns:
231  *      0: Success
232  *      Error value otherwise.
233  *
234  * Environment:
235  *      Nothing interesting.
236  *
237  * Side Effects:
238  *      As advertised.
239  *------------------------------------------------------------------------*/
240
241 int
242 gator_light_release(onp)
243      struct onode *onp;
244
245 {                               /*gator_light_release */
246
247     /*
248      * For now, this is a no-op.
249      */
250     return (0);
251
252 }                               /*gator_light_release */
253
254 /*------------------------------------------------------------------------
255  * gator_light_set
256  *
257  * Description:
258  *      Set the value of the given gator light object.
259  *
260  * Arguments:
261  *        struct onode *onp : Ptr to the light onode to be set.
262  *        int setting       : Non-zero for ``on'', zero for ``off''.
263  *
264  * Returns:
265  *      0: Success
266  *      Error value otherwise.
267  *
268  * Environment:
269  *      We need to set not only the setting field, but the lower-
270  *      level structure stored in the rock must have its highlight
271  *      field set correctly.
272  *
273  * Side Effects:
274  *      Does NOT redisplay the light object.
275  *------------------------------------------------------------------------*/
276
277 int
278 gator_light_set(onp, setting)
279      struct onode *onp;
280      int setting;
281
282 {                               /*gator_light_set */
283
284     static char rn[] = "gator_light_set";       /*Routine name */
285     struct gator_lightobj *light_data;  /*Ptr to light obj data */
286     struct gwin_strparams *label_strparams;     /*String-drawing params */
287
288     /*
289      * Set the object correctly, then set the highlight field in
290      * the lower-level rock.
291      */
292     light_data = (struct gator_lightobj *)(onp->o_data);
293     label_strparams = (struct gwin_strparams *)(light_data->llrock);
294     if (objects_debug)
295         fprintf(stderr, "[%s:%s] Setting light object at 0x%x to %d (%s)", mn,
296                 rn, onp, setting, (setting ? "ON" : "OFF"));
297     light_data->setting = setting;
298     label_strparams->highlight = setting;
299
300     return (0);
301
302 }                               /*gator_light_set */