battery: FreeBSD multiple batteries.

model/vendor/tech support. Some indentation to make the BSD
support easier to read and distinguish. Pleas test.
This commit is contained in:
Alastair Poole 2021-03-07 16:23:50 +00:00
parent a4630c0679
commit 945697ba0a
3 changed files with 142 additions and 173 deletions

View File

@ -520,7 +520,7 @@ _battery_config_updated(void)
{ {
#ifdef HAVE_EEZE #ifdef HAVE_EEZE
ok = _battery_udev_start(); ok = _battery_udev_start();
#elif defined __OpenBSD__ || defined __DragonFly__ || defined __FreeBSD__ || defined __NetBSD__ #elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
ok = _battery_sysctl_start(); ok = _battery_sysctl_start();
#else #else
ok = _battery_upower_start(); ok = _battery_upower_start();
@ -892,7 +892,7 @@ e_modapi_init(E_Module *m)
E_CONFIG_VAL(D, T, alert_timeout, INT); E_CONFIG_VAL(D, T, alert_timeout, INT);
E_CONFIG_VAL(D, T, suspend_below, INT); E_CONFIG_VAL(D, T, suspend_below, INT);
E_CONFIG_VAL(D, T, force_mode, INT); E_CONFIG_VAL(D, T, force_mode, INT);
#if defined HAVE_EEZE || defined __OpenBSD__ || defined __NetBSD__ #if defined HAVE_EEZE || defined(__OpenBSD__)
E_CONFIG_VAL(D, T, fuzzy, INT); E_CONFIG_VAL(D, T, fuzzy, INT);
#endif #endif
E_CONFIG_VAL(D, T, desktop_notifications, INT); E_CONFIG_VAL(D, T, desktop_notifications, INT);
@ -907,7 +907,7 @@ e_modapi_init(E_Module *m)
battery_config->alert_timeout = 0; battery_config->alert_timeout = 0;
battery_config->suspend_below = 0; battery_config->suspend_below = 0;
battery_config->force_mode = 0; battery_config->force_mode = 0;
#if defined HAVE_EEZE || defined __OpenBSD__ || defined __NetBSD__ #if defined HAVE_EEZE || defined(__OpenBSD__)
battery_config->fuzzy = 0; battery_config->fuzzy = 0;
#endif #endif
battery_config->desktop_notifications = 0; battery_config->desktop_notifications = 0;
@ -980,7 +980,7 @@ e_modapi_shutdown(E_Module *m EINA_UNUSED)
#ifdef HAVE_EEZE #ifdef HAVE_EEZE
_battery_udev_stop(); _battery_udev_stop();
#elif defined (__OpenBSD__) || defined (__DragonFly__) || defined (__FreeBSD__) || defined (__NetBSD__) #elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
_battery_sysctl_stop(); _battery_sysctl_stop();
#else #else
_battery_upower_stop(); _battery_upower_stop();

View File

@ -7,8 +7,6 @@
# include <Eldbus.h> # include <Eldbus.h>
#endif #endif
typedef struct _Config Config;
#define CHECK_NONE 0 #define CHECK_NONE 0
#define CHECK_ACPI 1 #define CHECK_ACPI 1
#define CHECK_APM 2 #define CHECK_APM 2
@ -25,9 +23,10 @@ typedef struct _Config Config;
#define POPUP_DEBOUNCE_CYCLES 2 #define POPUP_DEBOUNCE_CYCLES 2
typedef struct _Config Config;
struct _Config struct _Config
{ {
/* saved * loaded config values */ /* saved / loaded config values */
int poll_interval; int poll_interval;
int alert; /* Alert on minutes remaining */ int alert; /* Alert on minutes remaining */
int alert_p; /* Alert on percentage remaining */ int alert_p; /* Alert on percentage remaining */
@ -68,10 +67,8 @@ struct _Battery
const char *udi; const char *udi;
#if defined HAVE_EEZE || defined __OpenBSD__ || defined __DragonFly__ || defined __FreeBSD__ || defined __NetBSD__ #if defined HAVE_EEZE || defined __OpenBSD__ || defined __DragonFly__ || defined __FreeBSD__ || defined __NetBSD__
Ecore_Poller *poll; Ecore_Poller *poll;
#endif
Eina_Bool present E_BITFIELD; Eina_Bool present E_BITFIELD;
Eina_Bool charging E_BITFIELD; Eina_Bool charging E_BITFIELD;
#if defined HAVE_EEZE || defined __OpenBSD__ || defined __DragonFly__ || defined __FreeBSD__ || defined __NetBSD__
double last_update; double last_update;
double percent; double percent;
double current_charge; double current_charge;
@ -96,14 +93,14 @@ struct _Battery
const char *vendor; const char *vendor;
Eina_Bool got_prop E_BITFIELD; Eina_Bool got_prop E_BITFIELD;
Eldbus_Proxy *proxy; Eldbus_Proxy *proxy;
int * mib; #if defined __FreeBSD__ || defined __DragonFly__
#if defined(__FreeBSD__) || defined(__DragonFly__) int unit;
int * mib_state;
int * mib_units;
int * mib_time;
int batteries; int batteries;
int time_min; int time_min;
#endif #endif
#if defined __OpenBSD__
int *mib;
#endif
}; };
struct _Ac_Adapter struct _Ac_Adapter
@ -112,12 +109,15 @@ struct _Ac_Adapter
Eina_Bool present E_BITFIELD; Eina_Bool present E_BITFIELD;
const char *product; const char *product;
Eldbus_Proxy *proxy; Eldbus_Proxy *proxy;
#if defined __OpenBSD__
int *mib; int *mib;
#endif
}; };
Battery *_battery_battery_find(const char *udi); Battery *_battery_battery_find(const char *udi);
Ac_Adapter *_battery_ac_adapter_find(const char *udi); Ac_Adapter *_battery_ac_adapter_find(const char *udi);
void _battery_device_update(void); void _battery_device_update(void);
#ifdef HAVE_EEZE #ifdef HAVE_EEZE
/* in e_mod_udev.c */ /* in e_mod_udev.c */
int _battery_udev_start(void); int _battery_udev_start(void);
@ -142,20 +142,7 @@ E_API int e_modapi_shutdown (E_Module *m);
E_API int e_modapi_save (E_Module *m); E_API int e_modapi_save (E_Module *m);
E_Config_Dialog *e_int_config_battery_module(Evas_Object *parent, const char *params EINA_UNUSED); E_Config_Dialog *e_int_config_battery_module(Evas_Object *parent, const char *params EINA_UNUSED);
void _battery_config_updated(void);
extern Config *battery_config; extern Config *battery_config;
void _battery_config_updated(void);
/**
* @addtogroup Optional_Gadgets
* @{
*
* @defgroup Module_Battery Battery
*
* Shows battery level and current status, may do actions given some
* thresholds.
*
* @}
*/
#endif #endif

View File

@ -1,12 +1,17 @@
#include "e.h" #include "e.h"
#include "e_mod_main.h" #include "e_mod_main.h"
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
# include <err.h> # include <err.h>
# include <sys/types.h> # include <sys/types.h>
# include <sys/sysctl.h> # include <sys/sysctl.h>
#if defined(__OpenBSD__) || defined(__NetBSD__) #if defined(__FreeBSD__) || defined(__DragonFly__)
# include <sys/ioctl.h>
# include <dev/acpica/acpiio.h>
#endif
#if defined(__OpenBSD__)
# include <sys/param.h> # include <sys/param.h>
# include <sys/sensors.h> # include <sys/sensors.h>
#endif #endif
@ -24,17 +29,14 @@ static Battery *bat = NULL;
int int
_battery_sysctl_start(void) _battery_sysctl_start(void)
{ {
# if defined(__OpenBSD__) || defined(__NetBSD__) # if defined(__OpenBSD__)
int mib[] = {CTL_HW, HW_SENSORS, 0, 0, 0}; int mib[] = {CTL_HW, HW_SENSORS, 0, 0, 0};
int devn; int devn;
struct sensordev snsrdev; struct sensordev snsrdev;
size_t sdlen = sizeof(struct sensordev); size_t sdlen = sizeof(struct sensordev);
# elif defined(__FreeBSD__) || defined(__DragonFly__)
size_t len;
# endif
# if defined(__OpenBSD__) || defined(__NetBSD__) for (devn = 0;; devn++)
for (devn = 0;; devn++) { {
mib[2] = devn; mib[2] = devn;
if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1) if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1)
{ {
@ -76,53 +78,48 @@ _battery_sysctl_start(void)
} }
} }
# elif defined(__FreeBSD__) || defined(__DragonFly__) # elif defined(__FreeBSD__) || defined(__DragonFly__)
if ((sysctlbyname("hw.acpi.battery.life", NULL, &len, NULL, 0)) != -1) int fd, n_units, val;
union acpi_battery_ioctl_arg battio;
char buf[128];
if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return 0;
if (ioctl(fd, ACPIIO_BATT_GET_UNITS, &n_units) == -1) return 0;
for (int i = 0; i < n_units; i++)
{ {
battio.unit = i;
if (ioctl(fd, ACPIIO_BATT_GET_BIX, &battio) != -1)
{
if (battio.bst.state == ACPI_BATT_STAT_NOT_PRESENT)
continue;
if (!(bat = E_NEW(Battery, 1))) if (!(bat = E_NEW(Battery, 1)))
return 0; return 0;
bat->mib = malloc(sizeof(int) * 4); snprintf(buf, sizeof(buf), "hw.acpi.battery.%i", i);
if (!bat->mib) return 0; bat->udi = eina_stringshare_add(buf);
sysctlnametomib("hw.acpi.battery.life", bat->mib, &len); bat->technology = eina_stringshare_add(battio.bix.type);
bat->vendor = eina_stringshare_add(battio.bix.oeminfo);
bat->mib_state = malloc(sizeof(int) * 4); bat->model = eina_stringshare_add(battio.bix.model);
if (!bat->mib_state) return 0;
sysctlnametomib("hw.acpi.battery.state", bat->mib_state, &len);
bat->mib_time = malloc(sizeof(int) * 4);
if (!bat->mib_time) return 0;
sysctlnametomib("hw.acpi.battery.time", bat->mib_time, &len);
bat->mib_units = malloc(sizeof(int) * 4);
if(!bat->mib_units) return 0;
sysctlnametomib("hw.acpi.battery.units", bat->mib_units, &len);
bat->udi = eina_stringshare_add("hw.acpi.battery");
bat->technology = eina_stringshare_add("Unknown");
bat->model = eina_stringshare_add("Unknown");
bat->vendor = eina_stringshare_add("Unknown");
bat->poll = ecore_poller_add(ECORE_POLLER_CORE, bat->poll = ecore_poller_add(ECORE_POLLER_CORE,
battery_config->poll_interval, battery_config->poll_interval,
_battery_sysctl_battery_update_poll, NULL); _battery_sysctl_battery_update_poll, NULL);
bat->unit = i;
device_batteries = eina_list_append(device_batteries, bat); device_batteries = eina_list_append(device_batteries, bat);
} }
}
if ((sysctlbyname("hw.acpi.acline", NULL, &len, NULL, 0)) != -1) if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) != -1)
{ {
if (!(ac = E_NEW(Ac_Adapter, 1))) if (!(ac = E_NEW(Ac_Adapter, 1)))
return 0; return 0;
ac->mib = malloc(sizeof(int) * 3);
if (!ac->mib) return 0;
len = sizeof(ac->mib);
sysctlnametomib("hw.acpi.acline", ac->mib, &len);
ac->udi = eina_stringshare_add("hw.acpi.acline"); ac->udi = eina_stringshare_add("hw.acpi.acline");
ac->present = val;
device_ac_adapters = eina_list_append(device_ac_adapters, ac); device_ac_adapters = eina_list_append(device_ac_adapters, ac);
} }
close(fd);
# endif # endif
_battery_sysctl_battery_update(); _battery_sysctl_battery_update();
@ -138,7 +135,9 @@ _battery_sysctl_stop(void)
if (ac) if (ac)
{ {
eina_stringshare_del(ac->udi); eina_stringshare_del(ac->udi);
#if defined(__OpenBSD__)
E_FREE(ac->mib); E_FREE(ac->mib);
#endif
E_FREE(ac); E_FREE(ac);
} }
@ -149,12 +148,9 @@ _battery_sysctl_stop(void)
eina_stringshare_del(bat->model); eina_stringshare_del(bat->model);
eina_stringshare_del(bat->vendor); eina_stringshare_del(bat->vendor);
ecore_poller_del(bat->poll); ecore_poller_del(bat->poll);
# if defined(__FreeBSD__) || defined(__DragonFly__) #if defined(__OpenBSD__)
E_FREE(bat->mib_state);
E_FREE(bat->mib_time);
E_FREE(bat->mib_units);
# endif
E_FREE(bat->mib); E_FREE(bat->mib);
#endif
E_FREE(bat); E_FREE(bat);
} }
} }
@ -170,13 +166,13 @@ static int
_battery_sysctl_battery_update() _battery_sysctl_battery_update()
{ {
double _time; double _time;
# if defined(__OpenBSD__) || defined(__NetBSD__) # if defined(__OpenBSD__)
double charge; double charge;
struct sensor s; struct sensor s;
size_t slen = sizeof(struct sensor); size_t slen = sizeof(struct sensor);
# elif defined(__FreeBSD__) || defined(__DragonFly__) # elif defined(__FreeBSD__) || defined(__DragonFly__)
int value; int fd, val;
size_t len; union acpi_battery_ioctl_arg battio;
# endif # endif
if (bat) if (bat)
@ -184,7 +180,7 @@ _battery_sysctl_battery_update()
/* update the poller interval */ /* update the poller interval */
ecore_poller_poller_interval_set(bat->poll, ecore_poller_poller_interval_set(bat->poll,
battery_config->poll_interval); battery_config->poll_interval);
# if defined(__OpenBSD__) || defined(__NetBSD__) # if defined(__OpenBSD__)
charge = 0; charge = 0;
/* last full capacity */ /* last full capacity */
@ -270,49 +266,32 @@ _battery_sysctl_battery_update()
} }
# elif defined(__FreeBSD__) || defined(__DragonFly__) # elif defined(__FreeBSD__) || defined(__DragonFly__)
len = sizeof(value); if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return 0;
if ((sysctl(bat->mib, 4, &value, &len, NULL, 0)) == -1)
battio.unit = bat->unit;
if (ioctl(fd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
{ {
if (fd != -1)
close(fd);
return 0; return 0;
} }
bat->got_prop = 1;
bat->percent = value; bat->percent = battio.battinfo.cap;
_time = ecore_time_get(); _time = ecore_time_get();
bat->last_update = _time; bat->last_update = _time;
len = sizeof(value); bat->charging = (battio.battinfo.state == ACPI_BATT_STAT_CHARGING) ? 1 : 0;
if ((sysctl(bat->mib_state, 4, &value, &len, NULL, 0)) == -1) bat->time_min = battio.battinfo.min;
{
return 0;
}
bat->charging = !value;
bat->got_prop = 1;
bat->time_full = -1;
bat->time_left = -1;
len = sizeof(bat->time_min);
if ((sysctl(bat->mib_time, 4, &bat->time_min, &len, NULL, 0)) == -1)
{
bat->time_min = -1;
}
len = sizeof(bat->batteries);
if ((sysctl(bat->mib_units, 4, &bat->batteries, &len, NULL, 0)) == -1)
{
bat->batteries = 1;
}
if (bat->time_min >= 0) bat->time_left = bat->time_min * 60; if (bat->time_min >= 0) bat->time_left = bat->time_min * 60;
if (bat->batteries == 1) bat->time_left = -1; close(fd);
# endif # endif
} }
if (ac) if (ac)
{ {
# if defined(__OpenBSD__) || defined(__NetBSD__) # if defined(__OpenBSD__)
/* AC State */ /* AC State */
ac->mib[3] = 9; ac->mib[3] = 9;
ac->mib[4] = 0; ac->mib[4] = 0;
@ -324,11 +303,14 @@ _battery_sysctl_battery_update()
ac->present = 0; ac->present = 0;
} }
# elif defined(__FreeBSD__) || defined(__DragonFly__) # elif defined(__FreeBSD__) || defined(__DragonFly__)
len = sizeof(value); if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return 0;
if ((sysctl(ac->mib, 3, &value, &len, NULL, 0)) != -1) if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) == -1)
{ {
ac->present = value; close(fd);
return 0;
} }
ac->present = val;
close(fd);
# endif # endif
} }