Skip to content

Commit dcb5668

Browse files
authored
Merge pull request #391 from rubiefawn/updated_example_scripts
Make example scripts more portable
2 parents d3a086c + 48aff85 commit dcb5668

File tree

2 files changed

+45
-88
lines changed

2 files changed

+45
-88
lines changed

example_scripts/swww_randomize.sh

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
#!/bin/bash
1+
#!/bin/sh
2+
# Changes the wallpaper to a randomly chosen image in a given directory
3+
# at a set interval.
24

3-
# This script will randomly go through the files of a directory, setting it
4-
# up as the wallpaper at regular intervals
5-
#
6-
# NOTE: this script is in bash (not posix shell), because the RANDOM variable
7-
# we use is not defined in posix
5+
DEFAULT_INTERVAL=300 # In seconds
86

9-
if [[ $# -lt 1 ]] || [[ ! -d $1 ]]; then
10-
echo "Usage:
11-
$0 <dir containing images>"
7+
if [ $# -lt 1 ] || [ ! -d "$1" ]; then
8+
printf "Usage:\n\t\e[1m%s\e[0m \e[4mDIRECTORY\e[0m [\e[4mINTERVAL\e[0m]\n" "$0"
9+
printf "\tChanges the wallpaper to a randomly chosen image in DIRECTORY every\n\tINTERVAL seconds (or every %d seconds if unspecified)." "$DEFAULT_INTERVAL"
1210
exit 1
1311
fi
1412

15-
# Edit below to control the images transition
16-
export SWWW_TRANSITION_FPS=60
17-
export SWWW_TRANSITION_STEP=2
18-
19-
# This controls (in seconds) when to switch to the next image
20-
INTERVAL=300
13+
# See swww-img(1)
14+
RESIZE_TYPE="fit"
15+
export SWWW_TRANSITION_FPS="${SWWW_TRANSITION_FPS:-60}"
16+
export SWWW_TRANSITION_STEP="${SWWW_TRANSITION_STEP:-2}"
2117

2218
while true; do
2319
find "$1" -type f \
24-
| while read -r img; do
25-
echo "$((RANDOM % 1000)):$img"
26-
done \
27-
| sort -n | cut -d':' -f2- \
28-
| while read -r img; do
29-
swww img "$img"
30-
sleep $INTERVAL
31-
done
20+
| while read -r img; do
21+
echo "$(</dev/urandom tr -dc a-zA-Z0-9 | head -c 8):$img"
22+
done \
23+
| sort -n | cut -d':' -f2- \
24+
| while read -r img; do
25+
swww img --resize="$RESIZE_TYPE" "$img"
26+
sleep "${2:-$DEFAULT_INTERVAL}"
27+
done
3228
done
Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,34 @@
1-
#!/bin/bash
1+
#!/bin/sh
2+
# For each display, changes the wallpaper to a randomly chosen image in
3+
# a given directory at a set interval.
24

3-
# This script will randomly go through the files of a directory,
4-
# setting a different random wallpaper for each display
5-
# at regular intervals
6-
#
7-
# NOTE: this script is in bash (not posix shell), because the RANDOM variable
8-
# we use is not defined in posix
5+
DEFAULT_INTERVAL=300 # In seconds
96

10-
if [[ $# -lt 1 ]] || [[ ! -d $1 ]]; then
11-
echo "Usage:
12-
$0 <dir containing images>"
13-
exit 1
7+
if [ $# -lt 1 ] || [ ! -d "$1" ]; then
8+
printf "Usage:\n\t\e[1m%s\e[0m \e[4mDIRECTORY\e[0m [\e[4mINTERVAL\e[0m]\n" "$0"
9+
printf "\tChanges the wallpaper to a randomly chosen image in DIRECTORY every\n\tINTERVAL seconds (or every %d seconds if unspecified)." "$DEFAULT_INTERVAL"
10+
exit 1
1411
fi
1512

16-
# Make sure only 1 instance of swww_randomize
17-
PIDFILE=~/.local/state/swww-randomize-pidfile.txt
18-
if [ -e "${PIDFILE}" ]; then
19-
OLD_PID="$(<${PIDFILE})"
20-
if [ "X" != "X${OLD_PID}" -a -e "/proc/${OLD_PID}" ]; then
21-
OLD_NAME="$(</proc/${OLD_PID}/comm)"
22-
THIS_NAME="$(</proc/${BASHPID}/comm)"
23-
if [ "${OLD_NAME}" = "${THIS_NAME}" ]; then
24-
echo "old randomize process ${OLD_PID} is still running"
25-
exit 1
26-
else
27-
echo "process with same ID as old randomize is running: \"${OLD_NAME}\"@${OLD_PID}"
28-
echo "Replacing old process ID"
29-
fi
30-
fi
31-
fi
32-
echo "${BASHPID}" > ${PIDFILE}
33-
34-
# Edit below to control the images transition
35-
export SWWW_TRANSITION_FPS=60
36-
export SWWW_TRANSITION_STEP=2
37-
38-
# This controls (in seconds) when to switch to the next image
39-
INTERVAL=300
40-
41-
# Possible values:
42-
# - no: Do not resize the image
43-
# - crop: Resize the image to fill the whole screen, cropping out parts that don't fit
44-
# - fit: Resize the image to fit inside the screen, preserving the original aspect ratio
13+
# See swww-img(1)
4514
RESIZE_TYPE="fit"
46-
47-
DISPLAY_LIST=$(swww query | grep -Po "^[^:]+")
15+
export SWWW_TRANSITION_FPS="${SWWW_TRANSITION_FPS:-60}"
16+
export SWWW_TRANSITION_STEP="${SWWW_TRANSITION_STEP:-2}"
4817

4918
while true; do
50-
find "$1" -type f \
51-
| while read -r img; do
52-
echo "$RANDOM:$img"
53-
done \
54-
| sort -n | cut -d':' -f2- \
55-
| tee ~/.local/state/swww-randomize-list.txt \
56-
| while read -r img; do
57-
# Set a different image for each display
58-
for disp in $DISPLAY_LIST; do
59-
# if there is no image try to get one
60-
if [ "X" = "X${img}" ]; then
61-
if read -r img; then
62-
true
63-
else # if there are no more images, refresh the list
64-
break 2
65-
fi
66-
fi
67-
swww img --resize=$RESIZE_TYPE --outputs $disp $img
68-
# make sure each image is only used once
69-
img=""
70-
done
71-
sleep $INTERVAL
72-
done
19+
find "$1" -type f \
20+
| while read -r img; do
21+
echo "$(</dev/urandom tr -dc a-zA-Z0-9 | head -c 8):$img"
22+
done \
23+
| sort -n | cut -d':' -f2- \
24+
| while read -r img; do
25+
for d in $(swww query | grep -Po "^[^:]+"); do # see swww-query(1)
26+
# Get next random image for this display, or re-shuffle images
27+
# and pick again if no more unused images are remaining
28+
[ -z "$img" ] && if read -r img; then true; else break 2; fi
29+
swww img --resize "$RESIZE_TYPE" --outputs "$d" "$img"
30+
unset -v img # Each image should only be used once per loop
31+
done
32+
sleep "${2:-$DEFAULT_INTERVAL}"
33+
done
7334
done

0 commit comments

Comments
 (0)