aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2019-10-14 14:03:55 +0200
committerPatrick Spek <p.spek@tyil.nl>2019-10-14 14:03:55 +0200
commite22e5ed1ff7cc3c8746377752fd595d5dfd4aeb3 (patch)
treecbbea135482483296d97e671243668c2da26d94a
parenta8487d699bf8dee5a792d0207bd64b358488f2ed (diff)
Update chwp to make papes that work with multiple monitors of differing sizes
-rwxr-xr-x.local/bin/chwp99
1 files changed, 71 insertions, 28 deletions
diff --git a/.local/bin/chwp b/.local/bin/chwp
index 071bf3a..32e4707 100755
--- a/.local/bin/chwp
+++ b/.local/bin/chwp
@@ -1,30 +1,73 @@
#! /usr/bin/env sh
-if [ -f "$HOME/.local/etc/x/chwp" ]
-then
- . "$HOME/.local/etc/x/chwp"
-fi
-
-directory=${CHWP_BASEDIR:-$HOME/pictures/wallpapers}
-size=${CHWP_SIZE:-1920x1080}
-timeout=${1:-0}
-walldir=$directory/$size
-
-if [ ! -d "$walldir" ]
-then
- printf "%s\n" "No such directory: $walldir" >&2
-fi
-
-# If no timeout was set, just change it once
-if [ "$timeout" -eq 0 ]
-then
- find "${walldir}" -type f -print0 | shuf -n1 -z | xargs -0 feh --bg-fill
- exit 0
-fi
-
-# Otherwise, change it every so often
-while :
-do
- find "${walldir}" -type f -print0 | shuf -n1 -z | xargs -0 feh --bg-fill
- sleep "${timeout}"
-done
+main()
+{
+ # Load custom configuration if available
+ if [ -f "$HOME/.local/etc/x/chwp" ]
+ then
+ . "$HOME/.local/etc/x/chwp"
+ fi
+
+ directory=${CHWP_BASEDIR:-$HOME/pictures/wallpapers}
+ size=${CHWP_SIZE:-1920x1080}
+ timeout=${1:-0}
+ walldir=$directory/$size
+ screencount=${CHWP_SCREENCOUNT:-$(xrandr -q | grep -F ' connected ' | wc -l)}
+
+ # Fail early if there's no wallpaper directory
+ if [ ! -d "$walldir" ]
+ then
+ printf "No such directory: %s\n" "$walldir" >&2
+ exit 1
+ fi
+
+ CACHEFILE=$(mktemp)
+
+ find "${walldir}" -type f > "$CACHEFILE"
+
+ # If no timeout was set, just change it once
+ if [ "$timeout" -eq 0 ]
+ then
+ set_papes
+ exit 0
+ fi
+
+ # Otherwise, change it every so often
+ while :
+ do
+ set_papes
+ sleep "${timeout}"
+ done
+}
+
+set_papes()
+{
+ pape=$(make_pape)
+
+ feh --bg-fill "$pape"
+ rm -f -- "$pape"
+}
+
+make_pape()
+{
+ output=$(mktemp)
+ imagelist=$(mktemp)
+
+ for resolution in $(xrandr -q | grep -F ' connected ' | grep -Eo '\b[[:digit:]]+x[[:digit:]]+')
+ do
+ printf '"%s"\n' "$(get_pape "$resolution")" >> "$imagelist"
+ done
+
+ eval convert +append $(cat $imagelist) "$output"
+
+ printf "%s" "$output"
+
+ rm -f -- "$imagelist"
+}
+
+get_pape()
+{
+ printf "%s\n" "$(find "$directory/$1" -type f | shuf | head -n 1)"
+}
+
+main "$@"