Your IP : 172.28.240.42


Current Path : /usr/lib/byobu/
Upload File :
Current File : //usr/lib/byobu/battery

#!/bin/sh -e
#
#    battery: print the state of the battery
#
#    Copyright (C) 2009 Raphaël Pinson.
#    Copyright (C) 2011 Dustin Kirkland
#
#    Authors: Raphaël Pinson <raphink@ubuntu.com>
#             Dustin Kirkland <kirkland@ubuntu.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

__battery_detail() {
	local bat
	for bat in /proc/acpi/battery/*; do
		cat "$bat/info"
		cat "$bat/state"
	done
	# FIXME: do the same thing with the /sys interface
}

__battery() {
	local bat line present sign state percent full rem color bcolor
	for bat in $BATTERY /sys/class/power_supply/* /proc/acpi/battery/*; do
		present=""; full=""; rem=""; state=""
		case "$bat" in
			/sys/*)
				if [ -r "$bat/uevent" ]; then
					. "$bat/uevent"
					present="$POWER_SUPPLY_PRESENT"
					full="$POWER_SUPPLY_CHARGE_FULL"
					rem="$POWER_SUPPLY_CHARGE_NOW"
					state="$POWER_SUPPLY_STATUS"
					[ "$present" = "1" ] && [ -n "$full" ] && [ -n "$rem" ] && [ -n "$state" ] && break
				fi
			;;
			/proc/*)
				[ -f "$bat/info" ] || continue
				while read line; do
					set -- ${line}
					case "$line" in
						present:*)
							# make sure that this battery is present
							[ "$2" = "no" ] && continue 2
							present="$2";;
					   	last\ full\ capacity:*) full="$4";;
					esac
					[ -n "$present" -a -n "$full" ] && break
				done < "${bat}/info"
				while read line; do
					set -- ${line}
					case "$line" in
						remaining\ capacity:*) rem="$3";;
						charging\ state:*) state="$3";;
					esac
					[ -n "$rem" -a -n "$state" ] && break
				done < "$bat/state"
				[ -n "$full" ] && [ -n "$rem" ] && [ -n "$state" ] && break
			;;
		esac
	done
	if [ $rem -ge 0 ] && [ $full -gt 0 ]; then
		percent=$(((100*$rem)/$full))
		if [ "$percent" -lt 33 ]; then
			color="R k"
			bcolor="b R k"
		elif [ "$percent" -lt 67 ]; then
			color="Y k"
			bcolor="b Y k"
		else
			color="G k"
			bcolor="b G k"
		fi
		percent="$percent$PCT"
		case $state in
			charging|Charging|Unknown) sign="+" ;;
			discharging|Discharging) sign="-" ;;
			charged|Unknown|Full) sign="="; percent="" ;;
			*) sign="$state" ;;
		esac
		[ -n "$percent" ] || return
		color $bcolor; printf "%s" "$percent"; color -; color $color; printf "%s" "$sign"; color --
	fi
}

# vi: syntax=sh ts=4 noexpandtab