i3 - improved tiling WM


Add support for path_exists directive.

Patch status: needinfo

Patch by Kinware AB

To apply this patch, use:
curl http://cr.i3wm.org/patch/319/raw.patch | git am

b/i3status.c

18
@@ -210,6 +210,13 @@ int main(int argc, char *argv[]) {
19
                 CFG_END()
20
         };
21
 
22
+        cfg_opt_t path_exists_opts[] = {
23
+                CFG_STR("path", NULL, CFGF_NONE),
24
+                CFG_STR("format", "%title: %status", CFGF_NONE),
25
+                CFG_CUSTOM_COLOR_OPTS,
26
+                CFG_END()
27
+        };
28
+
29
         cfg_opt_t wireless_opts[] = {
30
                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
31
                 CFG_STR("format_down", "W: down", CFGF_NONE),
32
@@ -297,6 +304,7 @@ int main(int argc, char *argv[]) {
33
                 CFG_STR_LIST("order", "{}", CFGF_NONE),
34
                 CFG_SEC("general", general_opts, CFGF_NONE),
35
                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
36
+                CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
37
                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
38
                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
39
                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
40
@@ -480,6 +488,12 @@ int main(int argc, char *argv[]) {
41
                                 SEC_CLOSE_MAP;
42
                         }
43
 
44
+                        CASE_SEC_TITLE("path_exists") {
45
+                                SEC_OPEN_MAP("path_exists");
46
+                                print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
47
+                                SEC_CLOSE_MAP;
48
+                        }
49
+
50
                         CASE_SEC_TITLE("disk") {
51
                                 SEC_OPEN_MAP("disk_info");
52
                                 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"));

b/include/i3status.h

57
@@ -152,6 +152,7 @@ void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
58
 const char *get_ip_addr();
59
 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
60
 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
61
+void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
62
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
63
 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
64
 void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);

b/man/i3status.man

69
@@ -50,7 +50,8 @@ general {
70
 order += "ipv6"
71
 order += "disk /"
72
 order += "run_watch DHCP"
73
-order += "run_watch VPN"
74
+order += "run_watch VPNC"
75
+order += "path_exists VPN"
76
 order += "wireless wlan0"
77
 order += "ethernet eth0"
78
 order += "battery 0"
79
@@ -81,10 +82,16 @@ run_watch DHCP {
80
         pidfile = "/var/run/dhclient*.pid"
81
 }
82
 
83
-run_watch VPN {
84
+run_watch VPNC {
85
+        # file containing the PID of a vpnc process
86
         pidfile = "/var/run/vpnc/pid"
87
 }
88
 
89
+path_exists VPN {
90
+        # path exists when a VPN tunnel launched by nmcli/nm-applet is active
91
+        path = "/proc/sys/net/ipv4/conf/tun0"
92
+}
93
+
94
 tztime local {
95
         format = "%Y-%m-%d %H:%M:%S"
96
 }
97
@@ -193,6 +200,16 @@ a specific application, such as a VPN client or your DHCP client is running.
98
 
99
 *Example format*: +%title: %status+
100
 
101
+=== Path-exists
102
+
103
+Expands the given path and checks if it exists in the filesystem. You can use
104
+this to check if something is active, like for example a VPN tunnel managed by
105
+NetworkManager.
106
+
107
+*Example order*: +path_exists VPN+
108
+
109
+*Example format*: +%title: %status+
110
+
111
 === Wireless
112
 
113
 Gets the link quality and ESSID of the given wireless network interface. You

b/src/general.c

118
@@ -6,6 +6,7 @@
119
 #include <stdlib.h>
120
 #include <unistd.h>
121
 #include <sys/fcntl.h>
122
+#include <sys/stat.h>
123
 
124
 #include "i3status.h"
125
 

b/src/print_path_exists.c

131
@@ -0,0 +1,35 @@
132
+#include <stdio.h>
133
+#include <string.h>
134
+#include <yajl/yajl_gen.h>
135
+#include <yajl/yajl_version.h>
136
+#include <sys/stat.h>
137
+#include "i3status.h"
138
+
139
+void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format) {
140
+        const char *walk;
141
+        char *outwalk = buffer;
142
+        struct stat st;
143
+        const bool exists = (stat(path, &st) == 0);
144
+
145
+        INSTANCE(path);
146
+
147
+        START_COLOR((exists ? "color_good" : "color_bad"));
148
+
149
+        for (walk = format; *walk != '\0'; walk++) {
150
+                if (*walk != '%') {
151
+                        *(outwalk++) = *walk;
152
+                        continue;
153
+                }
154
+
155
+                if (strncmp(walk+1, "title", strlen("title")) == 0) {
156
+                        outwalk += sprintf(outwalk, "%s", title);
157
+                        walk += strlen("title");
158
+                } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
159
+                        outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
160
+                        walk += strlen("status");
161
+                }
162
+        }
163
+
164
+        END_COLOR;
165
+        OUTPUT_FULL_TEXT(buffer);
166
+}