battery 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. __battery_detail() {
  23. local bat
  24. for bat in /proc/acpi/battery/*; do
  25. cat "$bat/info"
  26. cat "$bat/state"
  27. done
  28. # FIXME: do the same thing with the /sys interface
  29. }
  30. __battery() {
  31. local bat line present sign state percent full rem color bcolor
  32. # Linux support
  33. present=""; full="0"; rem="0"; state=""
  34. for bat in $BATTERY /sys/class/power_supply/* /proc/acpi/battery/*; do
  35. case "$bat" in
  36. /sys/*)
  37. if [ -r "$bat/uevent" ]; then
  38. . "$bat/uevent"
  39. case "$POWER_SUPPLY_NAME" in AC|ac|Ac|aC) continue ;; esac
  40. present="$POWER_SUPPLY_PRESENT"
  41. # Some use "CHARGE", others use "ENERGY", still others "CAPACITY"
  42. [ -n "$POWER_SUPPLY_CHARGE_FULL" ] && full=$((POWER_SUPPLY_CHARGE_FULL+full))
  43. [ -n "$POWER_SUPPLY_ENERGY_FULL" ] && full=$((POWER_SUPPLY_ENERGY_FULL+full))
  44. [ -n "$POWER_SUPPLY_CHARGE_NOW" ] && rem=$((POWER_SUPPLY_CHARGE_NOW+rem))
  45. [ -n "$POWER_SUPPLY_ENERGY_NOW" ] && rem=$((POWER_SUPPLY_ENERGY_NOW+rem))
  46. if [ -n "$POWER_SUPPLY_CAPACITY" ] && [ ! -n "$POWER_SUPPLY_ENERGY_NOW" ] && [ ! -n "$POWER_SUPPLY_CHARGE_NOW" ]; then
  47. rem="$POWER_SUPPLY_CAPACITY" && full="100"
  48. fi
  49. [ "$POWER_SUPPLY_STATUS" != "Unknown" ] && state="$POWER_SUPPLY_STATUS"
  50. fi
  51. ;;
  52. /proc/*)
  53. [ -f "$bat/info" ] || continue
  54. while read line; do
  55. set -- ${line}
  56. case "$line" in
  57. present:*)
  58. # make sure that this battery is present
  59. [ "$2" = "no" ] && continue 2
  60. present="$2";;
  61. last\ full\ capacity:*) full="$4";;
  62. esac
  63. [ -n "$present" -a -n "$full" ] && break
  64. done < "${bat}/info"
  65. while read line; do
  66. set -- ${line}
  67. case "$line" in
  68. remaining\ capacity:*) rem="$3";;
  69. charging\ state:*) state="$3";;
  70. esac
  71. [ -n "$rem" -a -n "$state" ] && break
  72. done < "$bat/state"
  73. [ -n "$full" ] && [ -n "$rem" ] && [ -n "$state" ] && break
  74. ;;
  75. esac
  76. done
  77. # Mac OS X support
  78. if eval $BYOBU_TEST /usr/sbin/ioreg >/dev/null 2>&1; then
  79. # MacOS support
  80. local key
  81. for key in CurrentCapacity MaxCapacity ExternalChargeCapable FullyCharged; do
  82. line=$(/usr/sbin/ioreg -n AppleSmartBattery -w0 | grep $key | sed -e 's/|//g' | awk '{ print $3 }')
  83. case "$key" in
  84. CurrentCapacity) rem="$line";;
  85. MaxCapacity) full="$line";;
  86. ExternalChargeCapable)
  87. if [ "${line}" = "Yes" ]; then
  88. state="charging"
  89. elif [ "${line}" = "No" ]; then
  90. state="discharging"
  91. fi
  92. ;;
  93. FullyCharged)
  94. if [ "${line}" = "Yes" ]; then
  95. state="charged"
  96. fi
  97. ;;
  98. esac
  99. done
  100. fi
  101. if [ $rem -ge 0 ] && [ $full -gt 0 ]; then
  102. percent=$(((100*$rem)/$full))
  103. if [ "$percent" -lt 33 ]; then
  104. color="R w"
  105. bcolor="b R w"
  106. elif [ "$percent" -lt 67 ]; then
  107. color="Y k"
  108. bcolor="b Y k"
  109. else
  110. color="G k"
  111. bcolor="b G k"
  112. fi
  113. percent="${percent}${PCT}"
  114. # Convert state to lower case
  115. state=$(printf "%s" "$state" | $BYOBU_SED 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')
  116. case $state in
  117. charging) sign="+" ;;
  118. discharging) sign="-" ;;
  119. charged|unknown|full) sign="=" ;;
  120. *) sign="$state" ;;
  121. esac
  122. if [ -z "$percent" ]; then
  123. rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/battery"*
  124. return
  125. fi
  126. color $bcolor; printf "%s" "$percent"; color -; color $color; printf "%s" "$sign"; color --
  127. fi
  128. }
  129. # vi: syntax=sh ts=4 noexpandtab