6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
16 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
19 #if defined(__OpenBSD__)
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23 #include <sys/sensors.h>
27 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
30 static char *thermal_zone;
33 * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
34 * returns the temperature in degree celcius.
37 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
38 char *outwalk = buffer;
41 bool colorful_output = false;
44 asprintf(&thermal_zone, THERMAL_ZONE, zone);
46 asprintf(&thermal_zone, path, zone);
51 for (walk = format; *walk != '\0'; walk++) {
57 if (BEGINS_WITH(walk+1, "degrees")) {
61 if (!slurp(path, buf, sizeof(buf)))
63 temp = strtol(buf, NULL, 10);
64 if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
67 if ((temp/1000) >= max_threshold) {
68 START_COLOR("color_bad");
69 colorful_output = true;
71 outwalk += sprintf(outwalk, "%ld", (temp/1000));
72 if (colorful_output) {
74 colorful_output = false;
77 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
79 size_t sysctl_size = sizeof(sysctl_rslt);
80 if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
83 outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
84 #elif defined(__OpenBSD__)
85 struct sensordev sensordev;
88 int dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
90 sdlen = sizeof(sensordev);
91 slen = sizeof(sensor);
93 for (dev = 0; ; dev++) {
95 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
102 /* 'path' is the node within the full path (defaults to acpitz0). */
103 if (strncmp(sensordev.xname, path, strlen(path)) == 0) {
104 mib[3] = SENSOR_TEMP;
105 /* Limit to temo0, but should retrieve from a full path... */
106 for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
108 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
109 if (errno != ENOENT) {
114 if ((int)MUKTOC(sensor.value) >= max_threshold) {
115 START_COLOR("color_bad");
116 colorful_output = true;
119 outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value));
121 if (colorful_output) {
123 colorful_output = false;
129 walk += strlen("degrees");
132 OUTPUT_FULL_TEXT(buffer);
136 OUTPUT_FULL_TEXT("cant read temp");
137 (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);