netstar pushed a commit to branch master.

http://git.enlightenment.org/apps/evisum.git/commit/?id=85d07825339fcf438c67bf3f7bde94d8aaef1a2c

commit 85d07825339fcf438c67bf3f7bde94d8aaef1a2c
Author: Alastair Poole <nets...@gmail.com>
Date:   Mon May 17 12:04:03 2021 +0100

    wip: so we can query without blocking...
    
    Need to do cores.
---
 src/bin/evisum_watcher.c       |  2 +-
 src/bin/next/machine.h         |  2 ++
 src/bin/next/machine/network.x |  2 +-
 src/bin/next/machine/sensors.x | 66 +++++++++++++++++++++++++++++++++++++-----
 4 files changed, 63 insertions(+), 9 deletions(-)

diff --git a/src/bin/evisum_watcher.c b/src/bin/evisum_watcher.c
index 6932760..7e0063c 100644
--- a/src/bin/evisum_watcher.c
+++ b/src/bin/evisum_watcher.c
@@ -32,7 +32,7 @@ main(int argc, char **argv)
    EINA_LIST_FREE(batteries, bat)
      {
         battery_check(bat);
-        printf("battery %s (%s) => %1.2f\n", bat->name, bat->vendor, 
bat->percent);
+        printf("battery %s (%s) => %1.2f\n", bat->model, bat->vendor, 
bat->percent);
         battery_free(bat);
      }
 
diff --git a/src/bin/next/machine.h b/src/bin/next/machine.h
index 6d6b0db..3b8cfc6 100644
--- a/src/bin/next/machine.h
+++ b/src/bin/next/machine.h
@@ -71,6 +71,8 @@ typedef struct
    bool    present;
 #if defined(__OpenBSD__)
    int     mibs[5];
+#elif defined(__FreeBSD__) || defined(__DragonFly__)
+   char    unit;
 #endif
 } Battery;
 
diff --git a/src/bin/next/machine/network.x b/src/bin/next/machine/network.x
index 2dc0b8a..35a2f9e 100644
--- a/src/bin/next/machine/network.x
+++ b/src/bin/next/machine/network.x
@@ -1,6 +1,6 @@
 #if defined(__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__)
 static Eina_List *
-_freebsd_generic_network_status(int *n)
+_freebsd_generic_network_status(void)
 {
    struct ifmibdata *ifmd;
    size_t len;
diff --git a/src/bin/next/machine/sensors.x b/src/bin/next/machine/sensors.x
index d9cf0c0..2e6a017 100644
--- a/src/bin/next/machine/sensors.x
+++ b/src/bin/next/machine/sensors.x
@@ -288,6 +288,35 @@ batteries_find(void)
           }
      }
 #elif defined(__FreeBSD__)
+   int n_units, fd;
+   union acpi_battery_ioctl_arg battio;
+
+   fd = open("/dev/acpi", O_RDONLY);
+   if (fd == -1) return NULL;
+
+   if (ioctl(fd, ACPIIO_BATT_GET_UNITS, &n_units) == -1)
+     return NULL;
+
+   for (int i = 0; i < n_units; i++)
+     {
+        battio.unit = i;
+        if (ioctl(fd, ACPIIO_BATT_GET_BIX, &battio) != -1)
+          {
+             Battery *bat = calloc(1, sizeof(Battery));
+             if (bat)
+               {
+                  if (battio.bst.state == ACPI_BATT_STAT_NOT_PRESENT)
+                    bat->present = 0;
+                  else
+                    bat->present = 1;
+                  bat->vendor = strdup(battio.bix.oeminfo);
+                  bat->model = strdup(battio.bix.model);
+                  bat->unit = i;
+                  list = eina_list_append(list, bat);
+               }
+          }
+     }
+   close(fd);
 #elif defined(__linux__)
    char *type;
    struct dirent **names;
@@ -337,9 +366,12 @@ batteries_find(void)
 void
 battery_free(Battery *bat)
 {
-   free(bat->name);
-   free(bat->model);
-   free(bat->vendor);
+   if (bat->name)
+     free(bat->name);
+   if (bat->model)
+     free(bat->model);
+   if (bat->vendor)
+     free(bat->vendor);
    free(bat);
 }
 
@@ -377,8 +409,28 @@ battery_check(Battery *bat)
           charge_current = (double) snsr.value;
      }
 
-#elif defined(__FreeBSD__)
+#elif defined(__FreeBSD__) || defined(__DragonFly__)
+   int fd;
+   union acpi_battery_ioctl_arg battio;
+
+   if (!bat->present) return;
+
+   fd = open("/dev/acpi", O_RDONLY);
+   if (fd == -1) return;
+
+   battio.unit = bat->unit;
+   if (ioctl(fd, ACPIIO_BATT_GET_BIX, &battio) != -1)
+     {
+        if (battio.bif.lfcap == 0)
+          charge_full = battio.bif.dcap;
+        else
+          charge_full = battio.bif.lfcap;
+        battio.unit = bat->unit;
+        if (ioctl(fd, ACPIIO_BATT_GET_BST, &battio) != -1)
+          charge_current = battio.bst.cap;
+     }
 
+   close(fd);
 #elif defined(__linux__)
    char path[PATH_MAX];
    struct dirent *dh;
@@ -446,7 +498,7 @@ battery_check(Battery *bat)
              else if (buf[0] == 'L')
                bat->charge_current = 25;
              else if (buf[0] == 'C')
-              bat->charge_current = 5;
+               bat->charge_current = 5;
              free(buf);
           }
      }
@@ -459,8 +511,8 @@ done:
     bat->charge_full = charge_full;
     bat->charge_current = charge_current;
 
-    if (charge_full && charge_current)
-      bat->percent = 100 * (charge_full / charge_current);
+    if (charge_full)
+      bat->percent = 100 * (charge_current / charge_full);
     else
       bat->percent = 0;
 }

-- 


Reply via email to