netstar pushed a commit to branch master.

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

commit caa1dd29a76f2af682d473ec367f4388468e42cb
Author: Alastair Poole <nets...@gmail.com>
Date:   Sun May 16 17:39:04 2021 +0100

    wip: wip wip wip
---
 src/bin/evisum_watcher.c       |  34 +++------
 src/bin/meson.build            |   2 +
 src/bin/next/machine.h         |  24 +++++--
 src/bin/next/machine/sensors.x | 157 +++++++++++++++++++++--------------------
 src/bin/next/meson.build       |   2 +-
 5 files changed, 111 insertions(+), 108 deletions(-)

diff --git a/src/bin/evisum_watcher.c b/src/bin/evisum_watcher.c
index 6095088..2d17fc0 100644
--- a/src/bin/evisum_watcher.c
+++ b/src/bin/evisum_watcher.c
@@ -1,38 +1,26 @@
-#include <Eina.h>
-#include <Ecore.h>
 #include "next/machine.h"
 
-static Ecore_Timer *background_timer;
 static Eina_List   *cores = NULL;
 static Eina_List   *batteries = NULL;
 static Eina_List   *sensors = NULL;
 static Eina_List   *network_interfaces = NULL;
 
-static Eina_Bool
-_cb_background_timer(void *data EINA_UNUSED)
-{
-   static int64_t poll_count = 0;
-   static double t_prev = 0;
-   double t;
-
-   poll_count++;
-
-   t = ecore_loop_time_get();
-
-   if (t_prev) printf("%1.4f\n", t - t_prev);
-   t_prev = t; 
-
-   return 1;
-}
-
 int
 main(int argc, char **argv)
 {
-   ecore_init();
+   Eina_List *l;
+   Battery *bat;
 
-   background_timer = ecore_timer_add(0.025, _cb_background_timer, NULL);
+   ecore_init();
 
-   ecore_main_loop_begin();
+   batteries = batteries_find();
+   EINA_LIST_FOREACH(batteries, l, bat)
+     {
+        battery_check(bat);
+        printf("battery %s (%s) => %1.2f\n", bat->name, bat->vendor, 
bat->percent);
+     }
+   EINA_LIST_FREE(batteries, bat)
+     battery_free(bat);
 
    ecore_shutdown();
 
diff --git a/src/bin/meson.build b/src/bin/meson.build
index ee7a023..2361f76 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -26,6 +26,8 @@ src_watch = files([
    'evisum_watcher.h',
 ])
 
+subdir('next')
+
 executable('evisum_watcher', src_watch,
    include_directories  : inc,
    dependencies         : [ deps, deps_os, deps_nls ],
diff --git a/src/bin/next/machine.h b/src/bin/next/machine.h
index aae6045..dd268ff 100644
--- a/src/bin/next/machine.h
+++ b/src/bin/next/machine.h
@@ -1,5 +1,5 @@
-#ifndef __MACHINE_H__
-#define __MACHINE_H__
+#ifndef MACHINE_H
+#define MACHINE_H
 
 /* All functions and data types implementing these APIs have no additional
  * system dependencies deliberately.
@@ -7,6 +7,7 @@
  * See machine.c and the files includes in machine/ sub directory.
  */
 
+#include <Eina.h>
 #include <stdint.h>
 #include <stdbool.h>
 
@@ -62,19 +63,16 @@ typedef struct
    char   *model;
    double  charge_full;
    double  charge_current;
-   uint8_t percent;
+   double  percent;
    bool    present;
 #if defined(__OpenBSD__)
    int     mibs[5];
 #endif
-} bat_t;
+} Battery;
 
 typedef struct
 {
    bool     have_ac;
-   int      battery_count;
-
-   bat_t  **batteries;
 #if defined(__OpenBSD__)
    int      mibs[5];
 #endif
@@ -90,6 +88,18 @@ typedef struct
    } xfer;
 } net_iface_t;
 
+Eina_List *
+batteries_find(void);
+
+void
+battery_free(Battery *bat);
+
+void
+battery_check(Battery *bat);
+
+
+
+
 int
 system_cpu_online_count_get(void);
 
diff --git a/src/bin/next/machine/sensors.x b/src/bin/next/machine/sensors.x
index e561475..c00eb2e 100644
--- a/src/bin/next/machine/sensors.x
+++ b/src/bin/next/machine/sensors.x
@@ -237,48 +237,62 @@ system_sensors_thermal_get(int *sensor_count)
    return sensors;
 }
 
-static int
-_power_battery_count_get(power_t *power)
+Eina_List *
+batteries_find(void)
 {
+   Eina_List *list = NULL;
 #if defined(__OpenBSD__)
    struct sensordev snsrdev;
    size_t sdlen = sizeof(struct sensordev);
    int mibs[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
-   int i, devn;
+   int devn;
 
-   for (devn = 0;; devn++) {
+   for (devn = 0; ; devn++)
+     {
         mibs[2] = devn;
         if (sysctl(mibs, 3, &snsrdev, &sdlen, NULL, 0) == -1)
           {
-             if (errno == ENXIO)
-               continue;
-             if (errno == ENOENT)
-               break;
+             if (errno == ENXIO) continue;
+             if (errno == ENOENT) break;
           }
-
         if (!strncmp(snsrdev.xname, "acpibat", 7))
           {
-             i = power->battery_count;
-
-             void *t = realloc(power->batteries, 1 +
-                               power->battery_count++ * sizeof(bat_t **));
-             power->batteries = t;
-             power->batteries[i] = calloc(1, sizeof(bat_t));
-             power->batteries[i]->name = strdup(snsrdev.xname);
-             power->batteries[i]->model = strdup("Unknown");
-             power->batteries[i]->vendor = strdup("Unknown");
-             power->batteries[i]->present = true;
-             power->batteries[i]->mibs[0] = mibs[0];
-             power->batteries[i]->mibs[1] = mibs[1];
-             power->batteries[i]->mibs[2] = mibs[2];
-          }
-        if (!strcmp("acpiac0", snsrdev.xname))
-          {
-             power->mibs[0] = mibs[0];
-             power->mibs[1] = mibs[1];
-             power->mibs[2] = mibs[2];
+             Battery *bat = calloc(1, sizeof(Battery));
+             if (bat)
+               {
+                  bat->name = strdup(snsrdev.xname);
+                  bat->model = strdup("Unknown");
+                  bat->vendor = strdup("Unknown");
+                  bat->mibs[0] = mibs[0];
+                  bat->mibs[1] = mibs[1];
+                  bat->mibs[2] = mibs[2];
+                  bat->present = 1;
+                  list = eina_list_append(list, bat);
+               }
           }
      }
+#elif defined(__linux__)
+
+#elif defined(__FreeBSD__)
+
+#endif
+puts("AYE");
+   return list;
+}
+
+void
+battery_free(Battery *bat)
+{
+   free(bat->name);
+   free(bat->model);
+   free(bat->vendor);
+   free(bat);
+}
+
+static int
+_power_battery_count_get(power_t *power)
+{
+#if defined(__OpenBSD__)
 #elif defined(__FreeBSD__) || defined(__DragonFly__)
    int n_units, fd;
    char name[256];
@@ -345,52 +359,60 @@ _power_battery_count_get(power_t *power)
    free(names);
 #endif
 
-   return power->battery_count;
+   return 0; 
 }
 
-static void
-_battery_state_get(power_t *power)
+void
+battery_check(Battery *bat)
 {
+   double charge_full = 0, charge_current = 0;
 #if defined(__OpenBSD__)
-   int *mib;
-   double charge_full, charge_current;
    size_t slen = sizeof(struct sensor);
    struct sensor snsr;
 
-   for (int i = 0; i < power->battery_count; i++) {
-        charge_full = charge_current = 0;
-
-        mib = power->batteries[i]->mibs;
-        mib[3] = SENSOR_WATTHOUR;
-        mib[4] = 0;
+   bat->mibs[3] = SENSOR_WATTHOUR;
+   bat->mibs[4] = 0;
 
-        if (sysctl(mib, 5, &snsr, &slen, NULL, 0) != -1)
-          charge_full = (double)snsr.value;
+   if (sysctl(bat->mibs, 5, &snsr, &slen, NULL, 0) != -1)
+     charge_full = (double) snsr.value;
 
-        mib[3] = SENSOR_WATTHOUR;
-        mib[4] = 3;
+   bat->mibs[4] = 3;
 
-        if (sysctl(mib, 5, &snsr, &slen, NULL, 0) != -1)
-          charge_current = (double)snsr.value;
+   if (sysctl(bat->mibs, 5, &snsr, &slen, NULL, 0) != -1)
+     charge_current = (double) snsr.value;
 
-        if (charge_current == 0 || charge_full == 0)
-          {
-             mib[3] = SENSOR_AMPHOUR;
-             mib[4] = 0;
+   if ((!charge_current) || (!charge_full))
+     {
 
-             if (sysctl(mib, 5, &snsr, &slen, NULL, 0) != -1)
-               charge_full = (double)snsr.value;
+        bat->mibs[3] = SENSOR_AMPHOUR;
+        bat->mibs[4] = 0;
 
-             mib[3] = SENSOR_AMPHOUR;
-             mib[4] = 3;
+        if (sysctl(bat->mibs, 5, &snsr, &slen, NULL, 0) != -1)
+          charge_full = (double) snsr.value;
 
-             if (sysctl(mib, 5, &snsr, &slen, NULL, 0) != -1)
-               charge_current = (double)snsr.value;
-          }
+        bat->mibs[4] = 3;
 
-        power->batteries[i]->charge_full = charge_full;
-        power->batteries[i]->charge_current = charge_current;
+        if (sysctl(bat->mibs, 5, &snsr, &slen, NULL, 0) != -1)
+          charge_current = (double) snsr.value;
      }
+
+#elif defined(__FreeBSD__)
+
+#elif defined(__linux__)
+
+#else
+#endif
+
+    bat->charge_full = charge_full;
+    bat->charge_current = charge_current;
+
+    bat->percent = 100 * (charge_full / charge_current);
+}
+
+static void
+_battery_state_get(power_t *power)
+{
+#if defined(__OpenBSD__)
 #elif defined(__FreeBSD__) || defined(__DragonFly__)
    int fd, i;
    union acpi_battery_ioctl_arg battio;
@@ -501,7 +523,6 @@ done:
 void
 system_power_state_get(power_t *power)
 {
-   int i;
    memset(power, 0, sizeof(power_t));
 #if defined(__OpenBSD__)
    struct sensor snsr;
@@ -537,27 +558,9 @@ system_power_state_get(power_t *power)
 #endif
 
    _battery_state_get(power);
-
-   for (i = 0; i < power->battery_count; i++) {
-        power->batteries[i]->percent = 100 *
-           (power->batteries[i]->charge_current /
-                                    power->batteries[i]->charge_full);
-     }
 }
 
 void
 system_power_state_free(power_t *power)
 {
-   for (int i = 0; i < power->battery_count; i++)
-     {
-        if (power->batteries[i]->name)
-          free(power->batteries[i]->name);
-        if (power->batteries[i]->model)
-          free(power->batteries[i]->model);
-        if (power->batteries[i]->vendor)
-          free(power->batteries[i]->vendor);
-        free(power->batteries[i]);
-     }
-   if (power->batteries)
-     free(power->batteries);
 }
diff --git a/src/bin/next/meson.build b/src/bin/next/meson.build
index 5d7961e..6c0b60f 100644
--- a/src/bin/next/meson.build
+++ b/src/bin/next/meson.build
@@ -1,4 +1,4 @@
-src += files([
+src_watch += files([
    'machine.c',
    'machine.h',
 ])

-- 


Reply via email to