]> Dogcows Code - chaz/tint2/commitdiff
fixed battery drawing
authorThierry Lorthiois <lorthiois@bbsoft.fr>
Wed, 28 Oct 2009 23:01:32 +0000 (23:01 +0000)
committerThierry Lorthiois <lorthiois@bbsoft.fr>
Wed, 28 Oct 2009 23:01:32 +0000 (23:01 +0000)
src/battery/battery.c
src/battery/battery.h
src/config.c
src/panel.c
src/tint.c

index 1e7feea3bde02da5fe2644a0460a5171fa92079f..4c82893afc96fc99ebb08d7426c0a4cf4e26958d 100644 (file)
@@ -35,6 +35,7 @@
 PangoFontDescription *bat1_font_desc;
 PangoFontDescription *bat2_font_desc;
 struct batstate battery_state;
+int battery_enabled;
 
 static char buf_bat_percentage[10];
 static char buf_bat_time[20];
@@ -52,6 +53,8 @@ void init_battery()
        const char *entryname;
        char *battery_dir = 0;
 
+       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)
@@ -71,32 +74,55 @@ void init_battery()
        }
        if (directory)
                g_dir_close(directory);
-       if (battery_dir) {
-               char *path1 = g_build_filename(battery_dir, "energy_now", NULL);
-               if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
-                       path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
-                       path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
+       if (!battery_dir) {
+               battery_enabled = 0;
+               fprintf(stderr, "ERROR: battery applet can't found power_supply\n");
+               return;
+       }
+
+       char *path1 = g_build_filename(battery_dir, "energy_now", NULL);
+       if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
+               path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
+               path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
+       }
+       else {
+               char *path2 = g_build_filename(battery_dir, "charge_now", NULL);
+               if (g_file_test (path2, G_FILE_TEST_EXISTS)) {
+                       path_energy_now = g_build_filename(battery_dir, "charge_now", NULL);
+                       path_energy_full = g_build_filename(battery_dir, "charge_full", NULL);
                }
                else {
-                       char *path2 = g_build_filename(battery_dir, "charge_now", NULL);
-                       if (g_file_test (path2, G_FILE_TEST_EXISTS)) {
-                               path_energy_now = g_build_filename(battery_dir, "charge_now", NULL);
-                               path_energy_full = g_build_filename(battery_dir, "charge_full", NULL);
-                       }
-                       else {
-                               g_free(battery_dir);
-                               battery_dir = 0;
-                               fprintf(stderr, "ERROR: can't found energy_* or charge_*\n");
-                       }
-                       g_free(path2);
+                       fprintf(stderr, "ERROR: can't found energy_* or charge_*\n");
                }
+               g_free(path2);
+       }
+       if (path_energy_now && path_energy_full) {
                path_current_now = g_build_filename(battery_dir, "current_now", NULL);
                path_status = g_build_filename(battery_dir, "status", NULL);
-               g_free(path1);
+
+               // check file
+               FILE *fp1, *fp2, *fp3, *fp4;
+               fp1 = fopen(path_energy_now, "r");
+               fp2 = fopen(path_energy_full, "r");
+               fp3 = fopen(path_current_now, "r");
+               fp4 = fopen(path_status, "r");
+               if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL) {
+                       battery_enabled = 0;
+                       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);
+               fclose(fp3);
+               fclose(fp4);
        }
 
-       if (battery_dir)
-               g_free(battery_dir);
+       g_free(path1);
+       g_free(battery_dir);
 }
 
 
@@ -106,10 +132,9 @@ void init_battery_panel(void *p)
        Battery *battery = &panel->battery;
        FILE *fp;
        int bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
-       char *battery_dir = 0;
 
-       if (battery_dir == 0) battery->area.on_screen = 0;
-       if (!battery->area.on_screen) return;
+       if (!battery_enabled)
+               return;
 
        battery->area.parent = p;
        battery->area.panel = p;
@@ -117,39 +142,11 @@ void init_battery_panel(void *p)
        battery->area._resize = resize_battery;
        battery->area.resize = 1;
        battery->area.redraw = 1;
-
-       if((fp = fopen(path_energy_now, "r")) == NULL) {
-               fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
-               panel->battery.area.on_screen = 0;
-               return;
-       }
-       fclose(fp);
-       if((fp = fopen(path_energy_full, "r")) == NULL) {
-               fprintf(stderr, "ERROR: battery applet can't open energy_full\n");
-               panel->battery.area.on_screen = 0;
-               return;
-       }
-       fclose(fp);
-       if((fp = fopen(path_current_now, "r")) == NULL) {
-               fprintf(stderr, "ERROR: battery applet can't open current_now\n");
-               panel->battery.area.on_screen = 0;
-               return;
-       }
-       fclose(fp);
-       if((fp = fopen(path_status, "r")) == NULL) {
-               fprintf(stderr, "ERROR: battery applet can't open status");
-               panel->battery.area.on_screen = 0;
-               return;
-       }
-       fclose(fp);
+       battery->area.on_screen = 1;
 
        update_battery(&battery_state);
        snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
-       if(battery_state.state == BATTERY_FULL) {
-               strcpy(buf_bat_time, "Full");
-       } else {
-               snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
-       }
+       snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
 
        get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
        get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
@@ -177,9 +174,23 @@ void update_battery() {
        FILE *fp;
        char tmp[25];
        int64_t energy_now = 0, energy_full = 0, current_now = 0;
-       int seconds = 0;
+       int i, seconds = 0;
        int8_t new_percentage = 0;
 
+       fp = fopen(path_status, "r");
+       if(fp != NULL) {
+               fgets(tmp, sizeof tmp, fp);
+               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);
@@ -201,17 +212,6 @@ void update_battery() {
                fclose(fp);
        }
 
-       fp = fopen(path_status, "r");
-       if(fp != NULL) {
-               fgets(tmp, sizeof tmp, fp);
-               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(current_now > 0) {
                switch(battery_state.state) {
                        case BATTERY_CHARGING:
index c0b4c4f8f80bffa8215a0ce923917b481db0421e..fe3bb3a8a17fa7679ef1d16533202630e2546e7b 100644 (file)
@@ -18,6 +18,7 @@
 #include "area.h"
 
 
+// battery drawing parameter (per panel)
 typedef struct Battery {
        // always start with area
        Area area;
@@ -49,6 +50,7 @@ typedef struct batstate {
 extern struct batstate battery_state;
 extern PangoFontDescription *bat1_font_desc;
 extern PangoFontDescription *bat2_font_desc;
+extern int battery_enabled;
 
 extern int8_t battery_low_status;
 extern char *battery_low_cmd;
index 90cb31e41cd124b935aae91f7d0908814eba8796..02d0265d74fd9117f9c966f5d11b97e2c1a9b9fd 100644 (file)
@@ -274,7 +274,7 @@ void add_entry (char *key, char *value)
        else if (strcmp (key, "battery") == 0) {
 #ifdef ENABLE_BATTERY
                if(atoi(value) == 1)
-                       panel_config.battery.area.on_screen = 1;
+                       battery_enabled = 1;
 #else
                if(atoi(value) == 1)
                        fprintf(stderr, "tint2 is build without battery support\n");
index 7f7f0f9e4291a1a576a5917abd8359257c1a8d7b..9ac62f2d8fafb699fb7acac01c2b05eea4032581 100644 (file)
@@ -128,7 +128,7 @@ void init_panel()
                        p->area.list = g_slist_append(p->area.list, &p->clock);
                }
 #ifdef ENABLE_BATTERY
-               if (p->battery.area.on_screen) {
+               if (battery_enabled) {
                        init_battery_panel(p);
                        p->area.list = g_slist_append(p->area.list, &p->battery);
                }
index 1b4dc488906f67fe76d9e481ca5d0ef3dc20e565..edaa3cd6f3266cc16daedbcecc1238febf43d110 100644 (file)
@@ -662,7 +662,7 @@ void event_timer()
 
        // update battery
 #ifdef ENABLE_BATTERY
-       if (panel1[0].battery.area.on_screen) {
+       if (battery_enabled) {
                update_battery();
                for (i=0 ; i < nb_panel ; i++)
                        panel1[i].battery.area.resize = 1;
This page took 0.034286 seconds and 4 git commands to generate.