From b7bf1dd7220f73b0d27e94e69099d14064b14661 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Fri, 1 May 2009 17:53:27 +0200 Subject: [PATCH] Implement flags for batteries to use the last full capacity. This breaks configfiles. --- Makefile | 1 + i3status.1 | 25 ++++++++++----- i3status.c | 88 +++++++++++++++++++++++++++++++++------------------ i3status.conf | 18 ++++++++++- 4 files changed, 92 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 6ebd743..7dfc5b4 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ CFLAGS+=-DPREFIX=\"\" ifeq ($(shell uname),Linux) CFLAGS+=-DLINUX +CFLAGS+=-D_GNU_SOURCE endif # Define this if you want i3status to spit out dzen2-compatible output on stdout diff --git a/i3status.1 b/i3status.1 index 747d9fa..4345671 100644 --- a/i3status.1 +++ b/i3status.1 @@ -8,7 +8,7 @@ .fi .. -.TH i3status 1 "APRIL 2009" Linux "User Manuals" +.TH i3status 1 "MAY 2009" Linux "User Manuals" .SH NAME i3status \- Generates a status line for dzen2 or wmii @@ -25,7 +25,7 @@ its 9P pseudo filesystem. It is designed to be very efficient by issuing a very small number of systemcalls (as the bar should be updated every second or at your specified interval). This ensures that even under high load, your status bar is updated correctly and it saves a little bit of battery life by not spawning -new processes every second like shell scripts do. +new processes every second like shell scripts does. .SH CONFIGURATION .TP @@ -49,11 +49,19 @@ Format for the time/date to be displayed. See strftime(3) or date(1). Don't specify it if you don't want the time to be shown. .TP -.B battery_path -The path to your battery's uevent file in /sys, e.g. -/sys/class/power_supply/BAT0/uevent. You can specify this directive multiple -times to display data of more than one battery. Don't specify if you don't have -a battery. +.B battery +The number of the battery you want to display. This option can be specified +multiple times to display multiple batteries. Don't specify it if you don't +have a battery. To get the number of the specific battery, see +/sys/class/power_supply/BAT* + +If you want i3status to use the last full capacity instead of the design capacity +of the battery, specify the f-flag, like this: + +.Vb 10 +# Use the last full capacity +battery 0,f +.Ve .TP .B run_watch @@ -107,7 +115,8 @@ System-wide configuration file. \ð eth0 \&wmii_path /mnt/wmii/rbar/ \&time_format %d.%m.%Y %H:%M:%S -\&battery_path /sys/class/power_supply/BAT0/uevent +\&battery 0 +\&battery 1,f \&run_watch DHCP /var/run/dhclient*.pid \&run_watch VPN /var/run/vpnc*.pid \&order run,wlan,eth,battery,load,time diff --git a/i3status.c b/i3status.c index cc98fa0..c04a70d 100644 --- a/i3status.c +++ b/i3status.c @@ -71,7 +71,9 @@ #define BAR "^fg(#333333)^p(5;-2)^ro(2)^p()^fg()^p(5)" struct battery { - const char *path; + char *path; + /* Use last full capacity instead of design capacity */ + bool use_last_full; SIMPLEQ_ENTRY(battery) batteries; }; @@ -84,7 +86,6 @@ static const char *wlan_interface; static const char *eth_interface; static char *wmii_path; static const char *time_format; -static const char *battery_path; static bool use_colors; static bool get_ethspeed; static const char *wmii_normcolors = "#222222 #333333"; @@ -199,8 +200,6 @@ static void setup(void) { 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")); @@ -254,15 +253,16 @@ static void write_error_to_statusbar(const char *message) { * */ void die(const char *fmt, ...) { - if (wmii_path != NULL) { - char buffer[512]; - va_list ap; - va_start(ap, fmt); - (void)vsnprintf(buffer, sizeof(buffer), fmt, ap); - va_end(ap); + char buffer[512]; + va_list ap; + va_start(ap, fmt); + (void)vsnprintf(buffer, sizeof(buffer), fmt, ap); + va_end(ap); + if (wmii_path != NULL) write_error_to_statusbar(buffer); - } + else + fprintf(stderr, "%s", buffer); exit(EXIT_FAILURE); } @@ -289,7 +289,7 @@ static char *skip_character(char *input, char character, int amount) { * worn off your battery is. * */ -static char *get_battery_info(const char *path) { +static char *get_battery_info(struct battery *bat) { char buf[1024]; static char part[512]; char *walk, *last; @@ -299,27 +299,44 @@ static char *get_battery_info(const char *path) { present_rate = -1; charging_status_t status = CS_DISCHARGING; - if ((fd = open(path, O_RDONLY)) == -1) + if ((fd = open(bat->path, O_RDONLY)) == -1) 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") || - BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN")) - full_design = atoi(walk+1); - 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 if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging")) - status = CS_CHARGING; - else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full")) - status = CS_FULL; - } else if (*walk == '\n') + for (walk = buf, last = buf; (walk-buf) < 1024; walk++) { + if (*walk == '\n') { last = walk+1; + continue; + } + + if (*walk != '=') + continue; + + 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 if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging")) + status = CS_CHARGING; + else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full")) + status = CS_FULL; + else { + /* The only thing left is the full capacity */ + if (bat->use_last_full) { + if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") && + !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL")) + continue; + } else { + if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") && + !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN")) + continue; + } + + full_design = atoi(walk+1); + } + } (void)close(fd); if ((full_design == 1) || (remaining == -1)) @@ -536,11 +553,20 @@ static int load_configuration(const char *configfile) { eth_interface = strdup(dest_value); OPT("time_format") time_format = strdup(dest_value); - OPT("battery_path") { + OPT("battery") { struct battery *new = calloc(1, sizeof(struct battery)); if (new == NULL) die("Could not allocate memory\n"); - new->path = strdup(dest_value); + if (asprintf(&(new->path), "/sys/class/power_supply/BAT%d/uevent", atoi(dest_value)) == -1) + die("Could not build battery path\n"); + + /* check if flags were specified for this battery */ + if (strstr(dest_value, ",") != NULL) { + char *flags = strstr(dest_value, ","); + flags++; + if (*flags == 'f') + new->use_last_full = true; + } SIMPLEQ_INSERT_TAIL(&batteries, new, batteries); } OPT("color") use_colors = true; @@ -669,7 +695,7 @@ int main(int argc, char *argv[]) { write_to_statusbar(concat(order[ORDER_ETH], "eth"), get_eth_info(), false); struct battery *current_battery; SIMPLEQ_FOREACH(current_battery, &batteries, batteries) { - write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(current_battery->path), false); + write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(current_battery), false); } /* Get load */ diff --git a/i3status.conf b/i3status.conf index ef0a5cf..4b204b0 100644 --- a/i3status.conf +++ b/i3status.conf @@ -1,11 +1,27 @@ +# Name of wlan/ethernet devices wlan wlan0 eth eth0 + +# Path to wmii (not relevant for dzen mode) wmii_path /mnt/wmii/rbar/ + +# Format of date/time, see strftime(3) time_format %d.%m.%Y %H:%M:%S -battery_path /sys/class/power_supply/BAT0/uevent + +# Use battery 0,f to use the last full capacity instead of the design capacity +battery 0 + +# Check if DHCP/VPN clients are running run_watch DHCP /var/run/dhclient*.pid run_watch VPN /var/run/vpnc/pid + order run,wlan,eth,battery,load,time + +# Specifies background and border color normcolors #000000 #333333 + +# Enables colors color + +# Checks ethernet interface speed (this needs root privileges) get_ethspeed