~cmpitg/tint2/trunk

« back to all changes in this revision

Viewing changes to src/battery/battery.c

  • Committer: Yang Ha Nguyen
  • Date: 2010-08-27 15:12:31 UTC
  • Revision ID: cmpitg@gmail.com-20100827151231-6ke4ej9wxx9pvqcx
Sync with Google Code

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <cairo-xlib.h>
25
25
#include <pango/pangocairo.h>
26
26
 
 
27
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
 
28
#include <machine/apmvar.h>
 
29
#include <err.h>
 
30
#include <sys/ioctl.h>
 
31
#include <unistd.h>
 
32
#endif
 
33
 
27
34
#include "window.h"
28
35
#include "server.h"
29
36
#include "area.h"
32
39
#include "battery.h"
33
40
#include "clock.h"
34
41
#include "timer.h"
 
42
#include "common.h"
35
43
 
36
 
PangoFontDescription *bat1_font_desc=0;
37
 
PangoFontDescription *bat2_font_desc=0;
 
44
PangoFontDescription *bat1_font_desc;
 
45
PangoFontDescription *bat2_font_desc;
38
46
struct batstate battery_state;
39
47
int battery_enabled;
40
 
int percentage_hide = 101;
41
 
static timeout* battery_timeout=0;
 
48
int percentage_hide;
 
49
static timeout* battery_timeout;
42
50
 
43
51
static char buf_bat_percentage[10];
44
52
static char buf_bat_time[20];
45
53
 
46
54
int8_t battery_low_status;
47
 
char *battery_low_cmd=0;
48
 
unsigned char battery_low_cmd_send=0;
49
 
char *path_energy_now=0;
50
 
char *path_energy_full=0;
51
 
char *path_current_now=0;
52
 
char *path_status=0;
 
55
unsigned char battery_low_cmd_send;
 
56
char *battery_low_cmd;
 
57
char *path_energy_now;
 
58
char *path_energy_full;
 
59
char *path_current_now;
 
60
char *path_status;
 
61
 
 
62
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
 
63
int apm_fd;
 
64
#endif
 
65
 
53
66
 
54
67
void update_batterys(void* arg)
55
68
{
77
90
        }
78
91
}
79
92
 
 
93
void default_battery()
 
94
{
 
95
        battery_enabled = 0;
 
96
        percentage_hide = 101;
 
97
        battery_low_cmd_send = 0;
 
98
        battery_timeout = 0;
 
99
        bat1_font_desc = 0;
 
100
        bat2_font_desc = 0;
 
101
        battery_low_cmd = 0;
 
102
        path_energy_now = 0;
 
103
        path_energy_full = 0;
 
104
        path_current_now = 0;
 
105
        path_status = 0;
 
106
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
 
107
        apm_fd = -1;
 
108
#endif
 
109
}
 
110
 
 
111
void cleanup_battery()
 
112
{
 
113
        if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
 
114
        if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
 
115
        if (path_energy_now) g_free(path_energy_now);
 
116
        if (path_energy_full) g_free(path_energy_full);
 
117
        if (path_current_now) g_free(path_current_now);
 
118
        if (path_status) g_free(path_status);
 
119
        if (battery_low_cmd) g_free(battery_low_cmd);
 
120
 
 
121
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
 
122
        if ((apm_fd != -1) && (close(apm_fd) == -1))
 
123
                warn("cannot close /dev/apm");
 
124
#endif
 
125
}
 
126
 
80
127
 
81
128
void init_battery()
82
129
{
 
130
        if (!battery_enabled) return;
 
131
 
 
132
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
 
133
        apm_fd = open("/dev/apm", O_RDONLY);
 
134
        if (apm_fd < 0) {
 
135
                warn("init_battery: failed to open /dev/apm.");
 
136
                battery_enabled = 0;
 
137
                return;
 
138
        }
 
139
 
 
140
#else
83
141
        // check battery
84
142
        GDir *directory = 0;
85
143
        GError *error = NULL;
86
144
        const char *entryname;
87
145
        char *battery_dir = 0;
88
146
 
89
 
        if (!battery_enabled) return;
90
 
 
91
147
        directory = g_dir_open("/sys/class/power_supply", 0, &error);
92
148
        if (error)
93
149
                g_error_free(error);
107
163
        if (directory)
108
164
                g_dir_close(directory);
109
165
        if (!battery_dir) {
110
 
                cleanup_battery();
111
166
                fprintf(stderr, "ERROR: battery applet can't found power_supply\n");
 
167
                default_battery();
112
168
                return;
113
169
        }
114
170
 
140
196
                fp4 = fopen(path_status, "r");
141
197
                if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL) {
142
198
                        cleanup_battery();
 
199
                        default_battery();
143
200
                        fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
144
201
                }
145
202
                fclose(fp1);
150
207
 
151
208
        g_free(path1);
152
209
        g_free(battery_dir);
 
210
#endif
153
211
 
154
212
        if (battery_enabled && battery_timeout==0)
155
213
                battery_timeout = add_timeout(10, 10000, update_batterys, 0);
156
214
}
157
215
 
158
216
 
159
 
void cleanup_battery()
160
 
{
161
 
        battery_enabled = 0;
162
 
        if (bat1_font_desc)
163
 
                pango_font_description_free(bat1_font_desc);
164
 
        if (bat2_font_desc)
165
 
                pango_font_description_free(bat2_font_desc);
166
 
        if (path_energy_now)
167
 
                g_free(path_energy_now);
168
 
        if (path_energy_full)
169
 
                g_free(path_energy_full);
170
 
        if (path_current_now)
171
 
                g_free(path_current_now);
172
 
        if (path_status)
173
 
                g_free(path_status);
174
 
        if (battery_low_cmd)
175
 
                g_free(battery_low_cmd);
176
 
 
177
 
        battery_low_cmd = path_energy_now = path_energy_full = path_current_now = path_status = 0;
178
 
        bat1_font_desc = bat2_font_desc = 0;
179
 
}
180
 
 
181
 
 
182
217
void init_battery_panel(void *p)
183
218
{
184
219
        Panel *panel = (Panel*)p;
223
258
 
224
259
 
225
260
void update_battery() {
 
261
#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
 
262
        // unused on OpenBSD, silence compiler warnings
226
263
        FILE *fp;
227
264
        char tmp[25];
228
 
        int64_t energy_now = 0, energy_full = 0, current_now = 0;
 
265
        int64_t current_now = 0;
 
266
#endif
 
267
        int64_t energy_now = 0, energy_full = 0;
229
268
        int seconds = 0;
230
269
        int8_t new_percentage = 0;
231
270
 
 
271
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
 
272
        struct apm_power_info info;
 
273
        if (ioctl(apm_fd, APM_IOC_GETPOWER, &(info)) < 0)
 
274
                warn("power update: APM_IOC_GETPOWER");
 
275
 
 
276
        // best attempt at mapping to linux battery states
 
277
        battery_state.state = BATTERY_UNKNOWN;
 
278
        switch (info.battery_state) {
 
279
                case APM_BATT_CHARGING:
 
280
                        battery_state.state = BATTERY_CHARGING;
 
281
                        break;
 
282
                default:
 
283
                        battery_state.state = BATTERY_DISCHARGING;
 
284
                        break;
 
285
        }
 
286
 
 
287
        if (info.battery_life == 100)
 
288
                battery_state.state = BATTERY_FULL;
 
289
 
 
290
        // no mapping for openbsd really
 
291
        energy_full = 0;
 
292
        energy_now = 0;
 
293
 
 
294
        if (info.minutes_left != -1)
 
295
                seconds = info.minutes_left * 60;
 
296
        else
 
297
                seconds = -1;
 
298
 
 
299
        new_percentage = info.battery_life;
 
300
 
 
301
#else
232
302
        fp = fopen(path_status, "r");
233
303
        if(fp != NULL) {
234
304
                if (fgets(tmp, sizeof tmp, fp)) {
271
341
                                break;
272
342
                }
273
343
        } else seconds = 0;
 
344
#endif
274
345
 
275
346
        battery_state.time.hours = seconds / 3600;
276
347
        seconds -= 3600 * battery_state.time.hours;
282
353
                new_percentage = (energy_now*100)/energy_full;
283
354
 
284
355
        if(battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) {
285
 
                if (battery_low_cmd)
286
 
                        if (-1 != system(battery_low_cmd))
287
 
                                battery_low_cmd_send = 1;
 
356
                tint_exec(battery_low_cmd);
 
357
                battery_low_cmd_send = 1;
288
358
        }
289
359
        if(battery_low_status < new_percentage && battery_state.state == BATTERY_CHARGING && battery_low_cmd_send) {
290
360
                battery_low_cmd_send = 0;