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,38 +7,37 @@
# 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
#define CHECK_PMU 3 #define CHECK_PMU 3
#define CHECK_SYS_ACPI 4 #define CHECK_SYS_ACPI 4
#define UNKNOWN 0 #define UNKNOWN 0
#define NOSUBSYSTEM 1 #define NOSUBSYSTEM 1
#define SUBSYSTEM 2 #define SUBSYSTEM 2
#define SUSPEND 0 #define SUSPEND 0
#define HIBERNATE 1 #define HIBERNATE 1
#define SHUTDOWN 2 #define SHUTDOWN 2
#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 */
int alert_timeout; /* Popup dismissal timeout */ int alert_timeout; /* Popup dismissal timeout */
int suspend_below; /* Suspend if battery drops below this level */ int suspend_below; /* Suspend if battery drops below this level */
int suspend_method; /* Method used to suspend the machine */ int suspend_method; /* Method used to suspend the machine */
int force_mode; /* force use of batget or hal */ int force_mode; /* force use of batget or hal */
/* just config state */ /* just config state */
E_Module *module; E_Module *module;
E_Config_Dialog *config_dialog; E_Config_Dialog *config_dialog;
Eina_List *instances; Eina_List *instances;
Ecore_Exe *batget_exe; Ecore_Exe *batget_exe;
Ecore_Event_Handler *batget_data_handler; Ecore_Event_Handler *batget_data_handler;
Ecore_Event_Handler *batget_del_handler; Ecore_Event_Handler *batget_del_handler;
@ -49,7 +48,7 @@ struct _Config
int have_battery; int have_battery;
int have_power; int have_power;
Eina_Bool charging; Eina_Bool charging;
int desktop_notifications; int desktop_notifications;
#ifdef HAVE_EEZE #ifdef HAVE_EEZE
Eeze_Udev_Watch *acwatch; Eeze_Udev_Watch *acwatch;
Eeze_Udev_Watch *batwatch; Eeze_Udev_Watch *batwatch;
@ -65,73 +64,74 @@ typedef struct _Ac_Adapter Ac_Adapter;
struct _Battery 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; double last_update;
#if defined HAVE_EEZE || defined __OpenBSD__ || defined __DragonFly__ || defined __FreeBSD__ || defined __NetBSD__ double percent;
double last_update; double current_charge;
double percent; double design_charge;
double current_charge; double last_full_charge;
double design_charge; double charge_rate;
double last_full_charge; double time_full;
double charge_rate; double time_left;
double time_full;
double time_left;
#else #else
int percent; int percent;
int current_charge; int current_charge;
int design_charge; int design_charge;
int last_full_charge; int last_full_charge;
int charge_rate; int charge_rate;
int time_full; int time_full;
int time_left; int time_left;
const char *type; const char *type;
const char *charge_units; const char *charge_units;
#endif #endif
const char *technology; const char *technology;
const char *model; const char *model;
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 batteries;
int * mib_units; int time_min;
int * mib_time; #endif
int batteries; #if defined __OpenBSD__
int time_min; int *mib;
#endif #endif
}; };
struct _Ac_Adapter struct _Ac_Adapter
{ {
const char *udi; const char *udi;
Eina_Bool present E_BITFIELD; Eina_Bool present E_BITFIELD;
const char *product; const char *product;
Eldbus_Proxy *proxy; Eldbus_Proxy *proxy;
int * mib; #if defined __OpenBSD__
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);
void _battery_udev_stop(void); void _battery_udev_stop(void);
/* end e_mod_udev.c */ /* end e_mod_udev.c */
#elif !defined __OpenBSD__ && !defined __DragonFly__ && !defined __FreeBSD__ && !defined __NetBSD__ #elif !defined __OpenBSD__ && !defined __DragonFly__ && !defined __FreeBSD__ && !defined __NetBSD__
/* in e_mod_dbus.c */ /* in e_mod_dbus.c */
int _battery_upower_start(void); int _battery_upower_start(void);
void _battery_upower_stop(void); void _battery_upower_stop(void);
/* end e_mod_dbus.c */ /* end e_mod_dbus.c */
#else #else
/* in e_mod_sysctl.c */ /* in e_mod_sysctl.c */
int _battery_sysctl_start(void); int _battery_sysctl_start(void);
void _battery_sysctl_stop(void); void _battery_sysctl_stop(void);
/* end e_mod_sysctl.c */ /* end e_mod_sysctl.c */
#endif #endif
@ -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);
extern Config *battery_config;
void _battery_config_updated(void); void _battery_config_updated(void);
extern Config *battery_config;
/**
* @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;
if (!(bat = E_NEW(Battery, 1))) char buf[128];
return 0;
bat->mib = malloc(sizeof(int) * 4); if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return 0;
if (!bat->mib) return 0;
sysctlnametomib("hw.acpi.battery.life", bat->mib, &len);
bat->mib_state = malloc(sizeof(int) * 4); if (ioctl(fd, ACPIIO_BATT_GET_UNITS, &n_units) == -1) return 0;
if (!bat->mib_state) return 0;
sysctlnametomib("hw.acpi.battery.state", bat->mib_state, &len);
bat->mib_time = malloc(sizeof(int) * 4); for (int i = 0; i < n_units; i++)
if (!bat->mib_time) return 0; {
sysctlnametomib("hw.acpi.battery.time", bat->mib_time, &len); battio.unit = i;
if (ioctl(fd, ACPIIO_BATT_GET_BIX, &battio) != -1)
{
if (battio.bst.state == ACPI_BATT_STAT_NOT_PRESENT)
continue;
bat->mib_units = malloc(sizeof(int) * 4); if (!(bat = E_NEW(Battery, 1)))
if(!bat->mib_units) return 0; return 0;
sysctlnametomib("hw.acpi.battery.units", bat->mib_units, &len);
bat->udi = eina_stringshare_add("hw.acpi.battery"); snprintf(buf, sizeof(buf), "hw.acpi.battery.%i", i);
bat->technology = eina_stringshare_add("Unknown"); bat->udi = eina_stringshare_add(buf);
bat->model = eina_stringshare_add("Unknown"); bat->technology = eina_stringshare_add(battio.bix.type);
bat->vendor = eina_stringshare_add("Unknown"); bat->vendor = eina_stringshare_add(battio.bix.oeminfo);
bat->model = eina_stringshare_add(battio.bix.model);
bat->poll = ecore_poller_add(ECORE_POLLER_CORE,
battery_config->poll_interval,
_battery_sysctl_battery_update_poll, NULL);
bat->unit = i;
device_batteries = eina_list_append(device_batteries, bat);
}
}
bat->poll = ecore_poller_add(ECORE_POLLER_CORE, if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) != -1)
battery_config->poll_interval, {
_battery_sysctl_battery_update_poll, NULL); if (!(ac = E_NEW(Ac_Adapter, 1)))
return 0;
device_batteries = eina_list_append(device_batteries, bat); ac->udi = eina_stringshare_add("hw.acpi.acline");
ac->present = val;
device_ac_adapters = eina_list_append(device_ac_adapters, ac);
} }
close(fd);
if ((sysctlbyname("hw.acpi.acline", NULL, &len, NULL, 0)) != -1)
{
if (!(ac = E_NEW(Ac_Adapter, 1)))
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");
device_ac_adapters = eina_list_append(device_ac_adapters, ac);
}
# 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)
{ {
return 0; if (fd != -1)
close(fd);
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
} }