2 # Test program for client APIs.
12 from ibus import keysyms
13 from ibus import modifier
18 self.__bus = ibus.Bus()
19 self.__ic_path = self.__bus.create_input_context("DemoTerm")
20 self.__ic = ibus.InputContext(self.__bus, self.__ic_path, True)
21 self.__ic.set_capabilities(7)
22 self.__ic.connect("commit-text", self.__commit_text_cb)
24 self.__ic.connect("update-preedit-text", self.__update_preedit_text_cb)
25 self.__ic.connect("show-preedit-text", self.__show_preedit_text_cb)
26 self.__ic.connect("hide-preedit-text", self.__hide_preedit_text_cb)
28 self.__ic.connect("update-auxiliary-text", self.__update_aux_text_cb)
29 self.__ic.connect("show-auxiliary-text", self.__show_aux_text_cb)
30 self.__ic.connect("hide-auxiliary-text", self.__hide_aux_text_cb)
32 self.__ic.connect("update-lookup-table", self.__update_lookup_table_cb)
33 self.__ic.connect("show-lookup-table", self.__show_lookup_table_cb)
34 self.__ic.connect("hide-lookup-table", self.__hide_lookup_table_cb)
35 glib.io_add_watch(0, glib.IO_IN, self.__stdin_cb)
36 # glib.timeout_add(500, self.__timeout_cb)
38 # self.__master_fd, self.__slave_fd = os.openpty()
41 self.__is_invalidate = False
43 self.__preedit_visible = False
44 self.__aux_string = None
45 self.__aux_string_visible = False
46 self.__lookup_table = None
47 self.__lookup_table_visible = False
49 # self.__old_sigwinch_cb = signal.signal(signal.SIGWINCH, self.__sigwinch_cb)
51 def __timeout_cb(self):
55 def __sigwinch_cb(self, a, b):
56 self.__old_sigwinch_cb(a, b)
59 def __init_curses(self):
60 self.__screen = curses.initscr()
63 self.__screen.keypad(1)
64 self.__screen.refresh()
65 self.__screen.nodelay(1)
66 self.__max_y, self.__max_x = self.__screen.getmaxyx()
67 self.__state_pad = curses.newpad(2, self.__max_x)
68 self.__state_pad.bkgd(' ', curses.A_REVERSE)
69 self.__state_pad.addstr(0, 0, "Press Ctrl + v to enable or disable input method")
70 self.__state_pad.refresh(0, 0, self.__max_y - 2, 0, self.__max_y, self.__max_x)
72 def __fini_curses(self):
77 def __stdin_cb(self, fd, condition):
78 while self.__process_input():
82 def __process_input(self):
83 c = self.__screen.getch()
90 if c == 22: # Ctrl + V => Ctrl + space
91 retval = self.__ic.process_key_event(keysyms.space, 0, modifier.CONTROL_MASK)
92 elif c == curses.KEY_BACKSPACE: # BackSpace
93 self.__ic.process_key_event(keysyms.BackSpace, 0, 0)
95 elif c == curses.KEY_ENTER:
96 self.__ic.process_key_event(keysyms.Enter, 0, 0)
97 elif c == curses.KEY_RESIZE:
101 retval = self.__ic.process_key_event(c, 0, 0)
104 backtrace.print_exc()
108 self.__screen.addstr(unichr(c).encode("utf-8"))
109 self.__screen.refresh()
112 def __commit_text_cb(self, ic, text):
113 self.__screen.addstr(text.text)
114 self.__screen.refresh()
116 def __update_preedit_text_cb(self, ic, text, cursor_pos, visible):
117 self.__preedit = text
118 self.__preedit_visible = visible
121 def __show_preedit_text_cb(self, ic):
122 if self.__preedit_visible:
124 self.__preedit_visible = True
127 def __hide_preedit_text_cb(self, ic):
128 if not self.__preedit_visible:
130 self.__preedit_visible = False
133 def __update_aux_text_cb(self, ic, text, visible):
134 self.__aux_string = text
135 self.__aux_string_visible = visible
138 def __show_aux_text_cb(self, ic):
139 if self.__aux_string_visible:
141 self.__aux_string_visible = True
144 def __hide_aux_text_cb(self, ic):
145 if not self.__aux_string_visible:
147 self.__aux_string_visible = False
151 def __update_lookup_table_cb(self, ic, lookup_table, visible):
152 self.__lookup_table = lookup_table
153 self.__lookup_table_visible = visible
156 def __show_lookup_table_cb(self, ic):
157 if self.__lookup_table_visible:
159 self.__lookup_table_visible = True
162 def __hide_lookup_table_cb(self, ic):
163 if not self.__lookup_table_visible:
165 self.__lookup_table_visible = False
168 def __invalidate(self):
169 if self.__is_invalidate:
171 self.__is_invalidate = True
172 glib.idle_add(self.__update)
175 if not self.__is_invalidate:
177 self.__is_invalidate = False
179 y, x = self.__screen.getmaxyx()
180 if self.__max_x != x or self.__max_y != y:
183 self.__state_pad = curses.newpad(2, self.__max_x)
184 self.__state_pad.bkgd(' ', curses.A_REVERSE)
185 self.__state_pad.addstr(0, 0, "Press Ctrl + v to enable or disable input method")
186 self.__screen.clear()
188 self.__state_pad.clear()
191 if self.__preedit_visible and self.__preedit:
192 self.__state_pad.addstr(0, 0, self.__preedit.text, curses.A_REVERSE)
194 self.__state_pad.addstr(0, 0, "", curses.A_REVERSE)
197 if self.__aux_string_visible and self.__aux_string:
198 self.__state_pad.addstr(" ", curses.A_REVERSE)
199 self.__state_pad.addstr(self.__aux_string.text, curses.A_REVERSE)
200 self.__state_pad.addstr(1, 0, "", curses.A_REVERSE)
202 # update lookup table
203 if self.__lookup_table_visible and self.__lookup_table:
204 candidates = self.__lookup_table.get_candidates_in_current_page()
205 for i, c in enumerate(candidates):
206 text = u"%d.%s " % (i + 1, c.text)
207 self.__state_pad.addstr(text.encode("utf-8"), curses.A_REVERSE)
210 if self.__preedit_visible == False and self.__aux_string_visible == False and self.__lookup_table_visible == False:
211 self.__state_pad.addstr(0, 0, "Press Ctrl + v to enable or disable input method")
213 self.__state_pad.refresh(0, 0, self.__max_y - 2, 0, self.__max_y, self.__max_x)
214 self.__screen.refresh()
218 def __run_shell(self):
224 os.close(self.__master_fd)
225 os.dup2(self.__slave_fd, 0)
226 os.dup2(self.__slave_fd, 1)
227 os.dup2(self.__slave_fd, 2)
228 os.close(self.__slave_fd)
229 os.execv('/bin/bash', ["bash"])
235 self.__loop = glib.MainLoop()
243 locale.setlocale(locale.LC_ALL, "")
244 old = termios.tcgetattr(0)
250 termios.tcsetattr(0, termios.TCSAFLUSH, old)
252 traceback.print_exc()
255 termios.tcsetattr(0, termios.TCSAFLUSH, old)
260 if __name__ == "__main__":