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>
52 #include "wmiistatus.h"
55 static void write_to_statusbar(const char *message) {
56 int fd = open(wmii_path, O_RDWR);
59 write(fd, message, strlen(message));
64 * Write errormessage to statusbar and exit
67 static void die(const char *fmt, ...) {
71 vsprintf(buffer, fmt, ap);
74 write_to_statusbar(buffer);
78 static char *skip_character(char *input, char character, int amount) {
80 int len = strlen(input),
83 for (walk = input; ((walk - input) < len) && (blanks < amount); walk++)
84 if (*walk == character)
87 return (walk == input ? walk : walk-1);
90 static void push_part(const char *input, const int n) {
94 strncpy(output+strlen(output), " | ", strlen(" | "));
95 strncpy(output+strlen(output), input, n);
99 * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
100 * not the full capacity.
103 static char *get_battery_info() {
105 static char part[512];
106 char *walk, *last = buf;
107 int fd = open(battery, O_RDONLY);
109 die("Could not open %s", battery);
110 int full_design = -1,
113 charging_status_t status = CS_DISCHARGING;
114 memset(part, '\0', sizeof(part));
115 read(fd, buf, sizeof(buf));
116 for (walk = buf; (walk-buf) < 1024; walk++)
118 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
119 full_design = atoi(walk+1);
120 else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW"))
121 remaining = atoi(walk+1);
122 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
123 present_rate = atoi(walk+1);
124 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
125 status = CS_CHARGING;
126 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
128 } else if (*walk == '\n')
132 if ((full_design != -1) && (remaining != -1) && (present_rate != -1)) {
133 float remaining_time, perc;
134 if (status == CS_CHARGING)
135 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
136 else if (status == CS_DISCHARGING)
137 remaining_time = ((float)remaining / (float)present_rate);
139 snprintf(part, sizeof(part), "FULL");
142 perc = ((float)remaining / (float)full_design);
144 int seconds = (int)(remaining_time * 3600.0);
145 int hours = seconds / 3600;
146 seconds -= (hours * 3600);
147 int minutes = seconds / 60;
148 seconds -= (minutes * 60);
150 sprintf(part, "%s %.02f%% %02d:%02d:%02d", (status == CS_CHARGING? "CHR" : "BAT"),
151 (perc * 100), hours, minutes, seconds);
157 * Just parses /proc/net/wireless
160 static char *get_wireless_info() {
162 static char part[512];
164 memset(buf, '\0', sizeof(buf));
165 memset(part, '\0', sizeof(part));
167 int fd = open("/proc/net/wireless", O_RDONLY);
169 die("Could not open /proc/net/wireless");
170 read(fd, buf, sizeof(buf));
173 interfaces = skip_character(buf, '\n', 2) + 1;
174 while (interfaces < buf+strlen(buf)) {
175 while (isspace((int)*interfaces))
177 if (strncmp(interfaces, wlan_interface, strlen(wlan_interface)) == 0) {
178 /* Skip status field (0000) */
179 interfaces += strlen(wlan_interface) + 2;
180 interfaces = skip_character(interfaces, ' ', 1);
181 while (isspace((int)*interfaces))
183 int quality = atoi(interfaces);
184 /* For some reason, I get 255 sometimes */
185 if ((quality == 255) || (quality == 0))
186 snprintf(part, sizeof(part), "W: down");
188 snprintf(part, sizeof(part), "W: (%02d%%) ", quality);
189 char *ip_address = get_ip_address(wlan_interface);
190 strcpy(part+strlen(part), ip_address);
195 interfaces = skip_character(interfaces, '\n', 1) + 1;
201 static char *get_ip_address(const char *interface) {
202 static char part[512];
204 memset(part, '\0', sizeof(part));
206 int fd = socket(AF_INET, SOCK_DGRAM, 0);
208 strcpy(ifr.ifr_name, interface);
209 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
210 die("Could not get interface flags (SIOCGIFFLAGS)");
212 if (!(ifr.ifr_flags & IFF_UP) ||
213 !(ifr.ifr_flags & IFF_RUNNING)) {
218 strcpy(ifr.ifr_name, interface);
219 ifr.ifr_addr.sa_family = AF_INET;
220 if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
221 struct sockaddr_in addr;
222 memcpy(&addr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
223 inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, sizeof(struct sockaddr_in));
224 if (strlen(part) == 0)
225 sprintf(part, "no IP");
232 static char *get_eth_info() {
233 static char part[512];
234 char *ip_address = get_ip_address(eth_interface);
236 if (ip_address == NULL)
237 snprintf(part, sizeof(part), "E: down");
238 else snprintf(part, sizeof(part), "E: %s", ip_address);
244 * Checks if the PID in path is still valid by checking if /proc/<pid> exists
247 static bool process_runs(const char *path) {
252 static glob_t globbuf;
255 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
256 die("glob() failed");
257 const char *real_path = (globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
258 int fd = open(real_path, O_RDONLY);
262 n = read(fd, pidbuf, sizeof(pidbuf));
266 for (walk = pidbuf; *walk != '\0'; walk++)
267 if (!isdigit((int)(*walk))) {
271 sprintf(procbuf, "/proc/%s", pidbuf);
272 return (stat(procbuf, &statbuf) >= 0);
281 memset(output, '\0', sizeof(output));
284 for (i = 0; i < sizeof(run_watches) / sizeof(char*); i += 2) {
285 sprintf(part, "%s: %s", run_watches[i], (process_runs(run_watches[i+1]) ? "yes" : "no"));
286 push_part(part, strlen(part));
289 char *wireless_info = get_wireless_info();
290 push_part(wireless_info, strlen(wireless_info));
292 char *eth_info = get_eth_info();
293 push_part(eth_info, strlen(eth_info));
295 char *battery_info = get_battery_info();
296 push_part(battery_info, strlen(battery_info));
299 int load_avg = open("/proc/loadavg", O_RDONLY);
301 die("Could not open /proc/loadavg");
302 read(load_avg, part, sizeof(part));
304 end = skip_character(part, ' ', 3);
305 push_part(part, (end-part));
307 /* Get date & time */
308 time_t current_time = time(NULL);
309 struct tm *current_tm = localtime(¤t_time);
310 strftime(part, sizeof(part), time_format, current_tm);
311 push_part(part, strlen(part));
313 write_to_statusbar(output);