From 9e2c9217c60ca9c58b5071ec8a0da1a4abfef2d5 Mon Sep 17 00:00:00 2001 From: Huang Peng Date: Wed, 13 Aug 2008 11:04:32 +0800 Subject: [PATCH] Refine launcher code. --- ibus/common.py | 20 +++++++++++++++++++- launcher/ibus.in | 20 ++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/ibus/common.py b/ibus/common.py index e0a4561f..bd84862f 100644 --- a/ibus/common.py +++ b/ibus/common.py @@ -30,7 +30,9 @@ __all__ = ( "IBUS_PANEL_IFACE", "default_reply_handler", "default_error_handler", - "DEFAULT_ASYNC_HANDLERS" + "DEFAULT_ASYNC_HANDLERS", + "main", + "main_quit" ) import os @@ -76,3 +78,19 @@ DEFAULT_ASYNC_HANDLERS = { "reply_handler" : default_reply_handler, "error_handler" : default_error_handler } + +__mainloop = None + +def main(): + global __mainloop + if __mainloop == None: + import gobject + __mainloop = gobject.MainLoop() + + __mainloop.run() + +def main_quit(): + global __mainloop + if __mainloop: + __mainloop.quit() + diff --git a/launcher/ibus.in b/launcher/ibus.in index 77aaf91b..9db50d7e 100644 --- a/launcher/ibus.in +++ b/launcher/ibus.in @@ -27,7 +27,6 @@ import sys import time import ibus import dbus -import gtk import signal daemon = "@prefix@/libexec/ibus-daemon" @@ -56,21 +55,27 @@ class IBusLauncher(object): def __bus_destroy_cb(self, bus): self.__bus = None - gtk.main_quit() + ibus.main_quit() def __signal_cb(self, sig, stack): if sig == signal.SIGCHLD: pid, status = os.wait() if pid == self.__daemon_pid: self.__daemon_pid = 0 + self.__bus = None elif pid == self.__x11_pid: self.__x11_pid = 0 elif pid == self.__panel_pid: self.__panel_pid = 0 + elif sig == signal.SIGTERM or sig == signal.SIGINT: + sys.exit(1) def __atexit_cb(self): if self.__bus: - self.__bus.kill() + try: + self.__bus.kill() + except: + pass os.kill(-os.getpid(), signal.SIGTERM) def run(self): @@ -86,6 +91,9 @@ class IBusLauncher(object): os.setpgid(0, 0) signal.signal(signal.SIGCHLD, self.__signal_cb) + signal.signal(signal.SIGINT, self.__signal_cb) + signal.signal(signal.SIGTERM, self.__signal_cb) + atexit.register(self.__atexit_cb) # start ibus-daemon self.__daemon_pid = self.__start_process(daemon, [daemon], "ibus-daemon") @@ -106,10 +114,10 @@ class IBusLauncher(object): self.__bus.connect("destroy", self.__bus_destroy_cb) - self.__x11_pid = self.__start_process(x11, [x11], "ibus-x11") + self.__x11_pid = self.__start_process(x11, [x11, "--kill-daemon"], "ibus-x11") self.__panel_pid = self.__start_process(panel, [panel], "ibus-panel") - - gtk.main() + + ibus.main() if __name__ == "__main__": IBusLauncher().run() -- 2.34.1