-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxwin_decor.sh
executable file
·100 lines (84 loc) · 2.84 KB
/
xwin_decor.sh
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
#!/usr/bin/env sh
# xwin_decor.sh v1.0
# decorate root window
# fallback tiling background
bitmaps="$XDG_DATA_HOME/X11/bitmaps"
cpp -P <<- EOF | xargs xsetroot -bitmap "$bitmaps/diag.xbm"
#include <colors/nightdrive.h>
-bg COLOR15 -fg COLOR1
EOF
# custom background
# select N random images or video frames from any directory
# indicated by ~/.xdecor, one for each active display
zeropad() {
[ $1 -gt 9 ] && echo $1 || echo "0$1"
}
rand() {
{ od -N 4 -t u -A n | tr -d ' '; } < /dev/urandom
}
shuffle() {
input="$(cat /dev/stdin)"
lines=$(echo "$input" | wc -l)
echo "$input" | tail -n +$((($(rand) % lines) + 1)) | head -n 1
}
ffmpeg_cat() {
mediainfo "$1" --inform='Video;%FrameCount% %FrameRate%' \
| while read -r f_count fps; do
# calculate length and randomly select timestamp
len=$(echo "scale=2; $f_count * (1 / $fps)" | bc)
len=${len%.*}
skip_len=150
while :; do # reroll for random frame that skips over OPs/EDs
sel=$(($(rand) % len))
[ $len -le $skip_len ] && break # short video?
[ $sel -gt $skip_len ] || continue
[ $sel -lt $((len - skip_len)) ] || continue
break
done
hrs=$((sel / 3600))
min=$(((sel - (hrs * 3600)) / 60))
sec=$((sel % 60))
timest="$(zeropad $hrs):$(zeropad $min):$(zeropad $sec)"
# saved for future use
# ffmpeg -ss "$timest" -i "$1" -vframes 1 \
# -q:v 0 -f image2pipe -vcodec png - 2> /dev/null
# low performance version
ffmpegthumbnailer -i "$1" -s 0 -c png -t "$timest" -o -
done
}
# remove trash from previous iterations
rm -rf "$XDG_RUNTIME_DIR/${0##*/}"*
temp="$XDG_RUNTIME_DIR/${0##*/}-$$" && mkdir -p "$temp"
# pcmanfm will randomly re-read wallpaper files after they're deleted, leading
# to black screens, just leave temp dir trash to be cleaned up on next run
# trap 'rm -rf "$temp"' 0 1 2 3 6 15
config="$HOME/.xdecor"
[ -f "$config" ] || exit
# iterate through all active displays
xrandr -q | fgrep '*' | while read -r dpy; do
# randomly select directory from ~/.xdecor
{ sed -e 's/#.*//' -e '/^$/d' | shuffle; } < "$config" \
| while read -r dir; do
[ "${dir%${dir#?}}" = '~' ] && dir="$HOME/${dir#??}" # absolute path
[ ! -z "$dir" ] || exit
# randomly select file
find "$dir" -type f \
| egrep '\.(jpe?g|png|mkv|mp4|web(m|p))$' \
| shuffle | while read -r sel; do
[ ! -z "$sel" ] || exit
case "$sel" in
*mkv|*mp4|*webm) ffmpeg_cat "$sel";; # video file
*) cat "$sel"
esac
done > "$temp/$(rand)"
done
done
# pcmanfm desktop: set wallpaper by mangling config files and resetting
# use plain x window fallback via feh if pcmanfm not found
which pcmanfm > /dev/null && {
find "$temp" -type f | nl -v 0 -n ln | while read -r mon file; do
sed -E "s,^wallpaper=.*,wallpaper=$file,g" \
-i ~/.config/pcmanfm/default/desktop-items-$mon.conf
done
pcmanfm --desktop-off && pcmanfm --desktop &
} || find "$temp" -type f | xargs feh --no-fehbg --bg-fill