| 1 | #! /bin/bash |
| 2 | |
| 3 | PID=$$ |
| 4 | PIDFILE=${PIDFILE:-/tmp/wallpaper.pid} |
| 5 | WAIT_CYCLE="5m" |
| 6 | |
| 7 | trap "rm -f $PIDFILE" SIGTERM |
| 8 | |
| 9 | WP_SETTER="/usr/bin/feh" |
| 10 | QARMA="/usr/bin/qarma" |
| 11 | |
| 12 | function set_wp() { |
| 13 | NEW_WP=$1 |
| 14 | $WP_SETTER --bg-fill $NEW_WP |
| 15 | } |
| 16 | |
| 17 | # set background function (requires input) |
| 18 | function wpapers() { |
| 19 | if [[ -f $1 ]]; then |
| 20 | # We have a single file as input |
| 21 | set_wp $1 |
| 22 | exit 0 |
| 23 | elif [[ -d $1 ]]; then |
| 24 | # directory as input |
| 25 | while true; do |
| 26 | BGIMG=$(find $1 -type f -print | shuf -n1) |
| 27 | set_wp ${BGIMG} |
| 28 | sleep $WAIT_CYCLE |
| 29 | done |
| 30 | fi |
| 31 | } |
| 32 | |
| 33 | function file_or_dir() { |
| 34 | if [[ -f /tmp/choice ]]; then |
| 35 | rm /tmp/choice |
| 36 | fi |
| 37 | |
| 38 | FOD=$($QARMA --list --text="single image or directory?" --hide-header "file" "directory" > /tmp/choice) |
| 39 | case $? in |
| 40 | 0 ) |
| 41 | file_chooser $(cat /tmp/choice) |
| 42 | ;; |
| 43 | 1 ) |
| 44 | $QARMA --info --title="Exiting!" --text="No wallpaper was changed." |
| 45 | ;; |
| 46 | -1 ) |
| 47 | $QARMA --error --title="Ooops!!" --text="Something unexpected happened." |
| 48 | ;; |
| 49 | esac |
| 50 | } |
| 51 | |
| 52 | function file_chooser() { |
| 53 | case $1 in |
| 54 | "file" ) |
| 55 | FILE="$($QARMA --file-selection --title='Choose your Wallpaper')" |
| 56 | case $? in |
| 57 | 0 ) |
| 58 | wpapers $FILE |
| 59 | ;; |
| 60 | 1 ) |
| 61 | $QARMA --info --title="Exiting!" --text="No wallpaper was changed." |
| 62 | ;; |
| 63 | -1 ) |
| 64 | $QARMA --error --title="Ooops!!" --text="Something unexpected happened." |
| 65 | esac |
| 66 | ;; |
| 67 | "directory" ) |
| 68 | FILE="$($QARMA --file-selection --directory --title='Choose your Wallpaper directory')" |
| 69 | case $? in |
| 70 | 0 ) |
| 71 | wpapers $FILE |
| 72 | ;; |
| 73 | 1 ) |
| 74 | $QARMA --info --title="Exiting!" --text="No wallpaper was changed." |
| 75 | ;; |
| 76 | -1 ) |
| 77 | $QARMA --error --title="Ooops!!" --text="Something unexpected happened." |
| 78 | esac |
| 79 | ;; |
| 80 | * ) |
| 81 | $QARMA --error --title="Ooops!!" --text="Something unexpected happened." |
| 82 | ;; |
| 83 | esac |
| 84 | } |
| 85 | |
| 86 | function run() { |
| 87 | if [[ $(basename $0) == "wallpaper.sh" ]]; then |
| 88 | # we were called as wallpaper.sh, so simple wallpaper setter without qarma interaction |
| 89 | wpapers $1 |
| 90 | elif [[ $(basename $0) == "change_wallpaper.sh" ]]; then |
| 91 | # we use qarma to set the wallpaper |
| 92 | $QARMA --question --title="Change Wallpaper" --text="Do you want to change the wallpaper?" |
| 93 | case $? in |
| 94 | 0 ) |
| 95 | file_or_dir |
| 96 | ;; |
| 97 | 1 ) |
| 98 | $QARMA --info --title="Exiting!" --text="No wallpaper was changed." |
| 99 | ;; |
| 100 | -1 ) |
| 101 | $QARMA --error --title="Ooops!!" --text="Something unexpected happened." |
| 102 | esac |
| 103 | fi |
| 104 | } |
| 105 | |
| 106 | if [[ -r $PIDFILE ]]; then |
| 107 | # PIDFILE exists, so I guess there's already an instance running |
| 108 | # let's kill it and run again |
| 109 | kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 |
| 110 | # should already be deleted by trap, but just to be sure |
| 111 | rm $PIDFILE |
| 112 | fi |
| 113 | |
| 114 | # create PIDFILE |
| 115 | echo $PID > $PIDFILE |
| 116 | |
| 117 | run |