3 from __future__ import print_function
9 import dbus.mainloop.glib
11 from gi.repository import GObject
13 import gobject as GObject
16 class Player(dbus.service.Object):
20 def set_object(self, obj = None):
22 bus = dbus.SystemBus()
23 mp = dbus.Interface(bus.get_object("org.bluez", obj),
24 "org.bluez.MediaPlayer1")
25 prop = dbus.Interface(bus.get_object("org.bluez", obj),
26 "org.freedesktop.DBus.Properties")
28 self.properties = prop.GetAll("org.bluez.MediaPlayer1")
30 bus.add_signal_receiver(self.properties_changed,
32 dbus_interface = "org.freedesktop.DBus.Properties",
33 signal_name = "PropertiesChanged")
35 track = dbus.Dictionary({
36 "xesam:title" : "Title",
37 "xesam:artist" : ["Artist"],
38 "xesam:album" : "Album",
39 "xesam:genre" : ["Genre"],
40 "xesam:trackNumber" : dbus.Int32(1),
41 "mpris:length" : dbus.Int64(10000) },
44 self.properties = dbus.Dictionary({
45 "PlaybackStatus" : "playing",
46 "Identity" : "SimplePlayer",
47 "LoopStatus" : "None",
48 "Rate" : dbus.Double(1.0),
49 "Shuffle" : dbus.Boolean(False),
51 "Volume" : dbus.Double(1.0),
52 "Position" : dbus.Int64(0),
53 "MinimumRate" : dbus.Double(1.0),
54 "MaximumRate" : dbus.Double(1.0),
55 "CanGoNext" : dbus.Boolean(False),
56 "CanGoPrevious" : dbus.Boolean(False),
57 "CanPlay" : dbus.Boolean(False),
58 "CanSeek" : dbus.Boolean(False),
59 "CanControl" : dbus.Boolean(False),
63 handler = InputHandler(self)
64 GObject.io_add_watch(sys.stdin, GObject.IO_IN,
67 @dbus.service.method("org.freedesktop.DBus.Properties",
68 in_signature="ssv", out_signature="")
69 def Set(self, interface, key, value):
70 print("Set (%s, %s)" % (key, value), file=sys.stderr)
73 @dbus.service.signal("org.freedesktop.DBus.Properties",
75 def PropertiesChanged(self, interface, properties,
76 invalidated = dbus.Array()):
77 """PropertiesChanged(interface, properties, invalidated)
79 Send a PropertiesChanged signal. 'properties' is a dictionary
80 containing string parameters as specified in doc/media-api.txt.
85 help(self.__class__.__dict__[func])
87 def properties_changed(self, interface, properties, invalidated):
88 print("properties_changed(%s, %s)" % (properties, invalidated))
90 self.PropertiesChanged(interface, properties, invalidated)
93 commands = { 'PropertiesChanged': '(interface, properties)',
95 def __init__(self, player):
97 print('\n\nAvailable commands:')
98 for cmd in self.commands:
99 print('\t', cmd, self.commands[cmd], sep='')
101 print("\nUse python syntax to pass arguments to available methods.\n" \
102 "E.g.: PropertiesChanged({'Metadata' : {'Title': 'My title', \
103 'Album': 'my album' }})")
107 print('\n>>> ', end='')
110 def handle(self, fd, condition):
111 s = os.read(fd.fileno(), 1024).strip()
113 cmd = s[:s.find('(')]
114 if not cmd in self.commands:
115 print("Unknown command ", cmd)
117 print("Malformed command")
121 exec "self.player.%s" % s
122 except Exception as e:
129 if __name__ == '__main__':
130 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
132 bus = dbus.SystemBus()
134 if len(sys.argv) > 1:
135 path = bluezutils.find_adapter(sys.argv[1]).object_path
137 path = bluezutils.find_adapter().object_path
139 media = dbus.Interface(bus.get_object("org.bluez", path),
142 path = "/test/player"
143 player = Player(bus, path)
144 mainloop = GObject.MainLoop()
146 if len(sys.argv) > 2:
147 player.set_object(sys.argv[2])
151 print('Register media player with:\n\tProperties: %s' \
152 % (player.properties))
154 media.RegisterPlayer(dbus.ObjectPath(path), player.properties)