169 lines
4.8 KiB
Bash
Executable file
169 lines
4.8 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/spmenu/wallpaper/config}" # Use same directory as spmenu
|
|
WALLPAPER_CONFIG_DIR="$(dirname "$WALLPAPER_CONFIG_FILE")"
|
|
WALLPAPER_DIR="${WALLPAPER_DIR:-~/Wallpapers}" # Default wallpaper directory
|
|
DEFAULT_FEATURE="${DEFAULT_FEATURE:-pw}"
|
|
DEFAULT_MODE="${DEFAULT_MODE:-fill}"
|
|
ASK_MODE="${ASK_MODE:-false}"
|
|
|
|
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" "$@" | wc -l)"
|
|
|
|
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 "$DISPLAY" ]; then
|
|
PROTOCOL="0"
|
|
elif [ -n "$WAYLAND_DISPLAY" ]; then
|
|
PROTOCOL="1"
|
|
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
|
|
|
|
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)
|
|
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"
|
|
cat << EOF > "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh"
|
|
EOF
|
|
chmod +x "$WALLPAPER_CONFIG_DIR/set_wallpaper.sh"
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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=always > /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"
|
|
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
|
|
|
|
printf "%s\n" "$selwal"
|
|
|
|
return $?
|
|
}
|
|
|
|
main "$@"
|