]> Dogcows Code - chaz/tint2/blobdiff - src/battery/battery.c
*fix* use XFlush to really make use of the tooltip timeouts and do not rely on some...
[chaz/tint2] / src / battery / battery.c
index 4c82893afc96fc99ebb08d7426c0a4cf4e26958d..5c1f6cc8ee814b3f67d84a6eb0fe44bb2b4f15d2 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;
 
@@ -41,8 +42,19 @@ 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;
+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 +67,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 +86,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 +118,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 +129,32 @@ void init_battery()
 
        g_free(path1);
        g_free(battery_dir);
+
+       if (battery_enabled)
+               install_timer(0, 1000000, 5, 0, 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 +162,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 +205,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");
This page took 0.025211 seconds and 4 git commands to generate.