From a86361510c10ac5f19e2959d33e9ffb7f6c5099e Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 23 Jul 2009 18:40:49 +0200 Subject: [PATCH] Implement battery status and thermal zones for FreeBSD (patch by Baptiste Daroussin) --- i3status.h | 12 ++++++++++ src/config.c | 12 ++++------ src/get_battery_info.c | 49 +++++++++++++++++++++++++++++++++++++++ src/get_cpu_temperature.c | 21 +++++++++++++++-- 4 files changed, 85 insertions(+), 9 deletions(-) diff --git a/i3status.h b/i3status.h index 5d42881..241307a 100644 --- a/i3status.h +++ b/i3status.h @@ -20,6 +20,18 @@ generate(orderidx, name, function); \ } while (0) +#if defined(LINUX) + +#define THERMAL_ZONE "/sys/class/thermal/thermal_zone%d/temp" + +#elif defined(__FreeBSD__) + +#define THERMAL_ZONE "hw.acpi.thermal.tz%d.temperature" +#define BATT_LIFE "hw.acpi.battery.life" +#define BATT_TIME "hw.acpi.battery.time" +#define BATT_STATE "hw.acpi.battery.state" + +#endif typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t; enum { ORDER_RUN, ORDER_WLAN, ORDER_ETH, ORDER_BATTERY, ORDER_CPU_TEMPERATURE, ORDER_LOAD, ORDER_TIME, ORDER_IPV6, MAX_ORDER }; diff --git a/src/config.c b/src/config.c index 8fff8d0..2d9d6b6 100644 --- a/src/config.c +++ b/src/config.c @@ -69,13 +69,11 @@ int load_configuration(const char *configfile) { get_ethspeed = true; OPT("get_cpu_temperature") { get_cpu_temperature = true; - if (strlen(dest_value) > 0) { - if (asprintf(&thermal_zone, "/sys/class/thermal/thermal_zone%d/temp", atoi(dest_value)) == -1) - die("Could not build thermal_zone path\n"); - } else { - if (asprintf(&thermal_zone, "/sys/class/thermal/thermal_zone0/temp") == -1) - die("Could not build thermal_zone path\n"); - } + int zone = 0; + if (strlen(dest_value) > 0) + zone = atoi(dest_value); + if (asprintf(&thermal_zone, THERMAL_ZONE, zone) == -1) + die("Could not build thermal_zone path\n"); } OPT("normcolors") wmii_normcolors = strdup(dest_value); OPT("interval") diff --git a/src/get_battery_info.c b/src/get_battery_info.c index 707afde..8ac7743 100644 --- a/src/get_battery_info.c +++ b/src/get_battery_info.c @@ -5,6 +5,11 @@ #include "i3status.h" +#if defined(__FreeBSD__) +#include +#include +#endif + /* * Get battery information from /sys. Note that it uses the design capacity to * calculate the percentage, not the last full capacity, so you can see how @@ -20,6 +25,7 @@ const char *get_battery_info(struct battery *bat) { present_rate = -1; charging_status_t status = CS_DISCHARGING; +#if defined(LINUX) slurp(bat->path, buf, sizeof(buf)); for (walk = buf, last = buf; (walk-buf) < 1024; walk++) { @@ -85,5 +91,48 @@ const char *get_battery_info(struct battery *bat) { (status == CS_DISCHARGING ? "BAT" : "FULL")), (((float)remaining / (float)full_design) * 100)); } +#elif defined(__FreeBSD__) + int state; + int sysctl_rslt; + size_t sysctl_size = sizeof(sysctl_rslt); + + if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) + return "No battery"; + + present_rate = sysctl_rslt; + if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) + return "No battery"; + + remaining = sysctl_rslt; + if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) + return "No battery"; + + state = sysctl_rslt; + if (state == 0 && present_rate == 100) + status = CS_FULL; + else if (state == 0 && present_rate < 100) + status = CS_CHARGING; + else + status = CS_DISCHARGING; + + full_design = sysctl_rslt; + + if (state == 1) { + int hours, minutes; + minutes = remaining; + hours = minutes / 60; + minutes -= (hours * 60); + (void)snprintf(part, sizeof(part), "%s %02d%% %02dh%02d", + (status == CS_CHARGING ? "CHR" : + (status == CS_DISCHARGING ? "BAT" : "FULL")), + present_rate, + max(hours, 0), max(minutes, 0)); + } else { + (void)snprintf(part, sizeof(part), "%s %02d%%", + (status == CS_CHARGING ? "CHR" : + (status == CS_DISCHARGING ? "BAT" : "FULL")), + present_rate); + } +#endif return part; } diff --git a/src/get_cpu_temperature.c b/src/get_cpu_temperature.c index d000d2d..09fbd25 100644 --- a/src/get_cpu_temperature.c +++ b/src/get_cpu_temperature.c @@ -5,6 +5,15 @@ #include "i3status.h" +#if defined(__FreeBSD__) +#include +#include +#include +#define TZ_ZEROC 2732 +#define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10) +#endif + + /* * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and * returns the temperature in degree celcius. @@ -12,15 +21,23 @@ */ const char *get_cpu_temperature_info() { static char buf[16]; - long int temp; +#if defined(LINUX) + long int temp; slurp(thermal_zone, buf, sizeof(buf)); temp = strtol(buf, NULL, 10); - if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0) (void)snprintf(buf, sizeof(buf), "T: ? C"); else (void)snprintf(buf, sizeof(buf), "T: %ld C", (temp/1000)); +#elif defined(__FreeBSD__) + int sysctl_rslt; + size_t sysctl_size = sizeof (sysctl_rslt); + if (sysctlbyname(thermal_zone,&sysctl_rslt,&sysctl_size,NULL,0)) + return "No Thermal"; + + snprintf(buf,sizeof(buf),"T: %d.%d C",TZ_KELVTOC(sysctl_rslt)); +#endif return buf; }