| 1 | # Copyright (c) 2008-2009, samurai-x.org |
|---|
| 2 | # All rights reserved. |
|---|
| 3 | # |
|---|
| 4 | # Redistribution and use in source and binary forms, with or without |
|---|
| 5 | # modification, are permitted provided that the following conditions are met: |
|---|
| 6 | # * Redistributions of source code must retain the above copyright |
|---|
| 7 | # notice, this list of conditions and the following disclaimer. |
|---|
| 8 | # * Redistributions in binary form must reproduce the above copyright |
|---|
| 9 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 10 | # documentation and/or other materials provided with the distribution. |
|---|
| 11 | # * Neither the name of the samurai-x.org nor the |
|---|
| 12 | # names of its contributors may be used to endorse or promote products |
|---|
| 13 | # derived from this software without specific prior written permission. |
|---|
| 14 | # |
|---|
| 15 | # THIS SOFTWARE IS PROVIDED BY SAMURAI-X.ORG ``AS IS'' AND ANY |
|---|
| 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 17 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 18 | # DISCLAIMED. IN NO EVENT SHALL SAMURAI-X.ORG BE LIABLE FOR ANY |
|---|
| 19 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|---|
| 20 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 21 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|---|
| 22 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 24 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 25 | |
|---|
| 26 | """ |
|---|
| 27 | sx-gobject is a plugin that replaces the default samurai-x loop with a gobject based loop. |
|---|
| 28 | |
|---|
| 29 | """ |
|---|
| 30 | |
|---|
| 31 | import logging |
|---|
| 32 | log = logging.getLogger(__name__) |
|---|
| 33 | |
|---|
| 34 | import gobject |
|---|
| 35 | |
|---|
| 36 | from samuraix.plugin import Plugin |
|---|
| 37 | |
|---|
| 38 | def _make_handler(fn): |
|---|
| 39 | def h(source, cb_condition): |
|---|
| 40 | ret = fn() |
|---|
| 41 | if ret is None: |
|---|
| 42 | ret = True |
|---|
| 43 | return ret |
|---|
| 44 | return h |
|---|
| 45 | |
|---|
| 46 | class SXGObject(Plugin): |
|---|
| 47 | key = 'gobject' |
|---|
| 48 | |
|---|
| 49 | def __init__(self, app): |
|---|
| 50 | self.app = app |
|---|
| 51 | self.app.stop = lambda *args: self.mainloop.quit() |
|---|
| 52 | app.run = self.run |
|---|
| 53 | |
|---|
| 54 | def run(self): |
|---|
| 55 | app = self.app |
|---|
| 56 | app.running = True |
|---|
| 57 | |
|---|
| 58 | # process any events that are waiting first |
|---|
| 59 | while True: |
|---|
| 60 | try: |
|---|
| 61 | ev = app.conn.poll_for_event() |
|---|
| 62 | except Exception, e: |
|---|
| 63 | log.exception(e) |
|---|
| 64 | else: |
|---|
| 65 | if ev is None: |
|---|
| 66 | break |
|---|
| 67 | try: |
|---|
| 68 | ev.dispatch() |
|---|
| 69 | except Exception, e: |
|---|
| 70 | log.exception(e) |
|---|
| 71 | |
|---|
| 72 | #dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
|---|
| 73 | |
|---|
| 74 | #session_bus = dbus.SessionBus() |
|---|
| 75 | #name = dbus.service.BusName("com.example.SampleService", session_bus) |
|---|
| 76 | #object = SomeObject(session_bus, '/SomeObject') |
|---|
| 77 | |
|---|
| 78 | mainloop = self.mainloop = gobject.MainLoop() |
|---|
| 79 | for typ, cond in (('read', gobject.IO_IN), ('write', gobject.IO_OUT), ('error', gobject.IO_ERR)): |
|---|
| 80 | for fd in app.fds[typ].keys(): |
|---|
| 81 | gobject.io_add_watch(fd, cond, _make_handler(app.fds[typ][fd])) |
|---|
| 82 | mainloop.run() |
|---|
| 83 | |
|---|
| 84 | app.conn.disconnect() |
|---|
| 85 | |
|---|