i3 - improved tiling WM


Added bitrate_format options to wireless configuration block

Patch status: superseded

Patch by Enrico Carlesso

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

b/i3status.c

16
@@ -292,6 +292,7 @@ int main(int argc, char *argv[]) {
17
         cfg_opt_t wireless_opts[] = {
18
                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
19
                 CFG_STR("format_down", "W: down", CFGF_NONE),
20
+                CFG_STR("bitrate_format", "%g %c/s", CFGF_NONE),
21
                 CFG_CUSTOM_ALIGN_OPT,
22
                 CFG_CUSTOM_COLOR_OPTS,
23
                 CFG_CUSTOM_MIN_WIDTH_OPT,
24
@@ -576,7 +577,7 @@ int main(int argc, char *argv[]) {
25
 
26
                         CASE_SEC_TITLE("wireless") {
27
                                 SEC_OPEN_MAP("wireless");
28
-                                print_wireless_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
29
+                                print_wireless_info(json_gen, buffer, title, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "bitrate_format"));
30
                                 SEC_CLOSE_MAP;
31
                         }
32
 

b/include/i3status.h

37
@@ -142,6 +142,11 @@ enum { O_DZEN2, O_XMOBAR, O_I3BAR, O_TERM, O_NONE } output_format;
38
 		} \
39
 	} while (0)
40
 
41
+#define KILO 1e3
42
+#define MEGA 1e6
43
+#define GIGA 1e9
44
+
45
+
46
 
47
 typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
48
 
49
@@ -176,7 +181,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
50
 void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t);
51
 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
52
 const char *get_ip_addr();
53
-void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
54
+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);
55
 void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
56
 void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
57
 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);

b/man/i3status.man

62
@@ -63,6 +63,7 @@ order += "tztime berlin"
63
 wireless wlan0 {
64
         format_up = "W: (%quality at %essid, %bitrate) %ip"
65
         format_down = "W: down"
66
+        bitrate_format = "%g %cb/s"
67
 }
68
 
69
 ethernet eth0 {
70
@@ -272,12 +273,19 @@ something is active, like for example a VPN tunnel managed by NetworkManager.
71
 
72
 Gets the link quality and ESSID of the given wireless network interface. You
73
 can specify different format strings for the network being connected or not
74
-connected.
75
+connected. You can also provide a printf-like format string for bitrate. This
76
+can be especially useful if you have a dual band access point and the default
77
+format displays values such as 96, 130, 158.6 changing the width of the text.
78
+Setting bitrate format to "%5.1f %cb/s" will always take the same amount of
79
+characters. The first argument is a float number indicating the scaled speed,
80
+the second is a character for the scale, K for Kilo, M for Mega and G for Giga.
81
 
82
 *Example order*: +wireless wlan0+
83
 
84
 *Example format*: +W: (%quality at %essid, %bitrate) %ip+
85
 
86
+*Example bitrate format*: +%g %cb/s+
87
+
88
 === Ethernet
89
 
90
 Gets the IP address and (if possible) the link speed of the given ethernet

b/src/print_wireless_info.c

95
@@ -71,6 +71,40 @@ typedef struct {
96
         int bitrate;
97
 } wireless_info_t;
98
 
99
+/*
100
+ * Format bitrate with provided formatter.
101
+ *
102
+ * Basically, a clone of iw_print_bitrate, but accepting a format instead of forcing "%g %cb/s"
103
+ * see http://sourcecodebrowser.com/wireless-tools/28/iwlib_8c.html#ad1674f1115db41e1f3405f915c772783
104
+ *
105
+ */
106
+void i3_iw_print_bitrate(char *buffer, int buflen, int bitrate, const char *bitrate_format)
107
+{
108
+        double      rate = bitrate;
109
+        char        scale;
110
+        int         divisor;
111
+
112
+        if(rate >= GIGA)
113
+        {
114
+                scale = 'G';
115
+                divisor = GIGA;
116
+        }
117
+        else
118
+        {
119
+                if(rate >= MEGA)
120
+                {
121
+                        scale = 'M';
122
+                        divisor = MEGA;
123
+                }
124
+                else
125
+                {
126
+                        scale = 'k';
127
+                        divisor = KILO;
128
+                }
129
+        }
130
+        snprintf(buffer, buflen, bitrate_format, rate / divisor, scale);
131
+}
132
+
133
 static int get_wireless_info(const char *interface, wireless_info_t *info) {
134
         memset(info, 0, sizeof(wireless_info_t));
135
 
136
@@ -320,7 +354,7 @@ static int get_wireless_info(const char *interface, wireless_info_t *info) {
137
 	return 0;
138
 }
139
 
140
-void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) {
141
+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) {
142
         const char *walk;
143
         char *outwalk = buffer;
144
         wireless_info_t info;
145
@@ -404,7 +438,7 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
146
                 if (BEGINS_WITH(walk+1, "bitrate")) {
147
                         char br_buffer[128];
148
 
149
-                        iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate);
150
+                        i3_iw_print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, bitrate_format);
151
 
152
                         outwalk += sprintf(outwalk, "%s", br_buffer);
153
                         walk += strlen("bitrate");