]> Dogcows Code - chaz/tint2/blobdiff - src/battery/battery.c
*add* use ACPI api for freebsd (thx to yamagi.burmeister)
[chaz/tint2] / src / battery / battery.c
index fd2ffce4bf59d74e6de728bad2e64bd638dcce59..b20b7d3193b911eaee818cf8f9a86c5fb893628f 100644 (file)
 #include <cairo-xlib.h>
 #include <pango/pangocairo.h>
 
+#if defined(__OpenBSD__) || defined(__NetBSD__)
+#include <machine/apmvar.h>
+#include <err.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#endif
+
+#if defined(__FreeBSD__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
+
 #include "window.h"
 #include "server.h"
 #include "area.h"
 #include "battery.h"
 #include "clock.h"
 #include "timer.h"
+#include "common.h"
 
-PangoFontDescription *bat1_font_desc=0;
-PangoFontDescription *bat2_font_desc=0;
+PangoFontDescription *bat1_font_desc;
+PangoFontDescription *bat2_font_desc;
 struct batstate battery_state;
 int battery_enabled;
-int percentage_hide = 101;
-static timeout* battery_timeout=0;
+int percentage_hide;
+static timeout* battery_timeout;
 
 static char buf_bat_percentage[10];
 static char buf_bat_time[20];
 
 int8_t battery_low_status;
-char *battery_low_cmd=0;
-unsigned char battery_low_cmd_send=0;
-char *path_energy_now=0;
-char *path_energy_full=0;
-char *path_current_now=0;
-char *path_status=0;
+unsigned char battery_low_cmd_send;
+char *battery_low_cmd;
+char *path_energy_now;
+char *path_energy_full;
+char *path_current_now;
+char *path_status;
+
+#if defined(__OpenBSD__) || defined(__NetBSD__)
+int apm_fd;
+#endif
 
 void update_batterys(void* arg)
 {
@@ -77,17 +94,60 @@ void update_batterys(void* arg)
        }
 }
 
+void default_battery()
+{
+       battery_enabled = 0;
+       percentage_hide = 101;
+       battery_low_cmd_send = 0;
+       battery_timeout = 0;
+       bat1_font_desc = 0;
+       bat2_font_desc = 0;
+       battery_low_cmd = 0;
+       path_energy_now = 0;
+       path_energy_full = 0;
+       path_current_now = 0;
+       path_status = 0;
+#if defined(__OpenBSD__) || defined(__NetBSD__)
+       apm_fd = -1;
+#endif
+}
+
+void cleanup_battery()
+{
+       if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
+       if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
+       if (path_energy_now) g_free(path_energy_now);
+       if (path_energy_full) g_free(path_energy_full);
+       if (path_current_now) g_free(path_current_now);
+       if (path_status) g_free(path_status);
+       if (battery_low_cmd) g_free(battery_low_cmd);
+
+#if defined(__OpenBSD__) || defined(__NetBSD__)
+       if ((apm_fd != -1) && (close(apm_fd) == -1))
+               warn("cannot close /dev/apm");
+#endif
+}
+
 
 void init_battery()
 {
+       if (!battery_enabled) return;
+
+#if defined(__OpenBSD__) || defined(__NetBSD__)
+       apm_fd = open("/dev/apm", O_RDONLY);
+       if (apm_fd < 0) {
+               warn("init_battery: failed to open /dev/apm.");
+               battery_enabled = 0;
+               return;
+       }
+
+#elif !defined(__FreeBSD__)
        // check battery
        GDir *directory = 0;
        GError *error = NULL;
        const char *entryname;
        char *battery_dir = 0;
 
-       if (!battery_enabled) return;
-
        directory = g_dir_open("/sys/class/power_supply", 0, &error);
        if (error)
                g_error_free(error);
@@ -107,8 +167,8 @@ void init_battery()
        if (directory)
                g_dir_close(directory);
        if (!battery_dir) {
-               cleanup_battery();
                fprintf(stderr, "ERROR: battery applet can't found power_supply\n");
+               default_battery();
                return;
        }
 
@@ -140,6 +200,7 @@ void init_battery()
                fp4 = fopen(path_status, "r");
                if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL) {
                        cleanup_battery();
+                       default_battery();
                        fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
                }
                fclose(fp1);
@@ -150,35 +211,13 @@ void init_battery()
 
        g_free(path1);
        g_free(battery_dir);
+#endif
 
        if (battery_enabled && battery_timeout==0)
                battery_timeout = add_timeout(10, 10000, update_batterys, 0);
 }
 
 
-void cleanup_battery()
-{
-       battery_enabled = 0;
-       if (bat1_font_desc)
-               pango_font_description_free(bat1_font_desc);
-       if (bat2_font_desc)
-               pango_font_description_free(bat2_font_desc);
-       if (path_energy_now)
-               g_free(path_energy_now);
-       if (path_energy_full)
-               g_free(path_energy_full);
-       if (path_current_now)
-               g_free(path_current_now);
-       if (path_status)
-               g_free(path_status);
-       if (battery_low_cmd)
-               g_free(battery_low_cmd);
-
-       battery_low_cmd = path_energy_now = path_energy_full = path_current_now = path_status = 0;
-       bat1_font_desc = bat2_font_desc = 0;
-}
-
-
 void init_battery_panel(void *p)
 {
        Panel *panel = (Panel*)p;
@@ -223,12 +262,90 @@ void init_battery_panel(void *p)
 
 
 void update_battery() {
+#if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__FreeBSD__)
+       // unused on OpenBSD, silence compiler warnings
        FILE *fp;
        char tmp[25];
-       int64_t energy_now = 0, energy_full = 0, current_now = 0;
+       int64_t current_now = 0;
+#endif
+#if defined(__FreeBSD__)
+       int sysctl_out =  0;
+       size_t len = 0;
+#endif
+       int64_t energy_now = 0, energy_full = 0;
        int seconds = 0;
        int8_t new_percentage = 0;
 
+#if defined(__OpenBSD__) || defined(__NetBSD__)
+       struct apm_power_info info;
+       if (ioctl(apm_fd, APM_IOC_GETPOWER, &(info)) < 0)
+               warn("power update: APM_IOC_GETPOWER");
+
+       // best attempt at mapping to linux battery states
+       battery_state.state = BATTERY_UNKNOWN;
+       switch (info.battery_state) {
+               case APM_BATT_CHARGING:
+                       battery_state.state = BATTERY_CHARGING;
+                       break;
+               default:
+                       battery_state.state = BATTERY_DISCHARGING;
+                       break;
+       }
+
+       if (info.battery_life == 100)
+               battery_state.state = BATTERY_FULL;
+
+       // no mapping for openbsd really
+       energy_full = 0;
+       energy_now = 0;
+
+       if (info.minutes_left != -1)
+               seconds = info.minutes_left * 60;
+       else
+               seconds = -1;
+
+       new_percentage = info.battery_life;
+
+#elif defined(__FreeBSD__)
+       len = sizeof(sysctl_out);
+
+       if (sysctlbyname("hw.acpi.battery.state", &sysctl_out, &len, NULL, 0) != 0)
+               fprintf(stderr, "power update: no such sysctl");
+
+       // attemp to map the battery state to linux
+       battery_state.state = BATTERY_UNKNOWN;
+
+       switch(sysctl_out) {
+               case 1:
+                       battery_state.state = BATTERY_DISCHARGING;
+                       break;
+               case 2:
+                       battery_state.state = BATTERY_CHARGING;
+                       break;
+               default:
+                       battery_state.state = BATTERY_FULL;
+                       break;
+       }
+
+       // no mapping for freebsd
+       energy_full = 0;
+       energy_now = 0;
+
+       if (sysctlbyname("hw.acpi.battery.time", &sysctl_out, &len, NULL, 0) != 0)
+               seconds = -1;
+       else
+               seconds = sysctl_out * 60;
+
+       // charging or error
+       if (seconds < 0)
+               seconds = 0;
+
+       if (sysctlbyname("hw.acpi.battery.life", &sysctl_out, &len, NULL, 0) != 0)
+               new_percentage = -1;
+       else
+               new_percentage = sysctl_out;
+
+#else
        fp = fopen(path_status, "r");
        if(fp != NULL) {
                if (fgets(tmp, sizeof tmp, fp)) {
@@ -271,6 +388,7 @@ void update_battery() {
                                break;
                }
        } else seconds = 0;
+#endif
 
        battery_state.time.hours = seconds / 3600;
        seconds -= 3600 * battery_state.time.hours;
@@ -282,8 +400,8 @@ void update_battery() {
                new_percentage = (energy_now*100)/energy_full;
 
        if(battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) {
-                       system(battery_low_cmd);  // return value == -1, since we've set SIGCHLD to SIGIGN
-                       battery_low_cmd_send = 1;
+               tint_exec(battery_low_cmd);
+               battery_low_cmd_send = 1;
        }
        if(battery_low_status < new_percentage && battery_state.state == BATTERY_CHARGING && battery_low_cmd_send) {
                battery_low_cmd_send = 0;
This page took 0.02977 seconds and 4 git commands to generate.