Refine launcher code.
authorHuang Peng <shawn.p.huang@gmail.com>
Wed, 13 Aug 2008 03:04:32 +0000 (11:04 +0800)
committerHuang Peng <shawn.p.huang@gmail.com>
Wed, 13 Aug 2008 03:04:32 +0000 (11:04 +0800)
ibus/common.py
launcher/ibus.in

index e0a4561f143a0f9125e69c9833edd12ffd1e4474..bd84862f31eb76e659c057e0158ceb21d3277cbb 100644 (file)
@@ -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()
+    
index 77aaf91b05f93f115b46041678c8e71a5b2c6c74..9db50d7e48417f9ed65d067b90b2ad4fc75d18fd 100644 (file)
@@ -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()