]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_temperature.c
7797f061dc9cacfa090784a0db592f896271876b
[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 #endif
27
28 static char *thermal_zone;
29
30 /*
31  * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
32  * returns the temperature in degree celcius.
33  *
34  */
35 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format) {
36 #ifdef THERMAL_ZONE
37         const char *walk;
38         char *outwalk = buffer;
39         static char buf[16];
40
41         if (path == NULL)
42                 asprintf(&thermal_zone, THERMAL_ZONE, zone);
43         else
44                 asprintf(&thermal_zone, path, zone);
45         path = thermal_zone;
46
47         INSTANCE(path);
48
49         for (walk = format; *walk != '\0'; walk++) {
50                 if (*walk != '%') {
51                         *(outwalk++) = *walk;
52                         continue;
53                 }
54
55                 if (BEGINS_WITH(walk+1, "degrees")) {
56 #if defined(LINUX)
57                         long int temp;
58                         if (!slurp(path, buf, sizeof(buf)))
59                                 goto error;
60                         temp = strtol(buf, NULL, 10);
61                         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
62                                 *(outwalk++) = '?';
63                         else
64                                 outwalk += sprintf(outwalk, "%ld", (temp/1000));
65 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66                         int sysctl_rslt;
67                         size_t sysctl_size = sizeof(sysctl_rslt);
68                         if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
69                                 goto error;
70
71                         outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
72 #elif defined(__OpenBSD__)
73         struct sensordev sensordev;
74         struct sensor sensor;
75         size_t sdlen, slen;
76         int dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
77
78         sdlen = sizeof(sensordev);
79         slen = sizeof(sensor);
80
81         for (dev = 0; ; dev++) {
82                 mib[2] = dev;
83                 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
84                         if (errno == ENXIO)
85                                 continue;
86                         if (errno == ENOENT)
87                                 break;
88                         goto error;
89                 }
90                 /*
91                  * 'path' is actually the node within the full path (eg, cpu0).
92                  * XXX: Extend the API to allow a string instead of just an int for path, this would
93                  * allow us to have a path of 'acpitz0' for example.
94                  */
95                 if (strncmp(sensordev.xname, path, strlen(path)) == 0) {
96                         mib[3] = SENSOR_TEMP;
97                         for (numt = 0; numt < sensordev.maxnumt[SENSOR_TEMP]; numt++) {
98                                 mib[4] = numt;
99                                 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
100                                         if (errno != ENOENT)
101                                                 warn("sysctl");
102                                         continue;
103                                 }
104                                 outwalk += sprintf(outwalk, "%.2f", (sensor.value - 273150000) / 1000000.0 );
105                         }
106                 }
107         }
108 #endif
109                         walk += strlen("degrees");
110                 }
111         }
112         OUTPUT_FULL_TEXT(buffer);
113         return;
114 error:
115 #endif
116         (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);
117 }