]> git.sur5r.net Git - i3/i3status/blobdiff - wmiistatus.c
Add forgotten file
[i3/i3status] / wmiistatus.c
index 2fc024e47c98f39f6ced8e79f7513383e594ca89..c0a1fe38bb5f87acb8fb458d5d5576c20c7106d0 100644 (file)
@@ -52,6 +52,8 @@
 #include <dirent.h>
 #include <getopt.h>
 
+#include "queue.h"
+
 #ifdef LINUX
 #include <linux/ethtool.h>
 #include <linux/sockios.h>
 
 #define BAR "^fg(#333333)^p(5;-2)^ro(2)^p()^fg()^p(5)"
 
+struct battery {
+       const char *path;
+       SIMPLEQ_ENTRY(battery) batteries;
+};
+
+SIMPLEQ_HEAD(battery_head, battery) batteries;
+
 /* socket file descriptor for general purposes */
 static int general_socket;
 
@@ -113,6 +122,9 @@ static char *color(const char *colorstr) {
  *
  */
 static void cleanup_rbar_dir() {
+#ifdef DZEN
+       return;
+#endif
        struct dirent *ent;
        DIR *dir;
        char pathbuf[strlen(wmii_path)+256+1];
@@ -137,6 +149,9 @@ static void cleanup_rbar_dir() {
  *
  */
 static void create_file(const char *name) {
+#ifdef DZEN
+       return;
+#endif
        char pathbuf[strlen(wmii_path)+256+1];
        int fd;
        int flags = O_CREAT | O_WRONLY;
@@ -165,11 +180,13 @@ static void create_file(const char *name) {
  */
 static void setup(void) {
        unsigned int i;
-       struct stat statbuf;
        char pathbuf[512];
 
+#ifndef DZEN
+       struct stat statbuf;
        /* Wait until wmii_path/rbar exists */
        for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
+#endif
 
        cleanup_rbar_dir();
        if (wlan_interface)
@@ -266,7 +283,7 @@ static char *skip_character(char *input, char character, int amount) {
  * worn off your battery is.
  *
  */
-static char *get_battery_info() {
+static char *get_battery_info(const char *path) {
        char buf[1024];
        static char part[512];
        char *walk, *last;
@@ -276,7 +293,7 @@ static char *get_battery_info() {
            present_rate = -1;
        charging_status_t status = CS_DISCHARGING;
 
-       if ((fd = open(battery_path, O_RDONLY)) == -1)
+       if ((fd = open(path, O_RDONLY)) == -1)
                return "No battery found";
 
        memset(part, 0, sizeof(part));
@@ -299,7 +316,10 @@ static char *get_battery_info() {
                        last = walk+1;
        (void)close(fd);
 
-       if ((full_design != -1) && (remaining != -1) && (present_rate != -1)) {
+       if ((full_design == 1) || (remaining == -1))
+               return part;
+
+       if (present_rate > 0) {
                float remaining_time;
                int seconds, hours, minutes;
                if (status == CS_CHARGING)
@@ -319,6 +339,11 @@ static char *get_battery_info() {
                         (status == CS_DISCHARGING ? "BAT" : "FULL")),
                        (((float)remaining / (float)full_design) * 100),
                        hours, minutes, seconds);
+       } else {
+               (void)snprintf(part, sizeof(part), "%s %.02f%%",
+                       (status == CS_CHARGING ? "CHR" :
+                        (status == CS_DISCHARGING ? "BAT" : "FULL")),
+                       (((float)remaining / (float)full_design) * 100));
        }
        return part;
 }
@@ -478,7 +503,7 @@ static int load_configuration(const char *configfile) {
        if (handle == NULL)
                die("Could not open configfile\n");
        char dest_name[512], dest_value[512], whole_buffer[1026];
-       struct stat stbuf;
+
        while (!feof(handle)) {
                char *ret;
                if ((ret = fgets(whole_buffer, 1024, handle)) == whole_buffer) {
@@ -498,9 +523,13 @@ static int load_configuration(const char *configfile) {
                        eth_interface = strdup(dest_value);
                OPT("time_format")
                        time_format = strdup(dest_value);
-               OPT("battery_path")
-                       battery_path = strdup(dest_value);
-               OPT("color")
+               OPT("battery_path") {
+                       struct battery *new = calloc(1, sizeof(struct battery));
+                       if (new == NULL)
+                               die("Could not allocate memory\n");
+                       new->path = strdup(dest_value);
+                       SIMPLEQ_INSERT_TAIL(&batteries, new, batteries);
+               } OPT("color")
                        use_colors = true;
                OPT("get_ethspeed")
                        get_ethspeed = true;
@@ -510,7 +539,9 @@ static int load_configuration(const char *configfile) {
                        interval = atoi(dest_value);
                OPT("wmii_path")
                {
+#ifndef DZEN
                        static glob_t globbuf;
+                       struct stat stbuf;
                        if (glob(dest_value, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
                                die("glob() failed\n");
                        wmii_path = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : dest_value);
@@ -523,6 +554,7 @@ static int load_configuration(const char *configfile) {
                        }
                        if (wmii_path[strlen(wmii_path)-1] != '/')
                                die("wmii_path is not terminated by /\n");
+#endif
                }
                OPT("run_watch")
                {
@@ -565,8 +597,10 @@ static int load_configuration(const char *configfile) {
        }
        fclose(handle);
 
+#ifndef DZEN
        if (wmii_path == NULL)
                exit(EXIT_FAILURE);
+#endif
 
        return result;
 }
@@ -584,6 +618,8 @@ int main(int argc, char *argv[]) {
                {0, 0, 0, 0}
        };
 
+       SIMPLEQ_INIT(&batteries);
+
        while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
                if ((char)o == 'c')
                        configfile = optarg;
@@ -618,8 +654,10 @@ int main(int argc, char *argv[]) {
                        write_to_statusbar(concat(order[ORDER_WLAN], "wlan"), get_wireless_info(), false);
                if (eth_interface)
                        write_to_statusbar(concat(order[ORDER_ETH], "eth"), get_eth_info(), false);
-               if (battery_path)
-                       write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(), false);
+               struct battery *current_battery;
+               SIMPLEQ_FOREACH(current_battery, &batteries, batteries) {
+                       write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(current_battery->path), false);
+               }
 
                /* Get load */
 #ifdef LINUX