| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import os |
| 7 |
import sys |
| 8 |
import dbus |
| 9 |
import dbus.service |
| 10 |
import dbus.mainloop.glib |
| 11 |
try: |
| 12 |
from gi.repository import GObject |
| 13 |
except ImportError: |
| 14 |
import gobject as GObject |
| 15 |
import bluezutils |
| 16 |
|
| 17 |
class Player(dbus.service.Object): |
| 18 |
properties = None |
| 19 |
metadata = None |
| 20 |
|
| 21 |
def set_object(self, obj = None): |
| 22 |
if obj != None: |
| 23 |
bus = dbus.SystemBus() |
| 24 |
mp = dbus.Interface(bus.get_object("org.bluez", obj), |
| 25 |
"org.bluez.MediaPlayer1") |
| 26 |
prop = dbus.Interface(bus.get_object("org.bluez", obj), |
| 27 |
"org.freedesktop.DBus.Properties") |
| 28 |
|
| 29 |
self.properties = prop.GetAll("org.bluez.MediaPlayer1") |
| 30 |
|
| 31 |
bus.add_signal_receiver(self.properties_changed, |
| 32 |
path = obj, |
| 33 |
dbus_interface = "org.freedesktop.DBus.Properties", |
| 34 |
signal_name = "PropertiesChanged") |
| 35 |
else: |
| 36 |
track = dbus.Dictionary({ |
| 37 |
"xesam:title" : "Title", |
| 38 |
"xesam:artist" : ["Artist"], |
| 39 |
"xesam:album" : "Album", |
| 40 |
"xesam:genre" : ["Genre"], |
| 41 |
"xesam:trackNumber" : dbus.Int32(1), |
| 42 |
"mpris:length" : dbus.Int64(10000) }, |
| 43 |
signature="sv") |
| 44 |
|
| 45 |
self.properties = dbus.Dictionary({ |
| 46 |
"PlaybackStatus" : "playing", |
| 47 |
"Identity" : "SimplePlayer", |
| 48 |
"LoopStatus" : "None", |
| 49 |
"Rate" : dbus.Double(1.0), |
| 50 |
"Shuffle" : dbus.Boolean(False), |
| 51 |
"Metadata" : track, |
| 52 |
"Volume" : dbus.Double(1.0), |
| 53 |
"Position" : dbus.Int64(0), |
| 54 |
"MinimumRate" : dbus.Double(1.0), |
| 55 |
"MaximumRate" : dbus.Double(1.0), |
| 56 |
"CanGoNext" : dbus.Boolean(False), |
| 57 |
"CanGoPrevious" : dbus.Boolean(False), |
| 58 |
"CanPlay" : dbus.Boolean(False), |
| 59 |
"CanSeek" : dbus.Boolean(False), |
| 60 |
"CanControl" : dbus.Boolean(False), |
| 61 |
}, |
| 62 |
signature="sv") |
| 63 |
|
| 64 |
handler = InputHandler(self) |
| 65 |
GObject.io_add_watch(sys.stdin, GObject.IO_IN, |
| 66 |
handler.handle) |
| 67 |
|
| 68 |
@dbus.service.method("org.freedesktop.DBus.Properties", |
| 69 |
in_signature="ssv", out_signature="") |
| 70 |
def Set(self, interface, key, value): |
| 71 |
print("Set (%s, %s)" % (key, value), file=sys.stderr) |
| 72 |
return |
| 73 |
|
| 74 |
@dbus.service.signal("org.freedesktop.DBus.Properties", |
| 75 |
signature="sa{sv}as") |
| 76 |
def PropertiesChanged(self, interface, properties, |
| 77 |
invalidated = dbus.Array()): |
| 78 |
"""PropertiesChanged(interface, properties, invalidated) |
| 79 |
|
| 80 |
Send a PropertiesChanged signal. 'properties' is a dictionary |
| 81 |
containing string parameters as specified in doc/media-api.rst. |
| 82 |
""" |
| 83 |
pass |
| 84 |
|
| 85 |
def help(self, func): |
| 86 |
help(self.__class__.__dict__[func]) |
| 87 |
|
| 88 |
def properties_changed(self, interface, properties, invalidated): |
| 89 |
print("properties_changed(%s, %s)" % (properties, invalidated)) |
| 90 |
|
| 91 |
self.PropertiesChanged(interface, properties, invalidated) |
| 92 |
|
| 93 |
class InputHandler: |
| 94 |
commands = { 'PropertiesChanged': '(interface, properties)', |
| 95 |
'help': '(cmd)' } |
| 96 |
def __init__(self, player): |
| 97 |
self.player = player |
| 98 |
print('\n\nAvailable commands:') |
| 99 |
for cmd in self.commands: |
| 100 |
print('\t', cmd, self.commands[cmd], sep='') |
| 101 |
|
| 102 |
print("\nUse python syntax to pass arguments to available methods.\n" \ |
| 103 |
"E.g.: PropertiesChanged({'Metadata' : {'Title': 'My title', \ |
| 104 |
'Album': 'my album' }})") |
| 105 |
self.prompt() |
| 106 |
|
| 107 |
def prompt(self): |
| 108 |
print('\n>>> ', end='') |
| 109 |
sys.stdout.flush() |
| 110 |
|
| 111 |
def handle(self, fd, condition): |
| 112 |
s = os.read(fd.fileno(), 1024).strip() |
| 113 |
try: |
| 114 |
cmd = s[:s.find('(')] |
| 115 |
if not cmd in self.commands: |
| 116 |
print("Unknown command ", cmd) |
| 117 |
except ValueError: |
| 118 |
print("Malformed command") |
| 119 |
return True |
| 120 |
|
| 121 |
try: |
| 122 |
exec("self.player.%s" % s) |
| 123 |
except Exception as e: |
| 124 |
print(e) |
| 125 |
pass |
| 126 |
self.prompt() |
| 127 |
return True |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == '__main__': |
| 131 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 132 |
|
| 133 |
bus = dbus.SystemBus() |
| 134 |
|
| 135 |
if len(sys.argv) > 1: |
| 136 |
path = bluezutils.find_adapter(sys.argv[1]).object_path |
| 137 |
else: |
| 138 |
path = bluezutils.find_adapter().object_path |
| 139 |
|
| 140 |
media = dbus.Interface(bus.get_object("org.bluez", path), |
| 141 |
"org.bluez.Media1") |
| 142 |
|
| 143 |
path = "/test/player" |
| 144 |
player = Player(bus, path) |
| 145 |
mainloop = GObject.MainLoop() |
| 146 |
|
| 147 |
if len(sys.argv) > 2: |
| 148 |
player.set_object(sys.argv[2]) |
| 149 |
else: |
| 150 |
player.set_object() |
| 151 |
|
| 152 |
print('Register media player with:\n\tProperties: %s' \ |
| 153 |
% (player.properties)) |
| 154 |
|
| 155 |
media.RegisterPlayer(dbus.ObjectPath(path), player.properties) |
| 156 |
|
| 157 |
mainloop.run() |