#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()
+{
+ 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);
}
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);
}
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);
}
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;