blob: ad670fcd9397b896ecc81575d9e2418c44df257d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#!/bin/sh
bgdpid="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/swaybg.pid"
bgroot="${XDG_DATA_HOME:-$HOME/.local/share}/wallpaper"
bgdesk="${bgroot}/desktop"
bglock="${bgroot}/lockscreen"
die()
{
printf "\033[31;1merr: %b\033[0m\n" "${1:-no args for die()}" 1>&2
exit "${2:-1}"
}
usage()
{
cat <<- EOF
Usage: ${0##*/} [wallpaper.img|directory]
A helper script for swaybg
EOF
}
dep_check()
{
# usage: cmd_check cmd_1 ...
: "${1:?}"
for dep; do
command -v "$dep" 1>/dev/null ||
die "${dep}: command not found" 127
done
unset dep
}
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"
cp "$1" "$bgdesk"
[ -s "$bgdpid" ] &&
kill -9 "$(cat "$bgdpid")"
swaybg --mode fill --image "$bgdesk" > /dev/null 2>&1 &
echo $! > "$bgdpid"
if command -v ffmpeg > /dev/null; then
rm -f "$bglock"
ffmpeg -loglevel quiet \
-i "$bgdesk" -vf "boxblur=50" -f mjpeg "$bglock"
else
ln -fs "$bgdesk" "$bglock"
fi
}
########
# MAIN #
########
[ ! -r "$1" ] && usage && exit 1
[ ! -d "$bgroot" ] && mkdir -p "$bgroot"
dep_check swaybg file
case "$(mimetype "$1")" in
"image/"*) updatewall "$1" ;;
"inode/directory")
img=$(randimg "$1") ||
die "no image file in directory"
updatewall "$img"
;;
*) die "invalid image file"
esac
|