From 66f257b886a87f621db91ce9f5898db9153d3b25 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sun, 30 Oct 2011 16:47:50 +0000 Subject: [PATCH] E17: add OpenBSD support for the battery module patch by Jonathan Armani SVN revision: 64526 --- configure.ac | 5 + src/modules/battery/Makefile.am | 8 +- src/modules/battery/e_mod_main.c | 12 +- src/modules/battery/e_mod_main.h | 19 ++- src/modules/battery/e_mod_openbsd.c | 180 ++++++++++++++++++++++++++++ 5 files changed, 215 insertions(+), 9 deletions(-) create mode 100644 src/modules/battery/e_mod_openbsd.c diff --git a/configure.ac b/configure.ac index db6a7600f..4775f149b 100644 --- a/configure.ac +++ b/configure.ac @@ -579,14 +579,19 @@ define([CHECK_MODULE_BATTERY], AC_E_CHECK_PKG(BATTERY, [ ecore >= 1.0.999 ecore-file >= 1.0.999 ecore-con >= 1.0.999 eina >= 1.0.999 ], [], [BATTERY=false]) ]) +have_openbsd="no" BATTERY_LDFLAGS= case "$host_os" in darwin*) BATTERY_LDFLAGS="-framework Foundation -framework IOKit" ;; + openbsd*) + have_openbsd="yes" + ;; esac AC_SUBST(BATTERY_LDFLAGS) +AM_CONDITIONAL([HAVE_OPENBSD], [test "x${have_openbsd}" = "xyes"]) AM_CONDITIONAL(HAVE_TEMPERATURE, false) define([CHECK_MODULE_TEMPERATURE], diff --git a/src/modules/battery/Makefile.am b/src/modules/battery/Makefile.am index afb87b841..d2789f282 100644 --- a/src/modules/battery/Makefile.am +++ b/src/modules/battery/Makefile.am @@ -2,9 +2,13 @@ MAINTAINERCLEANFILES = Makefile.in MODULE = battery if HAVE_EEZE - DEVICE_FILE = e_mod_udev.c +DEVICE_FILE = e_mod_udev.c else - DEVICE_FILE = e_mod_dbus.c +if HAVE_OPENBSD +DEVICE_FILE = e_mod_openbsd.c +else +DEVICE_FILE = e_mod_dbus.c +endif endif # data files for the module diff --git a/src/modules/battery/e_mod_main.c b/src/modules/battery/e_mod_main.c index 91faadc08..40c51cdd5 100644 --- a/src/modules/battery/e_mod_main.c +++ b/src/modules/battery/e_mod_main.c @@ -82,7 +82,7 @@ _gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style) #ifdef HAVE_EEZE eeze_init(); -#else +#elif !defined __OpenBSD__ e_dbus_init(); e_hal_init(); #endif @@ -103,7 +103,7 @@ _gc_shutdown(E_Gadcon_Client *gcc) #ifdef HAVE_EEZE eeze_shutdown(); -#else +#elif !defined __OpenBSD__ e_hal_shutdown(); e_dbus_shutdown(); #endif @@ -364,6 +364,8 @@ _battery_config_updated(void) { #ifdef HAVE_EEZE ok = _battery_udev_start(); +#elif defined __OpenBSD__ + ok = _battery_openbsd_start(); #else ok = _battery_dbus_start(); #endif @@ -674,7 +676,7 @@ e_modapi_init(E_Module *m) E_CONFIG_VAL(D, T, alert_timeout, INT); E_CONFIG_VAL(D, T, suspend_below, INT); E_CONFIG_VAL(D, T, force_mode, INT); -#ifdef HAVE_EEZE +#if defined HAVE_EEZE || defined __OpenBSD__ E_CONFIG_VAL(D, T, fuzzy, INT); #endif @@ -688,7 +690,7 @@ e_modapi_init(E_Module *m) battery_config->alert_timeout = 0; battery_config->suspend_below = 0; battery_config->force_mode = 0; -#ifdef HAVE_EEZE +#if defined HAVE_EEZE || defined __OpenBSD__ battery_config->fuzzy = 0; #endif } @@ -763,6 +765,8 @@ e_modapi_shutdown(E_Module *m __UNUSED__) #ifdef HAVE_EEZE _battery_udev_stop(); +#elif defined __OpenBSD__ + _battery_openbsd_stop(); #else _battery_dbus_stop(); #endif diff --git a/src/modules/battery/e_mod_main.h b/src/modules/battery/e_mod_main.h index a3a51b5dd..754facda8 100644 --- a/src/modules/battery/e_mod_main.h +++ b/src/modules/battery/e_mod_main.h @@ -47,6 +47,8 @@ struct _Config #ifdef HAVE_EEZE Eeze_Udev_Watch *acwatch; Eeze_Udev_Watch *batwatch; +#endif +#if defined HAVE_EEZE || defined __OpenBSD__ Eina_Bool fuzzy; int fuzzcount; #else @@ -67,7 +69,7 @@ typedef struct _Ac_Adapter Ac_Adapter; struct _Battery { const char *udi; -#ifdef HAVE_EEZE +#if defined HAVE_EEZE || defined __OpenBSD__ Ecore_Poller *poll; #else E_DBus_Signal_Handler *prop_change; @@ -75,7 +77,7 @@ struct _Battery #endif Eina_Bool present:1; Eina_Bool charging:1; -#ifdef HAVE_EEZE +#if defined HAVE_EEZE || defined __OpenBSD__ double last_update; double percent; double current_charge; @@ -99,6 +101,9 @@ struct _Battery const char *model; const char *vendor; Eina_Bool got_prop:1; +#ifdef __OpenBSD__ + int * mib; +#endif }; struct _Ac_Adapter @@ -109,6 +114,9 @@ struct _Ac_Adapter #endif Eina_Bool present:1; const char *product; +#ifdef __OpenBSD__ + int * mib; +#endif }; Battery *_battery_battery_find(const char *udi); @@ -119,11 +127,16 @@ void _battery_device_update(void); int _battery_udev_start(void); void _battery_udev_stop(void); /* end e_mod_udev.c */ -#else +#elif !defined __OpenBSD__ /* in e_mod_dbus.c */ int _battery_dbus_start(void); void _battery_dbus_stop(void); /* end e_mod_dbus.c */ +#else +/* in e_mod_openbsd.c */ +int _battery_openbsd_start(void); +void _battery_openbsd_stop(void); +/* end e_mod_openbsd.c */ #endif EAPI extern E_Module_Api e_modapi; diff --git a/src/modules/battery/e_mod_openbsd.c b/src/modules/battery/e_mod_openbsd.c new file mode 100644 index 000000000..5c4eae375 --- /dev/null +++ b/src/modules/battery/e_mod_openbsd.c @@ -0,0 +1,180 @@ + +#include + +#include +#include +#include + +#include "e.h" +#include "e_mod_main.h" + +static Eina_Bool _battery_openbsd_battery_update_poll(void *data); +static void _battery_openbsd_battery_update(); + +extern Eina_List *device_batteries; +extern Eina_List *device_ac_adapters; +extern double init_time; + +Ac_Adapter *ac; +Battery *bat; + +int +_battery_openbsd_start(void) +{ + Eina_List *devices; + int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0}; + int devn; + struct sensordev snsrdev; + size_t sdlen = sizeof(struct sensordev); + struct sensor s; + size_t slen = sizeof(struct sensor); + + for (devn = 0 ; ; devn++) { + mib[2] = devn; + if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1) { + if (errno == ENXIO) + continue; + if (errno == ENOENT) + break; + } + + if (!strcmp("acpibat0", snsrdev.xname)) { + if (!(bat = E_NEW(Battery, 1))) + return 0; + bat->udi = eina_stringshare_add("acpibat0"), + bat->mib = malloc(sizeof(int) * 5); + bat->mib[0] = mib[0]; + bat->mib[1] = mib[1]; + bat->mib[2] = mib[2]; + bat->technology = eina_stringshare_add("Unknow"); + bat->model = eina_stringshare_add("Unknow"); + bat->vendor = eina_stringshare_add("Unknow"); + bat->last_update = ecore_time_get(); + bat->poll = ecore_poller_add(ECORE_POLLER_CORE, + battery_config->poll_interval, + _battery_openbsd_battery_update_poll, NULL); + device_batteries = eina_list_append(device_batteries, bat); + } + else if (!strcmp("acpiac0", snsrdev.xname)) { + if (!(ac = E_NEW(Ac_Adapter, 1))) + return 0; + ac->udi = eina_stringshare_add("acpiac0"); + ac->mib = malloc(sizeof(int) * 5); + ac->mib[0] = mib[0]; + ac->mib[1] = mib[1]; + ac->mib[2] = mib[2]; + device_ac_adapters = eina_list_append(device_ac_adapters, ac); + } + } + + _battery_openbsd_battery_update(); + + init_time = ecore_time_get(); + return 1; +} + +void +_battery_openbsd_stop(void) +{ + if (ac) + free(ac); + if (bat) { + eina_stringshare_del(bat->udi); + eina_stringshare_del(bat->technology); + eina_stringshare_del(bat->model); + eina_stringshare_del(bat->vendor); + ecore_poller_del(bat->poll); + free(bat->mib); + free(bat); + } +} + +static Eina_Bool +_battery_openbsd_battery_update_poll(void *data) +{ + _battery_openbsd_battery_update(); + return EINA_TRUE; +} + +static void +_battery_openbsd_battery_update() +{ + double time, charge; + struct sensor s; + size_t slen = sizeof(struct sensor); + + /* update the poller interval */ + ecore_poller_poller_interval_set(bat->poll, + battery_config->poll_interval); + + /* last full capacity */ + bat->mib[3] = 8; + bat->mib[4] = 0; + if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1) { + bat->last_full_charge = (double)s.value; + } + + /* remaining capcity */ + bat->mib[3] = 8; + bat->mib[4] = 3; + if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) != -1) { + charge = (double)s.value; + } + + time = ecore_time_get(); + if ((bat->got_prop) && (charge != bat->current_charge)) + bat->charge_rate = ((charge - bat->current_charge) / (time - bat->last_update)); + bat->last_update = time; + bat->current_charge = charge; + bat->percent = 100 * (bat->current_charge / bat->last_full_charge); + if (bat->got_prop) + { + if (bat->charge_rate > 0) + { + if (battery_config->fuzzy && (++battery_config->fuzzcount <= 10) && (bat->time_full > 0)) + bat->time_full = (((bat->last_full_charge - bat->current_charge) / bat->charge_rate) + bat->time_full) / 2; + else + bat->time_full = (bat->last_full_charge - bat->current_charge) / bat->charge_rate; + bat->time_left = -1; + } + else + { + if (battery_config->fuzzy && (battery_config->fuzzcount <= 10) && (bat->time_left > 0)) + bat->time_left = (((0 - bat->current_charge) / bat->charge_rate) + bat->time_left) / 2; + else + bat->time_left = (0 - bat->current_charge) / bat->charge_rate; + bat->time_full = -1; + } + } + else + { + bat->time_full = -1; + bat->time_left = -1; + } + + /* battery state 1: discharge, 2: charge */ + bat->mib[3] = 10; + bat->mib[4] = 0; + if (sysctl(bat->mib, 5, &s, &slen, NULL, 0) == -1) { + if (s.value == 2) + bat->charging = 1; + else + bat->charging = 0; + } + + /* AC State */ + ac->mib[3] = 9; + ac->mib[4] = 0; + if (sysctl(ac->mib, 5, &s, &slen, NULL, 0) == -1) { + if (s.value) + ac->present = 1; + else + ac->present = 0; + } + + + + if (bat->got_prop) + _battery_device_update(); + bat->got_prop = 1; +}