i3status: Bugfix: Reading multiple temperature sensors not possible

This patch inlines the creation of the thermal zone string in order
to force computation on each invocation. This is necessary to be able
to read the values of several temperature sensors.
This commit is contained in:
Marco Hunsicker 2014-02-25 20:43:48 +01:00 committed by Michael Stapelberg
parent 530c82edfd
commit 26faed4c2f

View File

@ -37,11 +37,9 @@
#endif #endif
static char *thermal_zone;
/* /*
* Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and * Reads the CPU temperature from /sys/class/thermal/thermal_zone%d/temp (or
* returns the temperature in degree celcius. * the user provided path) and returns the temperature in degree celcius.
* *
*/ */
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) { void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
@ -49,16 +47,14 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
#ifdef THERMAL_ZONE #ifdef THERMAL_ZONE
const char *walk; const char *walk;
bool colorful_output = false; bool colorful_output = false;
char *thermal_zone;
if (thermal_zone == NULL) { if (path == NULL)
if (path == NULL) asprintf(&thermal_zone, THERMAL_ZONE, zone);
asprintf(&thermal_zone, THERMAL_ZONE, zone); else
else asprintf(&thermal_zone, path, zone);
asprintf(&thermal_zone, path, zone);
}
path = thermal_zone;
INSTANCE(path); INSTANCE(thermal_zone);
for (walk = format; *walk != '\0'; walk++) { for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') { if (*walk != '%') {
@ -70,7 +66,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
#if defined(LINUX) #if defined(LINUX)
static char buf[16]; static char buf[16];
long int temp; long int temp;
if (!slurp(path, buf, sizeof(buf))) if (!slurp(thermal_zone, buf, sizeof(buf)))
goto error; goto error;
temp = strtol(buf, NULL, 10); temp = strtol(buf, NULL, 10);
if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0) if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
@ -89,7 +85,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
int sysctl_rslt; int sysctl_rslt;
size_t sysctl_size = sizeof(sysctl_rslt); size_t sysctl_size = sizeof(sysctl_rslt);
if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0)) if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0))
goto error; goto error;
if (TZ_AVG(sysctl_rslt) >= max_threshold) { if (TZ_AVG(sysctl_rslt) >= max_threshold) {
@ -121,7 +117,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
goto error; goto error;
} }
/* 'path' is the node within the full path (defaults to acpitz0). */ /* 'path' is the node within the full path (defaults to acpitz0). */
if (strncmp(sensordev.xname, path, strlen(path)) == 0) { if (strncmp(sensordev.xname, thermal_zone, strlen(thermal_zone)) == 0) {
mib[3] = SENSOR_TEMP; mib[3] = SENSOR_TEMP;
/* Limit to temo0, but should retrieve from a full path... */ /* Limit to temo0, but should retrieve from a full path... */
for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) { for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
@ -195,7 +191,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const
while ((obj2 = prop_object_iterator_next(iter2)) != NULL) { while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
obj3 = prop_dictionary_get(obj2, "description"); obj3 = prop_dictionary_get(obj2, "description");
if (obj3 && if (obj3 &&
strcmp(path, prop_string_cstring_nocopy(obj3)) == 0) strcmp(thermal_zone, prop_string_cstring_nocopy(obj3)) == 0)
{ {
obj3 = prop_dictionary_get(obj2, "cur-value"); obj3 = prop_dictionary_get(obj2, "cur-value");
float temp = MUKTOC(prop_number_integer_value(obj3)); float temp = MUKTOC(prop_number_integer_value(obj3));
@ -230,10 +226,15 @@ error_netbsd1:
walk += strlen("degrees"); walk += strlen("degrees");
} }
} }
free(thermal_zone);
OUTPUT_FULL_TEXT(buffer); OUTPUT_FULL_TEXT(buffer);
return; return;
error: error:
#endif #endif
free(thermal_zone);
OUTPUT_FULL_TEXT("cant read temp"); OUTPUT_FULL_TEXT("cant read temp");
(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); (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);
} }