Re: [dev] dwm battery level alarm system

From: Peter John Hartman <peterjohnhartman_AT_gmail.com>
Date: Wed, 26 Jan 2011 17:21:21 -0500

acpid

e.g.

==
#!/bin/sh
# /etc/acpi/default.sh
# Default acpi script that takes an entry for all actions
. /home/peterjh/.bash_functions

# How long to wait once the lid is closed before going into suspend
LID_STALL=60
# How low can we go.
BAT_LIMIT=325
# script
HIB_SCRIPT=/home/peterjh/bin/hib.sh
# which bat?
if [ $HOSTNAME = "trillap" ]; then
    BAT="BAT1"
else
    BAT="BAT0"
fi

set $*

group=${1%%/*}
action=${1#*/}
device=$2
id=$3
value=$4

log_unhandled() {
    logger -t acpi "ACPI event unhandled: $*"
}

logger -t acpi "ACPI group($group) action($action) device($device) id($id) value($value) event: $*"

case "$group" in
    button)
        case "$action" in
            power)
                logger -t acpi "Power: hib called."
                                $HIB_SCRIPT
            # /sbin/init 0
                ;;
            lid)
                                if [ $HOSTNAME = "trillap" ]; then
                                        exit
                                fi
                if [ -e "/proc/acpi/button/lid/LID/state" ]; then
                        LIDSTATE=$(/bin/sed -ne "/state:/{s/^state:[ ]*\([a-zA-Z]*\)$/\1/p;q}" /proc/acpi/button/lid/LID/state)
                      
                    if [ "$LIDSTATE" = "closed" ]; then
                                                #
                                                # You can touch /tmp/nolid to disable automatic suspend when lid is closed
                        #
                                                if [ -f /tmp/nolid ] ; then
                                logger -t acpi "Lid: Nolid set. Not suspending."
                            else
                                logger -t acpi "Lid: Suspending to disk in ${LID_STALL}s."
                                                        #
                                                        # Wait 30 seconds before suspending if we close the lid
                                                        #
                                                        touch /tmp/lid-closed
                                sleep ${LID_STALL}s
                                LIDSTATE=$(/bin/sed -ne "/state:/{s/^state:[ ]*\([a-zA-Z]*\)$/\1/p;q}" /proc/acpi/button/lid/LID/state)
                            if [ "$LIDSTATE" = "closed" ]; then
                                       logger -t acpi "Lid: Still closed. Suspending now."
                                                                rm -f /tmp/lid-closed
                                                                $HIB_SCRIPT
                                                    else
                                                                logger -t acpi "Lid: Lid lifted. Not suspending."
                                                                rm -f /tmp/lid-closed
                                                        fi
                             fi
                    fi
                fi

                ;;

            sleep)
                $HIB_SCRIPT ;;
            
# *) log_unhandled $* ;;
        esac
        ;;

    battery)
                sleep 1s
        if [ -e "/proc/acpi/battery/$BAT/state" ]; then
                PRESENT=$(/bin/sed -ne "/present:/{s/^present:[ ]*\([a-z]*\)$/\1/p;q}" /proc/acpi/battery/$BAT/state)
            if [ "$PRESENT" = "yes" ]; then
                    STATE=$(/bin/sed -ne "/charging state:/{s/^charging state:[ ]*\([a-zA-Z]*\)$/\1/p;q}" /proc/acpi/battery/$BAT/state)
                BATTERY=$(/bin/sed -ne "/remaining capacity:/{s/^remaining capacity:[ ]*\([0-9]*\) m[WA]h$/\1/p;q}" /proc/acpi/battery/$BAT/state)
                #logger -t acpi "Checking battery level ($STATE): $BATTERY mWh."
                NEAR_LIMIT=`echo "$BAT_LIMIT + 300" | bc`
                if [ "$STATE" = "discharging" ]; then
                        if [ "$BATTERY" -lt "$BAT_LIMIT" ]; then
                            logger -t acpi"Battery at ${BATTERY} mWh. Suspending to disk."
                                                $HIB_SCRIPT
                          fi
                          #elif [ "$BATTERY" -lt "$NEAR_LIMIT" ]; then
                                #logger -t acpi "`yacpi -p -b`"
                                #sudo -u peterjh -- /home/peterjh/bin/tmux-echo "`yacpi -p -b`"
                    #else
                            #logger -t acpi "`yacpi -p -b`"
                                #sudo -u peterjh -- /home/peterjh/bin/tmux-echo "`yacpi -p -b`"
                    #fi
                  
                fi
            fi
        fi
        ;;
    ac_adapter)
        case "$value" in
            *0)
                logger -t acpi "Setting cpufreq-set -g powersave."
                cpufreq-set -g powersave
                ;;

            *1)
                logger -t acpi "Setting cpufreq-set -g performance."
                cpufreq-set -g performance
                ;;

# *) log_unhandled $* ;;
        esac
        ;;

Peter

On Wed, Jan 26, 2011 at 11:18:26PM +0100, Danilo Bargen wrote:
> Hey list
>
> I need some kind of alarm system in dwm, as my laptop has already
> turned off several times due to running out of battery.
>
> I thought of different possibilities. Maybe some kind of popup that is
> displayed on every tag. Or letting the blue window title area blink
> red. In combination with some warning sound that gets played even if
> the volume is turned down.
>
> Does anyone here already have a feasible solution for that problem? If
> not, what do you think would be the best approach?
>
> Cheers
> Danilo
>

-- 
sic dicit magister P
PhD Candidate
Collaborative Programme in Ancient and Medieval Philosophy
University of Toronto
http://individual.utoronto.ca/peterjh
Received on Wed Jan 26 2011 - 23:21:21 CET

This archive was generated by hypermail 2.2.0 : Wed Jan 26 2011 - 23:24:03 CET