]> git.sur5r.net Git - i3/i3status/blob - wmiistatus.c
f516849c5cfd8f56b61c0bc9a427672696c8c929
[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 <unistd.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <net/if.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <glob.h>
50
51 #include "wmiistatus.h"
52 #include "config.h"
53
54 static void write_to_statusbar(const char *message) {
55         int fd = open(wmii_path, O_RDWR);
56         if (fd == -1)
57                 exit(-2);
58         write(fd, message, strlen(message));
59         close(fd);
60 }
61
62 /*
63  * Write errormessage to statusbar and exit
64  *
65  */
66 static void die(const char *message) {
67         write_to_statusbar(message);
68         exit(-1);
69 }
70
71 static char *skip_character(char *input, char character, int amount) {
72         char *walk;
73         int len = strlen(input),
74             blanks = 0;
75
76         for (walk = input; ((walk - input) < len) && (blanks < amount); walk++)
77                 if (*walk == character)
78                         blanks++;
79
80         return (walk == input ? walk : walk-1);
81 }
82
83 static void push_part(const char *input, const int n) {
84         if (first_push)
85                 first_push = false;
86         else
87                 strncpy(output+strlen(output), " | ", strlen(" | "));
88         strncpy(output+strlen(output), input, n);
89 }
90
91 /*
92  * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
93  * not the full capacity.
94  *
95  */
96 static char *get_battery_info() {
97         char buf[1024];
98         static char part[512];
99         char *walk, *last = buf;
100         int fd = open("/sys/class/power_supply/BAT0/uevent", O_RDONLY);
101         if (fd == -1)
102                 die("Could not open /sys/class/power_supply/BAT0/uevent");
103         int full_design = -1,
104             remaining = -1,
105             present_rate = -1;
106         charging_status_t status = CS_DISCHARGING;
107         memset(part, '\0', sizeof(part));
108         read(fd, buf, sizeof(buf));
109         for (walk = buf; (walk-buf) < 1024; walk++)
110                 if (*walk == '=') {
111                         if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
112                                 full_design = atoi(walk+1);
113                         else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW"))
114                                 remaining = atoi(walk+1);
115                         else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
116                                 present_rate = atoi(walk+1);
117                         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
118                                 status = CS_CHARGING;
119                         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
120                                 status = CS_FULL;
121                 } else if (*walk == '\n')
122                         last = walk+1;
123         close(fd);
124
125         if ((full_design != -1) && (remaining != -1) && (present_rate != -1)) {
126                 float remaining_time, perc;
127                 if (status == CS_CHARGING)
128                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
129                 else if (status == CS_DISCHARGING)
130                         remaining_time = ((float)remaining / (float)present_rate);
131                 else {
132                         snprintf(part, sizeof(part), "FULL");
133                         return part;
134                 }
135                 perc = ((float)remaining / (float)full_design);
136
137                 int seconds = (int)(remaining_time * 3600.0);
138                 int hours = seconds / 3600;
139                 seconds -= (hours * 3600);
140                 int minutes = seconds / 60;
141                 seconds -= (minutes * 60);
142
143                 sprintf(part, "%s %.02f%% %02d:%02d:%02d", (status == CS_CHARGING? "CHR" : "BAT"),
144                         (perc * 100), hours, minutes, seconds);
145         }
146         return part;
147 }
148
149 /*
150  * Just parses /proc/net/wireless
151  *
152  */
153 static char *get_wireless_info() {
154         char buf[1024];
155         static char part[512];
156         char *interfaces;
157         memset(buf, '\0', sizeof(buf));
158         memset(part, '\0', sizeof(part));
159
160         int fd = open("/proc/net/wireless", O_RDONLY);
161         if (fd == -1)
162                 die("Could not open /proc/net/wireless");
163         read(fd, buf, sizeof(buf));
164         close(fd);
165
166         interfaces = skip_character(buf, '\n', 2) + 1;
167         while (interfaces < buf+strlen(buf)) {
168                 while (isspace((int)*interfaces))
169                         interfaces++;
170                 if (strncmp(interfaces, wlan_interface, strlen(wlan_interface)) == 0) {
171                         /* Skip status field (0000) */
172                         interfaces += strlen(wlan_interface) + 2;
173                         interfaces = skip_character(interfaces, ' ', 1);
174                         while (isspace((int)*interfaces))
175                                 interfaces++;
176                         int quality = atoi(interfaces);
177                         /* For some reason, I get 255 sometimes */
178                         if ((quality == 255) || (quality == 0))
179                                 snprintf(part, sizeof(part), "W: down");
180                         else {
181                                 snprintf(part, sizeof(part), "W: (%02d%%) ", quality);
182                                 char *ip_address = get_ip_address(wlan_interface);
183                                 strcpy(part+strlen(part), ip_address);
184                         }
185
186                         return part;
187                 }
188                 interfaces = skip_character(interfaces, '\n', 1) + 1;
189         }
190
191         return part;
192 }
193
194 static char *get_ip_address(const char *interface) {
195         static char part[512];
196         struct ifreq ifr;
197         memset(part, '\0', sizeof(part));
198
199         int fd = socket(AF_INET, SOCK_DGRAM, 0);
200
201         strcpy(ifr.ifr_name, interface);
202         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
203                 die("Could not get interface flags (SIOCGIFFLAGS)");
204
205         if (!(ifr.ifr_flags & IFF_UP) ||
206             !(ifr.ifr_flags & IFF_RUNNING)) {
207                 close(fd);
208                 return NULL;
209         }
210
211         strcpy(ifr.ifr_name, interface);
212         ifr.ifr_addr.sa_family = AF_INET;
213         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
214                 struct sockaddr_in addr;
215                 memcpy(&addr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
216                 inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, sizeof(struct sockaddr_in));
217                 if (strlen(part) == 0)
218                         sprintf(part, "no IP");
219         }
220
221         close(fd);
222         return part;
223 }
224
225 static char *get_eth_info() {
226         static char part[512];
227         char *ip_address = get_ip_address(eth_interface);
228
229         if (ip_address == NULL)
230                 snprintf(part, sizeof(part), "E: down");
231         else snprintf(part, sizeof(part), "E: %s", ip_address);
232
233         return part;
234 }
235
236 /*
237  * Checks if the PID in path is still valid by checking if /proc/<pid> exists
238  *
239  */
240 bool process_runs(const char *path) {
241         char pidbuf[512],
242              procbuf[512];
243         static glob_t globbuf;
244         struct stat statbuf;
245
246         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
247                 die("glob() failed");
248         const char *real_path = (globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
249         int fd = open(real_path, O_RDONLY);
250         globfree(&globbuf);
251         if (fd < 0)
252                 return false;
253         read(fd, pidbuf, sizeof(pidbuf));
254         close(fd);
255         sprintf(procbuf, "/proc/%s", pidbuf);
256         return (stat(procbuf, &statbuf) >= 0);
257 }
258
259 int main(void) {
260         char part[512],
261              *end;
262         unsigned int i;
263
264         while (1) {
265                 memset(output, '\0', sizeof(output));
266                 first_push = true;
267
268                 for (i = 0; i < sizeof(run_watches) / sizeof(char*); i += 2) {
269                         sprintf(part, "%s: %s", run_watches[i], (process_runs(run_watches[i+1]) ? "yes" : "no"));
270                         push_part(part, strlen(part));
271                 }
272
273                 char *wireless_info = get_wireless_info();
274                 push_part(wireless_info, strlen(wireless_info));
275
276                 char *eth_info = get_eth_info();
277                 push_part(eth_info, strlen(eth_info));
278
279                 char *battery_info = get_battery_info();
280                 push_part(battery_info, strlen(battery_info));
281
282                 /* Get load */
283                 int load_avg = open("/proc/loadavg", O_RDONLY);
284                 if (load_avg == -1)
285                         die("Could not open /proc/loadavg");
286                 read(load_avg, part, sizeof(part));
287                 close(load_avg);
288                 end = skip_character(part, ' ', 3);
289                 push_part(part, (end-part));
290
291                 /* Get date & time */
292                 time_t current_time = time(NULL);
293                 struct tm *current_tm = localtime(&current_time);
294                 strftime(part, sizeof(part), "%d.%m.%Y %H:%M:%S", current_tm);
295                 push_part(part, strlen(part));
296
297                 int fd = open("/mnt/wmii/rbar/status", O_RDWR);
298                 write(fd, output, strlen(output));
299                 close(fd);
300
301                 sleep(1);
302         }
303 }