scripts/sway/idle.sh

139 lines
4.4 KiB
Bash
Raw Normal View History

#!/bin/bash
FADE_TIMEOUT=60 # one minute
DIM_TIMEOUT=120 # two minutes
LOCK_TIMEOUT=300 # five minutes
SLEEP_TIMEOUT=600 # ten minutes
HIBERNATE_TIMEOUT=3600 # one hour
2022-11-21 23:56:50 +00:00
BLFILE="$HOME/.spool/idle.dim"
OPFILE="$HOME/.spool/active_outputs"
IDLEMODE=$(cat $HOME/.spool/idle_mode)
if [[ -n $2 ]]; then
if [[ $2 == '--ignore_mode' ]]; then
$IDLEMODE='hibernate'
else
echo "Unknown mode $IDLEMODE"
exit
fi
fi
if [[ -z $IDLEMODE ]]; then
IDLEMODE="lock"
fi
if [[ $IDLEMODE == 'none' ]]; then
IDLEMODE=0
elif [[ $IDLEMODE == 'fade' ]]; then
IDLEMODE=1
elif [[ $IDLEMODE == 'dim' ]]; then
IDLEMODE=2
elif [[ $IDLEMODE == 'lock' ]]; then
IDLEMODE=3
elif [[ $IDLEMODE == 'sleep' ]]; then
IDLEMODE=4
elif [[ $IDLEMODE == 'hibernate' ]]; then
IDLEMODE=5
else
echo "Invalid idle_mode '$IDLEMODE'"
fi
function usage() {
echo "usage: $0 <option> [--ignore_mode]
start - Initialize 'swayidle' script. This should be declared in Sway config
stop - Kill 'swayidle' script. Consider idle-inhibitor or sleepmode 'none' instead
daemon - Run 'start' as a presistent background process
mode <mode> - Set the idle_mode: none, fade, dim, lock, sleep, hibernate
fade - First idle action. Fades all windows to show background
unfade - Restore opacity for faded windows
dim - Reduce display brightness to minimum
undim - Restore brightness to level prior to 'dim'
lock - Mark as inactive and start 'swaylock'
unlock - (Run upon unlock) Mark as active again
sleep - Disable displays, but continue background processes
unsleep - Re-enable displays
hibernate - Enter suspend-then-hibernate target
unhibernate - Clean up after waking from hibernation
help - This message
Idle will result in the following actions in sequence:
fade - 1 minute. Fade all windows by reducing opacity to 0 via Sway IPC
dim - 2 minutes. Dim the display with \`gammactl\`
lock - 5 minutes. Lock the screen with \`swaylock\`
sleep - 10 minutes. Sleep by disabling power to all displays via DPMS
hibernate - 60 minutes. Hibernate by entering 'suspend-then-hibernate.target'
Timeouts define statically in the program.
2022-10-31 22:08:05 +00:00
Idle modes are additive, thus the idle_mode is the final action you would like to run in the
sequence. For example: 'sleep' mode will enable 'fade', 'lock', and 'sleep', but not 'hibernate'
2022-10-31 22:08:05 +00:00
--ignore_mode - Run <option> even if in a lower mode in the sequence
"
exit
2022-10-31 22:08:05 +00:00
}
if [[ -z $1 ]]; then
echo "Missing argument"
usage
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]] || [[ $1 == "help" ]]; then
usage
elif [[ $1 == "start" ]]; then
swayidle -w \
timeout $FADE_TIMEOUT "$0 fade" \
resume "$0 unfade" \
timeout $DIM_TIMEOUT "$0 dim" \
resume "$0 undim" \
timeout $LOCK_TIMEOUT "$0 lock" \
resume "$0 unlock" \
2023-07-21 21:06:24 +00:00
timeout $SLEEP_TIMEOUT "$0 sleep" \
resume "$0 unsleep" \
2023-07-21 21:06:24 +00:00
timeout $HIBERNATE_TIMEOUT "$0 hibernate" \
resume "$0 unhibernate"
elif [[ $1 == "daemon" ]]; then
nohup $0 start 2>/dev/null &
elif [[ $1 == "stop" ]]; then
pkill swayidle
elif [[ $1 == "fade" ]]; then
if [[ $IDLEMODE -gt 0 ]]; then
if [[ ! -e $HOME/.spool/sway-hidden ]]; then
kill -USR2 $(cat $HOME/.spool/sway-transparency)
fi
fi
elif [[ $1 == "unfade" ]]; then
if [[ -e $HOME/.spool/sway-hidden ]]; then
kill -USR2 $(cat $HOME/.spool/sway-transparency)
# TODO: This is a hack. There's no reason that waybar should be dead.
#$HOME/scripts/sway/displays.pl -w
fi
elif [[ $1 == "dim" ]]; then
if [[ $IDLEMODE -gt 1 ]]; then
# I don't currently have any dimable monitors, so fade was created instead
makoctl set-mode away
echo $($HOME/scripts/thinkpad/blc.pl %) >$BLFILE
$HOME/scripts/thinkpad/blc.pl = 1 &>-
fi
elif [[ $1 == "undim" ]]; then
makoctl set-mode default
$HOME/scripts/thinkpad/blc.pl = $(cat $BLFILE) &>-
elif [[ $1 == "lock" ]]; then
if [[ $IDLEMODE -gt 2 ]]; then
swaylock -f -c 00000000
fi
elif [[ $1 == "unlock" ]]; then
kill -USR1 $(pgrep swaylock)
elif [[ $1 == "sleep" ]]; then
if [[ $IDLEMODE -gt 3 ]]; then
for i in $(cat $OPFILE); do swaymsg "output $i dpms off"; done
fi
elif [[ $1 == "unsleep" ]]; then
for i in $(cat $OPFILE); do swaymsg "output $i dpms on"; done
#$HOME/scripts/sway/displays.pl
elif [[ $1 == "hibernate" ]]; then
if [[ $IDLEMODE -gt 4 ]]; then
sudo systemctl suspend-then-hibernate.target
fi
else
echo "Invalid argument: $1"
usage
fi