$OpenBSD$ --- src/battery/battery.c.orig Tue Jan 19 19:29:28 2010 +++ src/battery/battery.c Fri Apr 16 02:03:24 2010 @@ -24,6 +24,12 @@ #include #include +/* OpenBSD */ +#include +#include +#include +#include + #include "window.h" #include "server.h" #include "area.h" @@ -51,6 +57,9 @@ char *path_energy_full=0; char *path_current_now=0; char *path_status=0; +/* OpenBSD power */ +int apm_fd = NULL; + void update_batterys(void* arg) { int i; @@ -80,6 +89,7 @@ void update_batterys(void* arg) void init_battery() { +#if 0 // check battery GDir *directory = 0; GError *error = NULL; @@ -150,7 +160,16 @@ void init_battery() g_free(path1); g_free(battery_dir); +#endif + /* OpenBSD */ + apm_fd = open("/dev/apm", O_RDONLY, "r"); + if (apm_fd < 0) { + warn("init_battery: failed to open /dev/apm."); + battery_enabled = 0; + return; + } + if (battery_enabled && battery_timeout==0) battery_timeout = add_timeout(10, 10000, update_batterys, 0); } @@ -176,6 +195,10 @@ void cleanup_battery() battery_low_cmd = path_energy_now = path_energy_full = path_current_now = path_status = 0; bat1_font_desc = bat2_font_desc = 0; + + if (close(apm_fd) == -1) { + warn("cannot close /dev/apm"); + } } @@ -227,19 +250,40 @@ void update_battery() { char tmp[25]; int64_t energy_now = 0, energy_full = 0, current_now = 0; int seconds = 0; - int8_t new_percentage = 0; + unsigned int new_percentage = 0; - fp = fopen(path_status, "r"); + /* OpenBSD */ + struct apm_power_info info; + if (ioctl(apm_fd, APM_IOC_GETPOWER, &(info)) < 0) + warn("power update: APM_IOC_GETPOWER"); + +#if 0 + fopen(path_status, "r"); if(fp != NULL) { if (fgets(tmp, sizeof tmp, fp)) { - battery_state.state = BATTERY_UNKNOWN; - if(strcasecmp(tmp, "Charging\n")==0) battery_state.state = BATTERY_CHARGING; - if(strcasecmp(tmp, "Discharging\n")==0) battery_state.state = BATTERY_DISCHARGING; - if(strcasecmp(tmp, "Full\n")==0) battery_state.state = BATTERY_FULL; +#endif + + /* 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; + +#if 0 } fclose(fp); } +#endif +#if 0 fp = fopen(path_energy_now, "r"); if(fp != NULL) { if (fgets(tmp, sizeof tmp, fp)) energy_now = atoi(tmp); @@ -271,21 +315,36 @@ void update_battery() { break; } } else seconds = 0; +#endif + /* 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; + battery_state.time.hours = seconds / 3600; seconds -= 3600 * battery_state.time.hours; battery_state.time.minutes = seconds / 60; seconds -= 60 * battery_state.time.minutes; battery_state.time.seconds = seconds; +#if 0 if(energy_full > 0) new_percentage = (energy_now*100)/energy_full; +#endif if(battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) { if (battery_low_cmd) if (-1 != system(battery_low_cmd)) battery_low_cmd_send = 1; } + if(battery_low_status < new_percentage && battery_state.state == BATTERY_CHARGING && battery_low_cmd_send) { battery_low_cmd_send = 0; }