]> Dogcows Code - chaz/tint2/commitdiff
fixed battery detection
authorThierry Lorthiois <lorthiois@bbsoft.fr>
Sun, 31 May 2009 12:40:40 +0000 (12:40 +0000)
committerThierry Lorthiois <lorthiois@bbsoft.fr>
Sun, 31 May 2009 12:40:40 +0000 (12:40 +0000)
ChangeLog
src/battery/battery.c
src/battery/battery.h
src/tint.c
src/tint2

index 95bb4718d8397338b10430295a6e861eba0d882f..afc4a5e6a8d6d18a5fc5c6127f3f3c329cb2a68b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-05-31
+- fixed issue 87 and 88 : battery detection
+
+2009-05-26
+- fixed memory leak and battery detection
+
+2009-05-20
+- merge patch from yarin.kaul (fixed issue 52)
 
 2009-05-15
 - merge battery applet from Sebastian Reichel <elektranox@gmail.com>
index 6a6877e45d5f1b05c2afd270c4ddf9509ff94f3e..c07f072a8e11dd0ba7da2d4772d23f8043f6454e 100644 (file)
@@ -40,7 +40,9 @@ static char buf_bat_percentage[10];
 static char buf_bat_time[20];
 
 int8_t battery_low_status;
-char* battery_low_cmd;
+char *battery_low_cmd;
+char *path_energy_now, *path_energy_full, *path_current_now, *path_status;
+
 
 void update_battery(struct batstate *data) {
        FILE *fp;
@@ -49,28 +51,28 @@ void update_battery(struct batstate *data) {
        int seconds = 0;
        int8_t new_percentage = 0;
 
-       fp = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
+       fp = fopen(path_energy_now, "r");
        if(fp != NULL) {
                fgets(tmp, sizeof tmp, fp);
                energy_now = atoi(tmp);
                fclose(fp);
        }
 
-       fp = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
+       fp = fopen(path_energy_full, "r");
        if(fp != NULL) {
                fgets(tmp, sizeof tmp, fp);
                energy_full = atoi(tmp);
                fclose(fp);
        }
 
-       fp = fopen("/sys/class/power_supply/BAT0/current_now", "r");
+       fp = fopen(path_current_now, "r");
        if(fp != NULL) {
                fgets(tmp, sizeof tmp, fp);
                current_now = atoi(tmp);
                fclose(fp);
        }
 
-       fp = fopen("/sys/class/power_supply/BAT0/status", "r");
+       fp = fopen(path_status, "r");
        if(fp != NULL) {
                fgets(tmp, sizeof tmp, fp);
                fclose(fp);
@@ -114,6 +116,36 @@ void update_battery(struct batstate *data) {
 
 void init_battery()
 {
+       // check battery
+       GDir *directory;
+       GError *error = NULL;
+       const char *entryname;
+       char *battery_dir = 0;
+
+       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);
+       else {
+               while ((entryname=g_dir_read_name(directory))) {
+                       if (strncmp(entryname,"AC", 2) == 0) continue;
+
+                       char *path1 = g_build_filename("/sys/class/power_supply", entryname, "present", NULL);
+                       if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
+                               g_free(path1);
+                               battery_dir = g_build_filename("/sys/class/power_supply", entryname, NULL);
+                               break;
+                       }
+                       g_free(path1);
+               }
+       }
+       if (battery_dir != 0) {
+               path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
+               path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
+               path_current_now = g_build_filename(battery_dir, "current_now", NULL);
+               path_status = g_build_filename(battery_dir, "status", NULL);
+       }
+
        FILE *fp;
    Panel *panel;
    Battery *battery;
@@ -128,26 +160,27 @@ void init_battery()
                battery->area._draw_foreground = draw_battery;
                battery->area._resize = resize_battery;
 
+               if (battery_dir == 0) panel->battery.area.on_screen = 0;
                if (!battery->area.on_screen) continue;
-               if((fp = fopen("/sys/class/power_supply/BAT0/energy_now", "r")) == NULL) {
+               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;
                        continue;
                }
                fclose(fp);
-               if((fp = fopen("/sys/class/power_supply/BAT0/energy_full", "r")) == NULL) {
+               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;
                        continue;
                }
                fclose(fp);
-               if((fp = fopen("/sys/class/power_supply/BAT0/current_now", "r")) == NULL) {
+               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;
                        continue;
                }
                fclose(fp);
-               if((fp = fopen("/sys/class/power_supply/BAT0/status", "r")) == NULL) {
+               if((fp = fopen(path_status, "r")) == NULL) {
                        fprintf(stderr, "ERROR: battery applet can't open status");
                        panel->battery.area.on_screen = 0;
                        continue;
index 433be7f8466a6e5e8c3036e5b8335b38f140e9c0..f6d227ab0a03cec3b5819e6ec2f36e281066befd 100644 (file)
@@ -3,6 +3,7 @@
 *
 * Battery with functional data (percentage, time to life) and drawing data
 * (area, font, ...). Each panel use his own drawing data.
+* Need kernel > 2.6.23.
 *
 **************************************************************************/
 
@@ -48,7 +49,9 @@ extern PangoFontDescription *bat1_font_desc;
 extern PangoFontDescription *bat2_font_desc;
 
 extern int8_t battery_low_status;
-extern char* battery_low_cmd;
+extern char *battery_low_cmd;
+extern char *path_energy_now, *path_energy_full, *path_current_now, *path_status;
+
 
 // initialize clock : y position, ...
 void update_battery(struct batstate *data);
index f1c0356657d8b2e83533669db668c2ac181949ae..28366cc9a57515c69ae30a9338f2e213dac302bb 100644 (file)
@@ -92,7 +92,13 @@ void cleanup()
    if (time2_font_desc) pango_font_description_free(time2_font_desc);
    if (time1_format) g_free(time1_format);
    if (time2_format) g_free(time2_format);
+   if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
+   if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
        if (battery_low_cmd) g_free(battery_low_cmd);
+       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 (server.monitor) free(server.monitor);
    XFreeGC(server.dsp, server.gc);
index 48cba6985422ccc7e638e219fbf3e2ec4c461cca..5f9d2354e3960c0f03b4f077299e6b371fdc2af8 100755 (executable)
Binary files a/src/tint2 and b/src/tint2 differ
This page took 0.032228 seconds and 4 git commands to generate.