]> git.sur5r.net Git - i3/i3status/blob - src/get_cpu_temperature.c
Implement battery status and thermal zones for FreeBSD (patch by Baptiste Daroussin)
[i3/i3status] / src / get_cpu_temperature.c
1 // vim:ts=8:expandtab
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5
6 #include "i3status.h"
7
8 #if defined(__FreeBSD__)
9 #include <err.h>
10 #include <sys/types.h>
11 #include <sys/sysctl.h>
12 #define TZ_ZEROC 2732
13 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
14 #endif
15
16
17 /*
18  * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
19  * returns the temperature in degree celcius.
20  *
21  */
22 const char *get_cpu_temperature_info() {
23         static char buf[16];
24
25 #if defined(LINUX)
26         long int temp;
27         slurp(thermal_zone, buf, sizeof(buf));
28         temp = strtol(buf, NULL, 10);
29         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
30                 (void)snprintf(buf, sizeof(buf), "T: ? C");
31         else
32                 (void)snprintf(buf, sizeof(buf), "T: %ld C", (temp/1000));
33 #elif defined(__FreeBSD__)
34         int sysctl_rslt;
35         size_t sysctl_size = sizeof (sysctl_rslt);
36         if (sysctlbyname(thermal_zone,&sysctl_rslt,&sysctl_size,NULL,0))
37             return "No Thermal";
38
39         snprintf(buf,sizeof(buf),"T: %d.%d C",TZ_KELVTOC(sysctl_rslt));
40 #endif
41
42         return buf;
43 }