bashtriggerskeyboardkeyboard-shortcutsi3

Bind keyboard shortcut to bash function to interrupt sleep?


I have a collection of .jpg background images that I want to use as backgrounds for my i3-gaps desktop. Currently, I have these two lines in my i3 config file for my wallpapers.

exec --no-startup-id randomwallpaper
bindsym $mod+i exec --no-startup-id feh --bg-scale --randomize /home/user/Pictures/bgart/*.jpg

This is my randomwallpaper script. It uses feh to set an image and wal to create a colorscheme based off of it.

#!/bin/bash
cd /home/user/Pictures/bgart
for file in $(ls); do
        shopt -s nullglob
        for i in *.jpg; do
                feh --bg-scale --randomize /home/user/Pictures/bgart/$i
                wal -q -i $i
                sleep 300
        done
done

On startup, randomwallpaper starts and every 5 minutes the wallpaper changes along with the colorscheme. However, I can also press Win+I to manually switch to a random wallpaper. Is it possible to add a trigger of some sort to interrupt the cycle? Maybe have the script as a function and add a key to call it? That way, I can have the above script running and if I get bored of the wallpaper, I can switch to another with Win+I and still have it change 5 minutes later.


Solution

  • Unless you modified your bash with a built-in sleep, you can kill the sleep command. The script will then proceed to the next command as if sleep terminated normally. The only tricky part is to identify the correct process to kill. Here I assume that there is only one randomwallpaper process running on your system:

    exec --no-startup-id randomwallpaper
    bindsym $mod+i exec --no-startup-id sh -c 'pkill -P $(pgrep -ox randomwallpaper) sleep'
    

    By the way; Your script could use some improvement. For instance, the variable file is unused and --randomize has no effect since you only supply one picture.

    #!/bin/bash
    shopt -s nullglob
    cd /home/user/Pictures/bgart
    while true; do
      i=$(shuf -en1 ./*.jpg)
      if [ -n "$i" ]; then
        feh --bg-scale "$i"
        wal -q -i "$i"
      fi
      sleep 300
    done