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