]> git.sur5r.net Git - i3/i3status/commitdiff
Add support for path_exists directive.
authorKinware AB <oss@kinware.com>
Tue, 12 Nov 2013 19:51:23 +0000 (20:51 +0100)
committerMichael Stapelberg <michael@stapelberg.de>
Fri, 22 Nov 2013 14:52:27 +0000 (15:52 +0100)
i3status.c
include/i3status.h
man/i3status.man
src/general.c
src/print_path_exists.c [new file with mode: 0644]

index 1b9c59502ece1c5394e97d6bd965813fc4d4bdac..e2befbc6eae0d3235fb06e65a60a5f686c4773cd 100644 (file)
@@ -210,6 +210,13 @@ int main(int argc, char *argv[]) {
                 CFG_END()
         };
 
+        cfg_opt_t path_exists_opts[] = {
+                CFG_STR("path", NULL, CFGF_NONE),
+                CFG_STR("format", "%title: %status", CFGF_NONE),
+                CFG_CUSTOM_COLOR_OPTS,
+                CFG_END()
+        };
+
         cfg_opt_t wireless_opts[] = {
                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
                 CFG_STR("format_down", "W: down", CFGF_NONE),
@@ -298,6 +305,7 @@ int main(int argc, char *argv[]) {
                 CFG_STR_LIST("order", "{}", CFGF_NONE),
                 CFG_SEC("general", general_opts, CFGF_NONE),
                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
+                CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
@@ -481,6 +489,12 @@ int main(int argc, char *argv[]) {
                                 SEC_CLOSE_MAP;
                         }
 
+                        CASE_SEC_TITLE("path_exists") {
+                                SEC_OPEN_MAP("path_exists");
+                                print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
+                                SEC_CLOSE_MAP;
+                        }
+
                         CASE_SEC_TITLE("disk") {
                                 SEC_OPEN_MAP("disk_info");
                                 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"));
index 64cd660150b5ac53ca8b9c605512b5d7e2a60f6f..e64cde5f48a45241b7dc0b35564cc4ff24b7c523 100644 (file)
@@ -152,6 +152,7 @@ void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
 const char *get_ip_addr();
 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
+void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
index 8c6f6167d9137697153b66bd0be23fe516fe1672..e18dbc7b12bc34d5f39e82da7307ebfd75dd2a9b 100644 (file)
@@ -50,7 +50,8 @@ general {
 order += "ipv6"
 order += "disk /"
 order += "run_watch DHCP"
-order += "run_watch VPN"
+order += "run_watch VPNC"
+order += "path_exists VPN"
 order += "wireless wlan0"
 order += "ethernet eth0"
 order += "battery 0"
@@ -81,10 +82,16 @@ run_watch DHCP {
         pidfile = "/var/run/dhclient*.pid"
 }
 
-run_watch VPN {
+run_watch VPNC {
+        # file containing the PID of a vpnc process
         pidfile = "/var/run/vpnc/pid"
 }
 
+path_exists VPN {
+        # path exists when a VPN tunnel launched by nmcli/nm-applet is active
+        path = "/proc/sys/net/ipv4/conf/tun0"
+}
+
 tztime local {
         format = "%Y-%m-%d %H:%M:%S"
 }
@@ -193,6 +200,15 @@ a specific application, such as a VPN client or your DHCP client is running.
 
 *Example format*: +%title: %status+
 
+=== Path-exists
+
+Checks if the given path exists in the filesystem. You can use this to check if
+something is active, like for example a VPN tunnel managed by NetworkManager.
+
+*Example order*: +path_exists VPN+
+
+*Example format*: +%title: %status+
+
 === Wireless
 
 Gets the link quality and ESSID of the given wireless network interface. You
index 7ec80a84688446b8831edb30499de5bdf5f165d4..c5b33b46f652142dbfad53fdd7a73463c217fd37 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/fcntl.h>
+#include <sys/stat.h>
 
 #include "i3status.h"
 
diff --git a/src/print_path_exists.c b/src/print_path_exists.c
new file mode 100644 (file)
index 0000000..c5225a1
--- /dev/null
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <string.h>
+#include <yajl/yajl_gen.h>
+#include <yajl/yajl_version.h>
+#include <sys/stat.h>
+#include "i3status.h"
+
+void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format) {
+        const char *walk;
+        char *outwalk = buffer;
+        struct stat st;
+        const bool exists = (stat(path, &st) == 0);
+
+        INSTANCE(path);
+
+        START_COLOR((exists ? "color_good" : "color_bad"));
+
+        for (walk = format; *walk != '\0'; walk++) {
+                if (*walk != '%') {
+                        *(outwalk++) = *walk;
+                        continue;
+                }
+
+                if (strncmp(walk+1, "title", strlen("title")) == 0) {
+                        outwalk += sprintf(outwalk, "%s", title);
+                        walk += strlen("title");
+                } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
+                        outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
+                        walk += strlen("status");
+                }
+        }
+
+        END_COLOR;
+        OUTPUT_FULL_TEXT(buffer);
+}