-
Notifications
You must be signed in to change notification settings - Fork 6
/
ssmremote
executable file
·339 lines (306 loc) · 10.5 KB
/
ssmremote
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/sh
# ssmremote - simple stupid media remote
# It will play/pause mpv and mpc/mpd, depending on current status
# mpv(1) does not allow remote control by default. I'm using mpvSockets:
# https://github.com/wis/mpvSockets
: "${TMPDIR:=/tmp}"
: "${_mpvsdir:="$TMPDIR"/mpvSockets}"
__usage() {
cat << EOH
$0 is a simple stupid media remote that will try to execute the current
command to the focused mpv(1) window if applicable, or mpd(1) otherwise.
$0 [-d] next|prev|next-chapter|prev-chapter|toggle|pause
$0 -h
Flags:
-h: this help text
-d: debug output (set -x)
Commands:
next|prev: if mpv(1) is playing, send playlist-next or playlist-prev (this
might do nothing if you reached the end of the playlist); else if
mpv(1) has focus, send playlist-next or playlist-prev; else send to
mpd(1)
next-chapter|prev-chapter: if mpv(1) is playing, add or remove one chapter
(this might do nothing if you reached the first or last chapter);
else if mpv(1) has focus, add or remove one chapter; else send
next|prev to mpd(1)
pause: pause all mpv(1) instances and mpd(1)
toggle: if something is playing, pause everything; else start the current
mpv(1) that has focus or mpd(1)
store|restore: save the current status of mpv(1) instances and mpd(1) in
TMPDIR, overwriting any existing file; restore will only re-start
the instances that were previously saved as "playing".
These commands are intended to be used during machine lock or IRL
interruptions:
% $0 store
% $0 pause
% [...]
% $0 restore
isplaying: returns 0 if one mpv(1) or mpd(1) is playing
EOH
}
while getopts ":dh" _opt; do
case "$_opt" in
d) set -x ;;
h) __usage ; exit 0 ;;
*) __usage >&2 ; exit 1 ;;
esac
done
shift "$((OPTIND - 1))"
__exists() {
command -v "$1" >/dev/null 2>&1
}
__error() {
printf '%s\n' "$1" >&2
}
__die() {
__error "${1:-"unspecified error"}"
exit "${2:-10}"
}
for _cmd in jq mpc ncat openssl; do
__exists "$_cmd" || __die "$_cmd not found" 2
done
__mpd_playing() {
[ "$(mpc status %state%)" = "playing" ]
}
__isplaying() {
find "$_mpvsdir" -type s | while IFS='' read -r _socket; do
printf "%s:%s\n" \
"$_socket" \
"$(__mpv_command_data '"get_property", "pause"' "$_socket")"
done | grep -q "false" ||
__mpd_playing
}
# pause everything, return number of failed pauses
__pause() {
_ret="$(find "$_mpvsdir" -type s | while IFS='' read -r _socket; do
__mpv_command '"set_property", "pause", true' "$_socket" |
jq -r .error
done | grep -cv success
)"
mpc -q pause || _ret="$((_ret + 1))"
return "$_ret"
}
# return a PID if a mpv(1) window has focus, or NULL (nothing)
# see https://superuser.com/questions/382616/detecting-currently-active-window
__mpv_with_focus_or_null() {
_wid="$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"
case "$_wid" in
"0x0") : ;; # no window has focus
*) if xprop -id "$_wid" WM_CLASS | grep -q '"mpv"'; then
xprop -id "$_wid" _NET_WM_PID | grep -Eo '[0-9]+$'
else
:
fi ;;
esac
}
__mpv_command() {
_pid="${2:?"__mpv_command requires a command and a PID or socket"}"
if [ -S "$2" ]; then
_socket="$2"
else
_socket="$_mpvsdir/$2"
fi
if [ -S "$_socket" ]; then
while [ -z "$_rid" ] || [ "$_rid" -eq 0 ] ; do
# - no $RANDOM in FreeBSD's sh
# - we can't use $$ because we could use the same socket multiple times
# in the same call to our script, and we really shouldn't mix up
# outputs!
# - openssl could output `0` or tr(1) could remove all output
# - if _rid starts with ^0 then sh could consider it to be base8... TODO
_rid="$(openssl rand -base64 10 |
tr 'A-J' '0-9' | tr -dc '0-9' |
grep -v '^0')"
done
printf '{ "command" : [ %s ] , "request_id" : %s}\n' "$1" "$_rid" |
ncat -U "$_socket" | jq "select( .request_id == $_rid )"
else
__error "$_socket is not a socket!"
fi
}
__mpv_command_data() {
__mpv_command "$1" "$2" | jq --raw-output --exit-status .data
}
# return a PID if a mpv(1) window is playing, or NULL (nothing)
__mpv_playing_or_null() {
find "$_mpvsdir" -type s | while IFS='' read -r _socket; do
if __mpv_command '"get_property", "pause"' "$_socket" |
jq --exit-status 'select(.data==false)' >/dev/null;
then
_maybe_pid="${_socket##*/}"
if ps -p "${_maybe_pid}" >/dev/null 2>&1; then
echo "${_maybe_pid}"
else
__die "$_socket is a valid socket but $_maybe_pid is not a PID"
fi
break
fi
done
}
__playlist() {
case "$1" in
next|prev) : ;;
*) __die "__playlist() requires next or prev as argument" 3 ;;
esac
_pid="$(__mpv_playing_or_null)"
: "${_pid:="$(__mpv_with_focus_or_null)"}"
case "$_pid" in
'') mpc -q "$1" ;;
*) if __mpv_command "$(printf '"playlist-%s", "weak"' "$1")" "$_pid" |
jq -r .error | grep -v success; then
return 1
else
return 0
fi
esac
}
__wait_until_seekable () {
_pid="$1"
_deadline="$(( "$(date +%s)" + 5))"
while ! __mpv_command_data '"get_property","duration"' "$_pid" \
>/dev/null 2>&1; do
[ "$(date +%s)" -gt "$_deadline" ] && __die "timeout waiting for $_pid" 7
done
}
__mpv_goto_last_chapter () { # (or do nothing)
_pid="$1"
_c="$(__mpv_command_data '"get_property","chapters"' "$_pid")"
case "$_c" in
0|1) : ;; # no chapters, mpv(1) should have put us at the start of the file
*) if __mpv_command \
"$(printf '"set_property","chapter","%s"\n' "$((_c -1))" )" \
"$_pid" | jq .error | grep -qv success ; then
return 1
else
return 0
fi ;;
esac
}
__chapter() {
case "$1" in
next) _v="1" ;;
prev) _v="-1" ;;
*) __die "__chapter() requires next or prev as argument" 5 ;;
esac
# _pid is: * 0 if mpd is playing
# * a real PID if one mpv is currently playing or if nothing is playing
# AND we focus a mpv window
# * empty if nothing is playing
_pid="$(__mpv_playing_or_null)"
__mpd_playing && : "${_pid:=0}"
: "${_pid:="$(__mpv_with_focus_or_null)"}"
case "$_pid" in
''|0) mpc -q "$1" ;;
*) # Chapter manipulation on files that have none is completely broken with
# no sane defaults on mpv(1). Buckle up.
_c="$(__mpv_command_data '"get_property","chapters"' "$_pid")"
# _curc is "null" for files with no chapters
_curc="$(__mpv_command_data '"get_property","chapter"' "$_pid")"
_curp="$(__mpv_command_data '"get_property","playlist-pos"' "$_pid")"
# is time position greater than chapter-seek-threshold?
# no=empty; yes=non-empty
_cst="$(__mpv_command_data '"get_property","chapter-seek-threshold"' "$_pid")"
_tpgtcst="$(__mpv_command '"get_property","time-pos"' "$_pid" | jq "select(.data > $_cst)")"
if [ "$_curc" != "null" ]; then
# there are chapters in the current file
if [ -n "$_tpgtcst" ] || [ "$1" = "next" ]; then
# if we passed _cst, chapters work as expected (we will not be going
# back one file in the playlist)
# if we use "next-chapter", it works as expected (including the cases
# where we reach the end of the file or the end of the playlist)
__mpv_command "$(printf '"add", "chapter","%s"\n' "$_v")" "$_pid"
elif [ "$_curp" -eq 0 ]; then
# we are going back one chapter and we are at the very beginning of
# the playlist: we jump to time-pos 0
__mpv_command '"set_property","time-pos",0' "$_pid"
else
# we are going back in the playlist but we want to jump to the last
# chapter of that previous file
__playlist prev
__wait_until_seekable "$_pid"
# we jump to the beginning of the last chapter if it exists
__mpv_goto_last_chapter "$_pid"
fi
else # the current file has no chapters
if [ "$_curp" -eq 0 ] && [ "$1" = prev ]; then
__mpv_command '"set_property","time-pos",0' "$_pid"
elif [ "$1" = next ] ; then # next chapter = next file
__playlist "$1"
elif [ -z "$_tpgtcst" ]; then
# we are not at the beginning of the playlist, we want prev chapter
# and we have not passed _cst; we jump to last chater of the previous
# file
__playlist "$1"
__wait_until_seekable "$_pid"
__mpv_goto_last_chapter "$_pid"
else
# we have passed _cst, so the previous chapter is actually the
# beginning of the current file
__mpv_command '"set_property","time-pos",0' "$_pid"
fi
fi ;;
esac
}
__toggle() {
_pid="$(__mpv_playing_or_null)"
case "$_pid" in
'') # no mpv is playing
case "$(mpc status %state%)" in
"paused") # nothing is running!
_pid="$(__mpv_with_focus_or_null)"
case "$_pid" in
'') mpc play ;;
*) if __mpv_command '"set_property", "pause", false' "$_pid" |
jq -r .error | grep -v success; then
return 1
else
return 0
fi ;;
esac
;;
"playing") mpc pause ;;
esac
;;
*) # mpv is playing, we pause everything
__pause ;;
esac
}
__store() {
_tmp="$TMPDIR/$(basename "$0")"
touch "$_tmp" || __die "couldn't create $_tmp"
find "$_mpvsdir" -type s | while IFS='' read -r _socket; do
printf "%s:%s\n" \
"$_socket" \
"$(__mpv_command_data '"get_property", "pause"' "$_socket")"
done > "$_tmp"
printf "%s:%s\n" \
"mpd" \
"$(mpc status %state%)" >> "$_tmp"
}
__restore() {
_tmp="$TMPDIR/$(basename "$0")"
[ -e "$_tmp" ] || __die "no state to restore from $_tmp"
while IFS=':' read -r _pid _state; do
case "$_pid" in
mpd)
case "$_state" in
playing) mpd -q play ;;
esac ;;
*)
case "$_state" in
false) __mpv_command '"set_property", "pause", false' "$_pid" ;;
esac ;;
esac
done < "$_tmp"
}
case "$1" in
toggle) __toggle ; exit $? ;;
next) __playlist "next" ; exit $? ;;
prev) __playlist "prev" ; exit $? ;;
next-chapter) __chapter "next" ; exit $? ;;
prev-chapter) __chapter "prev" ; exit $? ;;
pause) __pause ; exit $? ;;
store) __store ; exit $? ;;
restore) __restore ; exit $? ;;
isplaying) __isplaying ; exit $? ;;
esac