~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to widget_bar.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_bar.c 1106 2010-02-07 14:03:46Z mzuther $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/widget_bar.c $
 
3
 *
 
4
 * bar 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
 * WIDGET_CLASS Widget_Bar
 
29
 *   the bar widget
 
30
 *
 
31
 */
 
32
 
 
33
 
 
34
#include "config.h"
 
35
 
 
36
#include <stdlib.h>
 
37
#include <stdio.h>
 
38
#include <string.h>
 
39
#include <ctype.h>
 
40
 
 
41
#include "debug.h"
 
42
#include "cfg.h"
 
43
#include "property.h"
 
44
#include "timer_group.h"
 
45
#include "widget.h"
 
46
#include "widget_bar.h"
 
47
 
 
48
#ifdef WITH_DMALLOC
 
49
#include <dmalloc.h>
 
50
#endif
 
51
 
 
52
 
 
53
void widget_bar_update(void *Self)
 
54
{
 
55
    WIDGET *W = (WIDGET *) Self;
 
56
    WIDGET_BAR *Bar = W->data;
 
57
 
 
58
    double val1, val2;
 
59
    double min, max;
 
60
 
 
61
    /* evaluate properties */
 
62
    property_eval(&Bar->expression1);
 
63
    val1 = P2N(&Bar->expression1);
 
64
 
 
65
    if (property_valid(&Bar->expression2)) {
 
66
        property_eval(&Bar->expression2);
 
67
        val2 = P2N(&Bar->expression2);
 
68
    } else {
 
69
        val2 = val1;
 
70
    }
 
71
 
 
72
    /* minimum: if expression is empty, do auto-scaling */
 
73
    if (property_valid(&Bar->expr_min)) {
 
74
        property_eval(&Bar->expr_min);
 
75
        min = P2N(&Bar->expr_min);
 
76
    } else {
 
77
        min = Bar->min;
 
78
        if (val1 < min) {
 
79
            min = val1;
 
80
        }
 
81
        if (val2 < min) {
 
82
            min = val2;
 
83
        }
 
84
    }
 
85
 
 
86
    /* maximum: if expression is empty, do auto-scaling */
 
87
    if (property_valid(&Bar->expr_max)) {
 
88
        property_eval(&Bar->expr_max);
 
89
        max = P2N(&Bar->expr_max);
 
90
    } else {
 
91
        max = Bar->max;
 
92
        if (val1 > max) {
 
93
            max = val1;
 
94
        }
 
95
        if (val2 > max) {
 
96
            max = val2;
 
97
        }
 
98
    }
 
99
 
 
100
    /* debugging */
 
101
    if (Bar->min != min || Bar->max != max) {
 
102
        debug("Bar '%s': new scale %G - %G", W->name, min, max);
 
103
    }
 
104
 
 
105
    /* calculate bar values */
 
106
    Bar->min = min;
 
107
    Bar->max = max;
 
108
    if (max > min) {
 
109
        Bar->val1 = (val1 - min) / (max - min);
 
110
        Bar->val2 = (val2 - min) / (max - min);
 
111
    } else {
 
112
        Bar->val1 = 0.0;
 
113
        Bar->val2 = 0.0;
 
114
    }
 
115
 
 
116
    /* finally, draw it! */
 
117
    if (W->class->draw)
 
118
        W->class->draw(W);
 
119
 
 
120
}
 
121
 
 
122
 
 
123
int widget_bar_init(WIDGET * Self)
 
124
{
 
125
    char *section;
 
126
    char *c;
 
127
    WIDGET_BAR *Bar;
 
128
 
 
129
    /* prepare config section */
 
130
    /* strlen("Widget:")=7 */
 
131
    section = malloc(strlen(Self->name) + 8);
 
132
    strcpy(section, "Widget:");
 
133
    strcat(section, Self->name);
 
134
 
 
135
    Bar = malloc(sizeof(WIDGET_BAR));
 
136
    memset(Bar, 0, sizeof(WIDGET_BAR));
 
137
 
 
138
    /* load properties */
 
139
    property_load(section, "expression", NULL, &Bar->expression1);
 
140
    property_load(section, "expression2", NULL, &Bar->expression2);
 
141
    property_load(section, "min", NULL, &Bar->expr_min);
 
142
    property_load(section, "max", NULL, &Bar->expr_max);
 
143
 
 
144
    /* sanity checks */
 
145
    if (!property_valid(&Bar->expression1)) {
 
146
        error("Warning: widget %s has no expression", section);
 
147
    }
 
148
 
 
149
    /* bar length, default 1 */
 
150
    cfg_number(section, "length", 1, 0, -1, &(Bar->length));
 
151
 
 
152
    /* direction: East (default), West, North, South */
 
153
    c = cfg_get(section, "direction", "E");
 
154
    switch (toupper(*c)) {
 
155
    case 'E':
 
156
        Bar->direction = DIR_EAST;
 
157
        Self->x2 = Self->col + Bar->length - 1;
 
158
        Self->y2 = Self->row;
 
159
        break;
 
160
    case 'W':
 
161
        Bar->direction = DIR_WEST;
 
162
        Self->x2 = Self->col + Bar->length - 1;
 
163
        Self->y2 = Self->row;
 
164
        break;
 
165
    case 'N':
 
166
        Bar->direction = DIR_NORTH;
 
167
        Self->x2 = Self->col;
 
168
        Self->y2 = Self->row + Bar->length - 1;
 
169
        break;
 
170
    case 'S':
 
171
        Bar->direction = DIR_SOUTH;
 
172
        Self->x2 = Self->col;
 
173
        Self->y2 = Self->row + Bar->length - 1;
 
174
        break;
 
175
    default:
 
176
        error("widget %s has unknown direction '%s'; known directions: 'E', 'W', 'N', 'S'; using 'E(ast)'", Self->name,
 
177
              c);
 
178
        Bar->direction = DIR_EAST;
 
179
    }
 
180
    free(c);
 
181
 
 
182
    /* style: none (default), hollow */
 
183
    c = cfg_get(section, "style", "0");
 
184
    switch (toupper(*c)) {
 
185
    case 'H':
 
186
        Bar->style = STYLE_HOLLOW;
 
187
        break;
 
188
    case '0':
 
189
        Bar->style = 0;
 
190
        break;
 
191
    default:
 
192
        error("widget %s has unknown style '%s'; known styles: '0' or 'H'; using '0'", Self->name, c);
 
193
        Bar->style = 0;
 
194
    }
 
195
    free(c);
 
196
 
 
197
    /* update interval (msec), default 1 sec */
 
198
    cfg_number(section, "update", 1000, 10, -1, &(Bar->update));
 
199
 
 
200
    /* get widget special colors */
 
201
    Bar->color_valid[0] = widget_color(section, Self->name, "barcolor0", &Bar->color[0]);
 
202
    Bar->color_valid[1] = widget_color(section, Self->name, "barcolor1", &Bar->color[1]);
 
203
 
 
204
    free(section);
 
205
    Self->data = Bar;
 
206
 
 
207
    timer_add_widget(widget_bar_update, Self, Bar->update, 0);
 
208
 
 
209
    return 0;
 
210
}
 
211
 
 
212
 
 
213
int widget_bar_quit(WIDGET * Self)
 
214
{
 
215
    if (Self) {
 
216
        if (Self->data) {
 
217
            WIDGET_BAR *Bar = Self->data;
 
218
            property_free(&Bar->expression1);
 
219
            property_free(&Bar->expression2);
 
220
            property_free(&Bar->expr_min);
 
221
            property_free(&Bar->expr_max);
 
222
            free(Self->data);
 
223
        }
 
224
        Self->data = NULL;
 
225
    }
 
226
 
 
227
 
 
228
    return 0;
 
229
 
 
230
}
 
231
 
 
232
 
 
233
 
 
234
WIDGET_CLASS Widget_Bar = {
 
235
    .name = "bar",
 
236
    .type = WIDGET_TYPE_RC,
 
237
    .init = widget_bar_init,
 
238
    .draw = NULL,
 
239
    .quit = widget_bar_quit,
 
240
};