|
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. |
2 | 4 |
|
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 |
9 | 6 |
|
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 |
14 | 11 | fi
|
15 | 12 |
|
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) |
45 | 14 | 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}" |
48 | 17 |
|
49 | 18 | 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 |
73 | 34 | done
|
0 commit comments