32
39
#include "battery.h"
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;
49
static timeout* battery_timeout;
43
51
static char buf_bat_percentage[10];
44
52
static char buf_bat_time[20];
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;
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;
62
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
54
67
void update_batterys(void* arg)
93
void default_battery()
96
percentage_hide = 101;
97
battery_low_cmd_send = 0;
103
path_energy_full = 0;
104
path_current_now = 0;
106
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
111
void cleanup_battery()
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);
121
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
122
if ((apm_fd != -1) && (close(apm_fd) == -1))
123
warn("cannot close /dev/apm");
81
128
void init_battery()
130
if (!battery_enabled) return;
132
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
133
apm_fd = open("/dev/apm", O_RDONLY);
135
warn("init_battery: failed to open /dev/apm.");
84
142
GDir *directory = 0;
85
143
GError *error = NULL;
86
144
const char *entryname;
87
145
char *battery_dir = 0;
89
if (!battery_enabled) return;
91
147
directory = g_dir_open("/sys/class/power_supply", 0, &error);
93
149
g_error_free(error);
152
209
g_free(battery_dir);
154
212
if (battery_enabled && battery_timeout==0)
155
213
battery_timeout = add_timeout(10, 10000, update_batterys, 0);
159
void cleanup_battery()
163
pango_font_description_free(bat1_font_desc);
165
pango_font_description_free(bat2_font_desc);
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);
175
g_free(battery_low_cmd);
177
battery_low_cmd = path_energy_now = path_energy_full = path_current_now = path_status = 0;
178
bat1_font_desc = bat2_font_desc = 0;
182
217
void init_battery_panel(void *p)
184
219
Panel *panel = (Panel*)p;
225
260
void update_battery() {
261
#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
262
// unused on OpenBSD, silence compiler warnings
228
int64_t energy_now = 0, energy_full = 0, current_now = 0;
265
int64_t current_now = 0;
267
int64_t energy_now = 0, energy_full = 0;
230
269
int8_t new_percentage = 0;
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");
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;
283
battery_state.state = BATTERY_DISCHARGING;
287
if (info.battery_life == 100)
288
battery_state.state = BATTERY_FULL;
290
// no mapping for openbsd really
294
if (info.minutes_left != -1)
295
seconds = info.minutes_left * 60;
299
new_percentage = info.battery_life;
232
302
fp = fopen(path_status, "r");
234
304
if (fgets(tmp, sizeof tmp, fp)) {
282
353
new_percentage = (energy_now*100)/energy_full;
284
355
if(battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) {
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;
289
359
if(battery_low_status < new_percentage && battery_state.state == BATTERY_CHARGING && battery_low_cmd_send) {
290
360
battery_low_cmd_send = 0;