Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: ui/base/ime/input_method_ibus.cc

Issue 8659033: IME (input method editor) support for Aura, part 1 of 3: Add ui/base/ime/ classes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cherry-picked patchset #9 and #10 in http://codereview.chromium.org/8576005/ Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/base/ime/input_method_ibus.h"
6
7 #include <ibus.h>
8
9 #include <X11/X.h>
10 #include <X11/Xlib.h>
11 #include <X11/Xutil.h>
12
13 #include <algorithm>
14 #include <cstring>
15 #include <set>
16 #include <vector>
17
18 #include "base/basictypes.h"
19 #include "base/i18n/char_iterator.h"
20 #include "base/logging.h"
21 #include "base/string_util.h"
22 #include "base/third_party/icu/icu_utf.h"
23 #include "base/utf_string_conversions.h"
24 #include "ui/base/events.h"
25 #include "ui/base/ime/text_input_client.h"
26 #include "ui/base/keycodes/keyboard_codes.h"
27 #include "ui/base/keycodes/keyboard_code_conversion.h"
28 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
29 #include "ui/gfx/rect.h"
30
31 namespace {
32
33 XKeyEvent* GetKeyEvent(XEvent* event) {
34 DCHECK(event && (event->type == KeyPress || event->type == KeyRelease));
35 return &event->xkey;
36 }
37
38 // Converts ibus key state flags to event flags.
39 int EventFlagsFromIBusState(guint32 state) {
40 return (state & IBUS_LOCK_MASK ? ui::EF_CAPS_LOCK_DOWN : 0) |
41 (state & IBUS_CONTROL_MASK ? ui::EF_CONTROL_DOWN : 0) |
42 (state & IBUS_SHIFT_MASK ? ui::EF_SHIFT_DOWN : 0) |
43 (state & IBUS_MOD1_MASK ? ui::EF_ALT_DOWN : 0) |
44 (state & IBUS_BUTTON1_MASK ? ui::EF_LEFT_BUTTON_DOWN : 0) |
45 (state & IBUS_BUTTON2_MASK ? ui::EF_MIDDLE_BUTTON_DOWN : 0) |
46 (state & IBUS_BUTTON3_MASK ? ui::EF_RIGHT_BUTTON_DOWN : 0);
47 }
48
49 // Converts X flags to ibus key state flags.
50 guint32 IBusStateFromXFlags(unsigned int flags) {
51 return (flags & LockMask ? IBUS_LOCK_MASK : 0U) |
52 (flags & ControlMask ? IBUS_CONTROL_MASK : 0U) |
53 (flags & ShiftMask ? IBUS_SHIFT_MASK : 0U) |
54 (flags & Mod1Mask ? IBUS_MOD1_MASK : 0U) |
55 (flags & Button1Mask ? IBUS_BUTTON1_MASK : 0U) |
56 (flags & Button2Mask ? IBUS_BUTTON2_MASK : 0U) |
57 (flags & Button3Mask ? IBUS_BUTTON3_MASK : 0U);
58 }
59
60 // Converts X flags to event flags.
61 guint32 EventFlagsFromXFlags(unsigned int flags) {
62 return EventFlagsFromIBusState(IBusStateFromXFlags(flags));
63 }
64
65 void IBusKeyEventFromNativeKeyEvent(const base::NativeEvent& native_event,
66 guint32* ibus_keyval,
67 guint32* ibus_keycode,
68 guint32* ibus_state) {
69 DCHECK(native_event); // A fabricated event is not supported here.
70 XKeyEvent* x_key = GetKeyEvent(native_event);
71
72 // Yes, ibus uses X11 keysym. We cannot use XLookupKeysym(), which doesn't
73 // translate Shift and CapsLock states.
74 KeySym keysym = NoSymbol;
75 ::XLookupString(x_key, NULL, 0, &keysym, NULL);
76 *ibus_keyval = keysym;
77 *ibus_keycode = x_key->keycode;
78 *ibus_state = IBusStateFromXFlags(x_key->state);
79 if (native_event->type == KeyRelease)
80 *ibus_state |= IBUS_RELEASE_MASK;
81 }
82
83 void ExtractCompositionTextFromIBusPreedit(IBusText* text,
84 guint cursor_position,
85 ui::CompositionText* composition) {
86 composition->Clear();
87 composition->text = UTF8ToUTF16(text->text);
88
89 if (composition->text.empty())
90 return;
91
92 // ibus uses character index for cursor position and attribute range, but we
93 // use char16 offset for them. So we need to do conversion here.
94 std::vector<size_t> char16_offsets;
95 size_t length = composition->text.length();
96 base::i18n::UTF16CharIterator char_iterator(&composition->text);
97 do {
98 char16_offsets.push_back(char_iterator.array_pos());
99 } while (char_iterator.Advance());
100
101 // The text length in Unicode characters.
102 guint char_length = static_cast<guint>(char16_offsets.size());
103 // Make sure we can convert the value of |char_length| as well.
104 char16_offsets.push_back(length);
105
106 size_t cursor_offset =
107 char16_offsets[std::min(char_length, cursor_position)];
108
109 composition->selection = ui::Range(cursor_offset);
110 if (text->attrs) {
111 guint i = 0;
112 while (true) {
113 IBusAttribute* attr = ibus_attr_list_get(text->attrs, i++);
114 if (!attr)
115 break;
116 if (attr->type != IBUS_ATTR_TYPE_UNDERLINE &&
117 attr->type != IBUS_ATTR_TYPE_BACKGROUND) {
118 continue;
119 }
120 guint start = std::min(char_length, attr->start_index);
121 guint end = std::min(char_length, attr->end_index);
122 if (start >= end)
123 continue;
124 ui::CompositionUnderline underline(
125 char16_offsets[start], char16_offsets[end],
126 SK_ColorBLACK, false /* thick */);
127 if (attr->type == IBUS_ATTR_TYPE_BACKGROUND) {
128 underline.thick = true;
129 // If the cursor is at start or end of this underline, then we treat
130 // it as the selection range as well, but make sure to set the cursor
131 // position to the selection end.
132 if (underline.start_offset == cursor_offset) {
133 composition->selection.set_start(underline.end_offset);
134 composition->selection.set_end(cursor_offset);
135 } else if (underline.end_offset == cursor_offset) {
136 composition->selection.set_start(underline.start_offset);
137 composition->selection.set_end(cursor_offset);
138 }
139 } else if (attr->type == IBUS_ATTR_TYPE_UNDERLINE) {
140 if (attr->value == IBUS_ATTR_UNDERLINE_DOUBLE)
141 underline.thick = true;
142 else if (attr->value == IBUS_ATTR_UNDERLINE_ERROR)
143 underline.color = SK_ColorRED;
144 }
145 composition->underlines.push_back(underline);
146 }
147 }
148
149 // Use a black thin underline by default.
150 if (composition->underlines.empty()) {
151 composition->underlines.push_back(ui::CompositionUnderline(
152 0, length, SK_ColorBLACK, false /* thick */));
153 }
154 }
155
156 } // namespace
157
158 namespace ui {
159
160 // InputMethodIBus::PendingKeyEvent implementation ------------------------
161 class InputMethodIBus::PendingKeyEvent {
162 public:
163 PendingKeyEvent(InputMethodIBus* input_method,
164 const base::NativeEvent& native_event,
165 guint32 ibus_keyval);
166 ~PendingKeyEvent();
167
168 // Abandon this pending key event. Its result will just be discarded.
169 void Abandon() { input_method_ = NULL; }
170
171 InputMethodIBus* input_method() const { return input_method_; }
172
173 // Process this pending key event after we receive its result from the input
174 // method. It just call through InputMethodIBus::ProcessKeyEventPostIME().
175 void ProcessPostIME(bool handled);
176
177 private:
178 InputMethodIBus* input_method_;
179
180 // TODO(yusukes): To support a fabricated key event (which is typically from
181 // a virtual keyboard), we might have to copy event type, event flags, key
182 // code, 'character_', and 'unmodified_character_'. See views::InputMethodIBus
183 // for details.
184
185 // corresponding XEvent data of a key event. It's a plain struct so we can do
186 // bitwise copy.
187 XKeyEvent x_event_;
188
189 const guint32 ibus_keyval_;
190
191 DISALLOW_COPY_AND_ASSIGN(PendingKeyEvent);
192 };
193
194 InputMethodIBus::PendingKeyEvent::PendingKeyEvent(
195 InputMethodIBus* input_method,
196 const base::NativeEvent& native_event,
197 guint32 ibus_keyval)
198 : input_method_(input_method),
199 ibus_keyval_(ibus_keyval) {
200 DCHECK(input_method_);
201
202 // TODO(yusukes): Support non-native event (from e.g. a virtual keyboard).
203 DCHECK(native_event);
204 x_event_ = *GetKeyEvent(native_event);
205 }
206
207 InputMethodIBus::PendingKeyEvent::~PendingKeyEvent() {
208 if (input_method_)
209 input_method_->FinishPendingKeyEvent(this);
210 }
211
212 void InputMethodIBus::PendingKeyEvent::ProcessPostIME(bool handled) {
213 if (!input_method_)
214 return;
215
216 if (x_event_.type == KeyPress || x_event_.type == KeyRelease) {
217 input_method_->ProcessKeyEventPostIME(reinterpret_cast<XEvent*>(&x_event_),
218 ibus_keyval_,
219 handled);
220 return;
221 }
222
223 // TODO(yusukes): Support non-native event (from e.g. a virtual keyboard).
224 // See views::InputMethodIBus for details. Never forget to set 'character_'
225 // and 'unmodified_character_' to support i18n VKs like a French VK!
226 }
227
228 // InputMethodIBus::PendingCreateICRequest implementation -----------------
229 class InputMethodIBus::PendingCreateICRequest {
230 public:
231 PendingCreateICRequest(InputMethodIBus* input_method,
232 PendingCreateICRequest** request_ptr);
233 ~PendingCreateICRequest();
234
235 // Abandon this pending key event. Its result will just be discarded.
236 void Abandon() {
237 input_method_ = NULL;
238 request_ptr_ = NULL;
239 }
240
241 // Stores the result input context to |input_method_|, or abandon it if
242 // |input_method_| is NULL.
243 void StoreOrAbandonInputContext(IBusInputContext* ic);
244
245 private:
246 InputMethodIBus* input_method_;
247 PendingCreateICRequest** request_ptr_;
248 };
249
250 InputMethodIBus::PendingCreateICRequest::PendingCreateICRequest(
251 InputMethodIBus* input_method,
252 PendingCreateICRequest** request_ptr)
253 : input_method_(input_method),
254 request_ptr_(request_ptr) {
255 }
256
257 InputMethodIBus::PendingCreateICRequest::~PendingCreateICRequest() {
258 if (request_ptr_) {
259 DCHECK_EQ(*request_ptr_, this);
260 *request_ptr_ = NULL;
261 }
262 }
263
264 void InputMethodIBus::PendingCreateICRequest::StoreOrAbandonInputContext(
265 IBusInputContext* ic) {
266 if (input_method_) {
267 input_method_->SetContext(ic);
268 } else {
269 // ibus_proxy_destroy() will not really release the object, we still need
270 // to call g_object_unref() explicitly.
271 ibus_proxy_destroy(reinterpret_cast<IBusProxy *>(ic));
272 g_object_unref(ic);
273 }
274 }
275
276 // InputMethodIBus implementation -----------------------------------------
277 InputMethodIBus::InputMethodIBus(
278 internal::InputMethodDelegate* delegate)
279 : context_(NULL),
280 pending_create_ic_request_(NULL),
281 context_focused_(false),
282 composing_text_(false),
283 composition_changed_(false),
284 suppress_next_result_(false) {
285 set_delegate(delegate);
286 }
287
288 InputMethodIBus::~InputMethodIBus() {
289 AbandonAllPendingKeyEvents();
290 DestroyContext();
291
292 // Disconnect bus signals
293 g_signal_handlers_disconnect_by_func(
294 GetIBus(), reinterpret_cast<gpointer>(OnIBusConnectedThunk), this);
295 g_signal_handlers_disconnect_by_func(
296 GetIBus(), reinterpret_cast<gpointer>(OnIBusDisconnectedThunk), this);
297 }
298
299 void InputMethodIBus::OnFocus() {
300 InputMethodBase::OnFocus();
301 UpdateContextFocusState();
302 }
303
304 void InputMethodIBus::OnBlur() {
305 ConfirmCompositionText();
306 InputMethodBase::OnBlur();
307 UpdateContextFocusState();
308 }
309
310 void InputMethodIBus::Init(const base::NativeWindow& system_toplevel_window) {
311 // Initializes the connection to ibus daemon. It may happen asynchronously,
312 // and as soon as the connection is established, the |context_| will be
313 // created automatically.
314 IBusBus* bus = GetIBus();
315
316 // connect bus signals
317 g_signal_connect(bus, "connected",
318 G_CALLBACK(OnIBusConnectedThunk), this);
319 g_signal_connect(bus, "disconnected",
320 G_CALLBACK(OnIBusDisconnectedThunk), this);
321
322 // Creates the |context_| if the connection is already established. In such
323 // case, we will not get "connected" signal.
324 if (ibus_bus_is_connected(bus))
325 CreateContext();
326
327 InputMethodBase::Init(system_toplevel_window);
328 }
329
330 void InputMethodIBus::DispatchKeyEvent(const base::NativeEvent& native_event) {
331 DCHECK(native_event && (native_event->type == KeyPress ||
332 native_event->type == KeyRelease));
333 DCHECK(system_toplevel_window_focused());
334
335 guint32 ibus_keyval = 0;
336 guint32 ibus_keycode = 0;
337 guint32 ibus_state = 0;
338 IBusKeyEventFromNativeKeyEvent(
339 native_event, &ibus_keyval, &ibus_keycode, &ibus_state);
340
341 // If |context_| is not usable, then we can only dispatch the key event as is.
342 // We also dispatch the key event directly if the current text input type is
343 // TEXT_INPUT_TYPE_PASSWORD, to bypass the input method.
344 // Note: We need to send the key event to ibus even if the |context_| is not
345 // enabled, so that ibus can have a chance to enable the |context_|.
346 if (!context_focused_ ||
347 GetTextInputType() == TEXT_INPUT_TYPE_PASSWORD) {
348 if (native_event->type == KeyPress)
349 ProcessUnfilteredKeyPressEvent(native_event, ibus_keyval);
350 else
351 DispatchKeyEventPostIME(native_event);
352 return;
353 }
354
355 PendingKeyEvent* pending_key =
356 new PendingKeyEvent(this, native_event, ibus_keyval);
357 pending_key_events_.insert(pending_key);
358
359 // Note:
360 // 1. We currently set timeout to -1, because ibus doesn't have a mechanism to
361 // associate input method results to corresponding key event, thus there is
362 // actually no way to abandon results generated by a specific key event. So we
363 // actually cannot abandon a specific key event and its result but accept
364 // following key events and their results. So a timeout to abandon a key event
365 // will not work.
366 // 2. We set GCancellable to NULL, because the operation of cancelling a async
367 // request also happens asynchronously, thus it's actually useless to us.
368 //
369 // The fundemental problem of ibus' async API is: it uses Glib's GIO API to
370 // realize async communication, but in fact, GIO API is specially designed for
371 // concurrent tasks, though it supports async communication as well, the API
372 // is much more complicated than an ordinary message based async communication
373 // API (such as Chrome's IPC).
374 // Thus it's very complicated, if not impossible, to implement a client that
375 // fully utilize asynchronous communication without potential problem.
376 ibus_input_context_process_key_event_async(
377 context_,
378 ibus_keyval, ibus_keycode, ibus_state, -1, NULL,
379 reinterpret_cast<GAsyncReadyCallback>(ProcessKeyEventDone),
380 pending_key);
381
382 // We don't want to suppress the result generated by this key event, but it
383 // may cause problem. See comment in ResetContext() method.
384 suppress_next_result_ = false;
385 }
386
387 void InputMethodIBus::OnTextInputTypeChanged(const TextInputClient* client) {
388 if (context_ && IsTextInputClientFocused(client)) {
389 ResetContext();
390 UpdateContextFocusState();
391 }
392 InputMethodBase::OnTextInputTypeChanged(client);
393 }
394
395 void InputMethodIBus::OnCaretBoundsChanged(const TextInputClient* client) {
396 if (!context_focused_ || !IsTextInputClientFocused(client))
397 return;
398
399 // The current text input type should not be NONE if |context_| is focused.
400 DCHECK(!IsTextInputTypeNone());
401 const gfx::Rect rect = GetTextInputClient()->GetCaretBounds();
402
403 // This function runs asynchronously.
404 ibus_input_context_set_cursor_location(
405 context_, rect.x(), rect.y(), rect.width(), rect.height());
406 }
407
408 void InputMethodIBus::CancelComposition(const TextInputClient* client) {
409 if (context_focused_ && IsTextInputClientFocused(client))
410 ResetContext();
411 }
412
413 std::string InputMethodIBus::GetInputLocale() {
414 // Not supported.
415 return "";
416 }
417
418 base::i18n::TextDirection InputMethodIBus::GetInputTextDirection() {
419 // Not supported.
420 return base::i18n::UNKNOWN_DIRECTION;
421 }
422
423 bool InputMethodIBus::IsActive() {
424 return true;
425 }
426
427 void InputMethodIBus::OnWillChangeFocusedClient(
428 TextInputClient* focused_before, TextInputClient* focused) {
429 ConfirmCompositionText();
430 }
431
432 void InputMethodIBus::OnDidChangeFocusedClient(
433 TextInputClient* focused_before, TextInputClient* focused) {
434 // Force to update the input type since client's TextInputStateChanged()
435 // function might not be called if text input types before the client loses
436 // focus and after it acquires focus again are the same.
437 OnTextInputTypeChanged(focused);
438
439 UpdateContextFocusState();
440 // Force to update caret bounds, in case the client thinks that the caret
441 // bounds has not changed.
442 if (context_focused_)
443 OnCaretBoundsChanged(focused);
444 }
445
446 void InputMethodIBus::CreateContext() {
447 DCHECK(!context_);
448 DCHECK(GetIBus());
449 DCHECK(ibus_bus_is_connected(GetIBus()));
450 DCHECK(!pending_create_ic_request_);
451
452 // Creates the input context asynchronously.
453 pending_create_ic_request_ = new PendingCreateICRequest(
454 this, &pending_create_ic_request_);
455 ibus_bus_create_input_context_async(
456 GetIBus(), "chrome", -1, NULL,
457 reinterpret_cast<GAsyncReadyCallback>(CreateInputContextDone),
458 pending_create_ic_request_);
459 }
460
461 void InputMethodIBus::SetContext(IBusInputContext* ic) {
462 DCHECK(ic);
463 DCHECK(!context_);
464 context_ = ic;
465
466 // connect input context signals
467 g_signal_connect(ic, "commit-text",
468 G_CALLBACK(OnCommitTextThunk), this);
469 g_signal_connect(ic, "forward-key-event",
470 G_CALLBACK(OnForwardKeyEventThunk), this);
471 g_signal_connect(ic, "update-preedit-text",
472 G_CALLBACK(OnUpdatePreeditTextThunk), this);
473 g_signal_connect(ic, "show-preedit-text",
474 G_CALLBACK(OnShowPreeditTextThunk), this);
475 g_signal_connect(ic, "hide-preedit-text",
476 G_CALLBACK(OnHidePreeditTextThunk), this);
477 g_signal_connect(ic, "destroy",
478 G_CALLBACK(OnDestroyThunk), this);
479
480 // TODO(suzhe): support surrounding text.
481 guint32 caps = IBUS_CAP_PREEDIT_TEXT | IBUS_CAP_FOCUS;
482 ibus_input_context_set_capabilities(ic, caps);
483
484 UpdateContextFocusState();
485 OnInputMethodChanged();
486 }
487
488 void InputMethodIBus::DestroyContext() {
489 if (pending_create_ic_request_) {
490 DCHECK(!context_);
491 // |pending_create_ic_request_| will be deleted in CreateInputContextDone().
492 pending_create_ic_request_->Abandon();
493 pending_create_ic_request_ = NULL;
494 } else if (context_) {
495 // ibus_proxy_destroy() will not really release the resource of |context_|
496 // object. We still need to handle "destroy" signal and call
497 // g_object_unref() there.
498 ibus_proxy_destroy(reinterpret_cast<IBusProxy *>(context_));
499 DCHECK(!context_);
500 }
501 }
502
503 void InputMethodIBus::ConfirmCompositionText() {
504 TextInputClient* client = GetTextInputClient();
505 if (client && client->HasCompositionText())
506 client->ConfirmCompositionText();
507
508 ResetContext();
509 }
510
511 void InputMethodIBus::ResetContext() {
512 if (!context_focused_ || !GetTextInputClient())
513 return;
514
515 DCHECK(system_toplevel_window_focused());
516
517 // Because ibus runs in asynchronous mode, the input method may still send us
518 // results after sending out the reset request, so we use a flag to discard
519 // all results generated by previous key events. But because ibus does not
520 // have a mechanism to identify each key event and corresponding results, this
521 // approach will not work for some corner cases. For example if the user types
522 // very fast, then the next key event may come in before the |context_| is
523 // really reset. Then we actually cannot know whether or not the next
524 // result should be discard.
525 suppress_next_result_ = true;
526
527 composition_.Clear();
528 result_text_.clear();
529 composing_text_ = false;
530 composition_changed_ = false;
531
532 // We need to abandon all pending key events, but as above comment says, there
533 // is no reliable way to abandon all results generated by these abandoned key
534 // events.
535 AbandonAllPendingKeyEvents();
536
537 // This function runs asynchronously.
538 // Note: some input method engines may not support reset method, such as
539 // ibus-anthy. But as we control all input method engines by ourselves, we can
540 // make sure that all of the engines we are using support it correctly.
541 ibus_input_context_reset(context_);
542
543 character_composer_.Reset();
544 }
545
546 void InputMethodIBus::UpdateContextFocusState() {
547 if (!context_) {
548 context_focused_ = false;
549 return;
550 }
551
552 const bool old_context_focused = context_focused_;
553 // Use switch here in case we are going to add more text input types.
554 switch (GetTextInputType()) {
555 case TEXT_INPUT_TYPE_NONE:
556 case TEXT_INPUT_TYPE_PASSWORD:
557 context_focused_ = false;
558 break;
559 default:
560 context_focused_ = true;
561 break;
562 }
563
564 // We only focus in |context_| when the focus is in a normal textfield.
565 // ibus_input_context_focus_{in|out}() run asynchronously.
566 if (old_context_focused && !context_focused_)
567 ibus_input_context_focus_out(context_);
568 else if (!old_context_focused && context_focused_)
569 ibus_input_context_focus_in(context_);
570 }
571
572 void InputMethodIBus::ProcessKeyEventPostIME(
573 const base::NativeEvent& native_event,
574 guint32 ibus_keyval,
575 bool handled) {
576 TextInputClient* client = GetTextInputClient();
577
578 if (!client) {
579 // As ibus works asynchronously, there is a chance that the focused client
580 // loses focus before this method gets called.
581 DispatchKeyEventPostIME(native_event);
582 return;
583 }
584
585 if (native_event->type == KeyPress && handled)
586 ProcessFilteredKeyPressEvent(native_event);
587
588 // In case the focus was changed by the key event. The |context_| should have
589 // been reset when the focused window changed.
590 if (client != GetTextInputClient())
591 return;
592
593 if (HasInputMethodResult())
594 ProcessInputMethodResult(native_event, handled);
595
596 // In case the focus was changed when sending input method results to the
597 // focused window.
598 if (client != GetTextInputClient())
599 return;
600
601 if (native_event->type == KeyPress && !handled)
602 ProcessUnfilteredKeyPressEvent(native_event, ibus_keyval);
603 else if (native_event->type == KeyRelease)
604 DispatchKeyEventPostIME(native_event);
605 }
606
607 void InputMethodIBus::ProcessFilteredKeyPressEvent(
608 const base::NativeEvent& native_event) {
609 if (NeedInsertChar())
610 DispatchKeyEventPostIME(native_event);
611 else
612 DispatchFabricatedKeyEventPostIME(
613 ET_KEY_PRESSED,
614 VKEY_PROCESSKEY,
615 EventFlagsFromXFlags(GetKeyEvent(native_event)->state));
616 }
617
618 void InputMethodIBus::ProcessUnfilteredKeyPressEvent(
619 const base::NativeEvent& native_event, guint32 ibus_keyval) {
620 // For a fabricated event, ProcessUnfilteredFabricatedKeyPressEvent should be
621 // called instead.
622 DCHECK(native_event);
623
624 TextInputClient* client = GetTextInputClient();
625 DispatchKeyEventPostIME(native_event);
626
627 // We shouldn't dispatch the character anymore if the key event dispatch
628 // caused focus change. For example, in the following scenario,
629 // 1. visit a web page which has a <textarea>.
630 // 2. click Omnibox.
631 // 3. enable Korean IME, press A, then press Tab to move the focus to the web
632 // page.
633 // We should return here not to send the Tab key event to RWHV.
634 if (client != GetTextInputClient())
635 return;
636
637 // Process compose and dead keys
638 if (character_composer_.FilterKeyPress(ibus_keyval)) {
639 string16 composed = character_composer_.composed_character();
640 if (!composed.empty()) {
641 client = GetTextInputClient();
642 if (client)
643 client->InsertText(composed);
644 }
645 return;
646 }
647
648 // If a key event was not filtered by |context_| and |character_composer_|,
649 // then it means the key event didn't generate any result text. So we need
650 // to send corresponding character to the focused text input client.
651 client = GetTextInputClient();
652
653 const uint32 state =
654 EventFlagsFromXFlags(GetKeyEvent(native_event)->state);
655 uint16 ch = ui::DefaultSymbolFromXEvent(native_event);
656 if (!ch) {
657 ch = ui::GetCharacterFromKeyCode(
658 ui::KeyboardCodeFromNative(native_event), state);
659 }
660
661 if (client && ch)
662 client->InsertChar(ch, state);
663 }
664
665 void InputMethodIBus::ProcessUnfilteredFabricatedKeyPressEvent(
666 EventType type, KeyboardCode key_code, int flags, guint32 ibus_keyval) {
667 TextInputClient* client = GetTextInputClient();
668 DispatchFabricatedKeyEventPostIME(type, key_code, flags);
669
670 if (client != GetTextInputClient())
671 return;
672
673 if (character_composer_.FilterKeyPress(ibus_keyval)) {
674 string16 composed = character_composer_.composed_character();
675 if (!composed.empty()) {
676 client = GetTextInputClient();
677 if (client)
678 client->InsertText(composed);
679 }
680 return;
681 }
682
683 client = GetTextInputClient();
684 const uint16 ch = ui::GetCharacterFromKeyCode(key_code, flags);
685 if (client && ch)
686 client->InsertChar(ch, flags);
687 }
688
689 void InputMethodIBus::ProcessInputMethodResult(
690 const base::NativeEvent& native_event, bool handled) {
691 TextInputClient* client = GetTextInputClient();
692 DCHECK(client);
693
694 if (result_text_.length()) {
695 if (handled && NeedInsertChar()) {
696 const uint32 state =
697 EventFlagsFromXFlags(GetKeyEvent(native_event)->state);
698 for (string16::const_iterator i = result_text_.begin();
699 i != result_text_.end(); ++i) {
700 client->InsertChar(*i, state);
701 }
702 } else {
703 client->InsertText(result_text_);
704 composing_text_ = false;
705 }
706 }
707
708 if (composition_changed_ && !IsTextInputTypeNone()) {
709 if (composition_.text.length()) {
710 composing_text_ = true;
711 client->SetCompositionText(composition_);
712 } else if (result_text_.empty()) {
713 client->ClearCompositionText();
714 }
715 }
716
717 // We should not clear composition text here, as it may belong to the next
718 // composition session.
719 result_text_.clear();
720 composition_changed_ = false;
721 }
722
723 bool InputMethodIBus::NeedInsertChar() const {
724 return GetTextInputClient() &&
725 (IsTextInputTypeNone() ||
726 (!composing_text_ && result_text_.length() == 1));
727 }
728
729 bool InputMethodIBus::HasInputMethodResult() const {
730 return result_text_.length() || composition_changed_;
731 }
732
733 void InputMethodIBus::SendFakeProcessKeyEvent(bool pressed) const {
734 DispatchFabricatedKeyEventPostIME(pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED,
735 VKEY_PROCESSKEY,
736 0);
737 }
738
739 void InputMethodIBus::FinishPendingKeyEvent(PendingKeyEvent* pending_key) {
740 DCHECK(pending_key_events_.count(pending_key));
741
742 // |pending_key| will be deleted in ProcessKeyEventDone().
743 pending_key_events_.erase(pending_key);
744 }
745
746 void InputMethodIBus::AbandonAllPendingKeyEvents() {
747 for (std::set<PendingKeyEvent*>::iterator i = pending_key_events_.begin();
748 i != pending_key_events_.end(); ++i) {
749 // The object will be deleted in ProcessKeyEventDone().
750 (*i)->Abandon();
751 }
752 pending_key_events_.clear();
753 }
754
755 void InputMethodIBus::OnCommitText(
756 IBusInputContext* context, IBusText* text) {
757 DCHECK_EQ(context_, context);
758 if (suppress_next_result_ || !text || !text->text)
759 return;
760
761 // We need to receive input method result even if the text input type is
762 // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
763 // character for each key event to the focused text input client.
764 if (!GetTextInputClient())
765 return;
766
767 string16 utf16_text(UTF8ToUTF16(text->text));
768
769 // Append the text to the buffer, because commit signal might be fired
770 // multiple times when processing a key event.
771 result_text_.append(utf16_text);
772
773 // If we are not handling key event, do not bother sending text result if the
774 // focused text input client does not support text input.
775 if (pending_key_events_.empty() && !IsTextInputTypeNone()) {
776 SendFakeProcessKeyEvent(true);
777 GetTextInputClient()->InsertText(utf16_text);
778 SendFakeProcessKeyEvent(false);
779 result_text_.clear();
780 }
781 }
782
783 void InputMethodIBus::OnForwardKeyEvent(IBusInputContext* context,
784 guint keyval,
785 guint keycode,
786 guint state) {
787 DCHECK_EQ(context_, context);
788
789 KeyboardCode ui_key_code = KeyboardCodeFromXKeysym(keyval);
790 if (!ui_key_code)
791 return;
792
793 const EventType event =
794 (state & IBUS_RELEASE_MASK) ? ET_KEY_RELEASED : ET_KEY_PRESSED;
795 const int flags = EventFlagsFromIBusState(state);
796
797 // It is not clear when the input method will forward us a fake key event.
798 // If there is a pending key event, then we may already received some input
799 // method results, so we dispatch this fake key event directly rather than
800 // calling ProcessKeyEventPostIME(), which will clear pending input method
801 // results.
802 if (event == ET_KEY_PRESSED)
803 ProcessUnfilteredFabricatedKeyPressEvent(event, ui_key_code, flags, keyval);
804 else
805 DispatchFabricatedKeyEventPostIME(event, ui_key_code, flags);
806 }
807
808 void InputMethodIBus::OnShowPreeditText(IBusInputContext* context) {
809 DCHECK_EQ(context_, context);
810 if (suppress_next_result_ || IsTextInputTypeNone())
811 return;
812
813 composing_text_ = true;
814 }
815
816 void InputMethodIBus::OnUpdatePreeditText(IBusInputContext* context,
817 IBusText* text,
818 guint cursor_pos,
819 gboolean visible) {
820 DCHECK_EQ(context_, context);
821 if (suppress_next_result_ || IsTextInputTypeNone())
822 return;
823
824 // |visible| argument is very confusing. For example, what's the correct
825 // behavior when:
826 // 1. OnUpdatePreeditText() is called with a text and visible == false, then
827 // 2. OnShowPreeditText() is called afterwards.
828 //
829 // If it's only for clearing the current preedit text, then why not just use
830 // OnHidePreeditText()?
831 if (!visible) {
832 OnHidePreeditText(context);
833 return;
834 }
835
836 ExtractCompositionTextFromIBusPreedit(text, cursor_pos, &composition_);
837
838 composition_changed_ = true;
839
840 // In case OnShowPreeditText() is not called.
841 if (composition_.text.length())
842 composing_text_ = true;
843
844 // If we receive a composition text without pending key event, then we need to
845 // send it to the focused text input client directly.
846 if (pending_key_events_.empty()) {
847 SendFakeProcessKeyEvent(true);
848 GetTextInputClient()->SetCompositionText(composition_);
849 SendFakeProcessKeyEvent(false);
850 composition_changed_ = false;
851 composition_.Clear();
852 }
853 }
854
855 void InputMethodIBus::OnHidePreeditText(IBusInputContext* context) {
856 DCHECK_EQ(context_, context);
857 if (composition_.text.empty() || IsTextInputTypeNone())
858 return;
859
860 // Intentionally leaves |composing_text_| unchanged.
861 composition_changed_ = true;
862 composition_.Clear();
863
864 if (pending_key_events_.empty()) {
865 TextInputClient* client = GetTextInputClient();
866 if (client && client->HasCompositionText())
867 client->ClearCompositionText();
868 composition_changed_ = false;
869 }
870 }
871
872 void InputMethodIBus::OnDestroy(IBusInputContext* context) {
873 DCHECK_EQ(context_, context);
874 g_object_unref(context_);
875 context_ = NULL;
876 context_focused_ = false;
877
878 ConfirmCompositionText();
879
880 // We are dead, so we need to ask the client to stop relying on us.
881 // We cannot do it in DestroyContext(), because OnDestroy() may be called
882 // automatically.
883 OnInputMethodChanged();
884 }
885
886 void InputMethodIBus::OnIBusConnected(IBusBus* bus) {
887 DCHECK_EQ(GetIBus(), bus);
888 DCHECK(ibus_bus_is_connected(bus));
889
890 DestroyContext();
891 CreateContext();
892 }
893
894 void InputMethodIBus::OnIBusDisconnected(IBusBus* bus) {
895 DCHECK_EQ(GetIBus(), bus);
896
897 // TODO(suzhe): Make sure if we really do not need to handle this signal.
898 // And I'm actually wondering if ibus-daemon will release the resource of the
899 // |context_| correctly when the connection is lost.
900 }
901
902 // static
903 IBusBus* InputMethodIBus::GetIBus() {
904 // Everything happens in UI thread, so we do not need to care about
905 // synchronization issue.
906 static IBusBus* ibus = NULL;
907
908 if (!ibus) {
909 ibus_init();
910 ibus = ibus_bus_new();
911 DCHECK(ibus);
912 }
913 return ibus;
914 }
915
916 // static
917 void InputMethodIBus::ProcessKeyEventDone(IBusInputContext* context,
918 GAsyncResult* res,
919 PendingKeyEvent* data) {
920 DCHECK(data);
921 DCHECK(!data->input_method() ||
922 data->input_method()->context_ == context);
923
924 gboolean handled = ibus_input_context_process_key_event_async_finish(
925 context, res, NULL);
926 data->ProcessPostIME(handled);
927 delete data;
928 }
929
930 // static
931 void InputMethodIBus::CreateInputContextDone(IBusBus* bus,
932 GAsyncResult* res,
933 PendingCreateICRequest* data) {
934 DCHECK_EQ(GetIBus(), bus);
935 DCHECK(data);
936 IBusInputContext* ic =
937 ibus_bus_create_input_context_async_finish(bus, res, NULL);
938 if (ic)
939 data->StoreOrAbandonInputContext(ic);
940 delete data;
941 }
942
943 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698