3 A client to a device given on the commnd line, that is activated whenever the
11 from dbus.mainloop.glib import DBusGMainLoop
12 from gi.repository import GObject
15 import onoff.dbusutils
17 class MpdWatcher(object):
18 def __init__(self, onoffproxy, mpdaddress=("localhost", 6600)):
19 self.mpdaddress = mpdaddress
20 self.mpdclient = mpd.MPDClient()
21 self.onoffproxy = onoffproxy
22 self.activatefd = None
23 self.statechanged = True
26 def handle_write(self, source, condition):
28 self.mpdclient.send_status()
30 self.mpdclient.send_idle()
31 GObject.io_add_watch(self.mpdclient, GObject.IO_IN, self.handle_read)
34 def handle_read(self, source, condition):
37 state = self.mpdclient.fetch_status()["state"]
38 self.statechanged = False
40 if self.activatefd is None:
41 st, fd = self.onoffproxy.activatefd()
42 self.activatefd = fd.take()
44 if self.activatefd is not None:
45 os.close(self.activatefd)
46 self.activatefd = None
48 changes = self.mpdclient.fetch_idle()
49 if "player" in changes:
50 self.statechanged = True
51 except mpd.ConnectionError:
54 GObject.io_add_watch(self.mpdclient, GObject.IO_OUT, self.handle_write)
59 self.mpdclient.disconnect()
60 except mpd.ConnectionError:
62 GObject.timeout_add(1000, self.do_connect)
66 self.mpdclient.connect(*self.mpdaddress)
70 self.statechanged = True
71 GObject.io_add_watch(self.mpdclient, GObject.IO_OUT, self.handle_write)
75 parser = argparse.ArgumentParser(parents=[onoff.dbusutils.dbus_options])
76 args = parser.parse_args()
77 DBusGMainLoop(set_as_default=True)
78 proxy = onoff.dbusutils.get_dbus_proxy(args)
79 watcher = MpdWatcher(proxy)
80 GObject.MainLoop().run()
82 if __name__ == "__main__":