123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- error () {
- echo $1 >&2
- echo "Exiting" >&2
- exit $2
- }
- while getopts 'L:l:m:t:F:n' opt; do
- case $opt in
- L)
- [[ $OPTARG =~ ^[0-9]+$ ]] || error "${opt}: ${OPTARG} is not a number" 2
- UPPER_LIMIT="${OPTARG}"
- ;;
- l)
- [[ $OPTARG =~ ^[0-9]+$ ]] || error "${opt}: ${OPTARG} is not a number" 2
- LOWER_LIMIT="${OPTARG}"
- ;;
- m)
- MESSAGE="${OPTARG}"
- ;;
- n)
- DONT_USE_WISH="-n"
- ;;
- t)
- [[ $OPTARG =~ ^[0-9]+[ms]?$ ]] || error "${opt}: ${OPTARG} is not a valid period" 2
- SLEEP_TIME="${OPTARG}"
- ;;
- F)
- LOGFILE="${OPTARG}"
- ;;
- :)
- error "Option -$OPTARG requires an argument." 2
- ;;
- \?)
- exit 2
- ;;
- esac
- done
- get_awk_source() {
- cat <<EOF
- BEGIN {
- FS="=";
- }
- \$1 ~ /ENERGY_FULL_DESIGN$/ {
- f += \$2;
- }
- \$1 ~ /ENERGY_NOW\$/ {
- n += \$2;
- }
- END {
- print int(100*n/f);
- }
- EOF
- }
- is_battery_discharging() {
- cat $BATTERIES | grep Charging && return 1
- return 0
- } >/dev/null
- get_battery_perc() {
- cat $BATTERIES | awk -f <(get_awk_source)
- }
- show_popup() {
- # WISH_SCRIPT="wm state . withdrawn; tk_messageBox -icon warning -title \"Battery Warning\" -message \"${MESSAGE}\"; exit"
- # echo "$WISH_SCRIPT" | wish
- notify-send --urgency=critical --icon=warning "Battery Warning" "${MESSAGE}"
- }
- show_nagbar(){
- /usr/bin/i3-nagbar -m "${MESSAGE}"
- }
- show_message(){
- echo "showing warning" >&2
- # if [[ -z $DONT_USE_WISH ]] && which wish; then
- show_popup
- # else
- # show_nagbar
- # fi
- }
- main (){
- # Setting defaults
- UPPER_LIMIT="${UPPER_LIMIT:-10}"
- UPPER_HALF=$(( $UPPER_LIMIT / 2 ))
- LOWER_LIMIT=${LOWER_LIMIT:-$UPPER_HALF}
- MESSAGE="${MESSAGE:-Warning: Battery is getting low}"
- SLEEP_TIME="${SLEEP_TIME:-5m}"
- BATTERIES=/sys/class/power_supply/BAT*/uevent
- echo "Upper ${UPPER_LIMIT}; Lower ${LOWER_LIMIT}; sleep ${SLEEP_TIME}"
- echo "Current: $(get_battery_perc)%"
- LIMIT="${UPPER_LIMIT}"
- # This will be set to "y" after first click
- # So we know when to stop nagging
- POPUP_CLICKED=""
- while true; do
- echo -n "Checking.. "
- PERC=$(get_battery_perc)
- echo "got ${PERC}%"
- if is_battery_discharging; then
- if [[ $PERC -lt $LIMIT ]]; then
- show_message
- if [[ -z $POPUP_CLICKED ]]; then
- # first click; set limit lower
- POPUP_CLICKED="y"
- LIMIT=${LOWER_LIMIT}
- else
- # We clicked twice; No more popups
- LIMIT=0
- fi
- fi
- else
- echo "Battery Charging"
- # restart messages, reset limits
- POPUP_CLICKED=""
- if [[ $PERC -gt $UPPER_LIMIT ]]; then
- LIMIT=${UPPER_LIMIT}
- else
- LIMIT=${LOWER_LIMIT}
- fi
- fi
- echo "sleeping ${SLEEP_TIME}; current limit ${LIMIT}%; ${POPUP_CLICKED:+Popup was clicked}"
- sleep "${SLEEP_TIME}"
- done
- } >&2
- LOCK_FILE="${HOME}/.battery_state.lock"
- (
- if [[ -n $LOGFILE ]]; then
- exec >>$LOGFILE 2>&1
- fi
- flock -xn 200 || error "Cannot acquire lock ${LOCK_FILE}" 3
- main
- ) 200>"${LOCK_FILE}"
|