X-Git-Url: https://git.danix.xyz/?p=my-dotfiles.git;a=blobdiff_plain;f=bin%2Fchange_wallpaper.sh;fp=bin%2Fchange_wallpaper.sh;h=dd93b2313a75e6111ab58830e0fa093ceb52bc8d;hp=0000000000000000000000000000000000000000;hb=7abef725078fc52196b630c95f354fb58864c743;hpb=6be4fcd546b01b14b5f06306ab5b7c0391913bfa diff --git a/bin/change_wallpaper.sh b/bin/change_wallpaper.sh new file mode 100644 index 0000000..dd93b23 --- /dev/null +++ b/bin/change_wallpaper.sh @@ -0,0 +1,117 @@ +#! /bin/bash + +PID=$$ +PIDFILE=${PIDFILE:-/tmp/wallpaper.pid} +WAIT_CYCLE="5m" + +trap "rm -f $PIDFILE" SIGTERM + +WP_SETTER="/usr/bin/feh" +QARMA="/usr/bin/qarma" + +function set_wp() { + NEW_WP=$1 + $WP_SETTER --bg-fill $NEW_WP +} + +# set background function (requires input) +function wpapers() { + if [[ -f $1 ]]; then + # We have a single file as input + set_wp $1 + exit 0 + elif [[ -d $1 ]]; then + # directory as input + while true; do + BGIMG=$(find $1 -type f -print | shuf -n1) + set_wp ${BGIMG} + sleep $WAIT_CYCLE + done + fi +} + +function file_or_dir() { + if [[ -f /tmp/choice ]]; then + rm /tmp/choice + fi + + FOD=$($QARMA --list --text="single image or directory?" --hide-header "file" "directory" > /tmp/choice) + case $? in + 0 ) + file_chooser $(cat /tmp/choice) + ;; + 1 ) + $QARMA --info --title="Exiting!" --text="No wallpaper was changed." + ;; + -1 ) + $QARMA --error --title="Ooops!!" --text="Something unexpected happened." + ;; + esac +} + +function file_chooser() { + case $1 in + "file" ) + FILE="$($QARMA --file-selection --title='Choose your Wallpaper')" + case $? in + 0 ) + wpapers $FILE + ;; + 1 ) + $QARMA --info --title="Exiting!" --text="No wallpaper was changed." + ;; + -1 ) + $QARMA --error --title="Ooops!!" --text="Something unexpected happened." + esac + ;; + "directory" ) + FILE="$($QARMA --file-selection --directory --title='Choose your Wallpaper directory')" + case $? in + 0 ) + wpapers $FILE + ;; + 1 ) + $QARMA --info --title="Exiting!" --text="No wallpaper was changed." + ;; + -1 ) + $QARMA --error --title="Ooops!!" --text="Something unexpected happened." + esac + ;; + * ) + $QARMA --error --title="Ooops!!" --text="Something unexpected happened." + ;; + esac +} + +function run() { + if [[ $(basename $0) == "wallpaper.sh" ]]; then + # we were called as wallpaper.sh, so simple wallpaper setter without qarma interaction + wpapers $1 + elif [[ $(basename $0) == "change_wallpaper.sh" ]]; then + # we use qarma to set the wallpaper + $QARMA --question --title="Change Wallpaper" --text="Do you want to change the wallpaper?" + case $? in + 0 ) + file_or_dir + ;; + 1 ) + $QARMA --info --title="Exiting!" --text="No wallpaper was changed." + ;; + -1 ) + $QARMA --error --title="Ooops!!" --text="Something unexpected happened." + esac + fi +} + +if [[ -r $PIDFILE ]]; then + # PIDFILE exists, so I guess there's already an instance running + # let's kill it and run again + kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 + # should already be deleted by trap, but just to be sure + rm $PIDFILE +fi + +# create PIDFILE +echo $PID > $PIDFILE + +run