-
Notifications
You must be signed in to change notification settings - Fork 18
/
mpvc-mpris
executable file
·144 lines (122 loc) · 4.4 KB
/
mpvc-mpris
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
#!/usr/bin/env sh
#
# @file mpvc-mpris
# @description mpvc-mpris speaks MPRIS to control mplayer through key-bindings
# @author gmt4 <gmt4 at github.com> (c) Copyright 2022 GPLv2+
# @url github.com/gmt4/mpvc
# SPDX-License-Identifier: GPL-2.0-or-later
#
PROGNAME=${0##*/}
PROGVERSION="v1.5"
PROGAUTHOR=gmt4
PROGURL="https://github.com/gmt4/mpvc"
set -euf
# globals really
mpris_defaults()
{
DBUS=$(command -v dbus-send)
PACTL=$(command -v pactl)
PULSE_SOCKET="/run/user/$(id -u)/pulse/native"
if [ -z "${PULSE_SERVER:-}" ] && [ -S "$PULSE_SOCKET" ]
then
export PULSE_SERVER="unix:$PULSE_SOCKET"
fi
playerPath=org.mpris.MediaPlayer2.Player
MPVC_MPRIS_PLAYERS=${MPVC_MPRIS_PLAYERS:-"mpv mpd vlc"}
if [ -z "${DBUS:-}" ]; then usage "$PROGNAME: Error no dbus-send found (required)"; fi
if [ -z "${PACTL:-}" ]; then usage "$PROGNAME: Error no pactl found (required)"; fi
}
mpris_cmd()
{
$DBUS \
--print-reply \
--type=method_call \
--dest="org.mpris.MediaPlayer2.$mpris_name" /org/mpris/MediaPlayer2 \
"$@"
}
mpris_method(){ mpris_cmd "org.mpris.MediaPlayer2.Player.$*"; }
mpris_get(){ mpris_cmd org.freedesktop.DBus.Properties.Get string:$playerPath "$@"; }
mpris_set(){ mpris_cmd org.freedesktop.DBus.Properties.Set string:$playerPath "$@"; }
mpris_get_volume()
{
mpris_get string:Volume | awk '/^ variant/ {print $3}'
}
mpris_set_volume()
{
cVolume=$(mpris_get_volume)
pVolume=$cVolume
case "$1" in
volmute) pVolume=$(awk "END { print ($pVolume == 0)? 0.5: 0; }" /dev/null );;
voldown) pVolume=$(awk "END { print $cVolume - 0.02 }" /dev/null );;
volup) pVolume=$(awk "END { print $cVolume + 0.02 }" /dev/null );;
volset) pVolume=$2;;
*) usage;;
esac
mpris_set string:Volume "variant:double:$pVolume"
}
mpris_get_status()
{
#mpris_get string:Volume | awk '/^ variant / {print $3}'
mpris_get string:PlaybackStatus | awk '/^ variant / {print $3}'
mpris_get string:Metadata | awk '/^ *variant +/ && ! /\[/ {gsub("^ *"$1" *"$2" ",""); print}'
}
mpris_set_shuffle()
{
current=$(mpris_get string:Shuffle | awk '/^ variant / {print $3}')
if [ "$current" = "true" ]; then other=false; else other=false; fi
mpris_set string:Shuffle variant:boolean:$other
}
mpris_detect_player()
{
name=""
for name in $MPVC_MPRIS_PLAYERS "";
do
mpris_name="$name"
if mpris_get string:PlaybackStatus >/dev/null 2>&1;
then
break
fi
done
echo "$name"
}
usage()
{
echo "usage: $PROGNAME [prev|next|stop] # @version $PROGVERSION (c) $PROGAUTHOR $PROGURL"
if [ $# -gt 0 ]; then echo "$@" 1>&2; fi
exit
}
main()
{
mpris_defaults
if [ $# -lt 1 ]; then usage; fi
mpris_name=$(mpris_detect_player)
if [ -z "${mpris_name}" ] && ! [ "$1" = "volstatus" -o "$1" = "volget" -o "$1" = "volset" -o "$1" = "volup" -o "$1" = "voldown" -o "$1" = "volon" -o "$1" = "voloff" -o "$1" = "volmute" ]; then
usage "$PROGNAME: Error no player found that speaks MPRIS. Check that you started mpv with mpris.so in ~/.config/mpv/scripts/";
fi
cmd="$1"
case "$cmd" in
get) shift; mpris_get "$@" ;;
set) shift; mpris_set "$@" ;;
method) shift; mpris_method "$@" ;;
prev) shift; mpris_method Previous ;;
next) shift; mpris_method Next ;;
toggle) shift; mpris_method PlayPause ;;
stop) shift; mpris_method Pause ;;
seek) shift; mpris_method Seek "int64:$1"; ;;
status) shift; mpris_get_status ;;
vol) shift; mpris_get_volume ;;
shuffle) shift; mpris_set_shuffle "$*" ;;
open) shift; mpris_method OpenUri "string:$1" ;;
#loop) shift; mpris_get "string:LoopStatus" ;;
volon) $PACTL set-sink-mute @DEFAULT_SINK@ off ;;
voloff) $PACTL set-sink-mute @DEFAULT_SINK@ on ;;
volmute) $PACTL set-sink-mute @DEFAULT_SINK@ toggle || mpris_set_volume "$cmd" ;;
voldown) $PACTL set-sink-volume @DEFAULT_SINK@ -250 || mpris_set_volume "$cmd" ;;
volup) $PACTL set-sink-volume @DEFAULT_SINK@ +250 || mpris_set_volume "$cmd" ;;
volset) $PACTL set-sink-volume @DEFAULT_SINK@ "${2:-}" || mpris_set_volume "$cmd" "${2:-}";;
volget) $PACTL list sinks | awk '/^\s*Volume:/{print $5}';;
volstatus) $PACTL list sinks | awk '/^\s*Mute:/{print "mute:"$2}';;
*) usage "$PROGNAME: unknown command: $cmd" ;;
esac
}
main "$@"