]> git.sur5r.net Git - i3/i3status/blob - wmiistatus.c
Initial commit
[i3/i3status] / wmiistatus.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <stdbool.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <net/if.h>
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16
17 const char *wlan_interface = "wlan0";
18 const char *eth_interface = "eth0";
19
20 // TODO: run-watches, z.B.
21 // "/var/run/dhcp*.pid" --> checkt ob's das file gibt, ob der prozess läuft und zeigt dann yes/no an
22 // "/var/run/vpnc*.pid"
23
24 char output[512];
25 bool first_push = true;
26
27 static char *skip_character(char *input, char character, int amount) {
28         char *walk;
29         int len = strlen(input),
30             blanks = 0;
31
32         for (walk = input; ((walk - input) < len) && (blanks < amount); walk++)
33                 if (*walk == character)
34                         blanks++;
35
36         return (walk == input ? walk : walk-1);
37 }
38
39 static void push_part(const char *input, const int n) {
40         if (first_push)
41                 first_push = false;
42         else
43                 strncpy(output+strlen(output), " | ", strlen(" | "));
44         strncpy(output+strlen(output), input, n);
45 }
46
47 /*
48  * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
49  * not the full capacity.
50  *
51  */
52 static char *get_battery_info() {
53         char buf[1024];
54         static char output[512];
55         char *walk, *last = buf;
56         int fd = open("/sys/class/power_supply/BAT0/uevent", O_RDONLY);
57         int full_design = -1,
58             remaining = -1,
59             present_rate = -1;
60         bool charging = false;
61         memset(output, '\0', sizeof(output));
62         read(fd, buf, sizeof(buf));
63         for (walk = buf; (walk-buf) < 1024; walk++)
64                 if (*walk == '=') {
65                         if (strncmp(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN", strlen("POWER_SUPPLY_ENERGY_FULL_DESIGN")) == 0)
66                                 full_design = atoi(walk+1);
67                         else if (strncmp(last, "POWER_SUPPLY_ENERGY_NOW", strlen("POWER_SUPPLY_ENERGY_NOW")) == 0)
68                                 remaining = atoi(walk+1);
69                         else if (strncmp(last, "POWER_SUPPLY_CURRENT_NOW", strlen("POWER_SUPPLY_CURRENT_NOW")) == 0)
70                                 present_rate = atoi(walk+1);
71                         else if (strncmp(last, "POWER_SUPPLY_STATUS=Charging", strlen("POWER_SUPPLY_STATUS=Charging")) == 0)
72                                 charging = true;
73                 } else if (*walk == '\n')
74                         last = walk+1;
75         close(fd);
76
77         if ((full_design != -1) && (remaining != -1) && (present_rate != -1)) {
78                 float time, perc;
79                 if (charging)
80                         time = ((float)full_design - (float)remaining) / (float)present_rate;
81                 else time = ((float)remaining / (float)present_rate);
82                 perc = ((float)remaining / (float)full_design);
83
84                 int seconds = (int)(time * 3600.0);
85                 int hours = seconds / 3600;
86                 seconds -= (hours * 3600);
87                 int minutes = seconds / 60;
88                 seconds -= (minutes * 60);
89
90                 sprintf(output, "%s %.02f%% %02d:%02d:%02d", (charging ? "CHR" : "BAT"), (perc * 100), hours, minutes, seconds);
91         }
92         return output;
93 }
94
95 /*
96  * Just parses /proc/net/wireless
97  *
98  */
99 static char *get_wireless_info() {
100         char buf[1024];
101         static char output[512];
102         char *interfaces;
103         memset(buf, '\0', sizeof(buf));
104         memset(output, '\0', sizeof(output));
105
106         int fd = open("/proc/net/wireless", O_RDONLY);
107         read(fd, buf, sizeof(buf));
108         close(fd);
109
110         interfaces = skip_character(buf, '\n', 2) + 1;
111         while (interfaces < buf+strlen(buf)) {
112                 while (isspace((int)*interfaces))
113                         interfaces++;
114                 if (strncmp(interfaces, wlan_interface, strlen(wlan_interface)) == 0) {
115                         /* Skip status field (0000) */
116                         interfaces += strlen(wlan_interface) + 2;
117                         interfaces = skip_character(interfaces, ' ', 1);
118                         while (isspace((int)*interfaces))
119                                 interfaces++;
120                         int quality = atoi(interfaces);
121                         /* For some reason, I get 255 sometimes */
122                         if (quality == 255)
123                                 quality = 0;
124                         snprintf(output, sizeof(output), "W: (%02d%%) ", quality);
125                         return output;
126                 }
127                 interfaces = skip_character(interfaces, '\n', 1) + 1;
128         }
129
130         return output;
131 }
132
133 static char *get_eth_info() {
134         static char output[512];
135         struct ifreq ifr;
136         memset(output, '\0', sizeof(output));
137
138         int fd = socket(AF_INET, SOCK_DGRAM, 0);
139
140         strcpy(ifr.ifr_name, eth_interface);
141         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
142                 printf("böses fehler\n");
143                 /* TODO: errorhandling */
144         }
145         if (!(ifr.ifr_flags & IFF_UP) ||
146             !(ifr.ifr_flags & IFF_RUNNING)) {
147                 sprintf(output, "E: down");
148                 close(fd);
149                 return output;
150         }
151
152         strcpy(ifr.ifr_name, eth_interface);
153         ifr.ifr_addr.sa_family = AF_INET;
154         if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
155                 struct sockaddr_in addr;
156                 memcpy(&addr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
157                 inet_ntop(AF_INET, &addr.sin_addr.s_addr, output+3, sizeof(struct sockaddr_in));
158                 strncpy(output, "E: ", strlen("E: "));
159                 if (strlen(output) == 0)
160                         sprintf(output, "E: no IP");
161         }
162
163         close(fd);
164         return output;
165 }
166
167 int main() {
168         char part[512],
169              *end;
170
171         while (1) {
172                 memset(output, '\0', sizeof(output));
173                 first_push = true;
174
175                 char *wireless_info = get_wireless_info();
176                 push_part(wireless_info, strlen(wireless_info));
177
178                 char *eth_info = get_eth_info();
179                 push_part(eth_info, strlen(eth_info));
180
181                 char *battery_info = get_battery_info();
182                 push_part(battery_info, strlen(battery_info));
183
184                 
185                 /* Get load */
186                 int load_avg = open("/proc/loadavg", O_RDONLY);
187                 read(load_avg, part, sizeof(part));
188                 close(load_avg);
189                 end = skip_character(part, ' ', 3);
190                 push_part(part, (end-part));
191
192                 /* Get date & time */
193                 time_t current_time = time(NULL);
194                 struct tm *current_tm = localtime(&current_time);
195                 strftime(part, sizeof(part), "%d.%m.%Y %H:%M:%S", current_tm);
196                 push_part(part, strlen(part));
197
198                 printf("output = %s\n", output);
199
200                 int fd = open("/mnt/wmii/rbar/status", O_RDWR);
201                 write(fd, output, strlen(output));
202                 close(fd);
203
204                 sleep(1);
205         }
206 }