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