i3 - improved tiling WM


Add support for cpu temperature on NetBSD (using envsys(4))

Patch status: merged

Patch by Arnaud Degroote

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

b/Makefile

16
@@ -47,6 +47,11 @@ CFLAGS+=-I/usr/pkg/include/
17
 LDFLAGS+=-L/usr/pkg/lib/
18
 endif
19
 
20
+ifeq ($(OS), NetBSD)
21
+LIBS+= -lprop
22
+endif
23
+
24
+
25
 V ?= 0
26
 ifeq ($(V),0)
27
 # Don’t print command lines which are run

b/include/i3status.h

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

b/src/print_cpu_temperature.c

46
@@ -28,6 +28,15 @@
47
 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
48
 #endif
49
 
50
+#if defined(__NetBSD__)
51
+#include <fcntl.h>
52
+#include <prop/proplib.h>
53
+#include <sys/envsys.h>
54
+
55
+#define MUKTOC(v) ((v - 273150000) / 1000000.0)
56
+#endif
57
+
58
+
59
 static char *thermal_zone;
60
 
61
 /*
62
@@ -135,7 +144,88 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
63
                         }
64
                 }
65
         }
66
+#elif defined(__NetBSD__)
67
+        int fd, rval;
68
+        bool err = false;
69
+        prop_dictionary_t dict;
70
+        prop_array_t array;
71
+        prop_object_iterator_t iter;
72
+        prop_object_iterator_t iter2;
73
+        prop_object_t obj, obj2, obj3;
74
+
75
+        fd = open("/dev/sysmon", O_RDONLY);
76
+        if (fd == -1)
77
+                goto error;
78
+
79
+        rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
80
+        if (rval == -1) {
81
+            err = true;
82
+            goto error_netbsd1;
83
+        }
84
+
85
+        /* No drivers registered? */
86
+        if (prop_dictionary_count(dict) == 0) {
87
+            err = true;
88
+            goto error_netbsd2;
89
+        }
90
+
91
+        /* print sensors for all devices registered */
92
+        iter = prop_dictionary_iterator(dict);
93
+        if (iter == NULL) {
94
+            err = true;
95
+            goto error_netbsd2;
96
+        }
97
+
98
+        /* iterate over the dictionary returned by the kernel */
99
+        while ((obj = prop_object_iterator_next(iter)) != NULL) {
100
+                array = prop_dictionary_get_keysym(dict, obj);
101
+                if (prop_object_type(array) != PROP_TYPE_ARRAY) {
102
+                    err = true;
103
+                    goto error_netbsd3;
104
+                }
105
+                iter2 = prop_array_iterator(array);
106
+                if (!iter2) {
107
+                    err = true;
108
+                    goto error_netbsd3;
109
+                }
110
+
111
+                /* iterate over the array of dictionaries */
112
+                while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
113
+                        obj3 = prop_dictionary_get(obj2, "description");
114
+                        if (obj3 &&
115
+                            strcmp(path, prop_string_cstring_nocopy(obj3)) == 0) 
116
+                                                
117
+                        {
118
+                                obj3 = prop_dictionary_get(obj2, "cur-value");
119
+                                float temp = MUKTOC(prop_number_integer_value(obj3));
120
+                                if ((int)temp >= max_threshold) {
121
+                                        START_COLOR("color_bad");
122
+                                        colorful_output = true;
123
+                                }
124
+
125
+                                outwalk += sprintf(outwalk, "%.2f", temp);
126
+
127
+                                if (colorful_output) {
128
+                                        END_COLOR;
129
+                                        colorful_output = false;
130
+                                }
131
+                                break;
132
+                        }
133
+
134
+                }
135
+                prop_object_iterator_release(iter2);
136
+        }
137
+error_netbsd3:
138
+        prop_object_iterator_release(iter);
139
+error_netbsd2:
140
+	prop_object_release(dict);
141
+error_netbsd1:
142
+        close(fd);
143
+        if (err) goto error;
144
+
145
 #endif
146
+
147
+
148
                         walk += strlen("degrees");
149
                 }
150
         }