]> git.sur5r.net Git - i3/i3status/commitdiff
Implement battery status and thermal zones for FreeBSD (patch by Baptiste Daroussin)
authorMichael Stapelberg <michael@stapelberg.de>
Thu, 23 Jul 2009 16:40:49 +0000 (18:40 +0200)
committerMichael Stapelberg <michael@stapelberg.de>
Thu, 23 Jul 2009 16:40:49 +0000 (18:40 +0200)
i3status.h
src/config.c
src/get_battery_info.c
src/get_cpu_temperature.c

index 5d42881182c8a1d7a21d639c5d2f479e2bbc15f3..241307a2b73d3cf242b45cc2bfcfc2915839ed02 100644 (file)
                         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 };
index 8fff8d0e1e6d16466a3242087fe1fb4e47c643e6..2d9d6b67d5118b5233b2881b514ecd9ccdfa4507 100644 (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)
-                                        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")
index 707afdec0f1af5b46ce15173fc46536ab4783aee..8ac7743eef09d1682643be9477568cc14542cd45 100644 (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;
 }
index d000d2dc069516eef57a69781af2a296447de3c8..09fbd25f7eff3f3b0af9274a7cfacd129431f373 100644 (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.
  */
 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;
 }