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