1#!/bin/sh -e
2#
3#    battery: print the state of the battery
4#
5#    Copyright (C) 2009 Raphaël Pinson.
6#    Copyright (C) 2011-2014 Dustin Kirkland
7#
8#    Authors: Raphaël Pinson <raphink@ubuntu.com>
9#             Dustin Kirkland <kirkland@byobu.org>
10#
11#    This program is free software: you can redistribute it and/or modify
12#    it under the terms of the GNU General Public License as published by
13#    the Free Software Foundation, version 3 of the License.
14#
15#    This program is distributed in the hope that it will be useful,
16#    but WITHOUT ANY WARRANTY; without even the implied warranty of
17#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18#    GNU General Public License for more details.
19#
20#    You should have received a copy of the GNU General Public License
21#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23__battery_detail() {
24	local bat
25	for bat in /proc/acpi/battery/*; do
26		cat "$bat/info"
27		cat "$bat/state"
28	done
29	# FIXME: do the same thing with the /sys interface
30}
31
32__battery() {
33	local bat line present sign state percent full rem color bcolor
34	# Linux support
35	present=""; full="0"; rem="0"; state=""
36	for bat in $BATTERY /sys/class/power_supply/* /proc/acpi/battery/*; do
37		case "$bat" in
38			/sys/*)
39				if [ -r "$bat/uevent" ]; then
40					. "$bat/uevent"
41					case "$POWER_SUPPLY_NAME" in AC|ac|Ac|aC) continue ;; esac
42					present="$POWER_SUPPLY_PRESENT"
43					# Some use "CHARGE", others use "ENERGY", still others "CAPACITY"
44					[ -n "$POWER_SUPPLY_CHARGE_FULL" ] && full=$((POWER_SUPPLY_CHARGE_FULL+full))
45					[ -n "$POWER_SUPPLY_ENERGY_FULL" ] && full=$((POWER_SUPPLY_ENERGY_FULL+full))
46					[ -n "$POWER_SUPPLY_CHARGE_NOW" ] && rem=$((POWER_SUPPLY_CHARGE_NOW+rem))
47					[ -n "$POWER_SUPPLY_ENERGY_NOW" ] && rem=$((POWER_SUPPLY_ENERGY_NOW+rem))
48					if [ -n "$POWER_SUPPLY_CAPACITY" ] && [ ! -n "$POWER_SUPPLY_ENERGY_NOW" ] && [ ! -n "$POWER_SUPPLY_CHARGE_NOW" ]; then
49						rem="$POWER_SUPPLY_CAPACITY" && full="100"
50					fi
51					[ "$POWER_SUPPLY_STATUS" != "Unknown" ] && state="$POWER_SUPPLY_STATUS"
52				fi
53			;;
54			/proc/*)
55				[ -f "$bat/info" ] || continue
56				while read line; do
57					set -- ${line}
58					case "$line" in
59						present:*)
60							# make sure that this battery is present
61							[ "$2" = "no" ] && continue 2
62							present="$2";;
63					   	last\ full\ capacity:*) full="$4";;
64					esac
65					[ -n "$present" -a -n "$full" ] && break
66				done < "${bat}/info"
67				while read line; do
68					set -- ${line}
69					case "$line" in
70						remaining\ capacity:*) rem="$3";;
71						charging\ state:*) state="$3";;
72					esac
73					[ -n "$rem" -a -n "$state" ] && break
74				done < "$bat/state"
75				[ -n "$full" ] && [ -n "$rem" ] && [ -n "$state" ] && break
76			;;
77		esac
78	done
79	# Mac OS X support
80	if eval $BYOBU_TEST /usr/sbin/ioreg >/dev/null 2>&1; then
81		# MacOS support
82		local key
83		for key in CurrentCapacity MaxCapacity ExternalChargeCapable FullyCharged; do
84			line=$(/usr/sbin/ioreg -n AppleSmartBattery -w0 | grep $key | sed -e 's/|//g' | awk '{ print $3 }')
85			case "$key" in
86				CurrentCapacity) rem="$line";;
87				MaxCapacity) full="$line";;
88				ExternalChargeCapable)
89					if [ "${line}" = "Yes" ]; then
90						state="charging"
91					elif [ "${line}" = "No" ]; then
92						state="discharging"
93					fi
94				;;
95				FullyCharged)
96					if [ "${line}" = "Yes" ]; then
97						state="charged"
98					fi
99				;;
100			esac
101		done
102	fi
103	if [ $rem -ge 0 ] && [ $full -gt 0 ]; then
104		percent=$(((100*$rem)/$full))
105		if [ "$percent" -lt 33 ]; then
106			color="R w"
107			bcolor="b R w"
108		elif [ "$percent" -lt 67 ]; then
109			color="Y k"
110			bcolor="b Y k"
111		else
112			color="G k"
113			bcolor="b G k"
114		fi
115		percent="${percent}${PCT}"
116		# Convert state to lower case
117		state=$(printf "%s" "$state" | $BYOBU_SED 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')
118		case $state in
119			charging) sign="+" ;;
120			discharging) sign="-" ;;
121			charged|unknown|full) sign="=" ;;
122			*) sign="$state" ;;
123		esac
124		if [ -z "$percent" ]; then
125			rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/battery"*
126			return
127		fi
128		color $bcolor; printf "%s" "$percent"; color -; color $color; printf "%s" "$sign"; color --
129	fi
130}
131
132# vi: syntax=sh ts=4 noexpandtab
133