* Generates a status line for use with wmii or other minimal window managers
*
*
- * Copyright (c) 2008 Michael Stapelberg and contributors
+ * Copyright (c) 2008-2009 Michael Stapelberg and contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
#include <glob.h>
#include <dirent.h>
#include <getopt.h>
+#include <linux/ethtool.h>
+#include <linux/sockios.h>
+
#define _IS_WMIISTATUS_C
#include "wmiistatus.h"
#undef _IS_WMIISTATUS_C
#include "config.h"
+/* socket file descriptor for general purposes */
+static int general_socket;
+
/*
* This function just concats two strings in place, it should only be used
* for concatting order to the name of a file or concatting color codes.
static void create_file(const char *name) {
char pathbuf[strlen(wmii_path)+256+1];
int fd;
+ int flags = O_CREAT | O_WRONLY;
+ struct stat statbuf;
(void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
- if ((fd = open(pathbuf, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
+
+ /* Overwrite file's contents if it exists */
+ if (stat(pathbuf, &statbuf) >= 0)
+ flags |= O_TRUNC;
+
+ if ((fd = open(pathbuf, flags, S_IRUSR | S_IWUSR)) < 0)
exit(EXIT_FAILURE);
if (use_colors) {
char *tmp = concat("#888888 ", wmii_normcolors);
(void)close(fd);
}
+/*
+ * Waits until wmii_path/rbar exists (= the filesystem gets mounted),
+ * cleans up all files and creates the needed files
+ *
+ */
+static void setup(void) {
+ unsigned int i;
+ struct stat statbuf;
+ char pathbuf[512];
+
+ /* Wait until wmii_path/rbar exists */
+ for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
+
+ cleanup_rbar_dir();
+ if (wlan_interface)
+ create_file(concat(order[ORDER_WLAN],"wlan"));
+ if (eth_interface)
+ create_file(concat(order[ORDER_ETH],"eth"));
+ if (battery_path)
+ create_file(concat(order[ORDER_BATTERY],"battery"));
+ create_file(concat(order[ORDER_LOAD],"load"));
+ if (time_format)
+ create_file(concat(order[ORDER_TIME],"time"));
+ for (i = 0; i < num_run_watches; i += 2) {
+ snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
+ create_file(pathbuf);
+ }
+
+}
+
/*
* Writes the given message in the corresponding file in wmii's /rbar directory
*
int fd;
(void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
- if ((fd = open(pathbuf, O_RDWR)) == -1)
- exit(EXIT_FAILURE);
+ if ((fd = open(pathbuf, O_RDWR)) == -1) {
+ /* Try to re-setup stuff and just continue */
+ setup();
+ return;
+ }
if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
exit(EXIT_FAILURE);
(void)close(fd);
(void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
- write_error_to_statusbar(buffer);
+ if (wmii_path != NULL)
+ write_error_to_statusbar(buffer);
exit(EXIT_FAILURE);
}
+/*
+ * Skip the given character for maximum 'amount' times, returns
+ * a pointer to the first non-'character' character in 'input'.
+ *
+ */
static char *skip_character(char *input, char character, int amount) {
char *walk;
size_t len = strlen(input);
}
/*
- * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
- * not the full capacity.
+ * Get battery information from /sys. Note that it uses the design capacity to
+ * calculate the percentage, not the last full capacity, so you can see how
+ * worn off your battery is.
*
*/
static char *get_battery_info() {
charging_status_t status = CS_DISCHARGING;
if ((fd = open(battery_path, O_RDONLY)) == -1)
- die("Could not open %s", battery_path);
+ return "No battery found";
memset(part, 0, sizeof(part));
(void)read(fd, buf, sizeof(buf));
for (walk = buf, last = buf; (walk-buf) < 1024; walk++)
if (*walk == '=') {
- if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
+ if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN") ||
+ BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN"))
full_design = atoi(walk+1);
- else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW"))
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
+ BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
remaining = atoi(walk+1);
else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
present_rate = atoi(walk+1);
else (void)snprintf(part, sizeof(part), "W: down");
} else {
- char *ip_address;
- (void)snprintf(part, sizeof(part), "W: (%02d%%) ", quality);
+ const char *ip_address;
+ (void)snprintf(part, sizeof(part), "W: (%03d%%) ", quality);
ip_address = get_ip_address(wlan_interface);
strcpy(part+strlen(part), ip_address);
}
return part;
}
+/*
+ * Return the IP address for the given interface or "no IP" if the
+ * interface is up and running but hasn't got an IP address yet
+ *
+ */
static const char *get_ip_address(const char *interface) {
static char part[512];
struct ifreq ifr;
- int fd;
+ struct sockaddr_in addr;
+ socklen_t len = sizeof(struct sockaddr_in);
memset(part, 0, sizeof(part));
- fd = socket(AF_INET, SOCK_DGRAM, 0);
-
- strcpy(ifr.ifr_name, interface);
- if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
- die("Could not get interface flags (SIOCGIFFLAGS)");
-
- if (!(ifr.ifr_flags & IFF_UP) ||
- !(ifr.ifr_flags & IFF_RUNNING)) {
- close(fd);
+ (void)strcpy(ifr.ifr_name, interface);
+ ifr.ifr_addr.sa_family = AF_INET;
+ if (ioctl(general_socket, SIOCGIFADDR, &ifr) < 0)
return NULL;
- }
- strcpy(ifr.ifr_name, interface);
- ifr.ifr_addr.sa_family = AF_INET;
- if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
- struct sockaddr_in addr;
- memcpy(&addr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
- (void)inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, (socklen_t)sizeof(struct sockaddr_in));
- if (strlen(part) == 0)
- snprintf(part, sizeof(part), "no IP");
- }
+ memcpy(&addr, &ifr.ifr_addr, len);
+ (void)inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, len);
+ if (strlen(part) == 0)
+ (void)snprintf(part, sizeof(part), "no IP");
- (void)close(fd);
return part;
}
+/*
+ * Combines ethernet IP addresses and speed (if requested) for displaying
+ *
+ */
static char *get_eth_info() {
static char part[512];
const char *ip_address = get_ip_address(eth_interface);
+ int ethspeed = 0;
+
+ if (get_ethspeed) {
+ /* This code path requires root privileges */
+ struct ifreq ifr;
+ struct ethtool_cmd ecmd;
+ int err;
+
+ ecmd.cmd = ETHTOOL_GSET;
+ (void)memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_data = (caddr_t)&ecmd;
+ (void)strcpy(ifr.ifr_name, eth_interface);
+ if ((err = ioctl(general_socket, SIOCETHTOOL, &ifr)) == 0)
+ ethspeed = (ecmd.speed == 65535 ? 0 : ecmd.speed);
+ else get_ethspeed = false;
+ }
if (ip_address == NULL)
(void)snprintf(part, sizeof(part), "E: down");
- else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
+ else {
+ if (get_ethspeed)
+ (void)snprintf(part, sizeof(part), "E: %s (%d Mbit/s)", ip_address, ethspeed);
+ else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
+ }
return part;
}
if ((char)o == 'c')
configfile = optarg;
- load_configuration(configfile);
- cleanup_rbar_dir();
- if (wlan_interface)
- create_file(concat(order[ORDER_WLAN],"wlan"));
- if (eth_interface)
- create_file(concat(order[ORDER_ETH],"eth"));
- if (battery_path)
- create_file(concat(order[ORDER_BATTERY],"battery"));
- create_file(concat(order[ORDER_LOAD],"load"));
- if (time_format)
- create_file(concat(order[ORDER_TIME],"time"));
- for (i = 0; i < num_run_watches; i += 2) {
- snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
- create_file(pathbuf);
- }
+ if (load_configuration(configfile) < 0)
+ return EXIT_FAILURE;
+
+ setup();
+
+ if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+ die("Could not create socket");
while (1) {
for (i = 0; i < num_run_watches; i += 2) {