3 A client to a device given on the commnd line, that is activated whenever the
10 from dbus.mainloop.glib import DBusGMainLoop
11 from gi.repository import GObject
14 import onoff.dbusutils
16 class MpdWatcher(object):
17 def __init__(self, mpdclient, onoffproxy):
18 self.mpdclient = mpdclient
19 self.onoffproxy = onoffproxy
20 self.activatefd = None
22 self.mpdclient.send_idle()
23 GObject.io_add_watch(self.mpdclient, GObject.IO_IN, self.on_idle)
25 def on_idle(self, source, condition):
26 changes = self.mpdclient.fetch_idle()
27 if "player" in changes:
29 self.mpdclient.send_idle()
30 return True # want to be called again
32 def update_state(self):
33 state = self.mpdclient.status()["state"]
35 if self.activatefd is None:
36 st, fd = self.onoffproxy.activatefd(3)
37 self.activatefd = fd.take()
39 if self.activatefd is not None:
40 os.close(self.activatefd)
41 self.activatefd = None
44 parser = argparse.ArgumentParser(parents=[onoff.dbusutils.dbus_options])
45 args = parser.parse_args()
46 DBusGMainLoop(set_as_default=True)
47 proxy = onoff.dbusutils.get_dbus_proxy(args)
48 client = mpd.MPDClient()
49 client.connect("localhost", 6600)
50 watcher = MpdWatcher(client, proxy)
51 GObject.MainLoop().run()
53 if __name__ == "__main__":