]> git.sur5r.net Git - i3/i3status/blobdiff - wmiistatus.c
Bugfix: Check if the present rate is > 0, not only if it is not set
[i3/i3status] / wmiistatus.c
index ac1f778953c37484e82a4a04ac02dad85304a126..cba4d8b78e1c4df7348f2eea2fa2bb85498bbdf0 100644 (file)
@@ -64,6 +64,8 @@
 
 #include "wmiistatus.h"
 
+#define BAR "^fg(#333333)^p(5;-2)^ro(2)^p()^fg()^p(5)"
+
 /* socket file descriptor for general purposes */
 static int general_socket;
 
@@ -92,11 +94,28 @@ static char *concat(const char *str1, const char *str2) {
        return concatbuf;
 }
 
+/*
+ * Returns the correct color format for dzen (^fg(color)) or wmii (color <normcolors>)
+ *
+ */
+static char *color(const char *colorstr) {
+       static char colorbuf[32];
+#ifdef DZEN
+       (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", colorstr);
+#else
+       (void)snprintf(colorbuf, sizeof(colorbuf), "%s %s ", colorstr, wmii_normcolors);
+#endif
+       return colorbuf;
+}
+
 /*
  * Cleans wmii's /rbar directory by deleting all regular files
  *
  */
 static void cleanup_rbar_dir() {
+#ifdef DZEN
+       return;
+#endif
        struct dirent *ent;
        DIR *dir;
        char pathbuf[strlen(wmii_path)+256+1];
@@ -121,6 +140,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;
@@ -135,7 +157,7 @@ static void create_file(const char *name) {
        if ((fd = open(pathbuf, flags, S_IRUSR | S_IWUSR)) < 0)
                exit(EXIT_FAILURE);
        if (use_colors) {
-               char *tmp = concat("#888888 ", wmii_normcolors);
+               char *tmp = color("#888888");
                if (write(fd, tmp, strlen(tmp)) != (ssize_t)strlen(tmp))
                        exit(EXIT_FAILURE);
        }
@@ -149,11 +171,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)
@@ -175,7 +199,17 @@ static void setup(void) {
  * Writes the given message in the corresponding file in wmii's /rbar directory
  *
  */
-static void write_to_statusbar(const char *name, const char *message) {
+static void write_to_statusbar(const char *name, const char *message, bool final_entry) {
+#ifdef DZEN
+       if (final_entry) {
+               (void)printf("%s^p(6)\n", message);
+               fflush(stdout);
+               return;
+       }
+       (void)printf("%s" BAR, message);
+       return;
+#endif
+
        char pathbuf[strlen(wmii_path)+256+1];
        int fd;
 
@@ -197,7 +231,7 @@ static void write_to_statusbar(const char *name, const char *message) {
 static void write_error_to_statusbar(const char *message) {
        cleanup_rbar_dir();
        create_file("error");
-       write_to_statusbar("error", message);
+       write_to_statusbar("error", message, true);
 }
 
 /*
@@ -273,7 +307,7 @@ 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) && (present_rate > 0)) {
                float remaining_time;
                int seconds, hours, minutes;
                if (status == CS_CHARGING)
@@ -352,11 +386,10 @@ static char *get_wireless_info() {
                        continue;
                if ((quality == UCHAR_MAX) || (quality == 0)) {
                        if (use_colors)
-                               (void)snprintf(part, sizeof(part), "%s%s",
-                                       concat("#FF0000 ", wmii_normcolors), " W: down");
+                               (void)snprintf(part, sizeof(part), "%sW: down", color("#FF0000"));
                        else (void)snprintf(part, sizeof(part), "W: down");
-               } else (void)snprintf(part, sizeof(part), "W: (%03d%%) %s",
-                               quality, get_ip_address(wlan_interface));
+               } else (void)snprintf(part, sizeof(part), "%sW: (%03d%%) %s",
+                               color("#00FF00"), quality, get_ip_address(wlan_interface));
                return part;
        }
 
@@ -453,7 +486,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) {
@@ -485,7 +518,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);
@@ -498,6 +533,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")
                {
@@ -540,8 +576,10 @@ static int load_configuration(const char *configfile) {
        }
        fclose(handle);
 
+#ifndef DZEN
        if (wmii_path == NULL)
                exit(EXIT_FAILURE);
+#endif
 
        return result;
 }
@@ -580,23 +618,21 @@ int main(int argc, char *argv[]) {
                for (i = 0; i < num_run_watches; i += 2) {
                        bool running = process_runs(run_watches[i+1]);
                        if (use_colors)
-                               snprintf(part, sizeof(part), "%s %s: %s",
-                                       (running ?
-                                               concat("#00FF00 ", wmii_normcolors) :
-                                               concat("#FF0000 ", wmii_normcolors)),
+                               snprintf(part, sizeof(part), "%s%s: %s",
+                                       (running ? color("#00FF00") : color("#FF0000")),
                                        run_watches[i],
                                        (running ? "yes" : "no"));
                        else snprintf(part, sizeof(part), "%s: %s", run_watches[i], (running ? "yes" : "no"));
                        snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
-                       write_to_statusbar(pathbuf, part);
+                       write_to_statusbar(pathbuf, part, false);
                }
 
                if (wlan_interface)
-                       write_to_statusbar(concat(order[ORDER_WLAN], "wlan"), get_wireless_info());
+                       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());
+                       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());
+                       write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(), false);
 
                /* Get load */
 #ifdef LINUX
@@ -619,14 +655,14 @@ int main(int argc, char *argv[]) {
                                (double)load.ldavg[1] / scale,
                                (double)load.ldavg[2] / scale);
 #endif
-               write_to_statusbar(concat(order[ORDER_LOAD], "load"), part);
+               write_to_statusbar(concat(order[ORDER_LOAD], "load"), part, !time_format);
 
                if (time_format) {
                        /* Get date & time */
                        time_t current_time = time(NULL);
                        struct tm *current_tm = localtime(&current_time);
                        (void)strftime(part, sizeof(part), time_format, current_tm);
-                       write_to_statusbar(concat(order[ORDER_TIME], "time"), part);
+                       write_to_statusbar(concat(order[ORDER_TIME], "time"), part, true);
                }
 
                sleep(interval);