]> Dogcows Code - chaz/tint2/blobdiff - src/battery/battery.c
SIGUSR1 does now a full restat of tint2
[chaz/tint2] / src / battery / battery.c
index fadf968caa7cc2ce2c5dbb95906a38864d9a84e9..ff8e0a6d99a96b6294b9c0e8bfa6faf446f86473 100644 (file)
 #include "clock.h"
 #include "timer.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;
-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;
 
 void update_batterys(void* arg)
 {
        int i;
        update_battery();
-       for (i=0 ; i < nb_panel ; i++)
+       for (i=0 ; i < nb_panel ; i++) {
+               if (battery_state.percentage >= percentage_hide) {
+                       if (panel1[i].battery.area.on_screen == 1) {
+                               panel1[i].battery.area.on_screen = 0;
+                               // force resize on panel
+                               panel1[i].area.resize = 1;
+                               panel_refresh = 1;
+                       }
+                       continue;
+               }
+               else {
+                       if (panel1[i].battery.area.on_screen == 0) {
+                               panel1[i].battery.area.on_screen = 1;
+                               // force resize on panel
+                               panel1[i].area.resize = 1;
+                               panel_refresh = 1;
+                       }
+               }
                panel1[i].battery.area.resize = 1;
+       }
+}
+
+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;
+}
+
+void cleanup_battery()
+{
+       printf("*** cleanup_battery()\n");
+       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);
 }
 
 
@@ -88,7 +134,6 @@ 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");
                return;
        }
@@ -121,6 +166,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);
@@ -133,30 +179,7 @@ void init_battery()
        g_free(battery_dir);
 
        if (battery_enabled && battery_timeout==0)
-               battery_timeout = add_timeout(10, 5000, 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;
+               battery_timeout = add_timeout(10, 10000, update_batterys, 0);
 }
 
 
@@ -212,36 +235,30 @@ void update_battery() {
 
        fp = fopen(path_status, "r");
        if(fp != NULL) {
-               fgets(tmp, sizeof tmp, fp);
+               if (fgets(tmp, sizeof tmp, fp)) {
+                       battery_state.state = BATTERY_UNKNOWN;
+                       if(strcasecmp(tmp, "Charging\n")==0) battery_state.state = BATTERY_CHARGING;
+                       if(strcasecmp(tmp, "Discharging\n")==0) battery_state.state = BATTERY_DISCHARGING;
+                       if(strcasecmp(tmp, "Full\n")==0) battery_state.state = BATTERY_FULL;
+               }
                fclose(fp);
        }
-       battery_state.state = BATTERY_UNKNOWN;
-       if(strcasecmp(tmp, "Charging\n")==0) battery_state.state = BATTERY_CHARGING;
-       if(strcasecmp(tmp, "Discharging\n")==0) battery_state.state = BATTERY_DISCHARGING;
-       if(strcasecmp(tmp, "Full\n")==0) battery_state.state = BATTERY_FULL;
-       if (battery_state.state == BATTERY_DISCHARGING) {
-       }
-       else {
-       }
 
        fp = fopen(path_energy_now, "r");
        if(fp != NULL) {
-               fgets(tmp, sizeof tmp, fp);
-               energy_now = atoi(tmp);
+               if (fgets(tmp, sizeof tmp, fp)) energy_now = atoi(tmp);
                fclose(fp);
        }
 
        fp = fopen(path_energy_full, "r");
        if(fp != NULL) {
-               fgets(tmp, sizeof tmp, fp);
-               energy_full = atoi(tmp);
+               if (fgets(tmp, sizeof tmp, fp)) energy_full = atoi(tmp);
                fclose(fp);
        }
 
        fp = fopen(path_current_now, "r");
        if(fp != NULL) {
-               fgets(tmp, sizeof tmp, fp);
-               current_now = atoi(tmp);
+               if (fgets(tmp, sizeof tmp, fp)) current_now = atoi(tmp);
                fclose(fp);
        }
 
@@ -268,15 +285,13 @@ void update_battery() {
        if(energy_full > 0)
                new_percentage = (energy_now*100)/energy_full;
 
-  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_DISCHARGING && !battery_low_cmd_send) {
+                       system(battery_low_cmd);  // return value == -1, since we've set SIGCHLD to SIGIGN
+                       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;
        }
-  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.023722 seconds and 4 git commands to generate.