2 * Generates a status line for use with wmii or other minimal window managers
5 * Copyright (c) 2008 Michael Stapelberg and contributors
8 * Redistribution and use in source and binary forms, with or without modification,
9 * are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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.
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
34 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
53 #define _IS_WMIISTATUS_C
54 #include "wmiistatus.h"
55 #undef _IS_WMIISTATUS_C
59 * This function just concats two strings in place, it should only be used
60 * for concatting order to the name of a file or concatting color codes.
61 * Otherwise, the buffer size would have to be increased.
64 static char *concat(const char *str1, const char *str2) {
65 static char concatbuf[32];
66 snprintf(concatbuf, sizeof(concatbuf), "%s%s", str1, str2);
70 static void cleanup_rbar_dir() {
73 char pathbuf[strlen(wmii_path)+256+1];
75 if ((dir = opendir(wmii_path)) == NULL)
78 while ((ent = readdir(dir)) != NULL) {
79 if (ent->d_type == DT_REG) {
80 sprintf(pathbuf, "%s%s", wmii_path, ent->d_name);
88 static void create_file(const char *name) {
89 char pathbuf[strlen(wmii_path)+256+1];
91 sprintf(pathbuf, "%s%s", wmii_path, name);
92 int fd = creat(pathbuf, S_IRUSR | S_IWUSR);
96 char *tmp = concat("#888888 ", wmii_normcolors);
97 write(fd, tmp, strlen(tmp));
102 static void write_to_statusbar(const char *name, const char *message) {
103 char pathbuf[strlen(wmii_path)+256+1];
105 sprintf(pathbuf, "%s%s", wmii_path, name);
106 int fd = open(pathbuf, O_RDWR);
109 write(fd, message, strlen(message));
113 static void write_error_to_statusbar(const char *message) {
115 create_file("error");
116 write_to_statusbar("error", message);
120 * Write errormessage to statusbar and exit
123 void die(const char *fmt, ...) {
127 vsprintf(buffer, fmt, ap);
130 write_error_to_statusbar(buffer);
134 static char *skip_character(char *input, char character, int amount) {
136 int len = strlen(input),
139 for (walk = input; ((walk - input) < len) && (blanks < amount); walk++)
140 if (*walk == character)
143 return (walk == input ? walk : walk-1);
147 * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
148 * not the full capacity.
151 static char *get_battery_info() {
153 static char part[512];
154 char *walk, *last = buf;
155 int fd = open(battery_path, O_RDONLY);
157 die("Could not open %s", battery_path);
158 int full_design = -1,
161 charging_status_t status = CS_DISCHARGING;
162 memset(part, '\0', sizeof(part));
163 read(fd, buf, sizeof(buf));
164 for (walk = buf; (walk-buf) < 1024; walk++)
166 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
167 full_design = atoi(walk+1);
168 else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW"))
169 remaining = atoi(walk+1);
170 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
171 present_rate = atoi(walk+1);
172 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
173 status = CS_CHARGING;
174 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
176 } else if (*walk == '\n')
180 if ((full_design != -1) && (remaining != -1) && (present_rate != -1)) {
181 float remaining_time, perc;
182 if (status == CS_CHARGING)
183 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
184 else if (status == CS_DISCHARGING)
185 remaining_time = ((float)remaining / (float)present_rate);
187 snprintf(part, sizeof(part), "FULL");
190 perc = ((float)remaining / (float)full_design);
192 int seconds = (int)(remaining_time * 3600.0);
193 int hours = seconds / 3600;
194 seconds -= (hours * 3600);
195 int minutes = seconds / 60;
196 seconds -= (minutes * 60);
198 sprintf(part, "%s %.02f%% %02d:%02d:%02d", (status == CS_CHARGING? "CHR" : "BAT"),
199 (perc * 100), hours, minutes, seconds);
205 * Just parses /proc/net/wireless
208 static char *get_wireless_info() {
210 static char part[512];
212 memset(buf, '\0', sizeof(buf));
213 memset(part, '\0', sizeof(part));
215 int fd = open("/proc/net/wireless", O_RDONLY);
217 die("Could not open /proc/net/wireless");
218 read(fd, buf, sizeof(buf));
221 interfaces = skip_character(buf, '\n', 2) + 1;
222 while (interfaces < buf+strlen(buf)) {
223 while (isspace((int)*interfaces))
225 if (strncmp(interfaces, wlan_interface, strlen(wlan_interface)) == 0) {
226 /* Skip status field (0000) */
227 interfaces += strlen(wlan_interface) + 2;
228 interfaces = skip_character(interfaces, ' ', 1);
229 while (isspace((int)*interfaces))
231 int quality = atoi(interfaces);
232 /* For some reason, I get 255 sometimes */
233 if ((quality == 255) || (quality == 0)) {
235 snprintf(part, sizeof(part), "%s%s", concat("#FF0000 ", wmii_normcolors), " W: down");
236 else snprintf(part, sizeof(part), "W: down");
239 snprintf(part, sizeof(part), "W: (%02d%%) ", quality);
240 char *ip_address = get_ip_address(wlan_interface);
241 strcpy(part+strlen(part), ip_address);
246 interfaces = skip_character(interfaces, '\n', 1) + 1;
252 static char *get_ip_address(const char *interface) {
253 static char part[512];
255 memset(part, '\0', sizeof(part));
257 int fd = socket(AF_INET, SOCK_DGRAM, 0);
259 strcpy(ifr.ifr_name, interface);
260 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
261 die("Could not get interface flags (SIOCGIFFLAGS)");
263 if (!(ifr.ifr_flags & IFF_UP) ||
264 !(ifr.ifr_flags & IFF_RUNNING)) {
269 strcpy(ifr.ifr_name, interface);
270 ifr.ifr_addr.sa_family = AF_INET;
271 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
272 struct sockaddr_in addr;
273 memcpy(&addr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
274 inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, sizeof(struct sockaddr_in));
275 if (strlen(part) == 0)
276 sprintf(part, "no IP");
283 static char *get_eth_info() {
284 static char part[512];
285 char *ip_address = get_ip_address(eth_interface);
287 if (ip_address == NULL)
288 snprintf(part, sizeof(part), "E: down");
289 else snprintf(part, sizeof(part), "E: %s", ip_address);
295 * Checks if the PID in path is still valid by checking if /proc/<pid> exists
298 static bool process_runs(const char *path) {
303 static glob_t globbuf;
306 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
307 die("glob() failed");
308 const char *real_path = (globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
309 int fd = open(real_path, O_RDONLY);
313 n = read(fd, pidbuf, sizeof(pidbuf));
317 for (walk = pidbuf; *walk != '\0'; walk++)
318 if (!isdigit((int)(*walk))) {
322 sprintf(procbuf, "/proc/%s", pidbuf);
323 return (stat(procbuf, &statbuf) >= 0);
332 load_configuration("/etc/wmiistatus.conf");
335 create_file(concat(order[ORDER_WLAN],"wlan"));
337 create_file(concat(order[ORDER_ETH],"eth"));
339 create_file(concat(order[ORDER_BATTERY],"battery"));
340 create_file(concat(order[ORDER_LOAD],"load"));
342 create_file(concat(order[ORDER_TIME],"time"));
343 for (i = 0; i < num_run_watches; i += 2) {
344 sprintf(pathbuf, "%s%s", order[ORDER_RUN], run_watches[i]);
345 create_file(pathbuf);
349 for (i = 0; i < num_run_watches; i += 2) {
350 bool running = process_runs(run_watches[i+1]);
352 sprintf(part, "%s %s: %s", (running ? concat("#00FF00 ", wmii_normcolors) : concat("#FF0000 ", wmii_normcolors)), run_watches[i], (running ? "yes" : "no"));
353 else sprintf(part, "%s: %s", run_watches[i], (running ? "yes" : "no"));
354 sprintf(pathbuf, "%s%s", order[ORDER_RUN], run_watches[i]);
355 write_to_statusbar(pathbuf, part);
359 write_to_statusbar(concat(order[ORDER_WLAN], "wlan"), get_wireless_info());
361 write_to_statusbar(concat(order[ORDER_ETH], "eth"), get_eth_info());
363 write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info());
366 int load_avg = open("/proc/loadavg", O_RDONLY);
368 die("Could not open /proc/loadavg");
369 read(load_avg, part, sizeof(part));
371 end = skip_character(part, ' ', 3);
373 write_to_statusbar(concat(order[ORDER_LOAD], "load"), part);
376 /* Get date & time */
377 time_t current_time = time(NULL);
378 struct tm *current_tm = localtime(¤t_time);
379 strftime(part, sizeof(part), time_format, current_tm);
380 write_to_statusbar(concat(order[ORDER_TIME], "time"), part);