i3 - improved tiling WM


Add support for path_exists directive.

Patch status: needinfo

Patch by Kinware AB

Long description:

Updated patch renaming file_exists to path_exists and fixing things
that came up in the code review.

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

b/i3status.c

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

b/include/i3status.h

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

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

b/src/general.c

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

b/src/print_path_exists.c

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