i3status/src/print_cpu_temperature.c

72 lines
2.2 KiB
C
Raw Normal View History

// vim:ts=8:expandtab
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
2012-03-25 18:55:55 +00:00
#include <yajl/yajl_gen.h>
#include "i3status.h"
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <err.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#define TZ_ZEROC 2732
#define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
#endif
static char *thermal_zone;
/*
* Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
* returns the temperature in degree celcius.
*
*/
2012-03-25 18:55:55 +00:00
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format) {
#ifdef THERMAL_ZONE
const char *walk;
2012-03-25 18:55:55 +00:00
char *outwalk = buffer;
static char buf[16];
if (path == NULL) {
asprintf(&thermal_zone, THERMAL_ZONE, zone);
path = thermal_zone;
}
2012-03-25 18:55:55 +00:00
INSTANCE(path);
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
2012-03-25 18:55:55 +00:00
*(outwalk++) = *walk;
continue;
}
if (BEGINS_WITH(walk+1, "degrees")) {
#if defined(LINUX)
long int temp;
if (!slurp(path, buf, sizeof(buf)))
2011-08-25 21:24:06 +00:00
goto error;
temp = strtol(buf, NULL, 10);
if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
2012-03-25 18:55:55 +00:00
*(outwalk++) = '?';
else
2012-03-25 18:55:55 +00:00
outwalk += sprintf(outwalk, "%ld", (temp/1000));
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
int sysctl_rslt;
size_t sysctl_size = sizeof(sysctl_rslt);
2011-08-25 21:24:06 +00:00
if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
goto error;
2012-03-25 18:55:55 +00:00
outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
#endif
walk += strlen("degrees");
}
}
2012-03-25 18:55:55 +00:00
OUTPUT_FULL_TEXT(buffer);
2011-08-25 21:24:06 +00:00
return;
error:
#endif
2011-08-25 21:24:06 +00:00
(void)fputs("Cannot read temperature\n", stderr);
}