raster pushed a commit to branch master.

http://git.enlightenment.org/core/enlightenment.git/commit/?id=8421011f789152751c351a3e351ffd42a1a0c512

commit 8421011f789152751c351a3e351ffd42a1a0c512
Author: Carsten Haitzler (Rasterman) <ras...@rasterman.com>
Date:   Thu May 20 14:50:47 2021 +0100

    temp - split bsd and linux code to make it easier to read
---
 src/modules/temperature/e_mod_tempget.c     | 239 +-------------------------
 src/modules/temperature/e_mod_tempget_bsd.c | 254 ++++++++++++++++++++++++++++
 src/modules/temperature/meson.build         |   1 +
 3 files changed, 260 insertions(+), 234 deletions(-)

diff --git a/src/modules/temperature/e_mod_tempget.c 
b/src/modules/temperature/e_mod_tempget.c
index 2d87e67a9..15f530742 100644
--- a/src/modules/temperature/e_mod_tempget.c
+++ b/src/modules/temperature/e_mod_tempget.c
@@ -1,16 +1,7 @@
 #include "e.h"
 #include "e_mod_main.h"
 
-#if defined (__FreeBSD__) || defined(__DragonFly__)
-# include <sys/types.h>
-# include <sys/sysctl.h>
-# include <errno.h>
-#elif defined (__OpenBSD__)
-# include <sys/param.h>
-# include <sys/sysctl.h>
-# include <sys/sensors.h>
-# include <errno.h>
-# include <err.h>
+#if defined (__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
 #else
 // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
 typedef enum
@@ -589,155 +580,6 @@ _hwmon_update(void)
      }
    eina_lock_release(&mons_lock);
 }
-#endif
-
-#if defined(__OpenBSD__)
-
-static Eina_Lock mons_lock;
-static Eina_List *mons = NULL;
-
-typedef struct
-{
-   const char *name;
-   const char *label;
-   double      temp;
-   int         mib[5];
-} Temp;
-
-static void
-_sysctl_init(void)
-{
-   int dev, numt;
-   struct sensordev snsrdev;
-   struct sensor snsr;
-   size_t sdlen = sizeof(snsrdev);
-   size_t slen = sizeof(snsr);
-   int mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
-
-   for (dev = 0;; dev++)
-     {
-        mib[2] = dev;
-        if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1)
-          {
-             if (errno == ENOENT) /* no further sensors */
-               break;
-             else
-               continue;
-          }
-        for (numt = 0; numt < snsrdev.maxnumt[SENSOR_TEMP]; numt++)
-          {
-             mib[4] = numt;
-             slen = sizeof(snsr);
-             if (sysctl(mib, 5, &snsr, &slen, NULL, 0) == -1)
-               continue;
-             if (slen > 0 && (snsr.flags & SENSOR_FINVALID) == 0)
-               break;
-          }
-        slen = sizeof(snsr);
-        if (sysctl(mib, 5, &snsr, &slen, NULL, 0) == -1)
-          continue;
-        if (snsr.type != SENSOR_TEMP) continue;
-
-        Temp *temp = malloc(sizeof(Temp));
-        temp->name = eina_stringshare_add(snsrdev.xname);
-        temp->label = eina_stringshare_add(snsrdev.xname);
-        memcpy(temp->mib, &mib, sizeof(mib));
-        mons = eina_list_append(mons, temp);
-     }
-}
-
-static void
-_sysctl_update(void)
-{
-   Eina_List *l;
-   Temp *temp;
-   struct sensor snsr;
-   size_t slen = sizeof(struct sensor);
-
-   eina_lock_take(&mons_lock);
-   EINA_LIST_FOREACH(mons, l, temp)
-     {
-        if (sysctl(temp->mib, 5, &snsr, &slen, NULL, 0) != -1)
-          temp->temp = (snsr.value - 273150000) / 1000000.0;
-     }
-   eina_lock_release(&mons_lock);
-}
-
-#elif defined(__FreeBSD__) || defined(__DragonFly__)
-
-static Eina_Lock mons_lock;
-static Eina_List *mons = NULL;
-
-static const char *sources[] =
-{
-   "hw.acpi.thermal.tz0.temperature",
-   "dev.aibs.0.temp.0",
-   "dev.lm75.0.temperature",
-   NULL
-};
-
-typedef struct
-{
-   const char  *name;
-   const char  *label;
-   double       temp;
-   int          mib[CTL_MAXNAME];
-   unsigned int miblen;
-} Temp;
-
-static void
-_sysctl_init(void)
-{
-   size_t len;
-   int mib[CTL_MAXNAME];
-   char buf[128];
-
-   for (int i = 0; sources[i] != NULL; i++)
-     {
-        len = 4;
-        if (sysctlnametomib(sources[i], mib, &len) != -1)
-          {
-             Temp *temp = malloc(sizeof(Temp));
-             temp->name = eina_stringshare_add(sources[i]);
-             temp->label = eina_stringshare_add(sources[i]);
-             memcpy(temp->mib, &mib, sizeof(mib));
-             temp->miblen = len;
-             mons = eina_list_append(mons, temp);
-         }
-     }
-   for (int i = 0; i < 256; i++)
-     {
-        len = 4;
-        snprintf(buf, sizeof(buf), "dev.cpu.%i.temperature", i);
-        if (sysctlnametomib(buf, mib, &len) == -1) break;
-
-        Temp *temp = malloc(sizeof(Temp));
-        temp->name = eina_stringshare_add(buf);
-        temp->label = eina_stringshare_add(buf);
-        memcpy(temp->mib, &mib, sizeof(mib));
-        temp->miblen = len;
-        mons = eina_list_append(mons, temp);
-    }
-}
-
-static void
-_sysctl_update(void)
-{
-   Eina_List *l;
-   Temp *temp;
-   int val;
-   size_t len = sizeof(val);
-
-   eina_lock_take(&mons_lock);
-   EINA_LIST_FOREACH(mons, l, temp)
-     {
-        if (sysctl(temp->mib, temp->miblen, &val, &len, NULL, 0) != -1)
-          temp->temp = (val - 2732) / 10;
-     }
-   eina_lock_release(&mons_lock);
-}
-
-#endif
 
 static void
 init(Tempthread *tth)
@@ -748,21 +590,6 @@ init(Tempthread *tth)
    if (((!tth->sensor_name) || (tth->sensor_name[0] == 0)))
      {
         eina_stringshare_replace(&(tth->sensor_name), NULL);
-#if defined (__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
-        if (!tth->sensor_name)
-          {
-             Eina_List *l;
-             Temp *temp;
-
-             eina_lock_take(&mons_lock);
-             EINA_LIST_FOREACH(mons, l, temp)
-               {
-                  tth->sensor_name = eina_stringshare_add(temp->name);
-                  break;
-               }
-             eina_lock_release(&mons_lock);
-          }
-#else
         if (!tth->sensor_name)
           {
              Eina_List *l, *ll;
@@ -780,35 +607,15 @@ init(Tempthread *tth)
                }
              eina_lock_release(&mons_lock);
           }
-#endif
      }
+   // we couldn't find the sensor - so init failed - try again next time
+   if (((!tth->sensor_name) || (tth->sensor_name[0] == 0)))
+     tth->initted = EINA_FALSE;
 }
 
 static int
 check(Tempthread *tth)
 {
-#if defined (__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
-   Eina_List *l;
-   Temp *temp;
-
-   _sysctl_update();
-
-   if (!tth->sensor_name) return -999;
-
-   double t = 0.0;
-
-   eina_lock_take(&mons_lock);
-   EINA_LIST_FOREACH(mons, l, temp)
-     {
-        if (!strcmp(tth->sensor_name, temp->name))
-          {
-             t = temp->temp;
-             break;
-          }
-     }
-   eina_lock_release(&mons_lock);
-   return t;
-#else
    Eina_List *l, *ll;
    Mon *mon;
    Temp *temp;
@@ -833,7 +640,6 @@ check(Tempthread *tth)
         eina_lock_release(&mons_lock);
         return t;
      }
-#endif
    return -999;
 }
 
@@ -847,20 +653,6 @@ temperature_tempget_get(Tempthread *tth)
 Eina_List *
 temperature_tempget_sensor_list(void)
 {
-#if defined (__FreeBSD__) || defined(__DragonFly__) || defined (__OpenBSD__)
-   Eina_List *sensors = NULL, *l;
-   Sensor *sen;
-   Temp *temp;
-
-   EINA_LIST_FOREACH(mons, l, temp)
-     {
-        sen = calloc(1, sizeof(Sensor));
-        sen->name = eina_stringshare_add(temp->name);
-        sen->label = eina_stringshare_add(temp->label);
-        sensors = eina_list_append(sensors, sen);
-     }
-   return sensors;
-#else
    Eina_List *sensors = NULL, *l, *ll;
    Sensor *sen;
    Mon *mon;
@@ -881,39 +673,18 @@ temperature_tempget_sensor_list(void)
           }
      }
    return sensors;
-#endif
 }
 
 void
 temperature_tempget_setup(void)
 {
-#if defined (__FreeBSD__) || defined(__DragonFly__) || defined (__OpenBSD__)
-   eina_lock_new(&mons_lock);
-   _sysctl_init();
-#else
    eina_lock_new(&mons_lock);
    _hwmon_init();
-#endif
 }
 
 void
 temperature_tempget_clear(void)
 {
-#if defined (__FreeBSD__) || defined(__DragonFly__) || defined (__OpenBSD__)
-   Temp *temp;
-   eina_lock_take(&mons_lock);
-   EINA_LIST_FREE(mons, temp)
-     {
-        eina_stringshare_replace(&(temp->name), NULL);
-        eina_stringshare_replace(&(temp->label), NULL);
-        free(temp);
-     }
-   eina_lock_release(&mons_lock);
-   // extra lock take to cover race cond for thread
-   eina_lock_take(&mons_lock);
-   eina_lock_release(&mons_lock);
-   eina_lock_free(&mons_lock);
-#else
    Mon *mon;
    Temp *temp;
    Fan *fan;
@@ -1003,5 +774,5 @@ temperature_tempget_clear(void)
    eina_lock_take(&mons_lock);
    eina_lock_release(&mons_lock);
    eina_lock_free(&mons_lock);
-#endif
 }
+#endif
diff --git a/src/modules/temperature/e_mod_tempget_bsd.c 
b/src/modules/temperature/e_mod_tempget_bsd.c
new file mode 100644
index 000000000..409847d97
--- /dev/null
+++ b/src/modules/temperature/e_mod_tempget_bsd.c
@@ -0,0 +1,254 @@
+#include "e.h"
+#include "e_mod_main.h"
+
+#if defined (__FreeBSD__) || defined(__DragonFly__) || defined (__OpenBSD__)
+static Eina_Lock mons_lock;
+static Eina_List *mons = NULL;
+#endif
+
+#if defined (__FreeBSD__) || defined(__DragonFly__)
+# include <sys/types.h>
+# include <sys/sysctl.h>
+# include <errno.h>
+static const char *sources[] =
+{
+   "hw.acpi.thermal.tz0.temperature",
+   "dev.aibs.0.temp.0",
+   "dev.lm75.0.temperature",
+   NULL
+};
+
+typedef struct
+{
+   const char  *name;
+   const char  *label;
+   double       temp;
+   int          mib[CTL_MAXNAME];
+   unsigned int miblen;
+} Temp;
+
+static void
+_sysctl_init(void)
+{
+   size_t len;
+   int mib[CTL_MAXNAME];
+   char buf[128];
+
+   for (int i = 0; sources[i] != NULL; i++)
+     {
+        len = 4;
+        if (sysctlnametomib(sources[i], mib, &len) != -1)
+          {
+             Temp *temp = malloc(sizeof(Temp));
+             temp->name = eina_stringshare_add(sources[i]);
+             temp->label = eina_stringshare_add(sources[i]);
+             memcpy(temp->mib, &mib, sizeof(mib));
+             temp->miblen = len;
+             mons = eina_list_append(mons, temp);
+         }
+     }
+   for (int i = 0; i < 256; i++)
+     {
+        len = 4;
+        snprintf(buf, sizeof(buf), "dev.cpu.%i.temperature", i);
+        if (sysctlnametomib(buf, mib, &len) == -1) break;
+
+        Temp *temp = malloc(sizeof(Temp));
+        temp->name = eina_stringshare_add(buf);
+        temp->label = eina_stringshare_add(buf);
+        memcpy(temp->mib, &mib, sizeof(mib));
+        temp->miblen = len;
+        mons = eina_list_append(mons, temp);
+    }
+}
+
+static void
+_sysctl_update(void)
+{
+   Eina_List *l;
+   Temp *temp;
+   int val;
+   size_t len = sizeof(val);
+
+   eina_lock_take(&mons_lock);
+   EINA_LIST_FOREACH(mons, l, temp)
+     {
+        if (sysctl(temp->mib, temp->miblen, &val, &len, NULL, 0) != -1)
+          temp->temp = (val - 2732) / 10;
+     }
+   eina_lock_release(&mons_lock);
+}
+
+#elif defined (__OpenBSD__)
+# include <sys/param.h>
+# include <sys/sysctl.h>
+# include <sys/sensors.h>
+# include <errno.h>
+# include <err.h>
+typedef struct
+{
+   const char *name;
+   const char *label;
+   double      temp;
+   int         mib[5];
+} Temp;
+
+static void
+_sysctl_init(void)
+{
+   int dev, numt;
+   struct sensordev snsrdev;
+   struct sensor snsr;
+   size_t sdlen = sizeof(snsrdev);
+   size_t slen = sizeof(snsr);
+   int mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
+
+   for (dev = 0;; dev++)
+     {
+        mib[2] = dev;
+        if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1)
+          {
+             if (errno == ENOENT) /* no further sensors */
+               break;
+             else
+               continue;
+          }
+        for (numt = 0; numt < snsrdev.maxnumt[SENSOR_TEMP]; numt++)
+          {
+             mib[4] = numt;
+             slen = sizeof(snsr);
+             if (sysctl(mib, 5, &snsr, &slen, NULL, 0) == -1)
+               continue;
+             if (slen > 0 && (snsr.flags & SENSOR_FINVALID) == 0)
+               break;
+          }
+        slen = sizeof(snsr);
+        if (sysctl(mib, 5, &snsr, &slen, NULL, 0) == -1)
+          continue;
+        if (snsr.type != SENSOR_TEMP) continue;
+
+        Temp *temp = malloc(sizeof(Temp));
+        temp->name = eina_stringshare_add(snsrdev.xname);
+        temp->label = eina_stringshare_add(snsrdev.xname);
+        memcpy(temp->mib, &mib, sizeof(mib));
+        mons = eina_list_append(mons, temp);
+     }
+}
+
+static void
+_sysctl_update(void)
+{
+   Eina_List *l;
+   Temp *temp;
+   struct sensor snsr;
+   size_t slen = sizeof(struct sensor);
+
+   eina_lock_take(&mons_lock);
+   EINA_LIST_FOREACH(mons, l, temp)
+     {
+        if (sysctl(temp->mib, 5, &snsr, &slen, NULL, 0) != -1)
+          temp->temp = (snsr.value - 273150000) / 1000000.0;
+     }
+   eina_lock_release(&mons_lock);
+}
+#endif
+
+#if defined (__FreeBSD__) || defined(__DragonFly__) || defined (__OpenBSD__)
+static void
+init(Tempthread *tth)
+{
+   if (tth->initted) return;
+   tth->initted = EINA_TRUE;
+
+   if (((!tth->sensor_name) || (tth->sensor_name[0] == 0)))
+     {
+        eina_stringshare_replace(&(tth->sensor_name), NULL);
+        if (!tth->sensor_name)
+          {
+             Eina_List *l;
+             Temp *temp;
+
+             eina_lock_take(&mons_lock);
+             EINA_LIST_FOREACH(mons, l, temp)
+               {
+                  tth->sensor_name = eina_stringshare_add(temp->name);
+                  break;
+               }
+             eina_lock_release(&mons_lock);
+          }
+     }
+}
+
+static int
+check(Tempthread *tth)
+{
+   Eina_List *l;
+   Temp *temp;
+   double t = 0.0;
+
+   _sysctl_update();
+   if (!tth->sensor_name) return -999;
+
+   eina_lock_take(&mons_lock);
+   EINA_LIST_FOREACH(mons, l, temp)
+     {
+        if (!strcmp(tth->sensor_name, temp->name))
+          {
+             t = temp->temp;
+             break;
+          }
+     }
+   eina_lock_release(&mons_lock);
+   return t;
+}
+
+int
+temperature_tempget_get(Tempthread *tth)
+{
+   init(tth);
+   return check(tth);
+}
+
+Eina_List *
+temperature_tempget_sensor_list(void)
+{
+   Eina_List *sensors = NULL, *l;
+   Sensor *sen;
+   Temp *temp;
+
+   EINA_LIST_FOREACH(mons, l, temp)
+     {
+        sen = calloc(1, sizeof(Sensor));
+        sen->name = eina_stringshare_add(temp->name);
+        sen->label = eina_stringshare_add(temp->label);
+        sensors = eina_list_append(sensors, sen);
+     }
+   return sensors;
+}
+
+void
+temperature_tempget_setup(void)
+{
+   eina_lock_new(&mons_lock);
+   _sysctl_init();
+}
+
+void
+temperature_tempget_clear(void)
+{
+   Temp *temp;
+
+   eina_lock_take(&mons_lock);
+   EINA_LIST_FREE(mons, temp)
+     {
+        eina_stringshare_replace(&(temp->name), NULL);
+        eina_stringshare_replace(&(temp->label), NULL);
+        free(temp);
+     }
+   eina_lock_release(&mons_lock);
+   // extra lock take to cover race cond for thread
+   eina_lock_take(&mons_lock);
+   eina_lock_release(&mons_lock);
+   eina_lock_free(&mons_lock);
+}
+#endif
diff --git a/src/modules/temperature/meson.build 
b/src/modules/temperature/meson.build
index c41c0d3c7..9161f4fed 100644
--- a/src/modules/temperature/meson.build
+++ b/src/modules/temperature/meson.build
@@ -2,5 +2,6 @@ src = files(
   'e_mod_main.c',
   'e_mod_config.c',
   'e_mod_tempget.c',
+  'e_mod_tempget_bsd.c',
   'e_mod_main.h'
  )

-- 


Reply via email to