From 67dbc5473eef4453d862636324ff1b26b1bc3d97 Mon Sep 17 00:00:00 2001 From: Fawn Date: Fri, 24 Jan 2025 17:13:01 -0700 Subject: [PATCH 1/3] Make swww_randomize.sh and swww_randomize_multi.sh scripts more POSIX-friendly --- example_scripts/swww_randomize.sh | 42 ++++++------ example_scripts/swww_randomize_multi.sh | 91 +++++++------------------ 2 files changed, 45 insertions(+), 88 deletions(-) diff --git a/example_scripts/swww_randomize.sh b/example_scripts/swww_randomize.sh index 300d3e01..df4a7689 100755 --- a/example_scripts/swww_randomize.sh +++ b/example_scripts/swww_randomize.sh @@ -1,32 +1,28 @@ -#!/bin/bash +#!/bin/sh +# Changes the wallpaper to a randomly chosen image in a given directory +# at a set interval. -# This script will randomly go through the files of a directory, setting it -# up as the wallpaper at regular intervals -# -# NOTE: this script is in bash (not posix shell), because the RANDOM variable -# we use is not defined in posix +DEFAULT_INTERVAL=300 # In seconds -if [[ $# -lt 1 ]] || [[ ! -d $1 ]]; then - echo "Usage: - $0 " +if [ $# -lt 1 ] || [ ! -d "$1" ]; then + printf "Usage:\n\t\e[1m%s\e[0m \e[4mDIRECTORY\e[0m [\e[4mINTERVAL\e[0m]\n" "$0" + printf "\tChanges the wallpaper to a randomly chosen image in DIRECTORY every\n\tINTERVAL seconds (or every %d seconds if unspecified)." "$DEFAULT_INTERVAL" exit 1 fi -# Edit below to control the images transition -export SWWW_TRANSITION_FPS=60 -export SWWW_TRANSITION_STEP=2 - -# This controls (in seconds) when to switch to the next image -INTERVAL=300 +# See swww-img(1) +RESIZE_TYPE="fit" +export SWWW_TRANSITION_FPS="${SWWW_TRANSITION_FPS:-60}" +export SWWW_TRANSITION_STEP="${SWWW_TRANSITION_STEP:-2}" while true; do find "$1" -type f \ - | while read -r img; do - echo "$((RANDOM % 1000)):$img" - done \ - | sort -n | cut -d':' -f2- \ - | while read -r img; do - swww img "$img" - sleep $INTERVAL - done + | while read -r img; do + echo "$(" - exit 1 +if [ $# -lt 1 ] || [ ! -d "$1" ]; then + printf "Usage:\n\t\e[1m%s\e[0m \e[4mDIRECTORY\e[0m [\e[4mINTERVAL\e[0m]\n" "$0" + printf "\tChanges the wallpaper to a randomly chosen image in DIRECTORY every\n\tINTERVAL seconds (or every %d seconds if unspecified)." "$DEFAULT_INTERVAL" + exit 1 fi -# Make sure only 1 instance of swww_randomize -PIDFILE=~/.local/state/swww-randomize-pidfile.txt -if [ -e "${PIDFILE}" ]; then - OLD_PID="$(<${PIDFILE})" - if [ "X" != "X${OLD_PID}" -a -e "/proc/${OLD_PID}" ]; then - OLD_NAME="$( ${PIDFILE} - -# Edit below to control the images transition -export SWWW_TRANSITION_FPS=60 -export SWWW_TRANSITION_STEP=2 - -# This controls (in seconds) when to switch to the next image -INTERVAL=300 - -# Possible values: -# - no: Do not resize the image -# - crop: Resize the image to fill the whole screen, cropping out parts that don't fit -# - fit: Resize the image to fit inside the screen, preserving the original aspect ratio +# See swww-img(1) RESIZE_TYPE="fit" - -DISPLAY_LIST=$(swww query | grep -Po "^[^:]+") +export SWWW_TRANSITION_FPS="${SWWW_TRANSITION_FPS:-60}" +export SWWW_TRANSITION_STEP="${SWWW_TRANSITION_STEP:-2}" while true; do - find "$1" -type f \ - | while read -r img; do - echo "$RANDOM:$img" - done \ - | sort -n | cut -d':' -f2- \ - | tee ~/.local/state/swww-randomize-list.txt \ - | while read -r img; do - # Set a different image for each display - for disp in $DISPLAY_LIST; do - # if there is no image try to get one - if [ "X" = "X${img}" ]; then - if read -r img; then - true - else # if there are no more images, refresh the list - break 2 - fi - fi - swww img --resize=$RESIZE_TYPE --outputs $disp $img - # make sure each image is only used once - img="" - done - sleep $INTERVAL - done + find "$1" -type f \ + | while read -r img; do + echo "$( Date: Fri, 24 Jan 2025 17:26:47 -0700 Subject: [PATCH 2/3] Restore deleted comment --- example_scripts/swww_randomize_multi.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_scripts/swww_randomize_multi.sh b/example_scripts/swww_randomize_multi.sh index 8598b22e..aae3feba 100644 --- a/example_scripts/swww_randomize_multi.sh +++ b/example_scripts/swww_randomize_multi.sh @@ -27,7 +27,7 @@ while true; do # and pick again if no more unused images are remaining [ -z "$img" ] && read -r img || break 2 swww img --resize "$RESIZE_TYPE" --outputs "$d" "$img" - unset -v $img + unset -v $img # Each image should only be used once per loop done sleep "${2:-$DEFAULT_INTERVAL}" done From 48aff858ab5c8e8d81f9164b194e20d117960bb9 Mon Sep 17 00:00:00 2001 From: Fawn Sannar Date: Fri, 14 Feb 2025 00:31:58 -0700 Subject: [PATCH 3/3] Fix misuse of read(1) and unset(1) The use of read(1) in the innermost loop wasn't properly capturing the piped-in values, and would hang forever waiting for input. The syntax used in the original swww_randomize_multi.sh does not have this problem. I don't really understand how the other syntax does not also have this problem, but I am reverting it so that it at least works. I removed the $ in front of the $img variable when unsetting it, since including it was expanding the variable. Oops! --- example_scripts/swww_randomize_multi.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example_scripts/swww_randomize_multi.sh b/example_scripts/swww_randomize_multi.sh index aae3feba..8aeeb375 100644 --- a/example_scripts/swww_randomize_multi.sh +++ b/example_scripts/swww_randomize_multi.sh @@ -25,9 +25,9 @@ while true; do for d in $(swww query | grep -Po "^[^:]+"); do # see swww-query(1) # Get next random image for this display, or re-shuffle images # and pick again if no more unused images are remaining - [ -z "$img" ] && read -r img || break 2 + [ -z "$img" ] && if read -r img; then true; else break 2; fi swww img --resize "$RESIZE_TYPE" --outputs "$d" "$img" - unset -v $img # Each image should only be used once per loop + unset -v img # Each image should only be used once per loop done sleep "${2:-$DEFAULT_INTERVAL}" done