i3 - improved tiling WM


Added bitrate_format options to wireless configuration block

Patch status: needinfo

Patch by Enrico Carlesso

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

b/i3status.c

16
@@ -146,6 +146,40 @@ static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value,
17
 }
18
 
19
 /*
20
+ * Format bitrate with provided formatter.
21
+ *
22
+ * Basically, a clone of iw_print_bitrate, but accepting a format instead of forcing "%g %cb/s"
23
+ * see http://sourcecodebrowser.com/wireless-tools/28/iwlib_8c.html#ad1674f1115db41e1f3405f915c772783
24
+ *
25
+ */
26
+void i3_iw_print_bitrate(char *buffer, int buflen, int bitrate, const char *bitrate_format)
27
+{
28
+        double      rate = bitrate;
29
+        char        scale;
30
+        int         divisor;
31
+
32
+        if(rate >= GIGA)
33
+        {
34
+                scale = 'G';
35
+                divisor = GIGA;
36
+        }
37
+        else
38
+        {
39
+                if(rate >= MEGA)
40
+                {
41
+                        scale = 'M';
42
+                        divisor = MEGA;
43
+                }
44
+                else
45
+                {
46
+                        scale = 'k';
47
+                        divisor = KILO;
48
+                }
49
+        }
50
+        snprintf(buffer, buflen, bitrate_format, rate / divisor, scale);
51
+}
52
+
53
+/*
54
  * Validates a color in "#RRGGBB" format
55
  *
56
  */
57
@@ -292,6 +326,7 @@ int main(int argc, char *argv[]) {
58
         cfg_opt_t wireless_opts[] = {
59
                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
60
                 CFG_STR("format_down", "W: down", CFGF_NONE),
61
+                CFG_STR("bitrate_format", "%g %c/s", CFGF_NONE),
62
                 CFG_CUSTOM_ALIGN_OPT,
63
                 CFG_CUSTOM_COLOR_OPTS,
64
                 CFG_CUSTOM_MIN_WIDTH_OPT,
65
@@ -576,7 +611,7 @@ int main(int argc, char *argv[]) {
66
 
67
                         CASE_SEC_TITLE("wireless") {
68
                                 SEC_OPEN_MAP("wireless");
69
-                                print_wireless_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
70
+                                print_wireless_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "bitrate_format"));
71
                                 SEC_CLOSE_MAP;
72
                         }
73
 

b/include/i3status.h

78
@@ -142,6 +142,10 @@ enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
79
 		} \
80
 	} while (0)
81
 
82
+#define KILO 1e3
83
+#define MEGA 1e6
84
+#define GIGA 1e9
85
+
86
 
87
 typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
88
 
89
@@ -170,13 +174,14 @@ char *auto_detect_format();
90
 /* src/print_time.c */
91
 void set_timezone(const char *tz);
92
 
93
+void i3_iw_print_bitrate(char *buffer, int buflen, int bitrate, const char *bitrate_format);
94
 void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
95
 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *prefix_type, const char *threshold_type, const double low_threshold);
96
 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds);
97
 void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t);
98
 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
99
 const char *get_ip_addr();
100
-void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
101
+void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *bitrate_format);
102
 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
103
 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
104
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);

b/man/i3status.man

109
@@ -63,6 +63,7 @@ order += "tztime berlin"
110
 wireless wlan0 {
111
         format_up = "W: (%quality at %essid, %bitrate) %ip"
112
         format_down = "W: down"
113
+        bitrate_format = "%g %cb/s"
114
 }
115
 
116
 ethernet eth0 {
117
@@ -272,12 +273,14 @@ something is active, like for example a VPN tunnel managed by NetworkManager.
118
 
119
 Gets the link quality and ESSID of the given wireless network interface. You
120
 can specify different format strings for the network being connected or not
121
-connected.
122
+connected. You can also provide a printf-like format string for bitrate.
123
 
124
 *Example order*: +wireless wlan0+
125
 
126
 *Example format*: +W: (%quality at %essid, %bitrate) %ip+
127
 
128
+*Example bitrate format*: +%g %cb/s+
129
+
130
 === Ethernet
131
 
132
 Gets the IP address and (if possible) the link speed of the given ethernet

b/src/print_wireless_info.c

137
@@ -320,7 +320,7 @@ static int get_wireless_info(const char *interface, wireless_info_t *info) {
138
 	return 0;
139
 }
140
 
141
-void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
142
+void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *bitrate_format) {
143
         const char *walk;
144
         char *outwalk = buffer;
145
         wireless_info_t info;
146
@@ -404,7 +404,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
147
                 if (BEGINS_WITH(walk+1, "bitrate")) {
148
                         char br_buffer[128];
149
 
150
-                        iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
151
+                        i3_iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, bitrate_format);
152
 
153
                         outwalk += sprintf(outwalk, "%s", br_buffer);
154
                         walk += strlen("bitrate");