change activatefd signature
[~helmut/onoff.git] / dbus_client.py
1 #!/usr/bin/env python
2
3 import os
4 import sys
5
6 import dbus
7 from dbus.mainloop.glib import DBusGMainLoop
8 from gi.repository import GObject
9
10 from onoff.common import ST_ACTIVE
11
12 def wait_for_signal(proxy, signal):
13     loop = GObject.MainLoop()
14     state = []
15     def callback(st):
16         state.append(st)
17         loop.quit()
18     proxy.connect_to_signal(signal, callback)
19     loop.run()
20     return state[0]
21
22 def main():
23     DBusGMainLoop(set_as_default=True)
24     bus = dbus.SessionBus()
25     proxy = bus.get_object("de.subdivi.onoff0", "/de/subdivi/onoff0/redshift")
26     if len(sys.argv) > 1:
27         st, fd = proxy.activatefd(10)
28         fd = fd.take()
29         os.dup2(fd, 254)
30         os.close(fd)
31         if st != ST_ACTIVE:
32             print("state is %d waiting for signal" % st)
33             st = wait_for_signal(proxy, "changestate")
34             print("new state is %d" % st)
35         os.execvp(sys.argv[1], sys.argv[1:])
36     else:
37         st = proxy.activatetime(10)
38         if st != ST_ACTIVE:
39             print("state is %d waiting for signal" % st)
40             st = wait_for_signal(proxy, "changestate")
41             print("new state is %d" % st)
42
43 if __name__ == "__main__":
44     main()