f410ac21433f1959eb6ca95d80d90ee7e54865c7
[platform/upstream/ibus.git] / ibus / common.py
1 # vim:set et sts=4 sw=4:
2 #
3 # ibus - The Input Bus
4 #
5 # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA  02111-1307  USA
21
22 __all__ = (
23         "IBUS_ADDR",
24         "IBUS_IFACE",
25         "IBUS_NAME",
26         "IBUS_PATH",
27         "IBUS_CONFIG_IFACE",
28         "IBUS_ENGINE_FACTORY_IFACE",
29         "IBUS_ENGINE_IFACE",
30         "IBUS_PANEL_IFACE",
31         "default_reply_handler",
32         "default_error_handler",
33         "DEFAULT_ASYNC_HANDLERS",
34         "CONFIG_GENERAL_SHORTCUT_TRIGGER",
35         "CONFIG_GENERAL_SHORTCUT_NEXT_ENGINE",
36         "CONFIG_GENERAL_SHORTCUT_PREV_ENGINE",
37         "CONFIG_GENERAL_SHORTCUT_TRIGGER_DEFAULT",
38         "CONFIG_GENERAL_SHORTCUT_NEXT_ENGINE_DEFAULT",
39         "CONFIG_GENERAL_SHORTCUT_PREV_ENGINE_DEFAULT",
40         "main",
41         "main_quit",
42         "main_iteration"
43     )
44
45 import os
46 import sys
47
48 display = os.environ["DISPLAY"]
49 if "." not in display:
50     display += ".0" 
51
52 __username = None
53 try:
54     __username = os.getlogin()
55 except:
56     pass
57 if not __username:
58     __username = os.getenv ("LOGNAME")
59 if not __username:
60     __username = os.getenv ("USER")
61 if not __username:
62     __username = os.getenv ("LNAME")
63 if not __username:
64     __username = os.getenv ("USERNAME")
65
66 IBUS_ADDR = "unix:path=/tmp/ibus-%s/ibus-%s" % (__username, display.replace(":", "-"))
67 # IBUS_ADDR  = "tcp:host=localhost,port=7799"
68
69 IBUS_IFACE = "org.freedesktop.IBus"
70 IBUS_PATH  = "/org/freedesktop/IBus"
71 IBUS_NAME  = "org.freedesktop.IBus"
72
73 IBUS_CONFIG_IFACE = "org.freedesktop.ibus.Config"
74 IBUS_ENGINE_FACTORY_IFACE = "org.freedesktop.ibus.EngineFactory"
75 IBUS_ENGINE_IFACE = "org.freedesktop.ibus.Engine"
76 IBUS_PANEL_IFACE = "org.freedesktop.ibus.Panel"
77
78 def default_reply_handler( *args):
79     pass
80
81 def default_error_handler(e):
82     print >> sys.stderr, e
83
84 DEFAULT_ASYNC_HANDLERS = {
85     "reply_handler" : default_reply_handler,
86     "error_handler" : default_error_handler
87 }
88
89 CONFIG_GENERAL_SHORTCUT_TRIGGER     = "/general/keyboard_shortcut_trigger"
90 CONFIG_GENERAL_SHORTCUT_TRIGGER_DEFAULT = [
91     "Ctrl+space",
92     "Zenkaku_Hankaku",
93     "Hangul"]
94 CONFIG_GENERAL_SHORTCUT_NEXT_ENGINE = "/general/keyboard_shortcut_next_engine"
95 CONFIG_GENERAL_SHORTCUT_NEXT_ENGINE_DEFAULT = [
96     "Ctrl+Shift+Release+Shift_L",
97     "Ctrl+Shift+Release+Shift_R",
98     ]
99 CONFIG_GENERAL_SHORTCUT_PREV_ENGINE = "/general/keyboard_shortcut_prev_engine"
100 CONFIG_GENERAL_SHORTCUT_PREV_ENGINE_DEFAULT = []
101
102 __mainloop = None
103
104 def __init_main_loop():
105     global __mainloop
106     if __mainloop == None:
107         import gobject
108         __mainloop = gobject.MainLoop()
109
110 def main():
111     __init_main_loop()
112     __mainloop.run()
113
114 def main_quit():
115     global __mainloop
116     if __mainloop:
117         __mainloop.quit()
118     
119 def main_iteration(may_block=False):
120     __init_main_loop()
121     return __mainloop.get_context().iteration(may_block)