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