Browse code

Add implementation

Robert Cranston authored on 30/01/2022 19:39:29
Showing 3 changed files

... ...
@@ -6,6 +6,32 @@ Set [X Window System][] [background][] image/video.
6 6
 [X Window System]: https://en.wikipedia.org/wiki/X_Window_System
7 7
 [background]: https://en.wikipedia.org/wiki/Wallpaper_(computing)
8 8
 
9
+## Usage
10
+
11
+`background --help`:
12
+
13
+```
14
+background 1.0
15
+
16
+Set X Window System background image/video.
17
+
18
+usage:
19
+  background [-v|--video] [--] [<path>]
20
+  background -h|--help
21
+  background --version
22
+
23
+options:
24
+  -v, --video
25
+    Changes the default <path> to
26
+    ${XDG_DATA_HOME:-$HOME/.local/share}/backgrounds-video.
27
+arguments:
28
+  <path>
29
+    If not a directory, set it as background. If a directory, search
30
+    recursively in it (resolving symlinks) and set a random file as
31
+    background.
32
+    [default: ${XDG_DATA_HOME:-$HOME/.local/share}/backgrounds]
33
+```
34
+
9 35
 ## License
10 36
 
11 37
 Licensed under the [ISC License][] unless otherwise noted, see the
... ...
@@ -6,10 +6,14 @@ set -euC
6 6
 #//// Set X Window System background image/video.
7 7
 #////
8 8
 ###// usage:
9
-#////   background [<path>]
9
+#////   background [-v|--video] [--] [<path>]
10 10
 #////   background -h|--help
11 11
 #////   background --version
12 12
 #////
13
+###// options:
14
+####/   -v, --video
15
+#////     Changes the default <path> to
16
+#////     ${XDG_DATA_HOME:-$HOME/.local/share}/backgrounds-video.
13 17
 ###// arguments:
14 18
 ####/   <path>
15 19
 #////     If not a directory, set it as background. If a directory, search
... ...
@@ -37,9 +41,44 @@ in
37 41
   '--version') version; ;;
38 42
 esac
39 43
 
44
+## Parse options
45
+suffix=''
46
+while [ $# -gt 0 ]
47
+do
48
+  case "$1"
49
+  in
50
+    '-v'|'--video') suffix='-video'; shift; ;;
51
+    '--') shift; break; ;;
52
+    *) break; ;;
53
+  esac
54
+done
55
+
40 56
 ## Parse optional arguments
41
-path="${XDG_DATA_HOME:-"$HOME/.local/share"}/backgrounds"
57
+path="${XDG_DATA_HOME:-"$HOME/.local/share"}/backgrounds${suffix}"
42 58
 [ $# -eq 0 ] || { path="$1"; shift; }
43 59
 
44 60
 ## Parse unrecognized arguments
45 61
 argend "$@"
62
+
63
+## Convert `path` from directory to file if needed
64
+if [ -d "$path" ]
65
+then
66
+  path="$(find -L "$path" \! -type d | shuf -n 1)"
67
+fi
68
+
69
+## Set `daemon`
70
+daemon="$(dirname "$0")/../libexec/background/daemon"
71
+
72
+## Opportunistically use `hsetroot` for images
73
+if [ "$(command -v xdg-mime)" ] && [ "$(command -v hsetroot)" ]
74
+then
75
+  if xdg-mime query filetype "$path" | grep -q '^image/'
76
+  then
77
+    "$daemon" --kill
78
+    hsetroot -cover "$path"
79
+    exit 0
80
+  fi
81
+fi
82
+
83
+## Run daemon
84
+setsid "$daemon" "$path" &
46 85
new file mode 100755
... ...
@@ -0,0 +1,37 @@
1
+#!/bin/sh
2
+set -euC
3
+
4
+## Set `pidfile`
5
+pidfile="${XDG_RUNTIME_DIR:-"/var/run/user/$(id -u)"}/background.pid"
6
+
7
+## Kill any running daemon
8
+if [ -r "$pidfile" ]
9
+then
10
+  pid="$(cat "$pidfile")"
11
+  kill -- "-$pid"
12
+  while kill -0 "$pid" 2> '/dev/null' ; do : ; done
13
+fi
14
+
15
+## Exit early if only killing
16
+if [ "$1" = '--kill' ]
17
+then
18
+  exit 0
19
+fi
20
+
21
+## Set up `pidfile`
22
+printf '%s' "$$" >| "$pidfile"
23
+trap 'rm "$pidfile"' EXIT
24
+trap 'exit 0' INT TERM
25
+
26
+## Set background
27
+nice \
28
+  xwinwrap -fs -fdt -ov -un -b -s -st -sp -ni -nf -- \
29
+  mpv \
30
+    -wid WID \
31
+    --really-quiet \
32
+    --no-audio \
33
+    --panscan=1 \
34
+    --loop-file=inf \
35
+    --ytdl-format='bestvideo[height<=?720][fps<=?30]' \
36
+    "$@" \
37
+  2> '/dev/null'