X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=src%2Fbattery%2Fbattery.c;h=5480412ada524ba8d173fe85ef463e1532ba520f;hb=b60f1947e2397dc695dafda288b2989b4415ce02;hp=9124e1648ac148a0177d1a8ce7515253d7f857e1;hpb=0caca7524827e38c9d076f33dc66416d189184e8;p=chaz%2Ftint2 diff --git a/src/battery/battery.c b/src/battery/battery.c index 9124e16..5480412 100644 --- a/src/battery/battery.c +++ b/src/battery/battery.c @@ -24,6 +24,13 @@ #include #include +#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) +#include +#include +#include +#include +#endif + #include "window.h" #include "server.h" #include "area.h" @@ -33,23 +40,28 @@ #include "clock.h" #include "timer.h" -PangoFontDescription *bat1_font_desc=0; -PangoFontDescription *bat2_font_desc=0; +PangoFontDescription *bat1_font_desc; +PangoFontDescription *bat2_font_desc; struct batstate battery_state; int battery_enabled; int percentage_hide; -static timeout* battery_timeout=0; +static timeout* battery_timeout; static char buf_bat_percentage[10]; static char buf_bat_time[20]; int8_t battery_low_status; unsigned char battery_low_cmd_send; -char *battery_low_cmd=0; -char *path_energy_now=0; -char *path_energy_full=0; -char *path_current_now=0; -char *path_status=0; +char *battery_low_cmd; +char *path_energy_now; +char *path_energy_full; +char *path_current_now; +char *path_status; + +#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) +int apm_fd; +#endif + void update_batterys(void* arg) { @@ -77,17 +89,60 @@ void update_batterys(void* arg) } } +void default_battery() +{ + battery_enabled = 0; + percentage_hide = 101; + battery_low_cmd_send = 0; + battery_timeout = 0; + bat1_font_desc = 0; + bat2_font_desc = 0; + battery_low_cmd = 0; + path_energy_now = 0; + path_energy_full = 0; + path_current_now = 0; + path_status = 0; +#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) + apm_fd = -1; +#endif +} + +void cleanup_battery() +{ + if (bat1_font_desc) pango_font_description_free(bat1_font_desc); + if (bat2_font_desc) pango_font_description_free(bat2_font_desc); + if (path_energy_now) g_free(path_energy_now); + if (path_energy_full) g_free(path_energy_full); + if (path_current_now) g_free(path_current_now); + if (path_status) g_free(path_status); + if (battery_low_cmd) g_free(battery_low_cmd); + +#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) + if ((apm_fd != -1) && (close(apm_fd) == -1)) + warn("cannot close /dev/apm"); +#endif +} + void init_battery() { + if (!battery_enabled) return; + +#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) + apm_fd = open("/dev/apm", O_RDONLY); + if (apm_fd < 0) { + warn("init_battery: failed to open /dev/apm."); + battery_enabled = 0; + return; + } + +#else // check battery GDir *directory = 0; GError *error = NULL; const char *entryname; char *battery_dir = 0; - if (!battery_enabled) return; - directory = g_dir_open("/sys/class/power_supply", 0, &error); if (error) g_error_free(error); @@ -107,8 +162,8 @@ void init_battery() if (directory) g_dir_close(directory); if (!battery_dir) { - cleanup_battery(); fprintf(stderr, "ERROR: battery applet can't found power_supply\n"); + default_battery(); return; } @@ -140,6 +195,7 @@ void init_battery() fp4 = fopen(path_status, "r"); if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL) { cleanup_battery(); + default_battery(); fprintf(stderr, "ERROR: battery applet can't open energy_now\n"); } fclose(fp1); @@ -150,49 +206,13 @@ void init_battery() g_free(path1); g_free(battery_dir); +#endif if (battery_enabled && battery_timeout==0) battery_timeout = add_timeout(10, 10000, update_batterys, 0); } -void cleanup_battery() -{ - battery_enabled = 0; - percentage_hide = 101; - battery_low_cmd_send = 0; - - if (bat1_font_desc) { - pango_font_description_free(bat1_font_desc); - bat1_font_desc = 0; - } - if (bat2_font_desc) { - pango_font_description_free(bat2_font_desc); - bat2_font_desc = 0; - } - if (path_energy_now) { - g_free(path_energy_now); - path_energy_now = 0; - } - if (path_energy_full) { - g_free(path_energy_full); - path_energy_full = 0; - } - if (path_current_now) { - g_free(path_current_now); - path_current_now = 0; - } - if (path_status) { - g_free(path_status); - path_status = 0; - } - if (battery_low_cmd) { - g_free(battery_low_cmd); - battery_low_cmd = 0; - } -} - - void init_battery_panel(void *p) { Panel *panel = (Panel*)p; @@ -237,12 +257,47 @@ void init_battery_panel(void *p) void update_battery() { +#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__) + // unused on OpenBSD, silence compiler warnings FILE *fp; char tmp[25]; - int64_t energy_now = 0, energy_full = 0, current_now = 0; + int64_t current_now = 0; +#endif + int64_t energy_now = 0, energy_full = 0; int seconds = 0; int8_t new_percentage = 0; +#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) + struct apm_power_info info; + if (ioctl(apm_fd, APM_IOC_GETPOWER, &(info)) < 0) + warn("power update: APM_IOC_GETPOWER"); + + // best attempt at mapping to linux battery states + battery_state.state = BATTERY_UNKNOWN; + switch (info.battery_state) { + case APM_BATT_CHARGING: + battery_state.state = BATTERY_CHARGING; + break; + default: + battery_state.state = BATTERY_DISCHARGING; + break; + } + + if (info.battery_life == 100) + battery_state.state = BATTERY_FULL; + + // no mapping for openbsd really + energy_full = 0; + energy_now = 0; + + if (info.minutes_left != -1) + seconds = info.minutes_left * 60; + else + seconds = -1; + + new_percentage = info.battery_life; + +#else fp = fopen(path_status, "r"); if(fp != NULL) { if (fgets(tmp, sizeof tmp, fp)) { @@ -285,6 +340,7 @@ void update_battery() { break; } } else seconds = 0; +#endif battery_state.time.hours = seconds / 3600; seconds -= 3600 * battery_state.time.hours;