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 #!/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" BLFILE="$HOME/.spool/idle.dim"
OPFILE="$HOME/.spool/active_outputs" 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 function usage() {
DIM_TIMEOUT=120 # two minutes echo "usage: $0 <option> [--ignore_mode]
LOCK_TIMEOUT=300 # five minutes start - Initialize 'swayidle' script. This should be declared in Sway config
DPMS_TIMEOUT=600 # ten minutes stop - Kill 'swayidle' script. Consider idle-inhibitor or sleepmode 'none' instead
SUSPEND_TIMEOUT=3600 # one hour 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() Idle will result in the following actions in sequence:
{
echo "usage: $0 <option> fade - 1 minute. Fade all windows by reducing opacity to 0 via Sway IPC
start - Initialize 'swayidle' script. This should be declared in Sway config. dim - 2 minutes. Dim the display with \`gammactl\`
stop - Kill 'swayidle' script. Consider idle-inhibitor instead. lock - 5 minutes. Lock the screen with \`swaylock\`
fade - First idle action. Fades all windows to show background. sleep - 10 minutes. Sleep by disabling power to all displays via DPMS
unfade - Restore opacity for faded windows. hibernate - 60 minutes. Hibernate by entering 'suspend-then-hibernate.target'
dim - Reduce display brightness to minimum.
undim - Restore brightness to level prior to 'dim'. Timeouts define statically in the program.
lock - Mark as inactive and start 'swaylock'.
unlock - (Run upon unlock) Mark as active again. Idle modes are additive, thus the idle_mode is the final action you would like to run in the
sleep - Disable displays, but continue background processes. sequence. For example: 'sleep' mode will enable 'fade', 'lock', and 'sleep', but not 'hibernate'
wake - Re-enable displays.
hibernate - Hibernate. --ignore_mode - Run <option> even if in a lower mode in the sequence
help - This message." "
exit exit
} }
if [ -z $1 ]; then if [[ -z $1 ]]; then
echo "Missing argument" echo "Missing argument"
usage usage
elif [ $1 == "-h" ] || [ $1 == "--help" ] || [ $1 == "help" ]; then elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]] || [[ $1 == "help" ]]; then
usage usage
elif [ $1 == "start" ]; then elif [[ $1 == "start" ]]; then
swayidle -w \ swayidle -w \
timeout $FADE_TIMEOUT "$0 fade" \ timeout $FADE_TIMEOUT "$0 fade" \
resume "$0 unfade" \ resume "$0 unfade" \
timeout $DIM_TIMEOUT "$0 dim" \ timeout $DIM_TIMEOUT "$0 dim" \
resume "$0 undim" \ resume "$0 undim" \
timeout $LOCK_TIMEOUT "$0 lock" \ timeout $LOCK_TIMEOUT "$0 lock" \
resume "$0 unlock" \ resume "$0 unlock" \
timeout $DPMS_TIMEOUT "$0 sleep" \ timeout $DPMS_TIMEOUT "$0 sleep" \
resume "$0 wake" \ resume "$0 unsleep" \
timeout $SUSPEND_TIMEOUT "$0 hibernate" timeout $SUSPEND_TIMEOUT "$0 hibernate" \
elif [ $1 == "stop" ]; then resume "$0 unhibernate"
pkill swayidle elif [[ $1 == "daemon" ]]; then
elif [ $1 == "fade" ]; then nohup $0 start 2>/dev/null &
if [ ! -e $HOME/.spool/sway-hidden ]; then elif [[ $1 == "stop" ]]; then
kill -USR2 `cat $HOME/.spool/sway-transparency` pkill swayidle
fi elif [[ $1 == "fade" ]]; then
elif [ $1 == "unfade" ]; then if [[ $IDLEMODE -gt 0 ]]; then
if [ -e $HOME/.spool/sway-hidden ]; then if [[ ! -e $HOME/.spool/sway-hidden ]]; then
kill -USR2 `cat $HOME/.spool/sway-transparency` kill -USR2 $(cat $HOME/.spool/sway-transparency)
/home/jpm/scripts/sway/displays.pl -w fi
fi fi
elif [ $1 == "dim" ]; then elif [[ $1 == "unfade" ]]; then
echo $($HOME/scripts/thinkpad/blc.pl %) > $BLFILE if [[ -e $HOME/.spool/sway-hidden ]]; then
$HOME/scripts/thinkpad/blc.pl = 1 &>- kill -USR2 $(cat $HOME/.spool/sway-transparency)
elif [ $1 == "undim" ]; then # TODO: This is a hack. There's no reason that waybar should be dead.
$HOME/scripts/thinkpad/blc.pl = `cat $BLFILE` &>- #$HOME/scripts/sway/displays.pl -w
elif [ $1 == "lock" ]; then fi
# Change nick to AFK elif [[ $1 == "dim" ]]; then
#ssh jpm@john.me.tz -i /home/jpm/.ssh/no_pass -t \ if [[ $IDLEMODE -gt 1 ]]; then
#'screen -S irssi -X stuff "/nick jpmAFK^M"' # I don't currently have any dimable monitors, so fade was created instead
: echo $($HOME/scripts/thinkpad/blc.pl %) >$BLFILE
elif [ $1 == "unlock" ]; then $HOME/scripts/thinkpad/blc.pl = 1 &>-
# Change nick to AFK fi
#ssh jpm@john.me.tz -i /home/jpm/.ssh/no_pass -t \ elif [[ $1 == "undim" ]]; then
#'screen -S irssi -X stuff "/nick jpm^M"' $HOME/scripts/thinkpad/blc.pl = $(cat $BLFILE) &>-
: elif [[ $1 == "lock" ]]; then
elif [ $1 == "sleep" ]; then if [[ $IDLEMODE -gt 2 ]]; then
if [ -e $NOHIBERNATE ]; then swaylock -c 00000000
:: fi
else elif [[ $1 == "unlock" ]]; then
for i in `cat $OPFILE`; do swaymsg "output $i dpms off"; done kill -USR1 $(pgrep swaylock)
fi elif [[ $1 == "sleep" ]]; then
elif [ $1 == "wake" ]; then if [[ $IDLEMODE -gt 3 ]]; then
for i in `cat $OPFILE`; do swaymsg "output $i dpms on"; done for i in $(cat $OPFILE); do swaymsg "output $i dpms off"; done
#/home/jpm/scripts/sway/displays.pl fi
elif [ $1 == "hibernate" ]; then elif [[ $1 == "unsleep" ]]; then
if [ -e $NOHIBERNATE ]; then for i in $(cat $OPFILE); do swaymsg "output $i dpms on"; done
:: #$HOME/scripts/sway/displays.pl
else elif [[ $1 == "hibernate" ]]; then
sudo systemctl start hibernate.target if [[ $IDLEMODE -gt 4 ]]; then
fi sudo systemctl suspend-then-hibernate.target
fi
else else
echo "Invalid argument: $1" echo "Invalid argument: $1"
usage usage
fi 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