~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to widget.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: widget.c 996 2009-03-23 17:22:24Z volker $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/widget.c $
 
3
 *
 
4
 * generic widget handling
 
5
 *
 
6
 * Copyright (C) 2003, 2004 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2, or (at your option)
 
12
 * any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
22
 *
 
23
 */
 
24
 
 
25
/*
 
26
 * exported functions:
 
27
 *
 
28
 * int widget_junk(void)
 
29
 *   does something
 
30
 *
 
31
 */
 
32
 
 
33
 
 
34
#include "config.h"
 
35
 
 
36
#include <stdlib.h>
 
37
#include <stdio.h>
 
38
#include <string.h>
 
39
#include <errno.h>
 
40
 
 
41
#include "debug.h"
 
42
#include "cfg.h"
 
43
#include "widget.h"
 
44
 
 
45
#ifdef WITH_DMALLOC
 
46
#include <dmalloc.h>
 
47
#endif
 
48
 
 
49
 
 
50
/* we use a static array of widgets and not realloc() */
 
51
#define MAX_WIDGETS 256
 
52
 
 
53
static WIDGET_CLASS *Classes = NULL;
 
54
static int nClasses = 0;
 
55
 
 
56
static WIDGET *Widgets = NULL;
 
57
static int nWidgets = 0;
 
58
 
 
59
static int widget_added = 0;
 
60
 
 
61
int widget_register(WIDGET_CLASS * widget)
 
62
{
 
63
    int i;
 
64
 
 
65
    /* sanity check: disallow widget registering after at least one */
 
66
    /* widget has been added, because we use realloc here, and there may  */
 
67
    /* be pointers to the old memory area */
 
68
    if (widget_added) {
 
69
        error("internal error: register_widget(%s) after add_widget()", widget->name);
 
70
        return -1;
 
71
    }
 
72
 
 
73
    for (i = 0; i < nClasses; i++) {
 
74
        if (strcasecmp(widget->name, Classes[i].name) == 0) {
 
75
            error("internal error: widget '%s' already exists!", widget->name);
 
76
            return -1;
 
77
        }
 
78
    }
 
79
 
 
80
    nClasses++;
 
81
    Classes = realloc(Classes, nClasses * sizeof(WIDGET_CLASS));
 
82
    Classes[nClasses - 1] = *widget;
 
83
 
 
84
    return 0;
 
85
}
 
86
 
 
87
void widget_unregister(void)
 
88
{
 
89
    int i;
 
90
    for (i = 0; i < nWidgets; i++) {
 
91
        Widgets[i].class->quit(&(Widgets[i]));
 
92
        if (Widgets[i].name)
 
93
            free(Widgets[i].name);
 
94
    }
 
95
    free(Widgets);
 
96
 
 
97
    free(Classes);
 
98
 
 
99
    nWidgets = 0;
 
100
    nClasses = 0;
 
101
}
 
102
 
 
103
int widget_color(const char *section, const char *name, const char *key, RGBA * C)
 
104
{
 
105
    char *color;
 
106
 
 
107
    C->R = 0;
 
108
    C->G = 0;
 
109
    C->B = 0;
 
110
    C->A = 0;
 
111
 
 
112
    color = cfg_get(section, key, NULL);
 
113
 
 
114
    if (color == NULL)
 
115
        return 0;
 
116
 
 
117
    if (*color == '\0') {
 
118
        free(color);
 
119
        return 0;
 
120
    }
 
121
 
 
122
    if (color2RGBA(color, C) < 0) {
 
123
        error("widget '%s': ignoring illegal %s color '%s'", name, key, color);
 
124
        free(color);
 
125
        return 0;
 
126
    }
 
127
    free(color);
 
128
    return 1;
 
129
}
 
130
 
 
131
int intersect(WIDGET * w1, WIDGET * w2)
 
132
{
 
133
    int x1w1, y1w1, x2w1, y2w1; /* 1st rectangle */
 
134
    int x1w2, y1w2, x2w2, y2w2; /* 2nd rectangle */
 
135
 
 
136
    if (w1->x2 == NOCOORD || w1->y2 == NOCOORD || w2->x2 == NOCOORD || w2->y2 == NOCOORD) {
 
137
        /* w1 or w2 is no display widget: no intersection */
 
138
        return 0;
 
139
    }
 
140
    x1w1 = MIN(w1->col, w1->x2);
 
141
    x2w1 = MAX(w1->col, w1->x2);
 
142
    y1w1 = MIN(w1->row, w1->y2);
 
143
    y2w1 = MAX(w1->row, w1->y2);
 
144
    x1w2 = MIN(w2->col, w2->x2);
 
145
    x2w2 = MAX(w2->col, w2->x2);
 
146
    y1w2 = MIN(w2->row, w2->y2);
 
147
    y2w2 = MAX(w2->row, w2->y2);
 
148
 
 
149
    if (x1w2 < x2w1 && x2w2 > x1w1 && y1w2 < y2w1 && y2w2 > y1w1) {
 
150
        /* true: Intersection */
 
151
        return 1;
 
152
    } else {
 
153
        return 0;
 
154
    }
 
155
}
 
156
 
 
157
int widget_add(const char *name, const int type, const int layer, const int row, const int col)
 
158
{
 
159
    int i;
 
160
    char *section;
 
161
    char *class;
 
162
    int fg_valid, bg_valid;
 
163
    RGBA FG, BG;
 
164
 
 
165
    WIDGET_CLASS *Class;
 
166
    WIDGET *Widget;
 
167
    WIDGET *Parent;
 
168
 
 
169
    /* prepare config section */
 
170
    /* strlen("Widget:")=7 */
 
171
    section = malloc(strlen(name) + 8);
 
172
    strcpy(section, "Widget:");
 
173
    strcat(section, name);
 
174
 
 
175
    /* get widget class */
 
176
    class = cfg_get(section, "class", NULL);
 
177
    if (class == NULL || *class == '\0') {
 
178
        error("error: widget '%s' has no class!", name);
 
179
        if (class)
 
180
            free(class);
 
181
        free(section);
 
182
        return -1;
 
183
    }
 
184
 
 
185
    /* get widget foreground color */
 
186
    fg_valid = widget_color(section, name, "foreground", &FG);
 
187
    bg_valid = widget_color(section, name, "background", &BG);
 
188
 
 
189
    free(section);
 
190
 
 
191
    /* lookup widget class */
 
192
    Class = NULL;
 
193
    for (i = 0; i < nClasses; i++) {
 
194
        if (strcasecmp(class, Classes[i].name) == 0) {
 
195
            Class = &(Classes[i]);
 
196
            break;
 
197
        }
 
198
    }
 
199
    if (i == nClasses) {
 
200
        error("widget '%s': class '%s' not supported", name, class);
 
201
        if (class)
 
202
            free(class);
 
203
        return -1;
 
204
    }
 
205
 
 
206
    /* check if widget type matches */
 
207
    if (Class->type != type) {
 
208
        error("widget '%s': class '%s' not applicable", name, class);
 
209
        switch (Class->type) {
 
210
        case WIDGET_TYPE_RC:
 
211
            error("  Widgetclass %s is placed by Row/Column", class);
 
212
            break;
 
213
        case WIDGET_TYPE_XY:
 
214
            error("  Widgetclass %s is placed by X/Y", class);
 
215
            break;
 
216
        case WIDGET_TYPE_GPO:
 
217
        case WIDGET_TYPE_TIMER:
 
218
        case WIDGET_TYPE_KEYPAD:
 
219
        default:
 
220
            error("  Widgetclass %s has unknown type %d", class, Class->type);
 
221
        }
 
222
        free(class);
 
223
        return -1;
 
224
    }
 
225
 
 
226
    if (class)
 
227
        free(class);
 
228
 
 
229
 
 
230
    /* do NOT use realloc here because there may be pointers to the old */
 
231
    /* memory area, which would point to nowhere if realloc moves the area */
 
232
    if (Widgets == NULL) {
 
233
        Widgets = malloc(MAX_WIDGETS * sizeof(WIDGET));
 
234
        if (Widgets == NULL) {
 
235
            error("internal error: allocation of widget buffer failed: %s", strerror(errno));
 
236
            return -1;
 
237
        }
 
238
    }
 
239
 
 
240
    /* another sanity check */
 
241
    if (nWidgets >= MAX_WIDGETS) {
 
242
        error("internal error: widget buffer full! Tried to allocate %d widgets (max: %d)", nWidgets, MAX_WIDGETS);
 
243
        return -1;
 
244
    }
 
245
 
 
246
    /* look up parent widget (widget with the same name) */
 
247
    Parent = NULL;
 
248
    for (i = 0; i < nWidgets; i++) {
 
249
        if (strcmp(name, Widgets[i].name) == 0) {
 
250
            Parent = &(Widgets[i]);
 
251
            break;
 
252
        }
 
253
    }
 
254
 
 
255
    Widget = &(Widgets[nWidgets]);
 
256
    nWidgets++;
 
257
 
 
258
    Widget->name = strdup(name);
 
259
    Widget->class = Class;
 
260
    Widget->parent = Parent;
 
261
    Widget->fg_color = FG;
 
262
    Widget->bg_color = BG;
 
263
    Widget->fg_valid = fg_valid;
 
264
    Widget->bg_valid = bg_valid;
 
265
    Widget->layer = layer;
 
266
    Widget->row = row;
 
267
    Widget->col = col;
 
268
 
 
269
    if (Class->init != NULL) {
 
270
        Class->init(Widget);
 
271
    }
 
272
 
 
273
    info(" widget '%s': Class '%s', Parent '%s', Layer %d, %s %d, %s %d (to %d,%d)",
 
274
         name, (NULL == Class) ? "<none>" : Class->name,
 
275
         (NULL == Parent) ? "<root>" : Parent->name,
 
276
         layer, (WIDGET_TYPE_XY == Class->type) ? "Y" : "Row", row, (WIDGET_TYPE_XY == Class->type) ? "X" : "Col", col,
 
277
         Widget->y2, Widget->x2);
 
278
 
 
279
    /* sanity check: look for overlapping widgets */
 
280
    for (i = 0; i < nWidgets - 1; i++) {
 
281
        if (Widgets[i].layer == layer) {
 
282
            if (intersect(&(Widgets[i]), Widget)) {
 
283
                info("WARNING widget %s(%i,%i) intersects with %s(%i,%i) on layer %d",
 
284
                     Widgets[i].name, Widgets[i].row, Widgets[i].col, name, row, col, layer);
 
285
            }
 
286
        }
 
287
    }
 
288
 
 
289
    return 0;
 
290
}
 
291
 
 
292
/* return the found widget, or else NULL */
 
293
WIDGET *widget_find(int type, void *needle)
 
294
{
 
295
    WIDGET *widget = NULL;
 
296
    int i;
 
297
 
 
298
    for (i = 0; i < nWidgets; i++) {
 
299
        widget = &(Widgets[i]);
 
300
        if (widget->class->type == type) {
 
301
            if (widget->class->find != NULL && widget->class->find(widget, needle) == 0)
 
302
                break;
 
303
        }
 
304
        widget = NULL;
 
305
    }
 
306
 
 
307
    return widget;
 
308
}