1
/* $Id: widget.c 996 2009-03-23 17:22:24Z volker $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/widget.c $
4
* generic widget handling
6
* Copyright (C) 2003, 2004 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
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)
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.
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.
28
* int widget_junk(void)
50
/* we use a static array of widgets and not realloc() */
51
#define MAX_WIDGETS 256
53
static WIDGET_CLASS *Classes = NULL;
54
static int nClasses = 0;
56
static WIDGET *Widgets = NULL;
57
static int nWidgets = 0;
59
static int widget_added = 0;
61
int widget_register(WIDGET_CLASS * widget)
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 */
69
error("internal error: register_widget(%s) after add_widget()", widget->name);
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);
81
Classes = realloc(Classes, nClasses * sizeof(WIDGET_CLASS));
82
Classes[nClasses - 1] = *widget;
87
void widget_unregister(void)
90
for (i = 0; i < nWidgets; i++) {
91
Widgets[i].class->quit(&(Widgets[i]));
93
free(Widgets[i].name);
103
int widget_color(const char *section, const char *name, const char *key, RGBA * C)
112
color = cfg_get(section, key, NULL);
117
if (*color == '\0') {
122
if (color2RGBA(color, C) < 0) {
123
error("widget '%s': ignoring illegal %s color '%s'", name, key, color);
131
int intersect(WIDGET * w1, WIDGET * w2)
133
int x1w1, y1w1, x2w1, y2w1; /* 1st rectangle */
134
int x1w2, y1w2, x2w2, y2w2; /* 2nd rectangle */
136
if (w1->x2 == NOCOORD || w1->y2 == NOCOORD || w2->x2 == NOCOORD || w2->y2 == NOCOORD) {
137
/* w1 or w2 is no display widget: no intersection */
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);
149
if (x1w2 < x2w1 && x2w2 > x1w1 && y1w2 < y2w1 && y2w2 > y1w1) {
150
/* true: Intersection */
157
int widget_add(const char *name, const int type, const int layer, const int row, const int col)
162
int fg_valid, bg_valid;
169
/* prepare config section */
170
/* strlen("Widget:")=7 */
171
section = malloc(strlen(name) + 8);
172
strcpy(section, "Widget:");
173
strcat(section, name);
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);
185
/* get widget foreground color */
186
fg_valid = widget_color(section, name, "foreground", &FG);
187
bg_valid = widget_color(section, name, "background", &BG);
191
/* lookup widget class */
193
for (i = 0; i < nClasses; i++) {
194
if (strcasecmp(class, Classes[i].name) == 0) {
195
Class = &(Classes[i]);
200
error("widget '%s': class '%s' not supported", name, class);
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) {
211
error(" Widgetclass %s is placed by Row/Column", class);
214
error(" Widgetclass %s is placed by X/Y", class);
216
case WIDGET_TYPE_GPO:
217
case WIDGET_TYPE_TIMER:
218
case WIDGET_TYPE_KEYPAD:
220
error(" Widgetclass %s has unknown type %d", class, Class->type);
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));
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);
246
/* look up parent widget (widget with the same name) */
248
for (i = 0; i < nWidgets; i++) {
249
if (strcmp(name, Widgets[i].name) == 0) {
250
Parent = &(Widgets[i]);
255
Widget = &(Widgets[nWidgets]);
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;
269
if (Class->init != NULL) {
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);
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);
292
/* return the found widget, or else NULL */
293
WIDGET *widget_find(int type, void *needle)
295
WIDGET *widget = NULL;
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)