netstar pushed a commit to branch master.

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

commit 9fc5b94d6a8185b12fe9aaaa2b10065af7bfa478
Author: Alastair Poole <nets...@gmail.com>
Date:   Mon Oct 5 12:18:26 2020 +0100

    sensors: Use hwmon interface for thermal.
    
    Should work...
---
 src/bin/system/machine.c             |  2 +
 src/bin/system/machine.h             |  1 +
 src/bin/system/machine/sensors.bogox | 90 +++++++++++++++++++++++++-----------
 src/bin/ui/ui_misc.c                 | 11 ++++-
 4 files changed, 75 insertions(+), 29 deletions(-)

diff --git a/src/bin/system/machine.c b/src/bin/system/machine.c
index 33e3559..79bb89a 100644
--- a/src/bin/system/machine.c
+++ b/src/bin/system/machine.c
@@ -84,6 +84,8 @@ system_info_all_free(Sys_Info *info)
         snsr = info->sensors[i];
         if (snsr->name)
           free(snsr->name);
+        if (snsr->child_name)
+          free(snsr->child_name);
         free(snsr);
      }
    if (info->sensors)
diff --git a/src/bin/system/machine.h b/src/bin/system/machine.h
index 9e010f1..d6a12f7 100644
--- a/src/bin/system/machine.h
+++ b/src/bin/system/machine.h
@@ -33,6 +33,7 @@ typedef struct
 typedef struct
 {
    char   *name;
+   char   *child_name;
    double  value;
    bool    invalid;
 } sensor_t;
diff --git a/src/bin/system/machine/sensors.bogox 
b/src/bin/system/machine/sensors.bogox
index 6fab1d8..fc2b666 100644
--- a/src/bin/system/machine/sensors.bogox
+++ b/src/bin/system/machine/sensors.bogox
@@ -70,44 +70,78 @@ system_sensors_thermal_get(int *sensor_count)
      }
 #elif defined(__linux__)
    sensor_t *sensor;
-   char *type, *value;
-   struct dirent **names;
-   int i, n;
-
-   n = scandir("/sys/class/thermal", &names, 0, alphasort);
-   if (n < 0) return NULL;
+   DIR *dir;
+   struct dirent *dh;
+   struct dirent **names = NULL;
+   char *link;
+   char buf[4096];
+   int seen[128];
+   int n, idx;
 
-   for (i = 0; i < n; i++) {
-        if (strncmp(names[i]->d_name, "thermal_zone", 12))
-          {
-             free(names[i]);
-             continue;
-          }
+   dir = opendir("/sys/class/hwmon");
+   if (!dir) return NULL;
 
-        type = file_contents(strsli_printf("/sys/class/thermal/%s/type", 
names[i]->d_name));
-        if (type)
-          {
-             sensors =
-                realloc(sensors, (1 + (*sensor_count)) * sizeof(sensor_t *));
-             sensors[(*sensor_count)++] =
-                 sensor = calloc(1, sizeof(sensor_t));
+   while ((dh = readdir(dir)) != NULL)
+     {
+        snprintf(buf, sizeof(buf), "/sys/class/hwmon/%s", dh->d_name);
+        link = realpath(buf, NULL);
+        if (!link) continue;
 
-             sensor->name = type;
+        idx = 0;
+        memset(&seen, 0, sizeof(seen));
+        n = scandir(link, &names, 0, alphasort);
+        if (n < 0) continue;
 
-             value = file_contents(strsli_printf("/sys/class/thermal/%s/temp", 
names[i]->d_name));
-             if (!value)
-               sensor->invalid = true;
-             else
+        for (int i = 0; i < n; i++)
+          {
+             if (!strncmp(names[i]->d_name, "temp", 4))
                {
-                  sensor->value = (float)atoi(value) / 1000.0;
-                  free(value);
+                  int id = atoi(names[i]->d_name + 4);
+                  if ((!id) || (id > sizeof(seen)))
+                    {
+                       free(names[i]);
+                       continue;
+                    }
+
+                  int found = 0;
+
+                  for (int j = 0; seen[j] != 0 && j < sizeof(seen); j++)
+                     if (seen[j] == id) found = 1;
+
+                  if (found)
+                    {
+                       free(names[i]);
+                       continue;
+                    }
+
+                  sensors = realloc(sensors, (1 + (*sensor_count)) * 
sizeof(sensor_t *));
+                  sensors[(*sensor_count)++] = sensor = 
calloc(1,sizeof(sensor_t));
+
+                  snprintf(buf, sizeof(buf), "%s/name", link);
+                  sensor->name = file_contents(buf);
+
+                  snprintf(buf, sizeof(buf), "%s/temp%d_label", link, id);
+                  sensor->child_name = file_contents(buf);
+                  snprintf(buf, sizeof(buf), "%s/temp%d_input", link, id);
+
+                  char *d = file_contents(buf);
+                  if (d)
+                    {
+                       sensor->value = atoi(d);
+                       if (sensor->value) sensor->value /= 1000;
+                       free(d);
+                    }
+                  seen[idx++] = id;
                }
+             free(names[i]);
           }
 
-        free(names[i]);
+        free(names);
+        free(link);
      }
 
-   free(names);
+   closedir(dir);
+
 #elif defined(__MacOS__)
 #endif
    return sensors;
diff --git a/src/bin/ui/ui_misc.c b/src/bin/ui/ui_misc.c
index c8dedb1..9ef6681 100644
--- a/src/bin/ui/ui_misc.c
+++ b/src/bin/ui/ui_misc.c
@@ -71,6 +71,7 @@ static Eina_Bool
 _sensor_usage_add(Evas_Object *box, sensor_t **sensors, int count)
 {
    Evas_Object *frame, *vbox, *hbox, *pb, *ic, *label;
+   Eina_Strbuf *name;
    sensor_t *snsr;
 
    for (int i = 0; i < count; i++)
@@ -94,8 +95,14 @@ _sensor_usage_add(Evas_Object *box, sensor_t **sensors, int 
count)
         evas_object_show(label);
         elm_box_pack_end(vbox, label);
 
+        name = eina_strbuf_new();
+        eina_strbuf_append(name, snsr->name);
+        if (snsr->child_name)
+          eina_strbuf_append_printf(name, " (%s)", snsr->child_name);
+
         elm_object_text_set(label, eina_slstr_printf("%s",
-                        snsr->name));
+                        eina_strbuf_string_get(name)));
+        eina_strbuf_free(name);
 
         hbox = elm_box_add(box);
         evas_object_size_hint_align_set(hbox, FILL, FILL);
@@ -170,6 +177,8 @@ _misc_free(power_t *power, sensor_t **sensors, int 
sensor_count)
         sensor_t *snsr = sensors[i];
         if (snsr->name)
           free(snsr->name);
+        if (snsr->child_name)
+          free(snsr->child_name);
         free(snsr);
      }
    if (sensors)

-- 


Reply via email to