]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_temperature.c
e3340f6e66c1c8f775c11b8ab435ca87855a83ab
[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__)
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 #endif
18
19 #if defined(__OpenBSD__)
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23 #include <sys/sensors.h>
24 #include <errno.h>
25 #include <err.h>
26
27 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
28 #endif
29
30 static char *thermal_zone;
31
32 /*
33  * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
34  * returns the temperature in degree celcius.
35  *
36  */
37 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
38 #ifdef THERMAL_ZONE
39         const char *walk;
40         char *outwalk = buffer;
41         bool colorful_output;
42
43         if (path == NULL)
44                 asprintf(&thermal_zone, THERMAL_ZONE, zone);
45         else
46                 asprintf(&thermal_zone, path, zone);
47         path = thermal_zone;
48
49         INSTANCE(path);
50
51         for (walk = format; *walk != '\0'; walk++) {
52                 if (*walk != '%') {
53                         *(outwalk++) = *walk;
54                         continue;
55                 }
56
57                 if (BEGINS_WITH(walk+1, "degrees")) {
58 #if defined(LINUX)
59                         static char buf[16];
60                         long int temp;
61                         if (!slurp(path, buf, sizeof(buf)))
62                                 goto error;
63                         temp = strtol(buf, NULL, 10);
64                         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
65                                 *(outwalk++) = '?';
66                         else {
67                                 if ((temp/1000) >= max_threshold) {
68                                         START_COLOR("color_bad");
69                                         colorful_output = true;
70                                 }
71                                 outwalk += sprintf(outwalk, "%ld", (temp/1000));
72                                 if (colorful_output)
73                                         END_COLOR;
74                         }
75 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
76                         int sysctl_rslt;
77                         size_t sysctl_size = sizeof(sysctl_rslt);
78                         if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
79                                 goto error;
80
81                         outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
82 #elif defined(__OpenBSD__)
83         struct sensordev sensordev;
84         struct sensor sensor;
85         size_t sdlen, slen;
86         int dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
87
88         sdlen = sizeof(sensordev);
89         slen = sizeof(sensor);
90
91         for (dev = 0; ; dev++) {
92                 mib[2] = dev;
93                 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
94                         if (errno == ENXIO)
95                                 continue;
96                         if (errno == ENOENT)
97                                 break;
98                         goto error;
99                 }
100                 /* 'path' is the node within the full path (defaults to acpitz0). */
101                 if (strncmp(sensordev.xname, path, strlen(path)) == 0) {
102                         mib[3] = SENSOR_TEMP;
103                         /* Limit to temo0, but should retrieve from a full path... */
104                         for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
105                                 mib[4] = numt;
106                                 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
107                                         if (errno != ENOENT) {
108                                                 warn("sysctl");
109                                                 continue;
110                                         }
111                                 }
112                                 if ((int)MUKTOC(sensor.value) >= max_threshold) {
113                                         START_COLOR("color_bad");
114                                         colorful_output = true;
115                                 }
116
117                                 outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value));
118
119                                 if (colorful_output)
120                                         END_COLOR;
121                         }
122                 }
123         }
124 #endif
125                         walk += strlen("degrees");
126                 }
127         }
128         OUTPUT_FULL_TEXT(buffer);
129         return;
130 error:
131 #endif
132         OUTPUT_FULL_TEXT("cant read temp");
133         (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);
134 }