i3 - improved tiling WM


Add support for path_exists directive.

Patch status: needinfo

Patch by Kinware AB

Long description:

Third iteration of patch.

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

b/i3status.c

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

b/include/i3status.h

59
@@ -152,6 +152,7 @@ void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
60
 const char *get_ip_addr();
61
 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
62
 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
63
+void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
64
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
65
 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
66
 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

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

b/src/general.c

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

b/src/print_path_exists.c

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