]> Dogcows Code - chaz/tint2/blobdiff - src/battery/battery.c
fixed some segfault and memleak
[chaz/tint2] / src / battery / battery.c
index c07f072a8e11dd0ba7da2d4772d23f8043f6454e..7e47bd32699a8f2ee9a740b3ae099372d2f2f723 100644 (file)
@@ -44,80 +44,10 @@ char *battery_low_cmd;
 char *path_energy_now, *path_energy_full, *path_current_now, *path_status;
 
 
-void update_battery(struct batstate *data) {
-       FILE *fp;
-       char tmp[25];
-       int64_t energy_now = 0, energy_full = 0, current_now = 0;
-       int seconds = 0;
-       int8_t new_percentage = 0;
-
-       fp = fopen(path_energy_now, "r");
-       if(fp != NULL) {
-               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);
-               fclose(fp);
-       }
-
-       fp = fopen(path_current_now, "r");
-       if(fp != NULL) {
-               fgets(tmp, sizeof tmp, fp);
-               current_now = atoi(tmp);
-               fclose(fp);
-       }
-
-       fp = fopen(path_status, "r");
-       if(fp != NULL) {
-               fgets(tmp, sizeof tmp, fp);
-               fclose(fp);
-       }
-
-       data->state = BATTERY_UNKNOWN;
-       if(strcasecmp(tmp, "Charging\n")==0) data->state = BATTERY_CHARGING;
-       if(strcasecmp(tmp, "Discharging\n")==0) data->state = BATTERY_DISCHARGING;
-
-       if(current_now > 0) {
-               switch(data->state) {
-                       case BATTERY_CHARGING:
-                               seconds = 3600 * (energy_full - energy_now) / current_now;
-                               break;
-                       case BATTERY_DISCHARGING:
-                               seconds = 3600 * energy_now / current_now;
-                               break;
-                       default:
-                               seconds = 0;
-                               break;
-               }
-       } else seconds = 0;
-
-       data->time.hours = seconds / 3600;
-       seconds -= 3600 * data->time.hours;
-       data->time.minutes = seconds / 60;
-       seconds -= 60 * data->time.minutes;
-       data->time.seconds = seconds;
-
-       if(energy_full > 0)
-               new_percentage = (energy_now*100)/energy_full;
-
-       if(battery_low_status != 0 && battery_low_status == new_percentage && data->percentage > new_percentage) {
-               printf("battery low, executing: %s\n", battery_low_cmd);
-               if(battery_low_cmd) system(battery_low_cmd);
-       }
-
-       data->percentage = new_percentage;
-}
-
-
 void init_battery()
 {
        // check battery
-       GDir *directory;
+       GDir *directory = 0;
        GError *error = NULL;
        const char *entryname;
        char *battery_dir = 0;
@@ -139,29 +69,51 @@ void init_battery()
                        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);
+       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);
+               }
+               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);
+               }
                path_current_now = g_build_filename(battery_dir, "current_now", NULL);
                path_status = g_build_filename(battery_dir, "status", NULL);
+               g_free(path1);
        }
 
        FILE *fp;
-   Panel *panel;
-   Battery *battery;
-   int i, bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
+       Panel *panel;
+       Battery *battery;
+       int i, bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
 
        for (i=0 ; i < nb_panel ; i++) {
                panel = &panel1[i];
                battery = &panel->battery;
 
+               if (battery_dir == 0) battery->area.on_screen = 0;
+               if (!battery->area.on_screen) continue;
+
                battery->area.parent = panel;
                battery->area.panel = panel;
                battery->area._draw_foreground = draw_battery;
                battery->area._resize = resize_battery;
+               battery->area.resize = 1;
+               battery->area.redraw = 1;
 
-               if (battery_dir == 0) panel->battery.area.on_screen = 0;
-               if (!battery->area.on_screen) continue;
                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;
@@ -187,23 +139,103 @@ void init_battery()
                }
                fclose(fp);
 
-               battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
-               battery->area.height = panel->area.height - (2 * battery->area.posy);
-               battery->area.resize = 1;
-               battery->area.redraw = 1;
-
                update_battery(&battery_state);
                snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
                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));
-               battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
-
                get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
 
+               if (panel_horizontal) {
+                       // panel horizonal => fixed height and posy
+                       battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
+                       battery->area.height = panel->area.height - (2 * battery->area.posy);
+               }
+               else {
+                       // panel vertical => fixed width, height, posy and posx
+                       battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
+                       battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
+                       battery->area.posx = panel->area.pix.border.width + panel->area.paddingy;
+                       battery->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
+               }
+
+               battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
                battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
                battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2 - (bat_percentage_height - bat_percentage_height_ink)/2 - (bat_time_height - bat_time_height_ink)/2;
        }
+
+       if (battery_dir)
+               g_free(battery_dir);
+}
+
+
+void update_battery() {
+       FILE *fp;
+       char tmp[25];
+       int64_t energy_now = 0, energy_full = 0, current_now = 0;
+       int seconds = 0;
+       int8_t new_percentage = 0;
+
+       fp = fopen(path_energy_now, "r");
+       if(fp != NULL) {
+               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);
+               fclose(fp);
+       }
+
+       fp = fopen(path_current_now, "r");
+       if(fp != NULL) {
+               fgets(tmp, sizeof tmp, fp);
+               current_now = atoi(tmp);
+               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(current_now > 0) {
+               switch(battery_state.state) {
+                       case BATTERY_CHARGING:
+                               seconds = 3600 * (energy_full - energy_now) / current_now;
+                               break;
+                       case BATTERY_DISCHARGING:
+                               seconds = 3600 * energy_now / current_now;
+                               break;
+                       default:
+                               seconds = 0;
+                               break;
+               }
+       } else seconds = 0;
+
+       battery_state.time.hours = seconds / 3600;
+       seconds -= 3600 * battery_state.time.hours;
+       battery_state.time.minutes = seconds / 60;
+       seconds -= 60 * battery_state.time.minutes;
+       battery_state.time.seconds = seconds;
+
+       if(energy_full > 0)
+               new_percentage = (energy_now*100)/energy_full;
+
+       if(battery_low_status != 0 && battery_low_status == new_percentage && battery_state.percentage > new_percentage) {
+               //printf("battery low, executing: %s\n", battery_low_cmd);
+               if (battery_low_cmd) system(battery_low_cmd);
+       }
+
+       battery_state.percentage = new_percentage;
 }
 
 
@@ -238,9 +270,10 @@ void draw_battery (void *obj, cairo_t *c, int active)
        g_object_unref(layout);
 }
 
+
 void resize_battery(void *obj)
 {
-       Battery *battery = obj;
+       Battery *battery = obj;
        PangoLayout *layout;
        int percentage_width, time_width, new_width;
 
@@ -249,6 +282,8 @@ void resize_battery(void *obj)
 
        snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
        snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
+       // vertical panel doen't adjust width
+       if (!panel_horizontal) return;
 
        cairo_surface_t *cs;
        cairo_t *c;
@@ -275,23 +310,19 @@ void resize_battery(void *obj)
 
        new_width += (2*battery->area.paddingxlr) + (2*battery->area.pix.border.width);
 
-       if(new_width > battery->area.width || new_width < (battery->area.width-6)) {
-               int i;
-               Panel *panel = ((Area*)obj)->panel;
-
-               printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
-
-               // resize battery
-               // we try to limit the number of resize
-               battery->area.width = new_width + 1;
-               battery->area.posx = panel->area.width - battery->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
-               if (panel->clock.area.on_screen)
-                       battery->area.posx -= (panel->clock.area.width + panel->area.paddingx);
+       int old_width = battery->area.width;
 
-               // resize other objects on panel
-               for (i=0 ; i < nb_panel ; i++)
-                       panel1[i].area.resize = 1;
+       Panel *panel = ((Area*)obj)->panel;
+       battery->area.width = new_width + 1;
+       battery->area.posx = panel->area.width - battery->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
+       if (panel->clock.area.on_screen)
+               battery->area.posx -= (panel->clock.area.width + panel->area.paddingx);
 
+       if(new_width > old_width || new_width < (old_width-6)) {
+               // refresh and resize other objects on panel
+               // we try to limit the number of refresh
+               // printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
+               panel->area.resize = 1;
                systray.area.resize = 1;
                panel_refresh = 1;
        }
This page took 0.029539 seconds and 4 git commands to generate.