wallpaper-spmenu/wallpaper-spmenu
2023-07-24 19:19:40 +02:00

194 lines
5.5 KiB
Bash
Executable file

#!/bin/sh
# wallpaper-spmenu
# Wallpaper picker for X11 and Wayland using spmenu.
# Licensed under the GNU General Public License version 3.0
VERSION="${VERSION:-0.1}"
PROTOCOL="${PROTOCOL:-1}" # Protocol to use (0: X11, 1: Wayland/wlroots)
LOAD_CONFIG="${LOAD_CONFIG:-true}" # Load config (0/1)
WALLPAPER_CONFIG_FILE="${WALLPAPER_CONFIG_FILE:-$HOME/.config/wallpaper-spmenu/wallpaperrc}"
WALLPAPER_CONFIG_DIR="$(dirname "$WALLPAPER_CONFIG_FILE")"
WALLPAPER_DIR="${WALLPAPER_DIR:-$HOME/Wallpapers}" # Default wallpaper directory
DEFAULT_FEATURE="${DEFAULT_FEATURE:-pw}"
DEFAULT_MODE="${DEFAULT_MODE:-fill}"
ASK_MODE="${ASK_MODE:-false}"
PREVIEW_IMAGE="${PREVIEW_IMAGE:-true}"
COLOR="${COLOR:-always}"
die() { printf "%s\n" "$*" && exit 1; }
remove_arg() { args="$(printf "%s\n" "$args" | sed "s|$1||g")"; }
help() {
cat << EOF
usage: $0 [-sw] [-pw]
EOF
exit ${i:-0}
}
read_args() {
feature="${DEFAULT_FEATURE}"
args="$(printf "%s\n" "$@")"
argc="$(printf "%s\n" "$@" | grep -c "")"
while true; do
i=$((i+1))
arg="$(printf "%s\n" "$args" | sed "${i}q;d")"
narg="$(printf "%s\n" "$args" | sed "$((i+1))q;d")"
case "$arg" in
-h|--help) remove_arg "$arg" && help 0 ;;
"") : ;;
-sw|--set-wallpaper) feature="sw" && break ;;
-pw|--pick-wallpaper) feature="pw" && break ;;
*) help 1 ;;
esac
[ "$argc" = "$i" ] && break
done
args="$(printf "%s\n" "$*")"
}
check_deps() {
if [ -n "$WAYLAND_DISPLAY" ]; then
PROTOCOL="1"
elif [ -n "$DISPLAY" ]; then
PROTOCOL="0"
else
die "no valid display server running, is \$DISPLAY or \$WAYLAND_DISPLAY set?"
fi
[ ! -x "$(command -v xwallpaper)" ] && [ "$PROTOCOL" = "0" ] && die "xwallpaper not found"
[ ! -x "$(command -v swaybg)" ] && [ "$PROTOCOL" = "1" ] && die "swaybg not found"
[ ! -x "$(command -v spmenu)" ] && die "spmenu not found"
return 0
}
write_config() {
mkdir -p "$WALLPAPER_CONFIG_DIR" || die "failed to create directory $WALLPAPER_CONFIG_DIR"
cat << EOF > "$WALLPAPER_CONFIG_FILE"
# wallpaper-spmenu ${VERSION:-0.1}
# This is the default config file for wallpaper-spmenu.
# You can use any valid shell syntax here.
# See https://spmenu.speedie.site wiki for more information.
PROTOCOL="${PROTOCOL}" # Default protocol to use (0: X11, 1: Wayland w/ wlroots)
WALLPAPER_DIR="${WALLPAPER_DIR}" # Default wallpaper directory, can be set on runtime
COLOR="${COLOR}" # Color the entries (always/never)
DEFAULT_MODE="${DEFAULT_MODE}" # Default mode (stretch/fit/fill/center/tile)
ASK_MODE="${ASK_MODE}" # Ask how the wallpaper should be set (true/false)
DEFAULT_FEATURE="${DEFAULT_FEATURE}" # Default feature (sw/pw)
PREVIEW_IMAGE="${PREVIEW_IMAGE}" # Enable image previews (true/false)
EOF
}
read_config() {
[ "$LOAD_CONFIG" = "false" ] && return
[ ! -f "$WALLPAPER_CONFIG_FILE" ] && write_config
[ ! -f "$WALLPAPER_CONFIG_FILE" ] && die "failed to write config file"
. "$WALLPAPER_CONFIG_FILE"
}
write_script() {
rm -f "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh"
command -v pre_write_script > /dev/null && pre_write_script
if [ "$PROTOCOL" = "0" ]; then
cat << EOF > "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh"
#!/bin/sh
xwallpaper --stretch "${selwal}"
EOF
else
cat << EOF > "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh"
#!/bin/sh
swaybg -i "${selwal}" -m fill &
EOF
fi
command -v post_write_script > /dev/null && post_write_script "${selwal}"
chmod +x "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh"
}
set_wallpaper() {
[ ! -f "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh" ] && return 1
"$WALLPAPER_CONFIG_DIR/set_wallpaper.sh" > /dev/null
}
pick_wallpaper() {
pick_wallpaper_file
[ -z "$selwal" ] && exit 1 # No file was picked
[ -e "$WALLPAPER_CONFIG_DIR/currentwallpaper" ] && \
mv "$WALLPAPER_CONFIG_DIR/currentwallpaper" "$WALLPAPER_CONFIG_DIR/prevwallpaper"
ln -s "$selwal" "$WALLPAPER_CONFIG_DIR/currentwallpaper"
write_script
set_wallpaper
}
pick_wallpaper_file() {
[ ! -d "$WALLPAPER_DIR" ] && WALLPAPER_DIR="${HOME}"
cd "$WALLPAPER_DIR" || exit 1
# very much taken from spmenu_run
listing_files() {
command -v pre_list_func > /dev/null && pre_list_func
ls --color="$COLOR" > /tmp/spmenu_ls_list
printf "..\n" >> /tmp/spmenu_ls_list
while read -r l; do
command -v line_func > /dev/null && line_fnc "$(pwd)/$l"
[ "$PREVIEW_IMAGE" != "false" ] && printf "img://%s\t" "$(pwd)/$l" | sed -e 's/\x1b\[[0-9;]*m//g'
printf "%s\n" "$l"
done < "/tmp/spmenu_ls_list"; rm -f /tmp/spmenu_ls_list
command -v post_list_func > /dev/null && post_list_func
}
command -v pre_func > /dev/null && pre_func
listing_files | spmenu -l 40 -g 1 -p "$(pwd)" | sed -e 's/\x1b\[[0-9;]*m//g' > /tmp/spmenu_out
while read -r dir; do
case "$dir" in
*)
if [ -d "$dir" ]; then
WALLPAPER_DIR="$(pwd)/$dir"
command -v dir_func > /dev/null && dir_func "$dir"
pick_wallpaper_file
elif [ -f "$dir" ]; then
selwal="$(pwd)/$dir"
else
return 1 # file not found
fi
;;
esac
break # TODO: multisel
done < /tmp/spmenu_out; rm -f /tmp/spmenu_out
}
main() {
read_args "$@"
read_config "$@"
check_deps
case "$feature" in
"sw") set_wallpaper ;;
"pw") pick_wallpaper ;;
esac
return $?
}
main "$@"