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