]> git.sur5r.net Git - i3/i3status/blob - wmiistatus.c
Make some formats/paths configurable, add initscript
[i3/i3status] / wmiistatus.c
1 /*
2  * Generates a status line for use with wmii or other minimal window managers
3  *
4  *
5  * Copyright (c) 2008 Michael Stapelberg and contributors
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright notice, this
15  *   list of conditions and the following disclaimer in the documentation and/or other
16  *   materials provided with the distribution.
17  *
18  * * Neither the name of Michael Stapelberg nor the names of contributors
19  *   may be used to endorse or promote products derived from this software without
20  *   specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31  * DAMAGE.
32  *
33  */
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <time.h>
40 #include <stdbool.h>
41 #include <stdarg.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <ctype.h>
45 #include <net/if.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <glob.h>
51
52 #include "wmiistatus.h"
53 #include "config.h"
54
55 static void write_to_statusbar(const char *message) {
56         int fd = open(wmii_path, O_RDWR);
57         if (fd == -1)
58                 exit(-2);
59         write(fd, message, strlen(message));
60         close(fd);
61 }
62
63 /*
64  * Write errormessage to statusbar and exit
65  *
66  */
67 static void die(const char *fmt, ...) {
68         char buffer[512];
69         va_list ap;
70         va_start(ap, fmt);
71         vsprintf(buffer, fmt, ap);
72         va_end(ap);
73
74         write_to_statusbar(buffer);
75         exit(-1);
76 }
77
78 static char *skip_character(char *input, char character, int amount) {
79         char *walk;
80         int len = strlen(input),
81             blanks = 0;
82
83         for (walk = input; ((walk - input) < len) && (blanks < amount); walk++)
84                 if (*walk == character)
85                         blanks++;
86
87         return (walk == input ? walk : walk-1);
88 }
89
90 static void push_part(const char *input, const int n) {
91         if (first_push)
92                 first_push = false;
93         else
94                 strncpy(output+strlen(output), " | ", strlen(" | "));
95         strncpy(output+strlen(output), input, n);
96 }
97
98 /*
99  * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
100  * not the full capacity.
101  *
102  */
103 static char *get_battery_info() {
104         char buf[1024];
105         static char part[512];
106         char *walk, *last = buf;
107         int fd = open(battery, O_RDONLY);
108         if (fd == -1)
109                 die("Could not open %s", battery);
110         int full_design = -1,
111             remaining = -1,
112             present_rate = -1;
113         charging_status_t status = CS_DISCHARGING;
114         memset(part, '\0', sizeof(part));
115         read(fd, buf, sizeof(buf));
116         for (walk = buf; (walk-buf) < 1024; walk++)
117                 if (*walk == '=') {
118                         if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
119                                 full_design = atoi(walk+1);
120                         else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW"))
121                                 remaining = atoi(walk+1);
122                         else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
123                                 present_rate = atoi(walk+1);
124                         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
125                                 status = CS_CHARGING;
126                         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
127                                 status = CS_FULL;
128                 } else if (*walk == '\n')
129                         last = walk+1;
130         close(fd);
131
132         if ((full_design != -1) && (remaining != -1) && (present_rate != -1)) {
133                 float remaining_time, perc;
134                 if (status == CS_CHARGING)
135                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
136                 else if (status == CS_DISCHARGING)
137                         remaining_time = ((float)remaining / (float)present_rate);
138                 else {
139                         snprintf(part, sizeof(part), "FULL");
140                         return part;
141                 }
142                 perc = ((float)remaining / (float)full_design);
143
144                 int seconds = (int)(remaining_time * 3600.0);
145                 int hours = seconds / 3600;
146                 seconds -= (hours * 3600);
147                 int minutes = seconds / 60;
148                 seconds -= (minutes * 60);
149
150                 sprintf(part, "%s %.02f%% %02d:%02d:%02d", (status == CS_CHARGING? "CHR" : "BAT"),
151                         (perc * 100), hours, minutes, seconds);
152         }
153         return part;
154 }
155
156 /*
157  * Just parses /proc/net/wireless
158  *
159  */
160 static char *get_wireless_info() {
161         char buf[1024];
162         static char part[512];
163         char *interfaces;
164         memset(buf, '\0', sizeof(buf));
165         memset(part, '\0', sizeof(part));
166
167         int fd = open("/proc/net/wireless", O_RDONLY);
168         if (fd == -1)
169                 die("Could not open /proc/net/wireless");
170         read(fd, buf, sizeof(buf));
171         close(fd);
172
173         interfaces = skip_character(buf, '\n', 2) + 1;
174         while (interfaces < buf+strlen(buf)) {
175                 while (isspace((int)*interfaces))
176                         interfaces++;
177                 if (strncmp(interfaces, wlan_interface, strlen(wlan_interface)) == 0) {
178                         /* Skip status field (0000) */
179                         interfaces += strlen(wlan_interface) + 2;
180                         interfaces = skip_character(interfaces, ' ', 1);
181                         while (isspace((int)*interfaces))
182                                 interfaces++;
183                         int quality = atoi(interfaces);
184                         /* For some reason, I get 255 sometimes */
185                         if ((quality == 255) || (quality == 0))
186                                 snprintf(part, sizeof(part), "W: down");
187                         else {
188                                 snprintf(part, sizeof(part), "W: (%02d%%) ", quality);
189                                 char *ip_address = get_ip_address(wlan_interface);
190                                 strcpy(part+strlen(part), ip_address);
191                         }
192
193                         return part;
194                 }
195                 interfaces = skip_character(interfaces, '\n', 1) + 1;
196         }
197
198         return part;
199 }
200
201 static char *get_ip_address(const char *interface) {
202         static char part[512];
203         struct ifreq ifr;
204         memset(part, '\0', sizeof(part));
205
206         int fd = socket(AF_INET, SOCK_DGRAM, 0);
207
208         strcpy(ifr.ifr_name, interface);
209         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
210                 die("Could not get interface flags (SIOCGIFFLAGS)");
211
212         if (!(ifr.ifr_flags & IFF_UP) ||
213             !(ifr.ifr_flags & IFF_RUNNING)) {
214                 close(fd);
215                 return NULL;
216         }
217
218         strcpy(ifr.ifr_name, interface);
219         ifr.ifr_addr.sa_family = AF_INET;
220         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
221                 struct sockaddr_in addr;
222                 memcpy(&addr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
223                 inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, sizeof(struct sockaddr_in));
224                 if (strlen(part) == 0)
225                         sprintf(part, "no IP");
226         }
227
228         close(fd);
229         return part;
230 }
231
232 static char *get_eth_info() {
233         static char part[512];
234         char *ip_address = get_ip_address(eth_interface);
235
236         if (ip_address == NULL)
237                 snprintf(part, sizeof(part), "E: down");
238         else snprintf(part, sizeof(part), "E: %s", ip_address);
239
240         return part;
241 }
242
243 /*
244  * Checks if the PID in path is still valid by checking if /proc/<pid> exists
245  *
246  */
247 static bool process_runs(const char *path) {
248         char pidbuf[512],
249              procbuf[512];
250         static glob_t globbuf;
251         struct stat statbuf;
252
253         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
254                 die("glob() failed");
255         const char *real_path = (globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
256         int fd = open(real_path, O_RDONLY);
257         globfree(&globbuf);
258         if (fd < 0)
259                 return false;
260         read(fd, pidbuf, sizeof(pidbuf));
261         close(fd);
262         sprintf(procbuf, "/proc/%s", pidbuf);
263         return (stat(procbuf, &statbuf) >= 0);
264 }
265
266 int main(void) {
267         char part[512],
268              *end;
269         unsigned int i;
270
271         while (1) {
272                 memset(output, '\0', sizeof(output));
273                 first_push = true;
274
275                 for (i = 0; i < sizeof(run_watches) / sizeof(char*); i += 2) {
276                         sprintf(part, "%s: %s", run_watches[i], (process_runs(run_watches[i+1]) ? "yes" : "no"));
277                         push_part(part, strlen(part));
278                 }
279
280                 char *wireless_info = get_wireless_info();
281                 push_part(wireless_info, strlen(wireless_info));
282
283                 char *eth_info = get_eth_info();
284                 push_part(eth_info, strlen(eth_info));
285
286                 char *battery_info = get_battery_info();
287                 push_part(battery_info, strlen(battery_info));
288
289                 /* Get load */
290                 int load_avg = open("/proc/loadavg", O_RDONLY);
291                 if (load_avg == -1)
292                         die("Could not open /proc/loadavg");
293                 read(load_avg, part, sizeof(part));
294                 close(load_avg);
295                 end = skip_character(part, ' ', 3);
296                 push_part(part, (end-part));
297
298                 /* Get date & time */
299                 time_t current_time = time(NULL);
300                 struct tm *current_tm = localtime(&current_time);
301                 strftime(part, sizeof(part), time_format, current_tm);
302                 push_part(part, strlen(part));
303
304                 write_to_statusbar(output);
305
306                 sleep(1);
307         }
308 }