2021-01-18 02:54:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-07-21 02:07:23 +00:00
|
|
|
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
|
|
|
|
|
2024-03-08 19:11:04 +00:00
|
|
|
BLFILE="$HOME/.local/state/idle.dim"
|
2024-08-18 19:06:26 +00:00
|
|
|
POWERMODE="$HOME/.local/state/power_profile"
|
2024-03-08 19:11:04 +00:00
|
|
|
OPFILE="$HOME/.local/state/active_outputs"
|
2024-04-20 00:08:30 +00:00
|
|
|
STATEFILE="$HOME/.local/state/idle.state"
|
2024-03-08 19:11:04 +00:00
|
|
|
IDLEMODE=$(cat $HOME/.local/state/idle_mode)
|
2024-08-16 17:09:00 +00:00
|
|
|
|
2023-07-21 02:07:23 +00:00
|
|
|
if [[ -n $2 ]]; then
|
|
|
|
if [[ $2 == '--ignore_mode' ]]; then
|
2024-03-08 18:59:21 +00:00
|
|
|
IDLEMODE='hibernate'
|
2024-08-16 17:09:00 +00:00
|
|
|
elif [[ $1 == "mode" ]]; then
|
|
|
|
IDLEMODE=$2
|
2023-07-21 02:07:23 +00:00
|
|
|
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
|
|
|
|
2023-07-21 02:07:23 +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
|
|
|
|
2023-07-21 02:07:23 +00:00
|
|
|
--ignore_mode - Run <option> even if in a lower mode in the sequence
|
|
|
|
"
|
|
|
|
exit
|
2022-10-31 22:08:05 +00:00
|
|
|
}
|
2021-01-18 02:54:21 +00:00
|
|
|
|
2023-07-21 02:07:23 +00:00
|
|
|
if [[ -z $1 ]]; then
|
|
|
|
echo "Missing argument"
|
|
|
|
usage
|
2024-08-16 17:09:00 +00:00
|
|
|
elif [[ $1 == "mode" ]]; then
|
|
|
|
if [[ -n $2 ]]; then
|
|
|
|
echo -n $2 > $HOME/.local/state/idle_mode
|
|
|
|
elif [[ $IDLEMODE == 0 ]]; then
|
|
|
|
echo -n 'fade' > $HOME/.local/state/idle_mode
|
|
|
|
elif [[ $IDLEMODE == 1 ]]; then
|
|
|
|
echo -n 'dim' > $HOME/.local/state/idle_mode
|
|
|
|
elif [[ $IDLEMODE == 2 ]]; then
|
|
|
|
echo -n 'lock' > $HOME/.local/state/idle_mode
|
|
|
|
elif [[ $IDLEMODE == 3 ]]; then
|
|
|
|
echo -n 'sleep' > $HOME/.local/state/idle_mode
|
|
|
|
elif [[ $IDLEMODE == 4 ]]; then
|
|
|
|
echo -n 'hibernate' > $HOME/.local/state/idle_mode
|
|
|
|
elif [[ $IDLEMODE == 5 ]]; then
|
|
|
|
echo -n 'none' > $HOME/.local/state/idle_mode
|
|
|
|
fi
|
|
|
|
cat $HOME/.local/state/idle_mode
|
2023-07-21 02:07:23 +00:00
|
|
|
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" \
|
2023-07-21 02:07:23 +00:00
|
|
|
resume "$0 unsleep" \
|
2023-07-21 21:06:24 +00:00
|
|
|
timeout $HIBERNATE_TIMEOUT "$0 hibernate" \
|
2023-07-21 02:07:23 +00:00
|
|
|
resume "$0 unhibernate"
|
2024-04-20 00:08:30 +00:00
|
|
|
if [[ -e $STATEFILE ]]; then
|
|
|
|
rm $STATEFILE
|
|
|
|
fi
|
2023-07-21 02:07:23 +00:00
|
|
|
elif [[ $1 == "daemon" ]]; then
|
|
|
|
nohup $0 start 2>/dev/null &
|
|
|
|
elif [[ $1 == "stop" ]]; then
|
|
|
|
pkill swayidle
|
2024-04-20 00:08:30 +00:00
|
|
|
if [[ -e $STATEFILE ]]; then
|
|
|
|
rm $STATEFILE
|
|
|
|
fi
|
2023-07-21 02:07:23 +00:00
|
|
|
elif [[ $1 == "fade" ]]; then
|
|
|
|
if [[ $IDLEMODE -gt 0 ]]; then
|
2024-04-20 00:08:30 +00:00
|
|
|
echo 'hide' > $STATEFILE
|
2024-03-08 19:11:04 +00:00
|
|
|
if [[ ! -e $HOME/.local/state/sway-hidden ]]; then
|
|
|
|
kill -USR2 $(cat $HOME/.local/state/sway-transparency)
|
2024-04-20 00:07:12 +00:00
|
|
|
makoctl mode -a hide
|
2023-07-21 02:07:23 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
elif [[ $1 == "unfade" ]]; then
|
2024-03-08 19:11:04 +00:00
|
|
|
if [[ -e $HOME/.local/state/sway-hidden ]]; then
|
|
|
|
kill -USR2 $(cat $HOME/.local/state/sway-transparency)
|
2023-07-21 02:07:23 +00:00
|
|
|
# TODO: This is a hack. There's no reason that waybar should be dead.
|
|
|
|
#$HOME/scripts/sway/displays.pl -w
|
2024-04-20 00:07:12 +00:00
|
|
|
makoctl mode -r hide
|
2023-07-21 02:07:23 +00:00
|
|
|
fi
|
2024-04-20 00:08:30 +00:00
|
|
|
if [[ -e $STATEFILE ]]; then
|
|
|
|
rm $STATEFILE
|
|
|
|
fi
|
2023-07-21 02:07:23 +00:00
|
|
|
elif [[ $1 == "dim" ]]; then
|
|
|
|
if [[ $IDLEMODE -gt 1 ]]; then
|
2024-04-20 00:08:30 +00:00
|
|
|
echo 'dim' > $STATEFILE
|
2023-07-21 02:07:23 +00:00
|
|
|
# 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
|
2023-11-17 20:18:06 +00:00
|
|
|
makoctl set-mode default
|
2023-07-21 02:07:23 +00:00
|
|
|
$HOME/scripts/thinkpad/blc.pl = $(cat $BLFILE) &>-
|
2024-04-20 00:08:30 +00:00
|
|
|
if [[ -e $STATEFILE ]]; then
|
|
|
|
rm $STATEFILE
|
|
|
|
fi
|
2023-07-21 02:07:23 +00:00
|
|
|
elif [[ $1 == "lock" ]]; then
|
|
|
|
if [[ $IDLEMODE -gt 2 ]]; then
|
2024-04-20 00:08:30 +00:00
|
|
|
echo 'lock' > $STATEFILE
|
2024-02-22 18:06:07 +00:00
|
|
|
swaylock -f -c 00000000
|
2023-07-21 02:07:23 +00:00
|
|
|
fi
|
|
|
|
elif [[ $1 == "unlock" ]]; then
|
|
|
|
kill -USR1 $(pgrep swaylock)
|
2024-04-20 00:08:30 +00:00
|
|
|
if [[ -e $STATEFILE ]]; then
|
|
|
|
rm $STATEFILE
|
|
|
|
fi
|
2023-07-21 02:07:23 +00:00
|
|
|
elif [[ $1 == "sleep" ]]; then
|
|
|
|
if [[ $IDLEMODE -gt 3 ]]; then
|
2024-08-18 19:06:26 +00:00
|
|
|
powerprofilesctl get > $POWERMODE
|
|
|
|
powerprofilesctl set power-saver
|
2024-04-20 00:08:30 +00:00
|
|
|
echo 'sleep' > $STATEFILE
|
2023-07-21 02:07:23 +00:00
|
|
|
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
|
2024-03-08 19:11:04 +00:00
|
|
|
$HOME/scripts/sway/displays.pl 2>&1 >$HOME/.local/state/display_unsleep
|
2024-04-20 00:08:30 +00:00
|
|
|
if [[ -e $STATEFILE ]]; then
|
|
|
|
rm $STATEFILE
|
|
|
|
fi
|
2024-08-18 19:06:26 +00:00
|
|
|
powerprofilesctl set $(cat $POWERMODE)
|
2023-07-21 02:07:23 +00:00
|
|
|
elif [[ $1 == "hibernate" ]]; then
|
|
|
|
if [[ $IDLEMODE -gt 4 ]]; then
|
2024-04-20 00:08:30 +00:00
|
|
|
echo 'hibernate' > $STATEFILE
|
2023-07-21 02:07:23 +00:00
|
|
|
sudo systemctl suspend-then-hibernate.target
|
|
|
|
fi
|
2021-01-18 02:54:21 +00:00
|
|
|
else
|
2023-07-21 02:07:23 +00:00
|
|
|
echo "Invalid argument: $1"
|
|
|
|
usage
|
2021-01-18 02:54:21 +00:00
|
|
|
fi
|