Fix battery handling with the /sys interface

This commit is contained in:
zorun 2011-10-24 17:39:18 +02:00
commit fc5b78ebaf

View file

@ -26,10 +26,12 @@ __battery_detail() {
cat "$bat/info" cat "$bat/info"
cat "$bat/state" cat "$bat/state"
done done
# FIXME: do the same thing with the /sys interface
} }
__battery() { __battery() {
local bat line present sign state percent full rem color bcolor local bat has_bat line present sign state percent full rem color bcolor
has_bat=""
for bat in $BATTERY /sys/class/power_supply/* /proc/acpi/battery/*; do for bat in $BATTERY /sys/class/power_supply/* /proc/acpi/battery/*; do
present=""; full=""; rem=""; state="" present=""; full=""; rem=""; state=""
case "$bat" in case "$bat" in
@ -40,7 +42,7 @@ __battery() {
full="$POWER_SUPPLY_CHARGE_FULL" full="$POWER_SUPPLY_CHARGE_FULL"
rem="$POWER_SUPPLY_CHARGE_NOW" rem="$POWER_SUPPLY_CHARGE_NOW"
state="$POWER_SUPPLY_STATUS" state="$POWER_SUPPLY_STATUS"
[ -n "$present" ] && [ -n "$full" ] && [ -n "$rem" ] && [ "$state" ] && break [ "$present" = "1" ] && [ -n "$full" ] && [ -n "$rem" ] && [ -n "$state" ] && has_bat=1 && break
fi fi
;; ;;
/proc/*) /proc/*)
@ -64,9 +66,11 @@ __battery() {
esac esac
[ -n "$rem" -a -n "$state" ] && break [ -n "$rem" -a -n "$state" ] && break
done < "$bat/state" done < "$bat/state"
[ -n "$full" ] && [ -n "$rem" ] && [ -n "$state" ] && has_bat=1 && break
;; ;;
esac esac
done done
if [ "$has_bat" = "1" ]; then
percent=$(((100*$rem)/$full)) percent=$(((100*$rem)/$full))
if [ "$percent" -lt 33 ]; then if [ "$percent" -lt 33 ]; then
color="R k" color="R k"
@ -82,10 +86,11 @@ __battery() {
case $state in case $state in
charging|Charging|Unknown) sign="+" ;; charging|Charging|Unknown) sign="+" ;;
discharging|Discharging) sign="-" ;; discharging|Discharging) sign="-" ;;
charged|Unknown) sign="="; percent="" ;; charged|Unknown|Full) sign="="; percent="" ;;
*) sign="$state" ;; *) sign="$state" ;;
esac esac
color $bcolor; printf "%s" "$percent"; color -; color $color; printf "%s" "$sign"; color -- color $bcolor; printf "%s" "$percent"; color -; color $color; printf "%s" "$sign"; color --
fi
} }
# vi: syntax=sh ts=4 noexpandtab # vi: syntax=sh ts=4 noexpandtab