Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 1 | # Copyright (c) Benjamin Kietzman (github.com/bkietz) |
| 2 | # |
| 3 | # Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | from dbus.mainloop.glib import DBusGMainLoop |
| 9 | from gobject import MainLoop |
| 10 | |
| 11 | bus_name = 'com.example.Sample' |
| 12 | |
| 13 | class Example(dbus.service.Object): |
| 14 | def __init__(self, connection, path): |
| 15 | dbus.service.Object.__init__(self, connection, path) |
| 16 | self._last_input = None |
| 17 | |
| 18 | @dbus.service.method(bus_name+'.Iface', in_signature='v', out_signature='s') |
| 19 | def StringifyVariant(self, var): |
| 20 | self.LastInputChanged(var) # emits the signal |
| 21 | return str(var) |
| 22 | |
| 23 | @dbus.service.signal(bus_name+'.Iface', signature='v') |
| 24 | def LastInputChanged(self, var): |
| 25 | # run just before the signal is actually emitted |
| 26 | # just put "pass" if nothing should happen |
| 27 | self._last_input = var |
| 28 | |
| 29 | @dbus.service.method(bus_name+'.Iface', in_signature='', out_signature='v') |
| 30 | def GetLastInput(self): |
| 31 | return self._last_input |
| 32 | |
| 33 | bus = dbus.SessionBus(mainloop=DBusGMainLoop()) |
| 34 | bus.request_name(bus_name) |
| 35 | |
| 36 | example = Example(bus, '/path/to/obj') |
| 37 | |
| 38 | print bus.get_name_owner(bus_name) |
| 39 | MainLoop().run() |