foxygit / dotfiles Log in
commits tags

/bin/lidsuspend · 1.3 KB

raw
#!/bin/bash
# lidsuspend on|off|status
# Temporarily (dis/re)able what happens when the lid is closed, without
# touching /etc/systemd/logind.conf. Holds a systemd inhibitor lock alive
# in the background for as long as it's disabled.

PIDFILE="$HOME/.cache/lidsuspend.pid"

is_active() {
    [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null
}

case "$1" in
    off)
        if is_active; then
            echo "Already disabled (pid $(cat "$PIDFILE"))"
            exit 0
        fi
        mkdir -p "$(dirname "$PIDFILE")"
        setsid systemd-inhibit \
            --what=handle-lid-switch \
            --who="lidsuspend" \
            --why="manually disabled" \
            --mode=block \
            sleep infinity &
        disown
        echo $! > "$PIDFILE"
        echo "Lid-suspend disabled - closing the lid will do nothing (pid $!)"
        ;;
    on)
        if is_active; then
            kill "$(cat "$PIDFILE")" 2>/dev/null
        fi
        rm -f "$PIDFILE"
        echo "Lid-suspend re-enabled - normal behaviour on lid close"
        ;;
    status)
        if is_active; then
            echo "OFF - lid close is currently ignored"
        else
            echo "ON - normal behaviour"
        fi
        ;;
    *)
        echo "Usage: lidsuspend on|off|status"
        exit 1
        ;;
esac