summaryrefslogtreecommitdiff
path: root/random_wallpaper.sh
blob: 26fd5915783843d9cc32328c24ad429b6c8bd043 (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # Recursively searches the specified directory (the first script argument),
    4 # randomly selects one image, and set that image as the wallpaper.
    5 #
    6 # Usage:
    7 #   random_wallpaper.sh /path/to/wallpapers
    8 #
    9 # Requirements (X11):
   10 #   feh
   11 #
   12 # Requirements (Wayland):
   13 #   swaybg
   14 #
   15 
   16 x11_set() {
   17   local wallpaper="${1}"
   18 
   19   # Check for feh
   20   type -p feh 2>/dev/null 1>/dev/null
   21   if [ $? -eq 1 ]; then
   22     echo "Required program 'feh' not found. Please install and run again."
   23     return 1
   24   fi
   25 
   26   # Set the wallpaper
   27   feh --bg-fill "${wallpaper}"
   28 }
   29 
   30 wayland_set() {
   31   local wallpaper="${1}"
   32   local pidfile=/dev/shm/swaybg-$(whoami).pid
   33   printf 'Setting wallpaper to %s\n' "${wallpaper}"
   34 
   35   swaybg -o '*' -i "${wallpaper}" -m fill 2>/dev/null 1>/dev/null &
   36   echo $! > "${pidfile}.new"
   37   sleep .5
   38 
   39   if [ -f "${pidfile}" ] && [ -d /proc/$(cat ${pidfile}) ]; then
   40     kill $(cat "${pidfile}")
   41   fi
   42   mv "${pidfile}.new" "${pidfile}"
   43 }
   44 
   45 # Make sure a wallpaper path was specified
   46 if [ -z ${1} ]; then
   47   echo "Please specify a wallpaper path."
   48   exit 1
   49 fi
   50 
   51 search_path=${@}
   52 randpic=$(find ${search_path} -type f \
   53             -iname '*.jpg' \
   54             -o -iname '*.png' \
   55             | sort -R | head -n 1)
   56 
   57 if [ -n "${WAYLAND_DISPLAY:-}" ]; then
   58   wayland_set "${randpic}"
   59 elif [ -n "${DISPLAY:-}" ]; then
   60   x11_set "${randpic}"
   61 else
   62   printf 'ERROR: Could not detect display manager\n'
   63   exit 1
   64 fi

Generated by cgit