]> Dogcows Code - chaz/tint2/blobdiff - src/battery/battery.c
Remove useless Xrandr.h include.
[chaz/tint2] / src / battery / battery.c
index a0b122d39d21849b73d41ed599f2ee1692546c59..f0bb1ff0a0749efa3cf24c583065d230c17e6b58 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,32 +51,32 @@ 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);
-       } else printf("ERROR: battery applet can't open energy_now\n");
+       }
 
-       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);
-       } else printf("ERROR: battery applet can't open energy_full\n");
+       }
 
-       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);
-       } else printf("ERROR: battery applet can't open current_now\n");
+       }
 
-       fp = fopen("/sys/class/power_supply/BAT0/status", "r");
+       fp = fopen(path_status, "r");
        if(fp != NULL) {
                fgets(tmp, sizeof tmp, fp);
                fclose(fp);
-       } else printf("ERROR: battery applet can't open status");
+       }
 
        data->state = BATTERY_UNKNOWN;
        if(strcasecmp(tmp, "Charging\n")==0) data->state = BATTERY_CHARGING;
@@ -100,7 +102,8 @@ void update_battery(struct batstate *data) {
        seconds -= 60 * data->time.minutes;
        data->time.seconds = seconds;
 
-       new_percentage = (energy_now*100)/energy_full;
+       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);
@@ -110,8 +113,57 @@ void update_battery(struct batstate *data) {
        data->percentage = new_percentage;
 }
 
+
 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) {
+               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;
@@ -125,7 +177,32 @@ 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(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(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(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(path_status, "r")) == NULL) {
+                       fprintf(stderr, "ERROR: battery applet can't open status");
+                       panel->battery.area.on_screen = 0;
+                       continue;
+               }
+               fclose(fp);
 
                battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
                battery->area.height = panel->area.height - (2 * battery->area.posy);
This page took 0.023049 seconds and 4 git commands to generate.