#!/bin/sh # speedwm-swal # wallpaper utility written for speedwm. # Licensed under the GNU GPLv3 free software license. # Default configuration, should be fine for most users. SWAL_DEFAULT_CONFIG() { SWAL_WALLPAPERDIR=$HOME/Wallpapers SWAL_OLDWALLPAPERDIR=$SWAL_WALLPAPERDIR case "$RUNLAUNCHER" in "") RUNLAUNCHER=dmenu ;; esac SWAL_OPT=xwallpaper SWAL_BINDIR=$BINDIR SWAL_EXPORTDIR=$HOME/.config/speedwm-de/swal # Config directory SWAL_FSIGNAL_RELOAD_XRESOURCES=18 # Fsignal signum when reloading SWAL_IMAGE_VIEWER="sxiv -to" # Image viewer to use () SWAL_ASK_WAL=false # Ask whether or not to use Pywal if possible (true/false) SWAL_RANDOMIZE=false # Select a random wallpaper on login } HAVE_GRID="true" if [ -e "$HOME/.config/speedwm-de/global/config" ]; then . $HOME/.config/speedwm-de/global/config else mkdir -p $HOME/.config/speedwm-de/global printf "HAVE_GRID=$HAVE_GRID # Whether or not to use the Grid argument. If you do not have the dmenu grid patch, set this to false. Doing so will disable grid." > $HOME/.config/speedwm-de/global/config fi if [ "$HAVE_GRID" = "true" ]; then GRIDNUM="1" fi if [ "$HAVE_GRID" = "true" ]; then GRIDARG="-g" fi SWAL_MKCONFIG() { mkdir -p $SWAL_EXPORTDIR touch $SWAL_EXPORTDIR/config SWAL_ASK_WAL_NEW=$SWAL_ASK_WAL SWAL_WALLPAPERDIR_NEW=$SWAL_WALLPAPERDIR SWAL_CHANGE_WALLPAPERDIR() { SWAL_WALLPAPERDIR_NEW=$(printf "Your wallpaper directory" | $RUNLAUNCHER $GRIDARG $GRIDNUM -p "Where are all your wallpapers stored?" -l 1) } SWAL_CHANGE_ASK_WAL() { command -v wal > /dev/null && SWAL_ASK_WAL_NEW=$(printf "Yes\nNo" | $RUNLAUNCHER $GRIDARG $GRIDNUM -p "Ask whether or not to use Pywal?" -l 2 | sed "s|No|false|g" | sed "s|Yes|true|g") } SWAL_CHANGE_IMAGE_VIEWER() { SWAL_IMAGE_VIEWER=$(printf "" | $RUNLAUNCHER $GRIDARG $GRIDNUM -p "What image viewer do you want to use for previews?" -l 1) } SWAL_CHANGE_RANDOMIZE() { SWAL_RANDOMIZE=$(printf "Yes\nNo" | $RUNLAUNCHER $GRIDARG $GRIDNUM -p "Pick a random wallpaper on login?" -l 2 | sed "s|No|false|g" | sed "s|Yes|true|g") } USER_OPT_ACTION=$(printf "Wallpaper directory\nPywal\nImage Viewer\nRandomize\n------\nExit" | $RUNLAUNCHER $GRIDARG $GRIDNUM -l 10 -p "What setting do you want to change?") case "$USER_OPT_ACTION" in "") $0 && exit 0 ;; "Wallpaper directory") SWAL_CHANGE_WALLPAPERDIR && CHANGED=true ;; "Pywal") SWAL_CHANGE_ASK_WAL && CHANGED=true ;; "Image Viewer") SWAL_CHANGE_IMAGE_VIEWER && CHANGED=true ;; "Randomize") SWAL_CHANGE_RANDOMIZE && CHANGED=true ;; "Exit") exit 0 ;; "------") $0 && exit 0 ;; esac case "$CHANGED" in "") $0 && exit 0 ;; esac case "$SWAL_ASK_WAL_NEW" in "true") ASKED=true ;; "false") ASKED=true ;; esac if [ "$ASKED" = "" ]; then $0 && exit 0 fi printf "SWAL_WALLPAPERDIR=$SWAL_WALLPAPERDIR_NEW" > $SWAL_EXPORTDIR/config printf "\nRUNLAUNCHER=$RUNLAUNCHER" >> $SWAL_EXPORTDIR/config printf "\nSWAL_OPT=$SWAL_OPT" >> $SWAL_EXPORTDIR/config printf "\nSWAL_BINDIR=$SWAL_BINDIR" >> $SWAL_EXPORTDIR/config printf "\nSWAL_EXPORTDIR=$SWAL_EXPORTDIR" >> $SWAL_EXPORTDIR/config printf "\nSWAL_IMAGE_VIEWER='$SWAL_IMAGE_VIEWER'" >> $SWAL_EXPORTDIR/config printf "\nSWAL_ASK_WAL=$SWAL_ASK_WAL_NEW" >> $SWAL_EXPORTDIR/config printf "\nSWAL_RANDOMIZE=$SWAL_RANDOMIZE" >> $SWAL_EXPORTDIR/config } # use a solid color SOLIDCOLOR() { ISCOL="$(printf "#000000\n#bbbbbb" | $RUNLAUNCHER -l 2 $GRIDARG $GRIDNUM -p "Enter a hex color:")" echo $ISCOL | grep -q "#" || exit 1 xwallpaper --clear xsetroot -solid "$ISCOL" rm -f $SWAL_EXPORTDIR/swal_wm xrdb -remove xsetroot -name "fsignal:31" test ${BINDIR}notify-send && notify-send " Solid color $ISCOL set as wallpaper." pgrep -x picom > /dev/null && pkill picom # Fix picom related bug exit 0 } BINDIR=$(cat /usr/share/speedwm-bindir) SWAL_OLDWALLPAPERDIR=$SWAL_WALLPAPERDIR # Source things SWAL_SOURCE() { ls "$SWAL_EXPORTDIR/config" || SWAL_MKCONFIG . "$SWAL_EXPORTDIR/config" || rm -f "$SWAL_EXPORTDIR/config" } SWAL_CHECK() { if [ "$SWAL_IMAGE_VIEWER" = "" ]; then ls "$SWAL_BINDIR/feh" && SWAL_IMAGE_VIEWER=feh ls "$SWAL_BINDIR/sxiv" && SWAL_IMAGE_VIEWER=sxiv fi ls "$SWAL_BINDIR/xwallpaper" || SWAL_DIE } SWAL_DIE() { SWAL_EXIT=1 && exit 1 } SWAL_HELP() { echo "swal is a minimal xwallpaper frontend for dmenu/rofi." echo "-w /path/to/wallpaper.png | Set the wallpaper specified." echo "-h | View this help screen." echo "-f X | fsignal signum to reload .Xresources (with reloadxresources, fsignal and .Xresources speedwm patches)" } SWAL_USAGE() { echo "To set a wallpaper, you must create a configuration. To do this, select 'Options' in the menu and set all the options to your liking." echo "The image viewer must be set to an image viewer that supports printing the filename to stdout. If you use sxiv, set the image viewer to 'sxiv -o'." echo "In the case of sxiv, This means you can press 'm' when previewing an image to select it and then set it." echo "Once you've selected your image using the method you prefer, select how you want to set it." echo "Then your pywal colors will reload, and the wallpaper will be set." echo "For more information, see speedwm-help -a." } SWAL_SELWALLPAPER() { case "$NEW1" in "") ls /tmp/swal_cpath || SWAL_SELWALLPAPER="$(printf "$(ls "$SWAL_WALLPAPERDIR")\n------\n..\nPreview\nPrevious\nSolid Color\nRandom\nFavorites\nAdd used wallpaper to favorites\nOptions\nHelp\nExit" | $RUNLAUNCHER $GRIDARG $GRIDNUM -l 100 -p "What wallpaper would you like to set?")" ;; "-w") SWAL_SELWALLPAPER="$(basename "$NEW2")" && touch /tmp/swal_cpath ;; "-h") SWAL_HELP | $RUNLAUNCHER $GRIDARG $GRIDNUM 1 -l 90 ;; "-f") SWAL_FSIGNAL_RELOAD_XRESOURCES="$NEW2" ;; esac case "$NEW3" in "-w") SWAL_SELWALLPAPER="$(basename "$NEW4")" && touch /tmp/swal_cpath ;; "-h") SWAL_HELP | $RUNLAUNCHER $GRIDARG $GRIDNUM -l 90 ;; "-f") SWAL_FSIGNAL_RELOAD_XRESOURCES="$NEW4" ;; esac case "$NEW5" in "-w") SWAL_SELWALLPAPER="$(basename "$NEW6")" && touch /tmp/swal_cpath ;; "-h") SWAL_HELP | $RUNLAUNCHER $GRIDARG $GRIDNUM -l 90 ;; "-f") SWAL_FSIGNAL_RELOAD_XRESOURCES="$NEW6" ;; esac } SWAL_SETTYPE() { case "$SWAL_SELWALLPAPER" in "Options") SWAL_MKCONFIG && $0 $@ && exit 0 ;; "") $0 && exit 0 ;; "Favorites") SWAL_SELWALLPAPER="$(printf "$(cat $SWAL_EXPORTDIR/favorites.filename)\n------\n..\nClear\n\nExit" | $RUNLAUNCHER $GRIDARG $GRIDNUM -l 100 -p "What wallpaper would you like to set?")" ; FAVORITES=true ;; "Add used wallpaper to favorites") echo "$(readlink "$SWAL_EXPORTDIR/CurrentWallpaper")" >> $SWAL_EXPORTDIR/favorites ; echo "$(basename "$(readlink "$SWAL_EXPORTDIR/CurrentWallpaper")")" >> $SWAL_EXPORTDIR/favorites.filename && $0 && exit 0 ;; "Preview") SWAL_PREVIEWIMG="$(basename $($SWAL_IMAGE_VIEWER "$SWAL_WALLPAPERDIR"/*))" ;; "..") cd "$SWAL_WALLPAPERDIR/.." && SWAL_WALLPAPERDIR="$(pwd)" && SWAL_SELWALLPAPER && SET && exit 0 ;; "Random") SWAL_FULL="$(find $SWAL_WALLPAPERDIR/* -type f | shuf -n 1)" ; SWAL_WALLPAPERDIR="$(dirname "$SWAL_FULL")" ; SWAL_SELWALLPAPER="$(basename "$SWAL_FULL")" ; SWAL_SELWAL=$SWAL_SELWALLPAPER ;; "Previous") SWAL_PREVWAL=$(readlink $SWAL_EXPORTDIR/CurrentWallpaper_prev) ; SWAL_WALLPAPERDIR="$(dirname "$SWAL_PREVWAL")" ; SWAL_SELWALLPAPER="$(basename "$SWAL_PREVWAL")" ;; "Exit") exit 0 ;; "Solid Color") SOLIDCOLOR ;; "Help") SWAL_USAGE | $RUNLAUNCHER $GRIDARG $GRIDNUM -l 50 -p "How to use" && $0 && exit 0 ;; "------") $0 && exit 0 ;; esac if [ "$FAVORITES" = "true" ]; then case "$SWAL_SELWALLPAPER" in "") $0 && exit 0 ;; "Clear") rm -f $SWAL_EXPORTDIR/favorites* ; $0 && exit 0 ;; "Exit") exit 0 ;; "..") $0 && exit 0 ;; "------") $0 && exit 0 ;; esac SWAL_WALLPAPERDIR="$(dirname "$(cat "$SWAL_EXPORTDIR/favorites" | grep "$SWAL_SELWALLPAPER")")" fi # If it's an actual image, use it as wallpaper if [ "$SWAL_SELWALLPAPER" = "Preview" ]; then if [ "$SWAL_PREVIEWIMG" = "" ]; then $0 && exit 0 else SWAL_SELWALLPAPER="$SWAL_PREVIEWIMG" fi fi # Check if it's a directory if [ -d "$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" ]; then cd "$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" && SWAL_WALLPAPERDIR="$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" && SWAL_SELWALLPAPER && SET && exit 0 fi FAIL_NOT_REAL() { test ${BINDIR}notify-send && notify-send "The selected wallpaper is not valid/does not exist." $0 ; exit 1 } ls "$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" || FAIL_NOT_REAL SWAL_TYPE=$(printf "Center\nTile\nZoom\nStretch\n------\nExit" | $RUNLAUNCHER -p "How do you want to set the wallpaper?" $GRIDARG $GRIDNUM -l 7) case "$SWAL_TYPE" in "Center") SWAL_TYPE=center ;; "Tile") SWAL_TYPE=tile ;; "Zoom") SWAL_TYPE=zoom ;; "Stretch") SWAL_TYPE=stretch ;; "Exit") exit 0 ;; "------") $0 && exit 0 ;; "") $0 && exit 0 ;; esac SWAL_TYPE="--$SWAL_TYPE" SWAL_WAL_STATUS="0" case "$SWAL_ASK_WAL" in "true") command -v wal > /dev/null && SWAL_USE_WAL=$(printf "Yes\nNo" | $RUNLAUNCHER $GRIDARG $GRIDNUM -l 90 -p "Do you want to use Pywal?" -l 2) && SWAL_WAL_STATUS="1" ;; "false") command -v wal > /dev/null && SWAL_USE_WAL="Yes" && SWAL_WAL_STATUS="1" ;; esac # Don't perform pywal actions if not installed/used if [ "$SWAL_WAL_STATUS" = "0" ]; then SWAL_USE_WAL="No" fi } SWAL_SETWALLPAPER() { $SWAL_OPT $SWAL_TYPE "$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" || FAIL_NOT_REAL } SWAL_EXPORT_SCRIPT() { if [ "$SWAL_RANDOMIZE" = "false" ]; then printf "#!/bin/sh\n$SWAL_OPT $SWAL_TYPE ""'$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER'""" > $SWAL_EXPORTDIR/swal_wm if [ -e "$SWAL_EXPORTDIR/CurrentWallpaper" ]; then rm -f $SWAL_EXPORTDIR/CurrentWallpaper_prev ; ln -s "$(readlink $SWAL_EXPORTDIR/CurrentWallpaper)" $SWAL_EXPORTDIR/CurrentWallpaper_prev fi rm -f $SWAL_EXPORTDIR/CurrentWallpaper ; ln -s "$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" $SWAL_EXPORTDIR/CurrentWallpaper else printf "#!/bin/sh" > $SWAL_EXPORTDIR/swal_wm printf "\nSWAL_WALLPAPERDIR=""'$SWAL_OLDWALLPAPERDIR'""" >> $SWAL_EXPORTDIR/swal_wm printf "\nSWAL_EXPORTDIR=""'$SWAL_EXPORTDIR'""" >> $SWAL_EXPORTDIR/swal_wm printf '\nSWAL_SELWALLPAPER="$(find "$SWAL_WALLPAPERDIR"/* -type f | shuf -n 1)"' >> $SWAL_EXPORTDIR/swal_wm printf "\n$SWAL_OPT $SWAL_TYPE " >> $SWAL_EXPORTDIR/swal_wm printf '"$SWAL_SELWALLPAPER"' >> $SWAL_EXPORTDIR/swal_wm printf "\nrm -f $SWAL_EXPORTDIR/CurrentWallpaper_prev" >> $SWAL_EXPORTDIR/swal_wm printf '\nln -s "$(readlink $SWAL_EXPORTDIR/CurrentWallpaper)" ' >> $SWAL_EXPORTDIR/swal_wm printf "$SWAL_EXPORTDIR/CurrentWallpaper_prev" >> $SWAL_EXPORTDIR/swal_wm printf "\nrm -f $SWAL_EXPORTDIR/CurrentWallpaper" >> $SWAL_EXPORTDIR/swal_wm printf '\nln -s "$SWAL_SELWALLPAPER" ' >> $SWAL_EXPORTDIR/swal_wm printf "$SWAL_EXPORTDIR/CurrentWallpaper" >> $SWAL_EXPORTDIR/swal_wm fi chmod +x $SWAL_EXPORTDIR/swal_wm } SWAL_EXPORT_SCRIPT_WAL() { if [ "$SWAL_RANDOMIZE" = "false" ]; then printf "#!/bin/sh\n$SWAL_OPT $SWAL_TYPE ""'$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER'""\nwal -qi ""'$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER'"" || rm -rf $HOME/.cache/wal ; xrdb -remove\nxrdb ~/.cache/wal/colors.Xresources\nxsetroot -name 'fsignal:$SWAL_FSIGNAL_RELOAD_XRESOURCES'" > $SWAL_EXPORTDIR/swal_wm else printf "#!/bin/sh" > $SWAL_EXPORTDIR/swal_wm printf "\nSWAL_WALLPAPERDIR=""'$SWAL_OLDWALLPAPERDIR'""" >> $SWAL_EXPORTDIR/swal_wm printf "\nSWAL_EXPORTDIR=""'$SWAL_EXPORTDIR'""" >> $SWAL_EXPORTDIR/swal_wm printf '\nSWAL_SELWALLPAPER="$(find "$SWAL_WALLPAPERDIR"/* -type f | shuf -n 1)"' >> $SWAL_EXPORTDIR/swal_wm printf "\n$SWAL_OPT $SWAL_TYPE " >> $SWAL_EXPORTDIR/swal_wm printf '"$SWAL_SELWALLPAPER"' >> $SWAL_EXPORTDIR/swal_wm printf '\nwal -nqi "$SWAL_SELWALLPAPER" || rm -rf $HOME/.cache/wal ; xrdb -remove\nxrdb $HOME/.cache/wal/colors.Xresources\nxrdb $HOME/.cache/wal/colors.Xresources' >> $SWAL_EXPORTDIR/swal_wm printf "\nxsetroot -name "fsignal:$SWAL_FSIGNAL_RELOAD_XRESOURCES"" >> $SWAL_EXPORTDIR/swal_wm printf "\nrm -f $SWAL_EXPORTDIR/CurrentWallpaper_prev" >> $SWAL_EXPORTDIR/swal_wm printf '\nln -s "$(readlink $SWAL_EXPORTDIR/CurrentWallpaper)" ' >> $SWAL_EXPORTDIR/swal_wm printf "$SWAL_EXPORTDIR/CurrentWallpaper_prev" >> $SWAL_EXPORTDIR/swal_wm printf "\nrm -f $SWAL_EXPORTDIR/CurrentWallpaper" >> $SWAL_EXPORTDIR/swal_wm printf '\nln -s "$SWAL_SELWALLPAPER" ' >> $SWAL_EXPORTDIR/swal_wm printf "$SWAL_EXPORTDIR/CurrentWallpaper" >> $SWAL_EXPORTDIR/swal_wm fi chmod +x $SWAL_EXPORTDIR/swal_wm rm -rf $HOME/.cache/wal # Delete previous colors wal -nqi "'$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER'" && xrdb ~/.cache/wal/colors.Xresources && xsetroot -name "fsignal:$SWAL_FSIGNAL_RELOAD_XRESOURCES" || xrdb -remove # Compatibility with my speedwm build if [ -e "$SWAL_EXPORTDIR/CurrentWallpaper" ]; then rm -f $SWAL_EXPORTDIR/CurrentWallpaper_prev ; ln -s "$(readlink $SWAL_EXPORTDIR/CurrentWallpaper)" $SWAL_EXPORTDIR/CurrentWallpaper_prev fi rm -f $SWAL_EXPORTDIR/CurrentWallpaper ; ln -s "$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" $SWAL_EXPORTDIR/CurrentWallpaper if [ -e "$HOME/.config/speedwm-de/speedwmrc" ]; then . $HOME/.config/speedwm-de/speedwmrc fi wal -nqi "$SWAL_WALLPAPERDIR/$SWAL_SELWALLPAPER" # Reload dmenu colors if supported if [ -e "/usr/bin/${RUNLAUNCHER}_run" ]; then grep "PYWAL" ${BINDIR}${RUNLAUNCHER}_run && printf "\n${RUNLAUNCHER}_run -r" >> $SWAL_EXPORTDIR/swal_wm ${RUNLAUNCHER}_run -r # For speedie.gq/dmenu fi speedwm_run -loadconfig # For speedie.gq/speedwm speedwm_run -rnoload # For speedie.gq/speedwm pgrep -x status && pkill status ; status & } SWAL_XRESOURCES() { xrdb -query | grep "swal" | sed "s|: |=|" | sed "s|swal.|swal_|" > /tmp/swal . /tmp/swal } SWAL_SENDNOTIFICATION() { if [ "$SWAL_SELWAL" = "" ]; then test ${BINDIR}notify-send && notify-send " $SWAL_SELWALLPAPER set as wallpaper in $(echo $SWAL_TYPE | sed "s|--||g") mode." else test ${BINDIR}notify-send && notify-send " $SWAL_SELWAL set as wallpaper in $(echo $SWAL_TYPE | sed "s|--||g") mode." fi exit 0 } # Arguments NEW1=$1 NEW2=$2 NEW3=$3 NEW4=$4 NEW5=$5 NEW6=$6 rm -f /tmp/swal_cpath SWAL_DEFAULT_CONFIG SWAL_SOURCE || SWAL_DIE SWAL_XRESOURCES SWAL_CHECK || SWAL_DIE SWAL_SELWALLPAPER || SWAL_DIE SET() { SWAL_SETTYPE || SWAL_DIE SWAL_SETWALLPAPER || SWAL_DIE # Pywal case "$SWAL_USE_WAL" in "Yes") SWAL_EXPORT_SCRIPT_WAL || SWAL_DIE ;; "No") SWAL_EXPORT_SCRIPT || SWAL_DIE ;; "") exit 0 ;; esac rm -rf $HOME/.local/share/swal_wm cp $SWAL_EXPORTDIR/swal_wm $HOME/.local/share/swal_wm chmod +x $HOME/.local/share/swal_wm SWAL_SENDNOTIFICATION } SET $0 && exit 0