i3 - improved tiling WM


Fix NetBSD CPU temp gauge bug

Patch status: merged

Patch by Alexander Vasarab

Long description:

This patch fixes a bug in which multiple (conflicting) CPU temps may be
included in the output for the "cpu temperature" module.

The bug is due to the way that the code parsed the envsys(4)-returned data,
and would manifest itself on x86-based NetBSD machines, since those use
cputemp(4) as well as acpitz(4), thereby creating multiple envsys(4) entries
with identical descriptions but which refer to different physical sensors.

Instead of matching the description attribute of each device returned by
envsys(4) against the target format, this patch throws away non-matching keys
in the first instruction inside the dict walk. This has the benefit of sparing
unnecessary CPU cycles, and preventing other sensors from being included
erroneously.

Additionally, the THERMAL_ZONE format is now joined with OpenBSD in that it
uses acpitz(4) explicitly. This is prefered since it is much older (dating
back to NetBSD 2.0), and does not exclude x86-based users (as with cputemp(4)).

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

b/include/i3status.h

31
@@ -30,12 +30,9 @@ enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
32
 #define BATT_TIME "hw.acpi.battery.time"
33
 #define BATT_STATE "hw.acpi.battery.state"
34
 
35
-#elif defined(__OpenBSD__)
36
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
37
 /* Default to acpitz(4) if no path is set. */
38
 #define THERMAL_ZONE "acpitz%d"
39
-#elif defined(__NetBSD__)
40
-/* Rely on envsys(4). The key of the sensor is generally cpu%d temperature */
41
-#define THERMAL_ZONE "cpu%d temperature"
42
 #endif
43
 
44
 #if defined(__FreeBSD_kernel__) && defined(__GLIBC__)

b/src/print_cpu_temperature.c

49
@@ -167,7 +167,6 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
50
             goto error_netbsd2;
51
         }
52
 
53
-        /* print sensors for all devices registered */
54
         iter = prop_dictionary_iterator(dict);
55
         if (iter == NULL) {
56
             err = true;
57
@@ -176,39 +175,43 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
58
 
59
         /* iterate over the dictionary returned by the kernel */
60
         while ((obj = prop_object_iterator_next(iter)) != NULL) {
61
+                /* skip this dict if it's not what we're looking for */
62
+                if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) != strlen(thermal_zone)) ||
63
+                    (strncmp(thermal_zone,
64
+                             prop_dictionary_keysym_cstring_nocopy(obj),
65
+                             strlen(thermal_zone)) != 0))
66
+                        continue;
67
+
68
                 array = prop_dictionary_get_keysym(dict, obj);
69
                 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
70
                     err = true;
71
                     goto error_netbsd3;
72
                 }
73
+
74
                 iter2 = prop_array_iterator(array);
75
                 if (!iter2) {
76
                     err = true;
77
                     goto error_netbsd3;
78
                 }
79
 
80
-                /* iterate over the array of dictionaries */
81
+                /* iterate over array of dicts specific to target sensor */
82
                 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
83
-                        obj3 = prop_dictionary_get(obj2, "description");
84
-                        if (obj3 &&
85
-                            strcmp(thermal_zone, prop_string_cstring_nocopy(obj3)) == 0)
86
-                        {
87
-                                obj3 = prop_dictionary_get(obj2, "cur-value");
88
-                                float temp = MUKTOC(prop_number_integer_value(obj3));
89
-                                if ((int)temp >= max_threshold) {
90
-                                        START_COLOR("color_bad");
91
-                                        colorful_output = true;
92
-                                }
93
+                        obj3 = prop_dictionary_get(obj2, "cur-value");
94
+
95
+                        float temp = MUKTOC(prop_number_integer_value(obj3));
96
+                        if ((int)temp >= max_threshold) {
97
+                                START_COLOR("color_bad");
98
+                                colorful_output = true;
99
+                        }
100
 
101
-                                outwalk += sprintf(outwalk, "%.2f", temp);
102
+                        outwalk += sprintf(outwalk, "%.2f", temp);
103
 
104
-                                if (colorful_output) {
105
-                                        END_COLOR;
106
-                                        colorful_output = false;
107
-                                }
108
-                                break;
109
+                        if (colorful_output) {
110
+                                END_COLOR;
111
+                                colorful_output = false;
112
                         }
113
 
114
+                        break;
115
                 }
116
                 prop_object_iterator_release(iter2);
117
         }