diff options
-rwxr-xr-x | random_wallpaper.sh | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/random_wallpaper.sh b/random_wallpaper.sh index cd5ffe7..26fd591 100755 --- a/random_wallpaper.sh +++ b/random_wallpaper.sh @@ -6,36 +6,59 @@ # Usage: # random_wallpaper.sh /path/to/wallpapers # -# Requirements: +# Requirements (X11): # feh # +# Requirements (Wayland): +# swaybg +# -# Check for feh -which feh 2>/dev/null 1>/dev/null -if [[ $? == 1 ]]; then - echo "Required program 'feh' not found. Please install and run again." - exit 1 -fi +x11_set() { + local wallpaper="${1}" + + # Check for feh + type -p feh 2>/dev/null 1>/dev/null + if [ $? -eq 1 ]; then + echo "Required program 'feh' not found. Please install and run again." + return 1 + fi + + # Set the wallpaper + feh --bg-fill "${wallpaper}" +} + +wayland_set() { + local wallpaper="${1}" + local pidfile=/dev/shm/swaybg-$(whoami).pid + printf 'Setting wallpaper to %s\n' "${wallpaper}" + + swaybg -o '*' -i "${wallpaper}" -m fill 2>/dev/null 1>/dev/null & + echo $! > "${pidfile}.new" + sleep .5 + + if [ -f "${pidfile}" ] && [ -d /proc/$(cat ${pidfile}) ]; then + kill $(cat "${pidfile}") + fi + mv "${pidfile}.new" "${pidfile}" +} # Make sure a wallpaper path was specified -if [[ -z $1 ]]; then +if [ -z ${1} ]; then echo "Please specify a wallpaper path." exit 1 fi -# This needs to be set if scheduling in crontab since crontab don't have -# displays -# export DISPLAY=:0 - -search_path=$1 - -# Randomly select a wallpaper to set -picture=$(find ${search_path} -type f \ - -name '*.jpg' \ - -o -name '*.JPG' \ - -o -name '*.png' \ - -o -name '*.PNG' \ +search_path=${@} +randpic=$(find ${search_path} -type f \ + -iname '*.jpg' \ + -o -iname '*.png' \ | sort -R | head -n 1) -# Set the wallpaper -feh --bg-fill "${picture}" +if [ -n "${WAYLAND_DISPLAY:-}" ]; then + wayland_set "${randpic}" +elif [ -n "${DISPLAY:-}" ]; then + x11_set "${randpic}" +else + printf 'ERROR: Could not detect display manager\n' + exit 1 +fi |