12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
17 const char *wlan_interface = "wlan0";
18 const char *eth_interface = "eth0";
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"
25 bool first_push = true;
27 static char *skip_character(char *input, char character, int amount) {
29 int len = strlen(input),
32 for (walk = input; ((walk - input) < len) && (blanks < amount); walk++)
33 if (*walk == character)
36 return (walk == input ? walk : walk-1);
39 static void push_part(const char *input, const int n) {
43 strncpy(output+strlen(output), " | ", strlen(" | "));
44 strncpy(output+strlen(output), input, n);
48 * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
49 * not the full capacity.
52 static char *get_battery_info() {
54 static char output[512];
55 char *walk, *last = buf;
56 int fd = open("/sys/class/power_supply/BAT0/uevent", O_RDONLY);
60 bool charging = false;
61 memset(output, '\0', sizeof(output));
62 read(fd, buf, sizeof(buf));
63 for (walk = buf; (walk-buf) < 1024; 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)
73 } else if (*walk == '\n')
77 if ((full_design != -1) && (remaining != -1) && (present_rate != -1)) {
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);
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);
90 sprintf(output, "%s %.02f%% %02d:%02d:%02d", (charging ? "CHR" : "BAT"), (perc * 100), hours, minutes, seconds);
96 * Just parses /proc/net/wireless
99 static char *get_wireless_info() {
101 static char output[512];
103 memset(buf, '\0', sizeof(buf));
104 memset(output, '\0', sizeof(output));
106 int fd = open("/proc/net/wireless", O_RDONLY);
107 read(fd, buf, sizeof(buf));
110 interfaces = skip_character(buf, '\n', 2) + 1;
111 while (interfaces < buf+strlen(buf)) {
112 while (isspace((int)*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))
120 int quality = atoi(interfaces);
121 /* For some reason, I get 255 sometimes */
124 snprintf(output, sizeof(output), "W: (%02d%%) ", quality);
127 interfaces = skip_character(interfaces, '\n', 1) + 1;
133 static char *get_eth_info() {
134 static char output[512];
136 memset(output, '\0', sizeof(output));
138 int fd = socket(AF_INET, SOCK_DGRAM, 0);
140 strcpy(ifr.ifr_name, eth_interface);
141 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
142 printf("böses fehler\n");
143 /* TODO: errorhandling */
145 if (!(ifr.ifr_flags & IFF_UP) ||
146 !(ifr.ifr_flags & IFF_RUNNING)) {
147 sprintf(output, "E: down");
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");
172 memset(output, '\0', sizeof(output));
175 char *wireless_info = get_wireless_info();
176 push_part(wireless_info, strlen(wireless_info));
178 char *eth_info = get_eth_info();
179 push_part(eth_info, strlen(eth_info));
181 char *battery_info = get_battery_info();
182 push_part(battery_info, strlen(battery_info));
186 int load_avg = open("/proc/loadavg", O_RDONLY);
187 read(load_avg, part, sizeof(part));
189 end = skip_character(part, ' ', 3);
190 push_part(part, (end-part));
192 /* Get date & time */
193 time_t current_time = time(NULL);
194 struct tm *current_tm = localtime(¤t_time);
195 strftime(part, sizeof(part), "%d.%m.%Y %H:%M:%S", current_tm);
196 push_part(part, strlen(part));
198 printf("output = %s\n", output);
200 int fd = open("/mnt/wmii/rbar/status", O_RDWR);
201 write(fd, output, strlen(output));