i3 - improved tiling WM


i3status: Bugfix: Reading multiple temperature sensors not possible

Patch status: merged

Patch by Marco Hunsicker

Long description:

This patch inlines the creation of the thermal zone string in order
to force computation on each invocation. This is necessary to be able
to read the values of several temperature sensors.

To apply this patch, use:
curl http://cr.i3wm.org/patch/434/raw.patch | git am

b/src/print_cpu_temperature.c

17
@@ -37,11 +37,9 @@
18
 #endif
19
 
20
 
21
-static char *thermal_zone;
22
-
23
 /*
24
- * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
25
- * returns the temperature in degree celcius.
26
+ * Reads the CPU temperature from /sys/class/thermal/thermal_zone%d/temp (or
27
+ * the user provided path) and returns the temperature in degree celcius.
28
  *
29
  */
30
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
31
@@ -49,16 +47,14 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
32
 #ifdef THERMAL_ZONE
33
         const char *walk;
34
         bool colorful_output = false;
35
+        char *thermal_zone;
36
 
37
-        if (thermal_zone == NULL) {
38
-                if (path == NULL)
39
-                        asprintf(&thermal_zone, THERMAL_ZONE, zone);
40
-                else
41
-                        asprintf(&thermal_zone, path, zone);
42
-        }
43
-        path = thermal_zone;
44
+        if (path == NULL)
45
+                asprintf(&thermal_zone, THERMAL_ZONE, zone);
46
+        else
47
+                asprintf(&thermal_zone, path, zone);
48
 
49
-        INSTANCE(path);
50
+        INSTANCE(thermal_zone);
51
 
52
         for (walk = format; *walk != '\0'; walk++) {
53
                 if (*walk != '%') {
54
@@ -70,7 +66,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
55
 #if defined(LINUX)
56
                         static char buf[16];
57
                         long int temp;
58
-                        if (!slurp(path, buf, sizeof(buf)))
59
+                        if (!slurp(thermal_zone, buf, sizeof(buf)))
60
                                 goto error;
61
                         temp = strtol(buf, NULL, 10);
62
                         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
63
@@ -89,7 +85,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
64
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
65
                         int sysctl_rslt;
66
                         size_t sysctl_size = sizeof(sysctl_rslt);
67
-                        if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
68
+                        if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0))
69
                                 goto error;
70
 
71
                         if (TZ_AVG(sysctl_rslt) >= max_threshold) {
72
@@ -121,7 +117,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
73
                         goto error;
74
                 }
75
                 /* 'path' is the node within the full path (defaults to acpitz0). */
76
-                if (strncmp(sensordev.xname, path, strlen(path)) == 0) {
77
+                if (strncmp(sensordev.xname, thermal_zone, strlen(thermal_zone)) == 0) {
78
                         mib[3] = SENSOR_TEMP;
79
                         /* Limit to temo0, but should retrieve from a full path... */
80
                         for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
81
@@ -195,7 +191,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
82
                 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
83
                         obj3 = prop_dictionary_get(obj2, "description");
84
                         if (obj3 &&
85
-                            strcmp(path, prop_string_cstring_nocopy(obj3)) == 0)
86
+                            strcmp(thermal_zone, prop_string_cstring_nocopy(obj3)) == 0)
87
                         {
88
                                 obj3 = prop_dictionary_get(obj2, "cur-value");
89
                                 float temp = MUKTOC(prop_number_integer_value(obj3));
90
@@ -230,10 +226,15 @@ error_netbsd1:
91
                         walk += strlen("degrees");
92
                 }
93
         }
94
+
95
+        free(thermal_zone);
96
+
97
         OUTPUT_FULL_TEXT(buffer);
98
         return;
99
 error:
100
 #endif
101
+        free(thermal_zone);
102
+
103
         OUTPUT_FULL_TEXT("cant read temp");
104
         (void)fputs("i3status: Cannot read temperature. Verify that you have a thermal zone in /sys/class/thermal or disable the cpu_temperature module in your i3status config.\n", stderr);
105
 }