netstar pushed a commit to branch master.

http://git.enlightenment.org/core/enlightenment.git/commit/?id=945697ba0a040098df870da93cd61916e7fe5c73

commit 945697ba0a040098df870da93cd61916e7fe5c73
Author: Alastair Poole <nets...@gmail.com>
Date:   Sun Mar 7 16:23:50 2021 +0000

    battery: FreeBSD multiple batteries.
    
    model/vendor/tech support. Some indentation to make the BSD
    support easier to read and distinguish. Pleas test.
---
 src/modules/battery/e_mod_main.c   |   8 +-
 src/modules/battery/e_mod_main.h   | 147 ++++++++++++++++------------------
 src/modules/battery/e_mod_sysctl.c | 160 ++++++++++++++++---------------------
 3 files changed, 142 insertions(+), 173 deletions(-)

diff --git a/src/modules/battery/e_mod_main.c b/src/modules/battery/e_mod_main.c
index d86bf116f..d5408c574 100644
--- a/src/modules/battery/e_mod_main.c
+++ b/src/modules/battery/e_mod_main.c
@@ -520,7 +520,7 @@ _battery_config_updated(void)
      {
 #ifdef HAVE_EEZE
         ok = _battery_udev_start();
-#elif defined __OpenBSD__ || defined __DragonFly__ || defined __FreeBSD__ || 
defined __NetBSD__
+#elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
         ok = _battery_sysctl_start();
 #else
         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, suspend_below, 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);
 #endif
    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->suspend_below = 0;
         battery_config->force_mode = 0;
-#if defined HAVE_EEZE || defined __OpenBSD__ || defined __NetBSD__
+#if defined HAVE_EEZE || defined(__OpenBSD__)
         battery_config->fuzzy = 0;
 #endif
         battery_config->desktop_notifications = 0;
@@ -980,7 +980,7 @@ e_modapi_shutdown(E_Module *m EINA_UNUSED)
 
 #ifdef HAVE_EEZE
    _battery_udev_stop();
-#elif defined (__OpenBSD__) || defined (__DragonFly__) || defined 
(__FreeBSD__) || defined (__NetBSD__)
+#elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
    _battery_sysctl_stop();
 #else
    _battery_upower_stop();
diff --git a/src/modules/battery/e_mod_main.h b/src/modules/battery/e_mod_main.h
index fe815c3ce..0549b2f1a 100644
--- a/src/modules/battery/e_mod_main.h
+++ b/src/modules/battery/e_mod_main.h
@@ -7,38 +7,37 @@
 # include <Eldbus.h>
 #endif
 
-typedef struct _Config       Config;
-
 #define CHECK_NONE      0
 #define CHECK_ACPI      1
 #define CHECK_APM       2
 #define CHECK_PMU       3
 #define CHECK_SYS_ACPI  4
 
-#define UNKNOWN 0
-#define NOSUBSYSTEM 1
-#define SUBSYSTEM 2
+#define UNKNOWN         0
+#define NOSUBSYSTEM     1
+#define SUBSYSTEM       2
 
-#define SUSPEND 0
-#define HIBERNATE 1
-#define SHUTDOWN 2
+#define SUSPEND         0
+#define HIBERNATE       1
+#define SHUTDOWN        2
 
 #define POPUP_DEBOUNCE_CYCLES  2
 
+typedef struct _Config Config;
 struct _Config
 {
-   /* saved * loaded config values */
-   int              poll_interval;
-   int              alert;     /* Alert on minutes remaining */
-   int             alert_p;    /* Alert on percentage remaining */
-   int              alert_timeout;  /* Popup dismissal timeout */
-   int              suspend_below;  /* Suspend if battery drops below this 
level */
-   int              suspend_method; /* Method used to suspend the machine */
-   int              force_mode; /* force use of batget or hal */
+   /* saved / loaded config values */
+   int                  poll_interval;
+   int                  alert;          /* Alert on minutes remaining */
+   int                  alert_p;        /* Alert on percentage remaining */
+   int                  alert_timeout;  /* Popup dismissal timeout */
+   int                  suspend_below;  /* Suspend if battery drops below this 
level */
+   int                  suspend_method; /* Method used to suspend the machine 
*/
+   int                  force_mode;     /* force use of batget or hal */
    /* just config state */
-   E_Module        *module;
-   E_Config_Dialog *config_dialog;
-   Eina_List       *instances;
+   E_Module            *module;
+   E_Config_Dialog     *config_dialog;
+   Eina_List           *instances;
    Ecore_Exe           *batget_exe;
    Ecore_Event_Handler *batget_data_handler;
    Ecore_Event_Handler *batget_del_handler;
@@ -49,7 +48,7 @@ struct _Config
    int                  have_battery;
    int                  have_power;
    Eina_Bool            charging;
-   int              desktop_notifications;
+   int                  desktop_notifications;
 #ifdef HAVE_EEZE
    Eeze_Udev_Watch     *acwatch;
    Eeze_Udev_Watch     *batwatch;
@@ -65,73 +64,74 @@ typedef struct _Ac_Adapter Ac_Adapter;
 
 struct _Battery
 {
-   const char *udi;
+   const char   *udi;
 #if defined HAVE_EEZE || defined __OpenBSD__ || defined __DragonFly__ || 
defined __FreeBSD__ || defined __NetBSD__
    Ecore_Poller *poll;
-#endif
-   Eina_Bool present E_BITFIELD;
-   Eina_Bool charging E_BITFIELD;
-#if defined HAVE_EEZE || defined __OpenBSD__ || defined __DragonFly__ || 
defined __FreeBSD__ || defined __NetBSD__
-   double last_update;
-   double percent;
-   double current_charge;
-   double design_charge;
-   double last_full_charge;
-   double charge_rate;
-   double time_full;
-   double time_left;
+   Eina_Bool     present E_BITFIELD;
+   Eina_Bool     charging E_BITFIELD;
+   double        last_update;
+   double        percent;
+   double        current_charge;
+   double        design_charge;
+   double        last_full_charge;
+   double        charge_rate;
+   double        time_full;
+   double        time_left;
 #else
-   int percent;
-   int current_charge;
-   int design_charge;
-   int last_full_charge;
-   int charge_rate;
-   int time_full;
-   int time_left;
-   const char *type;
-   const char *charge_units;
+   int           percent;
+   int           current_charge;
+   int           design_charge;
+   int           last_full_charge;
+   int           charge_rate;
+   int           time_full;
+   int           time_left;
+   const char   *type;
+   const char   *charge_units;
 #endif
-   const char *technology;
-   const char *model;
-   const char *vendor;
-   Eina_Bool got_prop E_BITFIELD;
+   const char   *technology;
+   const char   *model;
+   const char   *vendor;
+   Eina_Bool     got_prop E_BITFIELD;
    Eldbus_Proxy *proxy;
-   int * mib;
-#if defined(__FreeBSD__) || defined(__DragonFly__)
-   int * mib_state;
-   int * mib_units;
-   int * mib_time;
-   int batteries;
-   int time_min;
+#if defined __FreeBSD__ || defined __DragonFly__
+   int           unit;
+   int           batteries;
+   int           time_min;
+#endif
+#if defined __OpenBSD__
+   int          *mib;
 #endif
 };
 
 struct _Ac_Adapter
 {
-   const char *udi;
-   Eina_Bool present E_BITFIELD;
-   const char *product;
+   const char   *udi;
+   Eina_Bool     present E_BITFIELD;
+   const char   *product;
    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);
-void _battery_device_update(void);
+void        _battery_device_update(void);
+
 #ifdef HAVE_EEZE
 /* in e_mod_udev.c */
-int  _battery_udev_start(void);
-void _battery_udev_stop(void);
+int         _battery_udev_start(void);
+void        _battery_udev_stop(void);
 /* end e_mod_udev.c */
 #elif !defined __OpenBSD__ && !defined __DragonFly__ && !defined __FreeBSD__ 
&& !defined __NetBSD__
 /* in e_mod_dbus.c */
-int _battery_upower_start(void);
-void _battery_upower_stop(void);
+int         _battery_upower_start(void);
+void        _battery_upower_stop(void);
 /* end e_mod_dbus.c */
 #else
 /* in e_mod_sysctl.c */
-int _battery_sysctl_start(void);
-void _battery_sysctl_stop(void);
+int         _battery_sysctl_start(void);
+void        _battery_sysctl_stop(void);
 /* end e_mod_sysctl.c */
 #endif
 
@@ -142,20 +142,7 @@ E_API int   e_modapi_shutdown (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);
-
-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.
- *
- * @}
- */
+extern Config   *battery_config;
+void             _battery_config_updated(void);
 
 #endif
diff --git a/src/modules/battery/e_mod_sysctl.c 
b/src/modules/battery/e_mod_sysctl.c
index 84a751cac..0928b0d90 100644
--- a/src/modules/battery/e_mod_sysctl.c
+++ b/src/modules/battery/e_mod_sysctl.c
@@ -1,12 +1,17 @@
 #include "e.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 <sys/types.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/sensors.h>
 #endif
@@ -24,17 +29,14 @@ static Battery *bat = NULL;
 int
 _battery_sysctl_start(void)
 {
-# if defined(__OpenBSD__) || defined(__NetBSD__)
+# if defined(__OpenBSD__)
    int mib[] = {CTL_HW, HW_SENSORS, 0, 0, 0};
    int devn;
    struct sensordev snsrdev;
    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;
         if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1)
           {
@@ -76,53 +78,48 @@ _battery_sysctl_start(void)
           }
      }
 # elif defined(__FreeBSD__) || defined(__DragonFly__)
-     if ((sysctlbyname("hw.acpi.battery.life", NULL, &len, NULL, 0)) != -1)
-       {
-          if (!(bat = E_NEW(Battery, 1)))
-            return 0;
+   int fd, n_units, val;
+   union acpi_battery_ioctl_arg battio;
+   char buf[128];
 
-          bat->mib = malloc(sizeof(int) * 4);
-          if (!bat->mib) return 0;
-          sysctlnametomib("hw.acpi.battery.life", bat->mib, &len);
+   if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return 0;
 
-          bat->mib_state = malloc(sizeof(int) * 4);
-          if (!bat->mib_state) return 0;
-          sysctlnametomib("hw.acpi.battery.state", bat->mib_state, &len);
+   if (ioctl(fd, ACPIIO_BATT_GET_UNITS, &n_units) == -1) return 0;
 
-          bat->mib_time  = malloc(sizeof(int) * 4);
-          if (!bat->mib_time) return 0;
-          sysctlnametomib("hw.acpi.battery.time", bat->mib_time, &len);
+   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;
 
-          bat->mib_units = malloc(sizeof(int) * 4);
-          if(!bat->mib_units) return 0;
-          sysctlnametomib("hw.acpi.battery.units", bat->mib_units, &len);
+             if (!(bat = E_NEW(Battery, 1)))
+               return 0;
 
-          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");
+             snprintf(buf, sizeof(buf), "hw.acpi.battery.%i", i);
+             bat->udi = eina_stringshare_add(buf);
+             bat->technology = eina_stringshare_add(battio.bix.type);
+             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,
-                                       battery_config->poll_interval,
-                                       _battery_sysctl_battery_update_poll, 
NULL);
+    if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) != -1)
+      {
+         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);
       }
-
-      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);
-        }
+    close(fd);
 # endif
    _battery_sysctl_battery_update();
 
@@ -138,7 +135,9 @@ _battery_sysctl_stop(void)
    if (ac)
      {
         eina_stringshare_del(ac->udi);
+#if defined(__OpenBSD__)
         E_FREE(ac->mib);
+#endif
         E_FREE(ac);
      }
 
@@ -149,12 +148,9 @@ _battery_sysctl_stop(void)
         eina_stringshare_del(bat->model);
         eina_stringshare_del(bat->vendor);
         ecore_poller_del(bat->poll);
-# if defined(__FreeBSD__) || defined(__DragonFly__)
-        E_FREE(bat->mib_state);
-        E_FREE(bat->mib_time);
-        E_FREE(bat->mib_units);
-# endif
+#if defined(__OpenBSD__)
         E_FREE(bat->mib);
+#endif
         E_FREE(bat);
      }
 }
@@ -170,13 +166,13 @@ static int
 _battery_sysctl_battery_update()
 {
    double _time;
-# if defined(__OpenBSD__) || defined(__NetBSD__)
+# if defined(__OpenBSD__)
    double charge;
    struct sensor s;
    size_t slen = sizeof(struct sensor);
 # elif defined(__FreeBSD__) || defined(__DragonFly__)
-   int value;
-   size_t len;
+   int fd, val;
+   union acpi_battery_ioctl_arg battio;
 # endif
 
    if (bat)
@@ -184,7 +180,7 @@ _battery_sysctl_battery_update()
        /* update the poller interval */
        ecore_poller_poller_interval_set(bat->poll,
                                         battery_config->poll_interval);
-# if defined(__OpenBSD__) || defined(__NetBSD__)
+# if defined(__OpenBSD__)
        charge = 0;
 
        /* last full capacity */
@@ -270,49 +266,32 @@ _battery_sysctl_battery_update()
          }
 
 # elif defined(__FreeBSD__) || defined(__DragonFly__)
-       len = sizeof(value);
-       if ((sysctl(bat->mib, 4, &value, &len, NULL, 0)) == -1)
-         {
-           return 0;
-         }
+       if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return 0;
 
-       bat->percent = value;
-
-       _time = ecore_time_get();
-       bat->last_update = _time;
-
-       len = sizeof(value);
-       if ((sysctl(bat->mib_state, 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->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;
-         }
+       bat->percent = battio.battinfo.cap;
+       _time = ecore_time_get();
+       bat->last_update = _time;
 
-       len = sizeof(bat->batteries);
-       if ((sysctl(bat->mib_units, 4, &bat->batteries, &len, NULL, 0)) == -1)
-         {
-            bat->batteries = 1;
-         }
+       bat->charging = (battio.battinfo.state == ACPI_BATT_STAT_CHARGING) ? 1 
: 0;
+       bat->time_min = battio.battinfo.min;
 
        if (bat->time_min >= 0) bat->time_left = bat->time_min * 60;
-       if (bat->batteries == 1) bat->time_left = -1;
+       close(fd);
 # endif
    }
 
    if (ac)
      {
-# if defined(__OpenBSD__) || defined(__NetBSD__)
+# if defined(__OpenBSD__)
        /* AC State */
        ac->mib[3] = 9;
        ac->mib[4] = 0;
@@ -324,11 +303,14 @@ _battery_sysctl_battery_update()
               ac->present = 0;
          }
 # elif defined(__FreeBSD__) || defined(__DragonFly__)
-       len = sizeof(value);
-       if ((sysctl(ac->mib, 3, &value, &len, NULL, 0)) != -1)
+       if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return 0;
+       if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) == -1)
          {
-            ac->present = value;
+            close(fd);
+            return 0;
          }
+       ac->present = val;
+       close(fd);
 # endif
      }
 

-- 


Reply via email to