summaryrefslogtreecommitdiffstats
path: root/launcher/ibus.in
blob: f4ac580b5fc009d3c22e1444df07356eaf3a24fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

import os
import os.path
import atexit
import sys
import time
import ibus
import dbus
import signal

daemon = "@prefix@/bin/ibus-daemon"
x11 = "@prefix@/bin/ibus-x11"
panel = "@prefix@/bin/ibus-panel"
conf = "@prefix@/bin/ibus-gconf"

class Launcher(object):
    def __init__(self):
        super(Launcher, self).__init__()
        self.__daemon_pid = 0
        self.__panel_pid = 0
        self.__conf_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
        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 pid == self.__conf_pid:
                self.__conf_pid = 0
        elif sig == signal.SIGTERM or sig == signal.SIGINT:
            sys.exit(1)

    def __atexit_cb(self):
        if self.__bus:
            try:
                self.__bus.kill()
            except:
                pass
        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)
        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")
        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.__conf_pid = self.__start_process(conf, [conf], "ibus-conf")
        self.__panel_pid = self.__start_process(panel, [panel], "ibus-panel")
        self.__x11_pid = self.__start_process(x11, [x11, "--kill-daemon"], "ibus-x11")
        time.sleep(1)
        while ibus.main_iteration(False):
            pass
        self.__launch_auto_load_engines()
        ibus.main()

    def __launch_auto_load_engines(self):
        engines = []
        try:
            engines = self.__bus.config_get_value("general", "preload_engines", None)
            if not engines:
                engines = []
        except:
            pass
        for e in engines:
            try:
                lang, name = e.split(":")
                self.__bus.register_start_engine(lang, name)
            except:
                import traceback
                traceback.print_exc()



if __name__ == "__main__":
    Launcher().run()