sbgmenu 4.31 KB
Newer Older
Protesilaos Stavrou's avatar
Protesilaos Stavrou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#!/bin/bash

    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.

    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.

    # You should have received a copy of the GNU General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Simple Background Menu: sbgmenu is a dmenu interface for viewing
# images and/or selecting a new wallpaper from the user's "Pictures"
# directory.  This script is part of my dotfiles and is primarily
# intended for use in my custom BSPWM session:
# https://gitlab.com/protesilaos/dotfiles
#
# Dependencies on Debian Buster:
# 	sudo apt install suckless-tools
#
# For BSPWM sessions, you also need:
# 	sudo apt install feh
#
# Optional dependency (binary is fdfind):
# 	sudo apt install fd-find
#
# Assign this script to a key binding for your convenience.

# TODO is "Pictures" automatically localised?
# TODO consider adding notify-send actions

# pass custom colours to dmenu command
source "$HOME/.local/share/my_colours/active-tempus-theme.sh"

# get the system-wide custom font
source "$HOME/.local/share/my_custom_ui_font.sh"

# the customised `dmenu` command.  Accepts the prompt's text as the
# first argument and the height as a second.  If the latter is not
# defined, then dmenu displays options horizontally.
my_dmenu() {
	local prompt="$1"
	local height="$2"

	if [ -n "$2" ]; then
		dmenu -i -p "$prompt" -nb "$background" -nf "$foreground" \
		-sb "$color13" -sf "$background" -fn "$my_custom_ui_font" -l "$2"
	else
		dmenu -i -p "$prompt" -nb "$background" -nf "$foreground" \
		-sb "$color13" -sf "$background" -fn "$my_custom_ui_font"
	fi
}

# Here we ask the user whether they want to just view the selected
# image, or set it as the new wallpaper.
sbgmenu_mode="$(echo -e 'View images\nSet desktop wallpaper' | my_dmenu 'Choose the sbgmenu mode')"

find_pictures() {
	if [ -x /usr/bin/fdfind ]; then
		fdfind 'jpg|jpeg|png' "$HOME/Pictures/"
	else
		find "$HOME/Pictures" -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \)
	fi
}

# Get image files that end in .{jpg,jpeg,png} from ~/Pictures and
# present them as dmenu items.
my_pictures_menu() {
	find_pictures | my_dmenu "$1" "$2"
}

# Act depending on the mode of action.
sbgmenu_action() {
	# If the option is to view an image, use the default image viewer
	# (user defined).
	if [ "$sbgmenu_mode" == 'View images' ]; then
		sbgmenu_output="$(my_pictures_menu 'View selected picture' 10)"
		echo "Opening file"
		xdg-open "$sbgmenu_output"
	# If the decision is to set a wallpaper, prepare the relevant
	# command based on the running session.  Note that the bspwm session
	# is not standardised.  Using `feh` is my way of doing things.
	elif [ "$sbgmenu_mode" == 'Set desktop wallpaper' ]; then
		sbgmenu_output="$(my_pictures_menu 'Set new desktop wallpaper' 10)"

		case "$DESKTOP_SESSION" in
			bspwm)
				# NOTE there is no standard way for defining a wallpaper in
				# BSPWM.  Using a hidden file from the home dir is specific
				# to my custom desktop session.
				#
				# Copy the selected image to the home directory, as a hidden
				# file.
				#
				# Dependency on Debian Buster:
				# 	sudo apt install feh
				cp -f "$sbgmenu_output" "$HOME/.wallpaper.jpg"
				feh --bg-fill "$HOME/.wallpaper.jpg"
				;;
			gnome)
				gsettings set org.gnome.desktop.background picture-uri "file://$sbgmenu_output"
				;;
			mate)
				gsettings set org.mate.background picture-filename "$sbgmenu_output"
				;;
			xfce)
				# TODO any way to define a wallpaper in Xfce without
				# specifying screen, monitor, workspace?
				xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s "$sbgmenu_output"
				;;
			*)
				echo "ERROR. Unknown Desktop Session."
				echo "Cannot set wallpaper."
				echo "Aborting..."
				exit 1
				;;
		esac

		echo "Changing wallpaper to $sbgmenu_output"
	# If neither of the available modes is selected, abort sbgmenu
	else
		exit 1
	fi
}

# Run the script
sbgmenu_action