Enhance idle options

Allow for calling idle with --ignore_mode to run an option beyond the
current sequence limit.

Allow 'dim' as it's own option.

Add 'lock' option to waybar script (and add it to source control in the
first place).

Simplify sequence logic by setting a numerical mode.

Improve help.
This commit is contained in:
John Mertz 2023-07-20 22:07:23 -04:00
parent 02a9dbef29
commit b87527f8b0
Signed by: jpm
GPG Key ID: E9C5EA2D867501AB
2 changed files with 166 additions and 81 deletions

View File

@ -1,91 +1,136 @@
#!/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/.spool/idle.dim"
OPFILE="$HOME/.spool/active_outputs"
NOHIBERNATE="$HOME/.spool/hibernate_inhibitor"
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
FADE_TIMEOUT=60 # one minute
DIM_TIMEOUT=120 # two minutes
LOCK_TIMEOUT=300 # five minutes
DPMS_TIMEOUT=600 # ten minutes
SUSPEND_TIMEOUT=3600 # one hour
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
function usage()
{
echo "usage: $0 <option>
start - Initialize 'swayidle' script. This should be declared in Sway config.
stop - Kill 'swayidle' script. Consider idle-inhibitor instead.
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.
wake - Re-enable displays.
hibernate - Hibernate.
help - This message."
exit
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.
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'
--ignore_mode - Run <option> even if in a lower mode in the sequence
"
exit
}
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" \
timeout $DPMS_TIMEOUT "$0 sleep" \
resume "$0 wake" \
timeout $SUSPEND_TIMEOUT "$0 hibernate"
elif [ $1 == "stop" ]; then
pkill swayidle
elif [ $1 == "fade" ]; then
if [ ! -e $HOME/.spool/sway-hidden ]; then
kill -USR2 `cat $HOME/.spool/sway-transparency`
fi
elif [ $1 == "unfade" ]; then
if [ -e $HOME/.spool/sway-hidden ]; then
kill -USR2 `cat $HOME/.spool/sway-transparency`
/home/jpm/scripts/sway/displays.pl -w
fi
elif [ $1 == "dim" ]; then
echo $($HOME/scripts/thinkpad/blc.pl %) > $BLFILE
$HOME/scripts/thinkpad/blc.pl = 1 &>-
elif [ $1 == "undim" ]; then
$HOME/scripts/thinkpad/blc.pl = `cat $BLFILE` &>-
elif [ $1 == "lock" ]; then
# Change nick to AFK
#ssh jpm@john.me.tz -i /home/jpm/.ssh/no_pass -t \
#'screen -S irssi -X stuff "/nick jpmAFK^M"'
:
elif [ $1 == "unlock" ]; then
# Change nick to AFK
#ssh jpm@john.me.tz -i /home/jpm/.ssh/no_pass -t \
#'screen -S irssi -X stuff "/nick jpm^M"'
:
elif [ $1 == "sleep" ]; then
if [ -e $NOHIBERNATE ]; then
::
else
for i in `cat $OPFILE`; do swaymsg "output $i dpms off"; done
fi
elif [ $1 == "wake" ]; then
for i in `cat $OPFILE`; do swaymsg "output $i dpms on"; done
#/home/jpm/scripts/sway/displays.pl
elif [ $1 == "hibernate" ]; then
if [ -e $NOHIBERNATE ]; then
::
else
sudo systemctl start hibernate.target
fi
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" \
timeout $DPMS_TIMEOUT "$0 sleep" \
resume "$0 unsleep" \
timeout $SUSPEND_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
echo $($HOME/scripts/thinkpad/blc.pl %) >$BLFILE
$HOME/scripts/thinkpad/blc.pl = 1 &>-
fi
elif [[ $1 == "undim" ]]; then
$HOME/scripts/thinkpad/blc.pl = $(cat $BLFILE) &>-
elif [[ $1 == "lock" ]]; then
if [[ $IDLEMODE -gt 2 ]]; then
swaylock -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
echo "Invalid argument: $1"
usage
fi

40
waybar/waybar-idle.sh Executable file
View File

@ -0,0 +1,40 @@
#!/bin/bash
FILE="$HOME/.spool/idle_mode"
if [ -e $FILE ]; then
MODE=$(cat $FILE)
else
MODE="fade"
echo -n $MODE >$FILE
fi
if [[ $MODE == "none" ]]; then
NEXT="fade"
ICON="☕"
elif [[ $MODE == "fade" ]]; then
ICON="🌌"
NEXT="dim"
elif [[ $MODE == "dim" ]]; then
ICON="⬛"
NEXT="lock"
elif [[ $MODE == "lock" ]]; then
ICON="🔒"
NEXT="sleep"
elif [[ $MODE == "sleep" ]]; then
ICON="💤"
NEXT="hibernate"
elif [[ $MODE == "hibernate" ]]; then
ICON="↯ "
NEXT="none"
else
echo "Invalid mode: $MODE"
fi
if [ -z $1 ] || [ "$1" == "bar" ]; then
echo '{"text":"'$ICON'","icon":"'$ICON'","tooltip":"Change sleep mode ('$MODE')","class":"'$MODE'"}'
elif [ "$1" == "rotate" ]; then
echo -n $NEXT >$FILE
$0 bar
else
echo "Invalid argumuent $1"
fi