]> 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 4c82893afc96fc99ebb08d7426c0a4cf4e26958d..4db696e4d73ea977e723133024bb66394518c610 100644 (file)
 #include "taskbar.h"
 #include "battery.h"
 #include "clock.h"
+#include "timer.h"
 
-PangoFontDescription *bat1_font_desc;
-PangoFontDescription *bat2_font_desc;
+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;
-char *path_energy_now, *path_energy_full, *path_current_now, *path_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()
@@ -55,7 +69,6 @@ void init_battery()
 
        if (!battery_enabled) return;
 
-       path_energy_now = path_energy_full = path_current_now = path_status = 0;
        directory = g_dir_open("/sys/class/power_supply", 0, &error);
        if (error)
                g_error_free(error);
@@ -75,7 +88,7 @@ void init_battery()
        if (directory)
                g_dir_close(directory);
        if (!battery_dir) {
-               battery_enabled = 0;
+               cleanup_battery();
                fprintf(stderr, "ERROR: battery applet can't found power_supply\n");
                return;
        }
@@ -107,13 +120,8 @@ void init_battery()
                fp3 = fopen(path_current_now, "r");
                fp4 = fopen(path_status, "r");
                if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL) {
-                       battery_enabled = 0;
+                       cleanup_battery();
                        fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
-                       g_free(path_energy_now);
-                       g_free(path_energy_full);
-                       g_free(path_current_now);
-                       g_free(path_status);
-                       path_energy_now = path_energy_full = path_current_now = path_status = 0;
                }
                fclose(fp1);
                fclose(fp2);
@@ -123,6 +131,32 @@ void init_battery()
 
        g_free(path1);
        g_free(battery_dir);
+
+       if (battery_enabled && battery_timeout==0)
+               battery_timeout = add_timeout(10, 5000, update_batterys);
+}
+
+
+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;
 }
 
 
@@ -130,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)
@@ -174,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");
@@ -235,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.027829 seconds and 4 git commands to generate.