-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocp-mpris
executable file
·85 lines (69 loc) · 2.31 KB
/
mocp-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
#!/usr/bin/env python3
# Author: Jan Larres <jan@majutsushi.net>
# License: MIT/X11
#
# References:
# https://github.com/mopidy/mopidy-mpris/blob/develop/mopidy_mpris/objects.py
# https://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html
# https://gist.github.com/caspian311/4676061
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import dbus.service
from gi.repository import GLib
import re
import signal
from subprocess import call, check_output, STDOUT, CalledProcessError
import sys
BUS_NAME = 'org.mpris.MediaPlayer2.mocp'
OBJECT_PATH = '/org/mpris/MediaPlayer2'
ROOT_IFACE = 'org.mpris.MediaPlayer2'
PLAYER_IFACE = 'org.mpris.MediaPlayer2.Player'
PLAYLISTS_IFACE = 'org.mpris.MediaPlayer2.Playlists'
class Mocp(dbus.service.Object):
def __init__(self):
bus_name = dbus.service.BusName(BUS_NAME, dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, OBJECT_PATH)
self.loop = GLib.MainLoop()
def run(self):
self.loop.run()
@dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE,
in_signature='ss', out_signature='v')
def Get(self, interface, prop):
if prop == 'PlaybackStatus':
state = get_mocp_state()
if state == 'UNKNOWN' or state == 'STOP':
return 'Stopped'
if state == 'PLAY':
return 'Playing'
elif state == 'PAUSE':
return 'Paused'
else:
return 'Stopped'
return 'Not implemented'
@dbus.service.method(dbus_interface=ROOT_IFACE)
def Quit(self):
sys.exit(0)
@dbus.service.method(dbus_interface=PLAYER_IFACE)
def PlayPause(self):
state = get_mocp_state()
if state == 'UNKNOWN' or state == 'STOP':
return
mocp_cmd(['--toggle-pause'])
def mocp_cmd(args):
return check_output(['mocp'] + args, stderr=STDOUT).decode('utf-8')
def get_mocp_state():
try:
out = mocp_cmd(['-i'])
except CalledProcessError as e:
return 'UNKNOWN'
for line in out.splitlines():
match = re.match('^State: (.*)', line)
if match:
return match.group(1)
# STOP, PLAY, PAUSE
return 'UNKNOWN'
def main():
DBusGMainLoop(set_as_default=True)
Mocp().run()
if __name__ == '__main__':
sys.exit(main())