scripts/sway/idle.sh

167 lines
5.1 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
BLFILE="$HOME/.local/state/idle.dim"
OPFILE="$HOME/.local/state/active_outputs"
STATEFILE="$HOME/.local/state/idle.state"
IDLEMODE=$(cat $HOME/.local/state/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"
if [[ -e $STATEFILE ]]; then
rm $STATEFILE
fi
elif [[ $1 == "daemon" ]]; then
nohup $0 start 2>/dev/null &
elif [[ $1 == "stop" ]]; then
pkill swayidle
if [[ -e $STATEFILE ]]; then
rm $STATEFILE
fi
elif [[ $1 == "fade" ]]; then
if [[ $IDLEMODE -gt 0 ]]; then
echo 'hide' > $STATEFILE
if [[ ! -e $HOME/.local/state/sway-hidden ]]; then
kill -USR2 $(cat $HOME/.local/state/sway-transparency)
makoctl mode -a hide
fi
fi
elif [[ $1 == "unfade" ]]; then
if [[ -e $HOME/.local/state/sway-hidden ]]; then
kill -USR2 $(cat $HOME/.local/state/sway-transparency)
# TODO: This is a hack. There's no reason that waybar should be dead.
#$HOME/scripts/sway/displays.pl -w
makoctl mode -r hide
fi
if [[ -e $STATEFILE ]]; then
rm $STATEFILE
fi
elif [[ $1 == "dim" ]]; then
if [[ $IDLEMODE -gt 1 ]]; then
echo 'dim' > $STATEFILE
# I don't currently have any dimable monitors, so fade was created instead
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) &>-
if [[ -e $STATEFILE ]]; then
rm $STATEFILE
fi
elif [[ $1 == "lock" ]]; then
if [[ $IDLEMODE -gt 2 ]]; then
echo 'lock' > $STATEFILE
swaylock -f -c 00000000
fi
elif [[ $1 == "unlock" ]]; then
kill -USR1 $(pgrep swaylock)
if [[ -e $STATEFILE ]]; then
rm $STATEFILE
fi
elif [[ $1 == "sleep" ]]; then
if [[ $IDLEMODE -gt 3 ]]; then
echo 'sleep' > $STATEFILE
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
2024-03-08 18:58:45 +00:00
cd $HOME/scripts/sway
source $HOME/.dotfiles/bash/plenv-path.sh
$HOME/scripts/sway/displays.pl 2>&1 >$HOME/.local/state/display_unsleep
if [[ -e $STATEFILE ]]; then
rm $STATEFILE
fi
elif [[ $1 == "hibernate" ]]; then
if [[ $IDLEMODE -gt 4 ]]; then
echo 'hibernate' > $STATEFILE
sudo systemctl suspend-then-hibernate.target
fi
else
echo "Invalid argument: $1"
usage
fi