diff options
| author | Huang Peng <shawn.p.huang@gmail.com> | 2008-08-13 10:13:33 +0800 |
|---|---|---|
| committer | Huang Peng <shawn.p.huang@gmail.com> | 2008-08-13 10:13:33 +0800 |
| commit | 180fd76c6ec30aa7b67d13237aaea4ca0fb7960d (patch) | |
| tree | b0267da63e998a7ecb75607dd79b2ef3f72cc75c /launcher | |
| parent | e9e8eae8b3aa14110680da08fa79ba71547bc2c3 (diff) | |
| download | ibus-180fd76c6ec30aa7b67d13237aaea4ca0fb7960d.tar.gz ibus-180fd76c6ec30aa7b67d13237aaea4ca0fb7960d.tar.xz ibus-180fd76c6ec30aa7b67d13237aaea4ca0fb7960d.zip | |
Refine ibus launcer code.
Diffstat (limited to 'launcher')
| -rw-r--r-- | launcher/ibus.in | 130 |
1 files changed, 82 insertions, 48 deletions
diff --git a/launcher/ibus.in b/launcher/ibus.in index fa285d8..77aaf91 100644 --- a/launcher/ibus.in +++ b/launcher/ibus.in @@ -1,4 +1,5 @@ #!/usr/bin/env python +# vim:set et sts=4 sw=4: # # ibus - The Input Bus # @@ -25,57 +26,90 @@ import atexit import sys import time import ibus +import dbus +import gtk +import signal daemon = "@prefix@/libexec/ibus-daemon" x11 = "@prefix@/libexec/ibus-x11" panel = "@prefix@/libexec/ibus-panel" conf = "@prefix@/libexec/ibus-gconf" -daemon_pid = 0 - -# logname = os.path.expanduser("~/.ibus-error") -# os.close(0) -# os.close(1) -# os.close(2) -# try: -# os.unlink(logname) -# except: -# pass -# fd = os.open(logname, os.O_CREAT | os.O_WRONLY) -# os.dup2(fd, 1) -# os.dup2(fd, 2) -bus = None - -try: - bus = ibus.Bus() -except: - pass -if bus: - print >> sys.stderr, "Found an ibus-daemon has been started!" - sys.exit(1) - -try: - print "start ibus-daemon" - daemon_pid = os.spawnv (os.P_NOWAIT, daemon, [daemon]) -except: - print >> sys.stderr, "start ibus-daemon failed" - sys.exit (1) - -time.sleep (1) - -try: - print "start ibus-x11" - x11_pid = os.spawnv (os.P_NOWAIT, x11, [x11, "--kill-daemon"]) -except: - print >> sys.stderr, "start ibus-x11 failed" - sys.exit (1) - -try: - print "start ibus-panel" - panel_pid = os.spawnv (os.P_NOWAIT, panel, [panel]) -except: - print >> sys.stderr, "start ibus-panel failed" - sys.exit (1) - -os.waitpid (daemon_pid, 0) -os.wait () + +class IBusLauncher(object): + def __init__(self): + super(IBusLauncher, self).__init__() + self.__daemon_pid = 0 + self.__panel_pid = 0 + self.__x11_pid = 0 + self.__bus = None + + def __start_process(self, path, args, cmd): + pid = 0 + try: + print "Starting %s" % cmd, + pid = os.spawnv (os.P_NOWAIT, path, args) + print "OK" + except: + print >> sys.stderr, "Start %s Failed" % cmd + return pid + + def __bus_destroy_cb(self, bus): + self.__bus = None + gtk.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 + elif pid == self.__x11_pid: + self.__x11_pid = 0 + elif pid == self.__panel_pid: + self.__panel_pid = 0 + + def __atexit_cb(self): + if self.__bus: + self.__bus.kill() + os.kill(-os.getpid(), signal.SIGTERM) + + def run(self): + try: + bus = ibus.Bus() + print >> sys.stderr, "Found an ibus-daemon has been started!" + sys.exit(1) + except dbus.DBusException, e: + pass + + # make self process group leader + if os.getpid() != os.getpgid(os.getpid()): + os.setpgid(0, 0) + + signal.signal(signal.SIGCHLD, self.__signal_cb) + + # start ibus-daemon + self.__daemon_pid = self.__start_process(daemon, [daemon], "ibus-daemon") + if self.__daemon_pid <= 0: + sys.exit(1) + + for i in range(5): + time.sleep (1) + try: + self.__bus = ibus.Bus() + break + except: + pass + + if self.__bus == None: + print >> sys.stderr, "Start ibus-daemon failed" + sys.exit(1) + + self.__bus.connect("destroy", self.__bus_destroy_cb) + + self.__x11_pid = self.__start_process(x11, [x11], "ibus-x11") + self.__panel_pid = self.__start_process(panel, [panel], "ibus-panel") + + gtk.main() + +if __name__ == "__main__": + IBusLauncher().run() |
