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