Implement battery status and thermal zones for FreeBSD (patch by Baptiste Daroussin)

This commit is contained in:
Michael Stapelberg 2009-07-23 18:40:49 +02:00
parent 3de2907427
commit a86361510c
4 changed files with 85 additions and 9 deletions

View File

@ -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 };

View File

@ -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)
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");
} else {
if (asprintf(&thermal_zone, "/sys/class/thermal/thermal_zone0/temp") == -1)
die("Could not build thermal_zone path\n");
}
} OPT("normcolors")
wmii_normcolors = strdup(dest_value);
OPT("interval")

View File

@ -5,6 +5,11 @@
#include "i3status.h"
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/sysctl.h>
#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;
}

View File

@ -5,6 +5,15 @@
#include "i3status.h"
#if defined(__FreeBSD__)
#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
/*
* 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;
}