]> git.sur5r.net Git - i3/i3status/blobdiff - wmiistatus.c
Make wmiistatus run all the time and wait for wmii_path to exist
[i3/i3status] / wmiistatus.c
index a7f0b7ab17742b1b89fdda787991a2f57d57a6ec..48c504c7219016569e4103266c51f486d3021c28 100644 (file)
@@ -50,6 +50,9 @@
 #include <glob.h>
 #include <dirent.h>
 #include <getopt.h>
+#include <linux/ethtool.h>
+#include <linux/sockios.h>
+
 
 #define _IS_WMIISTATUS_C
 #include "wmiistatus.h"
@@ -111,6 +114,31 @@ static void create_file(const char *name) {
        (void)close(fd);
 }
 
+static void setup(void) {
+       unsigned int i;
+       struct stat statbuf;
+       char pathbuf[512];
+
+       /* Wait until wmii_path/rbar exists */
+       for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
+
+       cleanup_rbar_dir();
+       if (wlan_interface)
+               create_file(concat(order[ORDER_WLAN],"wlan"));
+       if (eth_interface)
+               create_file(concat(order[ORDER_ETH],"eth"));
+       if (battery_path)
+               create_file(concat(order[ORDER_BATTERY],"battery"));
+       create_file(concat(order[ORDER_LOAD],"load"));
+       if (time_format)
+               create_file(concat(order[ORDER_TIME],"time"));
+       for (i = 0; i < num_run_watches; i += 2) {
+               snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
+               create_file(pathbuf);
+       }
+
+}
+
 /*
  * Writes the given message in the corresponding file in wmii's /rbar directory
  *
@@ -120,8 +148,11 @@ static void write_to_statusbar(const char *name, const char *message) {
        int fd;
 
        (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
-       if ((fd = open(pathbuf, O_RDWR)) == -1)
-               exit(EXIT_FAILURE);
+       if ((fd = open(pathbuf, O_RDWR)) == -1) {
+               /* Try to re-setup stuff and just continue */
+               setup();
+               return;
+       }
        if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
                exit(EXIT_FAILURE);
        (void)close(fd);
@@ -148,7 +179,8 @@ void die(const char *fmt, ...) {
        (void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
        va_end(ap);
 
-       write_error_to_statusbar(buffer);
+       if (wmii_path != NULL)
+               write_error_to_statusbar(buffer);
        exit(EXIT_FAILURE);
 }
 
@@ -186,9 +218,11 @@ static char *get_battery_info() {
        (void)read(fd, buf, sizeof(buf));
        for (walk = buf, last = buf; (walk-buf) < 1024; walk++)
                if (*walk == '=') {
-                       if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
+                       if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN") ||
+                           BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN"))
                                full_design = atoi(walk+1);
-                       else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW"))
+                       else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
+                                BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
                                remaining = atoi(walk+1);
                        else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
                                present_rate = atoi(walk+1);
@@ -262,7 +296,7 @@ static char *get_wireless_info() {
                        else (void)snprintf(part, sizeof(part), "W: down");
 
                        } else {
-                               char *ip_address;
+                               const char *ip_address;
                                (void)snprintf(part, sizeof(part), "W: (%02d%%) ", quality);
                                ip_address = get_ip_address(wlan_interface);
                                strcpy(part+strlen(part), ip_address);
@@ -311,10 +345,34 @@ static const char *get_ip_address(const char *interface) {
 static char *get_eth_info() {
        static char part[512];
        const char *ip_address = get_ip_address(eth_interface);
+       int ethspeed = 0;
+
+       if (get_ethspeed) {
+               struct ifreq ifr;
+               struct ethtool_cmd ecmd;
+               int fd, err;
+
+               if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
+                       write_error_to_statusbar("Could not open socket");
+
+               ecmd.cmd = ETHTOOL_GSET;
+               (void)memset(&ifr, 0, sizeof(ifr));
+               ifr.ifr_data = (caddr_t)&ecmd;
+               (void)strcpy(ifr.ifr_name, eth_interface);
+               if ((err = ioctl(fd, SIOCETHTOOL, &ifr)) == 0)
+                       ethspeed = ecmd.speed;
+               else write_error_to_statusbar("Could not get interface speed. Insufficient privileges?");
+
+               (void)close(fd);
+       }
 
        if (ip_address == NULL)
                (void)snprintf(part, sizeof(part), "E: down");
-       else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
+       else {
+               if (get_ethspeed)
+                       (void)snprintf(part, sizeof(part), "E: %s (%d Mbit/s)", ip_address, ethspeed);
+               else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
+       }
 
        return part;
 }
@@ -370,21 +428,10 @@ int main(int argc, char *argv[]) {
                if ((char)o == 'c')
                        configfile = optarg;
 
-       load_configuration(configfile);
-       cleanup_rbar_dir();
-       if (wlan_interface)
-               create_file(concat(order[ORDER_WLAN],"wlan"));
-       if (eth_interface)
-               create_file(concat(order[ORDER_ETH],"eth"));
-       if (battery_path)
-               create_file(concat(order[ORDER_BATTERY],"battery"));
-       create_file(concat(order[ORDER_LOAD],"load"));
-       if (time_format)
-               create_file(concat(order[ORDER_TIME],"time"));
-       for (i = 0; i < num_run_watches; i += 2) {
-               snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
-               create_file(pathbuf);
-       }
+       if (load_configuration(configfile) < 0)
+               return EXIT_FAILURE;
+
+       setup();
 
        while (1) {
                for (i = 0; i < num_run_watches; i += 2) {