]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_temperature.c
Breaks configfiles! Major refactoring of i3status, see below
[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
7 #include "i3status.h"
8
9 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
10 #include <err.h>
11 #include <sys/types.h>
12 #include <sys/sysctl.h>
13 #define TZ_ZEROC 2732
14 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
15 #endif
16
17 static char *thermal_zone;
18
19 /*
20  * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
21  * returns the temperature in degree celcius.
22  *
23  */
24 void print_cpu_temperature_info(int zone, const char *format) {
25         const char *walk;
26         static char buf[16];
27
28         asprintf(&thermal_zone, THERMAL_ZONE, zone);
29
30         for (walk = format; *walk != '\0'; walk++) {
31                 if (*walk != '%') {
32                         putchar(*walk);
33                         continue;
34                 }
35
36                 if (BEGINS_WITH(walk+1, "degrees")) {
37 #if defined(LINUX)
38                         long int temp;
39                         if (!slurp(thermal_zone, buf, sizeof(buf)))
40                                 die("Could not open \"%s\"\n", thermal_zone);
41                         temp = strtol(buf, NULL, 10);
42                         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
43                                 (void)printf("?");
44                         else
45                                 (void)printf("%ld", (temp/1000));
46 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
47                         int sysctl_rslt;
48                         size_t sysctl_size = sizeof(sysctl_rslt);
49                         if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0)) {
50                                 (void)printf("No thermal zone found");
51                                 return;
52                         }
53
54                         (void)printf("%d.%d", TZ_KELVTOC(sysctl_rslt));
55 #endif
56                         walk += strlen("degrees");
57                 }
58         }
59 }