]> Dogcows Code - chaz/tint2/blobdiff - src/battery/battery.c
*fix* use another timer implementation, coz *BSD does not support timerfd :(
[chaz/tint2] / src / battery / battery.c
index c88c3a5b57d8ccdc5b4d42df5cc5062aa4487ede..4db696e4d73ea977e723133024bb66394518c610 100644 (file)
 #include "taskbar.h"
 #include "battery.h"
 #include "clock.h"
+#include "timer.h"
 
 PangoFontDescription *bat1_font_desc=0;
 PangoFontDescription *bat2_font_desc=0;
 struct batstate battery_state;
 int battery_enabled;
+static const struct timeout* battery_timeout=0;
 
 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;
 
+void update_batterys()
+{
+       int i;
+       update_battery();
+       for (i=0 ; i < nb_panel ; i++)
+               panel1[i].battery.area.resize = 1;
+}
+
 
 void init_battery()
 {
@@ -120,6 +131,9 @@ void init_battery()
 
        g_free(path1);
        g_free(battery_dir);
+
+       if (battery_enabled && battery_timeout==0)
+               battery_timeout = add_timeout(10, 5000, update_batterys);
 }
 
 
@@ -150,7 +164,6 @@ void init_battery_panel(void *p)
 {
        Panel *panel = (Panel*)p;
        Battery *battery = &panel->battery;
-       FILE *fp;
        int bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
 
        if (!battery_enabled)
@@ -194,7 +207,7 @@ void update_battery() {
        FILE *fp;
        char tmp[25];
        int64_t energy_now = 0, energy_full = 0, current_now = 0;
-       int i, seconds = 0;
+       int seconds = 0;
        int8_t new_percentage = 0;
 
        fp = fopen(path_status, "r");
@@ -255,10 +268,15 @@ void update_battery() {
        if(energy_full > 0)
                new_percentage = (energy_now*100)/energy_full;
 
-       if(battery_low_status != 0 && battery_low_status == new_percentage && battery_state.percentage > new_percentage) {
-               //printf("battery low, executing: %s\n", battery_low_cmd);
-               if (battery_low_cmd) system(battery_low_cmd);
+  if(battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) {
+    printf("battery low, executing: %s\n", battery_low_cmd);
+    if (battery_low_cmd)
+      system(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;
+  }
 
        battery_state.percentage = new_percentage;
 
This page took 0.026037 seconds and 4 git commands to generate.