]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_temperature.c
Add support for cpu temperature on NetBSD (using envsys(4))
[i3/i3status] / src / print_cpu_temperature.c
1 // vim:ts=8:expandtab
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
9 #include "i3status.h"
10
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
12 #include <err.h>
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
15 #define TZ_ZEROC 2732
16 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
17 #define TZ_AVG(x) ((x) - TZ_ZEROC) / 10
18 #endif
19
20 #if defined(__OpenBSD__)
21 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <sys/sysctl.h>
24 #include <sys/sensors.h>
25 #include <errno.h>
26 #include <err.h>
27
28 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
29 #endif
30
31 #if defined(__NetBSD__)
32 #include <fcntl.h>
33 #include <prop/proplib.h>
34 #include <sys/envsys.h>
35
36 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
37 #endif
38
39
40 static char *thermal_zone;
41
42 /*
43  * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
44  * returns the temperature in degree celcius.
45  *
46  */
47 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
48         char *outwalk = buffer;
49 #ifdef THERMAL_ZONE
50         const char *walk;
51         bool colorful_output = false;
52
53         if (path == NULL)
54                 asprintf(&thermal_zone, THERMAL_ZONE, zone);
55         else
56                 asprintf(&thermal_zone, path, zone);
57         path = thermal_zone;
58
59         INSTANCE(path);
60
61         for (walk = format; *walk != '\0'; walk++) {
62                 if (*walk != '%') {
63                         *(outwalk++) = *walk;
64                         continue;
65                 }
66
67                 if (BEGINS_WITH(walk+1, "degrees")) {
68 #if defined(LINUX)
69                         static char buf[16];
70                         long int temp;
71                         if (!slurp(path, buf, sizeof(buf)))
72                                 goto error;
73                         temp = strtol(buf, NULL, 10);
74                         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
75                                 *(outwalk++) = '?';
76                         else {
77                                 if ((temp/1000) >= max_threshold) {
78                                         START_COLOR("color_bad");
79                                         colorful_output = true;
80                                 }
81                                 outwalk += sprintf(outwalk, "%ld", (temp/1000));
82                                 if (colorful_output) {
83                                         END_COLOR;
84                                         colorful_output = false;
85                                 }
86                         }
87 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
88                         int sysctl_rslt;
89                         size_t sysctl_size = sizeof(sysctl_rslt);
90                         if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
91                                 goto error;
92
93                         if (TZ_AVG(sysctl_rslt) >= max_threshold) {
94                                 START_COLOR("color_bad");
95                                 colorful_output = true;
96                         }
97                         outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
98                         if (colorful_output) {
99                                 END_COLOR;
100                                 colorful_output = false;
101                         }
102
103 #elif defined(__OpenBSD__)
104         struct sensordev sensordev;
105         struct sensor sensor;
106         size_t sdlen, slen;
107         int dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
108
109         sdlen = sizeof(sensordev);
110         slen = sizeof(sensor);
111
112         for (dev = 0; ; dev++) {
113                 mib[2] = dev;
114                 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
115                         if (errno == ENXIO)
116                                 continue;
117                         if (errno == ENOENT)
118                                 break;
119                         goto error;
120                 }
121                 /* 'path' is the node within the full path (defaults to acpitz0). */
122                 if (strncmp(sensordev.xname, path, strlen(path)) == 0) {
123                         mib[3] = SENSOR_TEMP;
124                         /* Limit to temo0, but should retrieve from a full path... */
125                         for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
126                                 mib[4] = numt;
127                                 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
128                                         if (errno != ENOENT) {
129                                                 warn("sysctl");
130                                                 continue;
131                                         }
132                                 }
133                                 if ((int)MUKTOC(sensor.value) >= max_threshold) {
134                                         START_COLOR("color_bad");
135                                         colorful_output = true;
136                                 }
137
138                                 outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value));
139
140                                 if (colorful_output) {
141                                         END_COLOR;
142                                         colorful_output = false;
143                                 }
144                         }
145                 }
146         }
147 #elif defined(__NetBSD__)
148         int fd, rval;
149         bool err = false;
150         prop_dictionary_t dict;
151         prop_array_t array;
152         prop_object_iterator_t iter;
153         prop_object_iterator_t iter2;
154         prop_object_t obj, obj2, obj3;
155
156         fd = open("/dev/sysmon", O_RDONLY);
157         if (fd == -1)
158                 goto error;
159
160         rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
161         if (rval == -1) {
162             err = true;
163             goto error_netbsd1;
164         }
165
166         /* No drivers registered? */
167         if (prop_dictionary_count(dict) == 0) {
168             err = true;
169             goto error_netbsd2;
170         }
171
172         /* print sensors for all devices registered */
173         iter = prop_dictionary_iterator(dict);
174         if (iter == NULL) {
175             err = true;
176             goto error_netbsd2;
177         }
178
179         /* iterate over the dictionary returned by the kernel */
180         while ((obj = prop_object_iterator_next(iter)) != NULL) {
181                 array = prop_dictionary_get_keysym(dict, obj);
182                 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
183                     err = true;
184                     goto error_netbsd3;
185                 }
186                 iter2 = prop_array_iterator(array);
187                 if (!iter2) {
188                     err = true;
189                     goto error_netbsd3;
190                 }
191
192                 /* iterate over the array of dictionaries */
193                 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
194                         obj3 = prop_dictionary_get(obj2, "description");
195                         if (obj3 &&
196                             strcmp(path, prop_string_cstring_nocopy(obj3)) == 0)
197                         {
198                                 obj3 = prop_dictionary_get(obj2, "cur-value");
199                                 float temp = MUKTOC(prop_number_integer_value(obj3));
200                                 if ((int)temp >= max_threshold) {
201                                         START_COLOR("color_bad");
202                                         colorful_output = true;
203                                 }
204
205                                 outwalk += sprintf(outwalk, "%.2f", temp);
206
207                                 if (colorful_output) {
208                                         END_COLOR;
209                                         colorful_output = false;
210                                 }
211                                 break;
212                         }
213
214                 }
215                 prop_object_iterator_release(iter2);
216         }
217 error_netbsd3:
218         prop_object_iterator_release(iter);
219 error_netbsd2:
220         prop_object_release(dict);
221 error_netbsd1:
222         close(fd);
223         if (err) goto error;
224
225 #endif
226
227
228                         walk += strlen("degrees");
229                 }
230         }
231         OUTPUT_FULL_TEXT(buffer);
232         return;
233 error:
234 #endif
235         OUTPUT_FULL_TEXT("cant read temp");
236         (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);
237 }