netstar pushed a commit to branch master.

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

commit ef961b275a2695ecece17d48da59b28257388a2f
Author: Alastair Poole <[email protected]>
Date:   Thu Jun 18 12:17:11 2020 +0100

    machine: remove horrible hack
---
 src/bin/system/machine.c | 60 +++++++++++++++++++++++++++++-------------------
 src/bin/system/machine.h |  7 +++---
 src/bin/ui/ui_misc.c     |  2 +-
 3 files changed, 41 insertions(+), 28 deletions(-)

diff --git a/src/bin/system/machine.c b/src/bin/system/machine.c
index 41b7d3f..fd734ff 100644
--- a/src/bin/system/machine.c
+++ b/src/bin/system/machine.c
@@ -753,10 +753,14 @@ _power_battery_count_get(power_t *power)
              snprintf(buf, sizeof(buf), "acpibat%d", i);
              if (!strcmp(buf, snsrdev.xname))
                {
-                  power->bat_mibs[power->battery_count] =
-                    malloc(sizeof(int) * 5);
-                  power->battery_names[power->battery_count] = strdup(buf);
-                  int *tmp = power->bat_mibs[power->battery_count++];
+                  power->batteries = realloc(power->batteries, 1 +
+                                     power->battery_count * sizeof(bat_t **));
+                  power->batteries[i] = calloc(1, sizeof(bat_t)
+                  power->batteries[i]->name = strdup(buf);
+                  power->batteries[i]->present = true;
+                  power->battery_count++;
+                  power->batteries[i]->mibs = malloc(sizeof(int) * 5);
+                  int *tmp = power->batteries[i]->mib;
                   tmp[0] = mib[0];
                   tmp[1] = mib[1];
                   tmp[2] = mib[2];
@@ -782,6 +786,13 @@ _power_battery_count_get(power_t *power)
      {
         sysctlnametomib("hw.acpi.acline", power->ac_mibs, &len);
      }
+
+   power->batteries = malloc(power->battery_count * sizeof(bat_t **));
+   for (int i = 0; i < power->battery_count; i++)
+     {
+        power->batteries[i] = calloc(1, sizeof(bat_t));
+        power->batteries[i]->present = true;
+     }
 #elif defined(__linux__)
    char *type;
    char path[PATH_MAX];
@@ -800,8 +811,14 @@ _power_battery_count_get(power_t *power)
         if (type)
           {
              if (!strncmp(type, "Battery", 7))
-               power->battery_names[power->battery_count++] =
-                  strdup(names[i]->d_name);
+               {
+                  power->batteries = realloc(power->batteries, 1 +
+                                     power->battery_count * sizeof(bat_t **));
+                  power->batteries[i] = calloc(1, sizeof(bat_t));
+                  power->batteries[i]->name = strdup(names[i]->d_name);
+                  power->batteries[i]->present = true;
+                  power->battery_count++;
+               }
              free(type);
           }
 
@@ -811,13 +828,6 @@ _power_battery_count_get(power_t *power)
    free(names);
 #endif
 
-   power->batteries = malloc(power->battery_count * sizeof(bat_t **));
-   for (int i = 0; i < power->battery_count; i++)
-     {
-        power->batteries[i] = calloc(1, sizeof(bat_t));
-        power->batteries[i]->present = true;
-     }
-
    return power->battery_count;
 }
 
@@ -911,7 +921,7 @@ _battery_state_get(power_t *power)
      {
         naming = NULL;
         snprintf(path, sizeof(path), "/sys/class/power_supply/%s",
-                 power->battery_names[i]);
+                 power->batteries[i]->name);
 
         if (stat(path, &st) < 0) continue;
         if (S_ISLNK(st.st_mode)) continue;
@@ -936,7 +946,7 @@ _battery_state_get(power_t *power)
           continue;
 
         snprintf(path, sizeof(path), "/sys/class/power_supply/%s/%s_full",
-                 power->battery_names[i], naming);
+                 power->batteries[i]->name, naming);
         buf = file_contents(path);
         if (buf)
           {
@@ -944,7 +954,7 @@ _battery_state_get(power_t *power)
              free(buf);
           }
         snprintf(path, sizeof(path), "/sys/class/power_supply/%s/%s_now",
-                 power->battery_names[i], naming);
+                 power->batteries[i]->name, naming);
         buf = file_contents(path);
         if (buf)
           {
@@ -953,11 +963,11 @@ _battery_state_get(power_t *power)
           }
 
         snprintf(path, sizeof(path), "/sys/class/power_supply/%s/manufacturer",
-                 power->battery_names[i]);
+                 power->batteries[i]->name);
         vendor = file_contents(path);
 
         snprintf(path, sizeof(path), "/sys/class/power_supply/%s/model_name",
-                 power->battery_names[i]);
+                 power->batteries[i]->name);
         model = file_contents(path);
 
         if (vendor && vendor[0] && model && model[0])
@@ -977,9 +987,9 @@ _battery_state_get(power_t *power)
                   model[len - 1] = '\0';
                }
 
-             free(power->battery_names[i]);
+             free(power->batteries[i]->name);;
              snprintf(name, sizeof(name), "%s %s", vendor, model);
-             power->battery_names[i] = strdup(name);
+             power->batteries[i]->name = strdup(name);
           }
 
         power->batteries[i]->charge_full = charge_full;
@@ -1038,8 +1048,10 @@ _power_state_get(power_t *power)
            (power->batteries[i]->charge_current /
                                     power->batteries[i]->charge_full);
         power->batteries[i]->percent = percent;
-        if (power->bat_mibs[i])
-          free(power->bat_mibs[i]);
+#if defined(__OpenBSD__)
+        if (power->batteries[i]->mibs);
+          free(power->batteries[i]->mibs);
+#endif
      }
 }
 
@@ -1203,8 +1215,8 @@ system_info_all_free(Sys_Info *info)
 
    for (i = 0; i < info->power.battery_count; i++)
      {
-        if (info->power.battery_names[i])
-          free(info->power.battery_names[i]);
+        if (info->power.batteries[i]->name)
+          free(info->power.batteries[i]->name);
         free(info->power.batteries[i]);
      }
    if (info->power.batteries)
diff --git a/src/bin/system/machine.h b/src/bin/system/machine.h
index aa88b0d..a518d11 100644
--- a/src/bin/system/machine.h
+++ b/src/bin/system/machine.h
@@ -33,10 +33,14 @@ typedef struct
 
 typedef struct
 {
+   char   *name;
    double  charge_full;
    double  charge_current;
    uint8_t percent;
    bool    present;
+#if defined(__OpenBSD__)
+   int    *mibs;
+#endif
 } bat_t;
 
 typedef struct
@@ -45,9 +49,6 @@ typedef struct
    int      battery_count;
 
    bat_t  **batteries;
-#define MAX_BATTERIES 10
-   char    *battery_names[MAX_BATTERIES];
-   int     *bat_mibs[MAX_BATTERIES];
    int      ac_mibs[5];
 } power_t;
 
diff --git a/src/bin/ui/ui_misc.c b/src/bin/ui/ui_misc.c
index 323a457..eb7e244 100644
--- a/src/bin/ui/ui_misc.c
+++ b/src/bin/ui/ui_misc.c
@@ -34,7 +34,7 @@ _battery_usage_add(Evas_Object *box, power_t *power)
           fmt = _("<bigger>%s</bigger>");
 
         elm_object_text_set(label, eina_slstr_printf(fmt,
-                        power->battery_names[i]));
+                        power->batteries[i]->name));
         hbox = elm_box_add(box);
         evas_object_size_hint_align_set(hbox, FILL, FILL);
         evas_object_size_hint_weight_set(hbox, EXPAND, EXPAND);

-- 


Reply via email to