blob: b6e66d7947c7e536341d8ed91b7aaee4331d063d (
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
#!/bin/sh
set -eu
usage() {
cat <<-'EOF'
Usage:
player ACTION
player -h
EOF
}
help() {
cat <<-'EOF'
Options:
-h, --help show this message
ACTION one of:
- backward: go back 5 seconds
- forward: go forward 5 seconds
- previous: go to the previous track
- next: go to the next track
- play-pause: play/pause
- rotate: rotate across available MPRIS players
- current: show the current MPRIS player
Manipulate the MPRIS audio player.
Examples:
Change the current MPRIS player:
$ player current
Play/pause:
$ player play-pause
EOF
}
for flag in "$@"; do
case "$flag" in
--)
break
;;
--help)
usage
help
exit
;;
*)
;;
esac
done
while getopts 'P:h' flag; do
case "$flag" in
h)
usage
help
exit
;;
*)
usage >&2
exit 2
;;
esac
done
shift $((OPTIND - 1))
ACTION="${1:-}"
eval "$(assert-arg "$ACTION" 'ACTION')"
CURRENT_PLAYER_PATH="$XDG_CACHE_HOME"/euandreh-mpris-player.txt
CURRENT_PLAYER="$(cat "$CURRENT_PLAYER_PATH" ||:)"
AVAILABLE_PLAYERS="$(playerctl --list-all | LANG=POSIX.UTF-8 sort)"
pick_first() {
echo "$AVAILABLE_PLAYERS" | head -n1
}
next_player() {
if [ -z "$CURRENT_PLAYER" ]; then
pick_first
elif ! echo "$AVAILABLE_PLAYERS" | grep -q "$CURRENT_PLAYER"; then
# Unknown $CURRENT_PLAYER, pick anyone
pick_first
else
INDEX="$(echo "$AVAILABLE_PLAYERS" | grep -n "$CURRENT_PLAYER" | cut -d: -f1)"
LENGTH="$(echo "$AVAILABLE_PLAYERS" | wc -l)"
if [ "$INDEX" = "$LENGTH" ]; then
# Reached the end of the $AVAILABLE_PLAYERS list, wrapping
pick_first
else
# Get the next player instead
echo "$AVAILABLE_PLAYERS" | awk -v idx="$INDEX" 'NR == idx+1 {print}'
fi
fi
}
case "$ACTION" in
backward)
playerctl --player="$CURRENT_PLAYER" position 5-
;;
forward)
playerctl --player="$CURRENT_PLAYER" position 5+
;;
previous)
playerctl --player="$CURRENT_PLAYER" previous
;;
next)
playerctl --player="$CURRENT_PLAYER" next
;;
play-pause)
playerctl --player="$CURRENT_PLAYER" play-pause
;;
rotate)
PLAYER="$(next_player)"
echo "$PLAYER" > "$CURRENT_PLAYER_PATH"
notify-send -t 1000 "$PLAYER" 'current MPRIS target'
;;
current)
printf '%s\n' "$CURRENT_PLAYER"
;;
*)
printf 'Bad ACTION: "%s".\n\n' "$ACTION" >&2
usage >&2
exit 2
;;
esac
|