From 1368cff428c3eb2112afeb53f4756bfdddaff363 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Wed, 5 Jun 2024 11:46:17 +0530 Subject: home/wayland/sway: add support for wallpapers --- home/wayland/pkgs/wayland-scripts/src/bin/cwall | 106 ++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100755 home/wayland/pkgs/wayland-scripts/src/bin/cwall (limited to 'home/wayland/pkgs/wayland-scripts/src/bin') diff --git a/home/wayland/pkgs/wayland-scripts/src/bin/cwall b/home/wayland/pkgs/wayland-scripts/src/bin/cwall new file mode 100755 index 0000000..6268390 --- /dev/null +++ b/home/wayland/pkgs/wayland-scripts/src/bin/cwall @@ -0,0 +1,106 @@ +#!/usr/bin/env bash + +bgroot="$XDG_DATA_HOME/wayland" +bgdesk="$bgroot/desktop" +bglock="$bgroot/lockscreen" +walldir="$HOME/pix/wall" + +die() +{ + command -v notify-send > /dev/null && + notify-send " cwall" "$1" + + printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2 + exit "${2:-1}" +} + +usage() +{ + echo 'Usage: cwall [wallpaper.img|directory]' +} + +mimetype() +{ + file --brief --dereference --mime-type "$1" +} + +randimg() +{ + # usage: randbg dir + : "${1:?}" + maxattempts=10 + attempt=0 + + img= + while :; do + img="$(find "$1" \ + -iname "*.avif" \ + -o -iname "*.heic" \ + -o -iname "*.heif" \ + -o -iname "*.jpeg" \ + -o -iname "*.jpg" \ + -o -iname "*.jxl" \ + -o -iname "*.png" \ + -o -iname "*.webp" | shuf -n1)" + + [ -z "$img" ] && + return 1 + + # make sure the file is an actual image + case "$(mimetype "$img")" in + "image/"*) + echo "$img" + break + ;; + *) + attempt=$((attempt + 1)) + [ "$attempt" -gt "$maxattempts" ] && + return 1 + esac + done +} + +updatewall() +{ + : "${1:?}" + + rm -f "$bgdesk" "$bglock" + cp "$1" "$bgdesk" + swaymsg "output * background '${bgdesk}' fill" + ffmpeg -loglevel quiet -i "$bgdesk" -vf "boxblur=50" -f mjpeg "$bglock" +} + +######## +# MAIN # +######## + +if [ "$#" -eq 0 ]; then + input="$walldir" +elif [ "$1" = "-h" ]; then + usage && exit 0 +else + input="$1" +fi + +if [ ! -r "$input" ]; then + die "can't read $input" +fi +if [ ! -d "$bgroot" ]; then + mkdir -p "$bgroot" +fi + +case "$(mimetype "$input")" in +"image/"*) + updatewall "$input" + ;; +"inode/directory") + if ! img=$(randimg "$input"); then + die "no image file in $input" + fi + + updatewall "$img" + ;; +*) + die "invalid input $input" + ;; +esac -- cgit v1.2.3