Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/ime/input_method_ibus.h" | 5 #include "ui/base/ime/input_method_ibus.h" |
| 6 | 6 |
| 7 #include <ibus.h> | 7 #include <ibus.h> |
| 8 #if defined(TOUCH_UI) | 8 |
| 9 #include <X11/X.h> | |
| 9 #include <X11/Xlib.h> | 10 #include <X11/Xlib.h> |
| 10 #include <X11/Xutil.h> | 11 #include <X11/Xutil.h> |
| 11 #endif | |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <cstring> | 14 #include <cstring> |
| 15 #include <set> | 15 #include <set> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
| 19 #include "base/command_line.h" | |
| 20 #include "base/i18n/char_iterator.h" | 19 #include "base/i18n/char_iterator.h" |
| 21 #include "base/logging.h" | 20 #include "base/logging.h" |
| 22 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 23 #include "base/third_party/icu/icu_utf.h" | 22 #include "base/third_party/icu/icu_utf.h" |
| 24 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
| 24 #include "ui/base/events.h" | |
| 25 #include "ui/base/ime/text_input_client.h" | |
| 25 #include "ui/base/keycodes/keyboard_codes.h" | 26 #include "ui/base/keycodes/keyboard_codes.h" |
| 26 #include "ui/gfx/point.h" | 27 #include "ui/base/keycodes/keyboard_code_conversion.h" |
| 28 #include "ui/base/keycodes/keyboard_code_conversion_x.h" | |
| 27 #include "ui/gfx/rect.h" | 29 #include "ui/gfx/rect.h" |
| 28 #include "ui/views/events/event.h" | |
| 29 #include "ui/views/widget/widget.h" | |
| 30 | |
| 31 #if defined(USE_AURA) | |
| 32 #include "ui/base/keycodes/keyboard_code_conversion_x.h" | |
| 33 #elif defined(TOOLKIT_USES_GTK) | |
| 34 #include "ui/base/keycodes/keyboard_code_conversion_gtk.h" | |
| 35 #endif | |
| 36 | 30 |
| 37 namespace { | 31 namespace { |
| 38 | 32 |
| 39 // A global flag to switch the InputMethod implementation to InputMethodIBus | 33 XKeyEvent* GetKeyEvent(XEvent* event) { |
| 40 bool inputmethod_ibus_enabled = false; | 34 DCHECK(event && (event->type == KeyPress || event->type == KeyRelease)); |
| 35 return &event->xkey; | |
| 36 } | |
| 41 | 37 |
| 42 // Converts ibus key state flags to event flags. | 38 // Converts ibus key state flags to event flags. |
| 43 int EventFlagsFromIBusState(guint32 state) { | 39 int EventFlagsFromIBusState(guint32 state) { |
| 44 return (state & IBUS_LOCK_MASK ? ui::EF_CAPS_LOCK_DOWN : 0) | | 40 return (state & IBUS_LOCK_MASK ? ui::EF_CAPS_LOCK_DOWN : 0) | |
| 45 (state & IBUS_CONTROL_MASK ? ui::EF_CONTROL_DOWN : 0) | | 41 (state & IBUS_CONTROL_MASK ? ui::EF_CONTROL_DOWN : 0) | |
| 46 (state & IBUS_SHIFT_MASK ? ui::EF_SHIFT_DOWN : 0) | | 42 (state & IBUS_SHIFT_MASK ? ui::EF_SHIFT_DOWN : 0) | |
| 47 (state & IBUS_MOD1_MASK ? ui::EF_ALT_DOWN : 0) | | 43 (state & IBUS_MOD1_MASK ? ui::EF_ALT_DOWN : 0) | |
| 48 (state & IBUS_BUTTON1_MASK ? ui::EF_LEFT_BUTTON_DOWN : 0) | | 44 (state & IBUS_BUTTON1_MASK ? ui::EF_LEFT_BUTTON_DOWN : 0) | |
| 49 (state & IBUS_BUTTON2_MASK ? ui::EF_MIDDLE_BUTTON_DOWN : 0) | | 45 (state & IBUS_BUTTON2_MASK ? ui::EF_MIDDLE_BUTTON_DOWN : 0) | |
| 50 (state & IBUS_BUTTON3_MASK ? ui::EF_RIGHT_BUTTON_DOWN : 0); | 46 (state & IBUS_BUTTON3_MASK ? ui::EF_RIGHT_BUTTON_DOWN : 0); |
| 51 } | 47 } |
| 52 | 48 |
| 53 // Converts event flags to ibus key state flags. | 49 // Converts X flags to ibus key state flags. |
| 54 guint32 IBusStateFromEventFlags(int flags) { | 50 guint32 IBusStateFromXFlags(unsigned int flags) { |
| 55 return (flags & ui::EF_CAPS_LOCK_DOWN ? IBUS_LOCK_MASK : 0) | | 51 return (flags & LockMask ? IBUS_LOCK_MASK : 0U) | |
| 56 (flags & ui::EF_CONTROL_DOWN ? IBUS_CONTROL_MASK : 0) | | 52 (flags & ControlMask ? IBUS_CONTROL_MASK : 0U) | |
| 57 (flags & ui::EF_SHIFT_DOWN ? IBUS_SHIFT_MASK : 0) | | 53 (flags & ShiftMask ? IBUS_SHIFT_MASK : 0U) | |
| 58 (flags & ui::EF_ALT_DOWN ? IBUS_MOD1_MASK : 0) | | 54 (flags & Mod1Mask ? IBUS_MOD1_MASK : 0U) | |
| 59 (flags & ui::EF_LEFT_BUTTON_DOWN ? IBUS_BUTTON1_MASK : 0) | | 55 (flags & Button1Mask ? IBUS_BUTTON1_MASK : 0U) | |
| 60 (flags & ui::EF_MIDDLE_BUTTON_DOWN ? IBUS_BUTTON2_MASK : 0) | | 56 (flags & Button2Mask ? IBUS_BUTTON2_MASK : 0U) | |
| 61 (flags & ui::EF_RIGHT_BUTTON_DOWN ? IBUS_BUTTON3_MASK : 0); | 57 (flags & Button3Mask ? IBUS_BUTTON3_MASK : 0U); |
| 62 } | 58 } |
| 63 | 59 |
| 64 void IBusKeyEventFromViewsKeyEvent(const views::KeyEvent& key, | 60 // Converts X flags to event flags. |
| 65 guint32* ibus_keyval, | 61 guint32 EventFlagsFromXFlags(unsigned int flags) { |
| 66 guint32* ibus_keycode, | 62 return EventFlagsFromIBusState(IBusStateFromXFlags(flags)); |
| 67 guint32* ibus_state) { | 63 } |
| 68 #if defined(USE_AURA) | |
| 69 // TODO(yusukes): Handle native_event()? | |
| 70 *ibus_keyval = ui::XKeysymForWindowsKeyCode( | |
| 71 key.key_code(), key.IsShiftDown() ^ key.IsCapsLockDown()); | |
| 72 *ibus_keycode = 0; | |
| 73 #elif defined(TOUCH_UI) | |
| 74 if (key.native_event()) { | |
| 75 XKeyEvent* x_key = reinterpret_cast<XKeyEvent*>(key.native_event()); | |
| 76 // Yes, ibus uses X11 keysym. We cannot use XLookupKeysym(), which doesn't | |
| 77 // translate Shift and CapsLock states. | |
| 78 KeySym keysym = NoSymbol; | |
| 79 ::XLookupString(x_key, NULL, 0, &keysym, NULL); | |
| 80 *ibus_keyval = keysym; | |
| 81 *ibus_keycode = x_key->keycode; | |
| 82 } else { | |
| 83 *ibus_keyval = ui::XKeysymForWindowsKeyCode( | |
| 84 key.key_code(), key.IsShiftDown() ^ key.IsCapsLockDown()); | |
| 85 *ibus_keycode = 0; | |
| 86 } | |
| 87 #elif defined(TOOLKIT_USES_GTK) | |
| 88 if (key.gdk_event()) { | |
| 89 GdkEventKey* gdk_key = reinterpret_cast<GdkEventKey*>(key.gdk_event()); | |
| 90 *ibus_keyval = gdk_key->keyval; | |
| 91 *ibus_keycode = gdk_key->hardware_keycode; | |
| 92 } else { | |
| 93 *ibus_keyval = ui::GdkKeyCodeForWindowsKeyCode( | |
| 94 key.key_code(), key.IsShiftDown() ^ key.IsCapsLockDown()); | |
| 95 *ibus_keycode = 0; | |
| 96 } | |
| 97 #endif | |
| 98 | 64 |
| 99 *ibus_state = IBusStateFromEventFlags(key.flags()); | 65 void IBusKeyEventFromNativeKeyEvent(const base::NativeEvent& native_event, |
| 100 if (key.type() == ui::ET_KEY_RELEASED) | 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) | |
| 101 *ibus_state |= IBUS_RELEASE_MASK; | 80 *ibus_state |= IBUS_RELEASE_MASK; |
| 102 } | 81 } |
| 103 | 82 |
| 104 void ExtractCompositionTextFromIBusPreedit(IBusText* text, | 83 void ExtractCompositionTextFromIBusPreedit(IBusText* text, |
| 105 guint cursor_position, | 84 guint cursor_position, |
| 106 ui::CompositionText* composition) { | 85 ui::CompositionText* composition) { |
| 107 composition->Clear(); | 86 composition->Clear(); |
| 108 composition->text = UTF8ToUTF16(text->text); | 87 composition->text = UTF8ToUTF16(text->text); |
| 109 | 88 |
| 110 if (composition->text.empty()) | 89 if (composition->text.empty()) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 } | 146 } |
| 168 } | 147 } |
| 169 | 148 |
| 170 // Use a black thin underline by default. | 149 // Use a black thin underline by default. |
| 171 if (composition->underlines.empty()) { | 150 if (composition->underlines.empty()) { |
| 172 composition->underlines.push_back(ui::CompositionUnderline( | 151 composition->underlines.push_back(ui::CompositionUnderline( |
| 173 0, length, SK_ColorBLACK, false /* thick */)); | 152 0, length, SK_ColorBLACK, false /* thick */)); |
| 174 } | 153 } |
| 175 } | 154 } |
| 176 | 155 |
| 177 // A switch to enable InputMethodIBus | |
| 178 const char kEnableInputMethodIBusSwitch[] = "enable-inputmethod-ibus"; | |
| 179 | |
| 180 } // namespace | 156 } // namespace |
| 181 | 157 |
| 182 namespace views { | 158 namespace ui { |
| 183 | 159 |
| 184 // InputMethodIBus::PendingKeyEvent implementation ---------------------------- | 160 // InputMethodIBus::PendingKeyEvent implementation ------------------------ |
| 185 class InputMethodIBus::PendingKeyEvent { | 161 class InputMethodIBus::PendingKeyEvent { |
| 186 public: | 162 public: |
| 187 PendingKeyEvent(InputMethodIBus* input_method, const KeyEvent& key, | 163 PendingKeyEvent(InputMethodIBus* input_method, |
| 164 const base::NativeEvent& native_event, | |
| 188 guint32 ibus_keyval); | 165 guint32 ibus_keyval); |
| 189 ~PendingKeyEvent(); | 166 ~PendingKeyEvent(); |
| 190 | 167 |
| 191 // Abandon this pending key event. Its result will just be discarded. | 168 // Abandon this pending key event. Its result will just be discarded. |
| 192 void abandon() { input_method_ = NULL; } | 169 void Abandon() { input_method_ = NULL; } |
| 193 | 170 |
| 194 InputMethodIBus* input_method() const { return input_method_; } | 171 InputMethodIBus* input_method() const { return input_method_; } |
| 195 | 172 |
| 196 // Process this pending key event after we receive its result from the input | 173 // Process this pending key event after we receive its result from the input |
| 197 // method. It just call through InputMethodIBus::ProcessKeyEventPostIME(). | 174 // method. It just call through InputMethodIBus::ProcessKeyEventPostIME(). |
| 198 void ProcessPostIME(bool handled); | 175 void ProcessPostIME(bool handled); |
| 199 | 176 |
| 200 private: | 177 private: |
| 201 InputMethodIBus* input_method_; | 178 InputMethodIBus* input_method_; |
| 202 | 179 |
| 203 // Complete information of a views::KeyEvent. Sadly, views::KeyEvent doesn't | 180 // TODO(yusukes): To support a fabricated key event (which is typically from |
| 204 // support copy. | 181 // a virtual keyboard), we might have to copy event type, event flags, key |
| 205 ui::EventType type_; | 182 // code, 'character_', and 'unmodified_character_'. See views::InputMethodIBus |
| 206 int flags_; | 183 // for details. |
| 207 ui::KeyboardCode key_code_; | |
| 208 uint16 character_; | |
| 209 uint16 unmodified_character_; | |
| 210 | 184 |
| 211 guint32 ibus_keyval_; | 185 // corresponding XEvent data of a key event. It's a plain struct so we can do |
| 186 // bitwise copy. | |
| 187 XKeyEvent x_event_; | |
| 212 | 188 |
| 213 #if defined(TOUCH_UI) | 189 const guint32 ibus_keyval_; |
| 214 // corresponding XEvent data of a views::KeyEvent. It's a plain struct so we | |
| 215 // can do bitwise copy. | |
| 216 XKeyEvent x_event_; | |
| 217 #endif | |
| 218 | 190 |
| 219 DISALLOW_COPY_AND_ASSIGN(PendingKeyEvent); | 191 DISALLOW_COPY_AND_ASSIGN(PendingKeyEvent); |
| 220 }; | 192 }; |
| 221 | 193 |
| 222 InputMethodIBus::PendingKeyEvent::PendingKeyEvent(InputMethodIBus* input_method, | 194 InputMethodIBus::PendingKeyEvent::PendingKeyEvent( |
| 223 const KeyEvent& key, | 195 InputMethodIBus* input_method, |
| 224 guint32 ibus_keyval) | 196 const base::NativeEvent& native_event, |
| 197 guint32 ibus_keyval) | |
| 225 : input_method_(input_method), | 198 : input_method_(input_method), |
| 226 type_(key.type()), | |
| 227 flags_(key.flags()), | |
| 228 key_code_(key.key_code()), | |
| 229 character_(key.GetCharacter()), | |
| 230 unmodified_character_(key.GetUnmodifiedCharacter()), | |
| 231 ibus_keyval_(ibus_keyval) { | 199 ibus_keyval_(ibus_keyval) { |
| 232 DCHECK(input_method_); | 200 DCHECK(input_method_); |
| 233 | 201 |
| 234 #if defined(TOUCH_UI) | 202 // TODO(yusukes): Support non-native event (from e.g. a virtual keyboard). |
| 235 if (key.native_event()) | 203 DCHECK(native_event); |
| 236 x_event_ = *reinterpret_cast<XKeyEvent*>(key.native_event()); | 204 x_event_ = *GetKeyEvent(native_event); |
| 237 else | |
| 238 memset(&x_event_, 0, sizeof(x_event_)); | |
| 239 #endif | |
| 240 } | 205 } |
| 241 | 206 |
| 242 InputMethodIBus::PendingKeyEvent::~PendingKeyEvent() { | 207 InputMethodIBus::PendingKeyEvent::~PendingKeyEvent() { |
| 243 if (input_method_) | 208 if (input_method_) |
| 244 input_method_->FinishPendingKeyEvent(this); | 209 input_method_->FinishPendingKeyEvent(this); |
| 245 } | 210 } |
| 246 | 211 |
| 247 void InputMethodIBus::PendingKeyEvent::ProcessPostIME(bool handled) { | 212 void InputMethodIBus::PendingKeyEvent::ProcessPostIME(bool handled) { |
| 248 if (!input_method_) | 213 if (!input_method_) |
| 249 return; | 214 return; |
| 250 | 215 |
| 251 #if defined(TOUCH_UI) | |
| 252 if (x_event_.type == KeyPress || x_event_.type == KeyRelease) { | 216 if (x_event_.type == KeyPress || x_event_.type == KeyRelease) { |
| 253 KeyEvent key(reinterpret_cast<XEvent*>(&x_event_)); | 217 input_method_->ProcessKeyEventPostIME(reinterpret_cast<XEvent*>(&x_event_), |
| 254 input_method_->ProcessKeyEventPostIME(key, ibus_keyval_, handled); | 218 ibus_keyval_, |
| 219 handled); | |
| 255 return; | 220 return; |
| 256 } | 221 } |
| 257 #endif | 222 |
| 258 KeyEvent key(type_, key_code_, flags_); | 223 // TODO(yusukes): Support non-native event (from e.g. a virtual keyboard). |
| 259 if (key_code_ == ui::VKEY_UNKNOWN) { | 224 // See views::InputMethodIBus for details. Never forget to set 'character_' |
| 260 key.set_character(character_); | 225 // and 'unmodified_character_' to support i18n VKs like a French VK! |
| 261 key.set_unmodified_character(unmodified_character_); | |
| 262 } | |
| 263 input_method_->ProcessKeyEventPostIME(key, ibus_keyval_, handled); | |
| 264 } | 226 } |
| 265 | 227 |
| 266 // InputMethodIBus::PendingCreateICRequest implementation --------------------- | 228 // InputMethodIBus::PendingCreateICRequest implementation ----------------- |
| 267 class InputMethodIBus::PendingCreateICRequest { | 229 class InputMethodIBus::PendingCreateICRequest { |
| 268 public: | 230 public: |
| 269 PendingCreateICRequest(InputMethodIBus* input_method, | 231 PendingCreateICRequest(InputMethodIBus* input_method, |
| 270 PendingCreateICRequest** request_ptr); | 232 PendingCreateICRequest** request_ptr); |
| 271 ~PendingCreateICRequest(); | 233 ~PendingCreateICRequest(); |
| 272 | 234 |
| 273 // Abandon this pending key event. Its result will just be discarded. | 235 // Abandon this pending key event. Its result will just be discarded. |
| 274 void abandon() { | 236 void Abandon() { |
| 275 input_method_ = NULL; | 237 input_method_ = NULL; |
| 276 request_ptr_ = NULL; | 238 request_ptr_ = NULL; |
| 277 } | 239 } |
| 278 | 240 |
| 279 // Stores the result input context to |input_method_|, or abandon it if | 241 // Stores the result input context to |input_method_|, or abandon it if |
| 280 // |input_method_| is NULL. | 242 // |input_method_| is NULL. |
| 281 void StoreOrAbandonInputContext(IBusInputContext* ic); | 243 void StoreOrAbandonInputContext(IBusInputContext* ic); |
| 282 | 244 |
| 283 private: | 245 private: |
| 284 InputMethodIBus* input_method_; | 246 InputMethodIBus* input_method_; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 304 if (input_method_) { | 266 if (input_method_) { |
| 305 input_method_->SetContext(ic); | 267 input_method_->SetContext(ic); |
| 306 } else { | 268 } else { |
| 307 // ibus_proxy_destroy() will not really release the object, we still need | 269 // ibus_proxy_destroy() will not really release the object, we still need |
| 308 // to call g_object_unref() explicitly. | 270 // to call g_object_unref() explicitly. |
| 309 ibus_proxy_destroy(reinterpret_cast<IBusProxy *>(ic)); | 271 ibus_proxy_destroy(reinterpret_cast<IBusProxy *>(ic)); |
| 310 g_object_unref(ic); | 272 g_object_unref(ic); |
| 311 } | 273 } |
| 312 } | 274 } |
| 313 | 275 |
| 314 // InputMethodIBus implementation --------------------------------------------- | 276 // InputMethodIBus implementation ----------------------------------------- |
| 315 InputMethodIBus::InputMethodIBus(internal::InputMethodDelegate* delegate) | 277 InputMethodIBus::InputMethodIBus( |
| 278 internal::InputMethodDelegate* delegate) | |
| 316 : context_(NULL), | 279 : context_(NULL), |
| 317 pending_create_ic_request_(NULL), | 280 pending_create_ic_request_(NULL), |
| 318 context_focused_(false), | 281 context_focused_(false), |
| 319 composing_text_(false), | 282 composing_text_(false), |
| 320 composition_changed_(false), | 283 composition_changed_(false), |
| 321 suppress_next_result_(false) { | 284 suppress_next_result_(false) { |
| 322 set_delegate(delegate); | 285 set_delegate(delegate); |
| 323 } | 286 } |
| 324 | 287 |
| 325 InputMethodIBus::~InputMethodIBus() { | 288 InputMethodIBus::~InputMethodIBus() { |
| 326 AbandonAllPendingKeyEvents(); | 289 AbandonAllPendingKeyEvents(); |
| 327 DestroyContext(); | 290 DestroyContext(); |
| 328 | 291 |
| 329 // Disconnect bus signals | 292 // Disconnect bus signals |
| 330 g_signal_handlers_disconnect_by_func( | 293 g_signal_handlers_disconnect_by_func( |
| 331 GetIBus(), reinterpret_cast<gpointer>(OnIBusConnectedThunk), this); | 294 GetIBus(), reinterpret_cast<gpointer>(OnIBusConnectedThunk), this); |
| 332 g_signal_handlers_disconnect_by_func( | 295 g_signal_handlers_disconnect_by_func( |
| 333 GetIBus(), reinterpret_cast<gpointer>(OnIBusDisconnectedThunk), this); | 296 GetIBus(), reinterpret_cast<gpointer>(OnIBusDisconnectedThunk), this); |
| 334 } | 297 } |
| 335 | 298 |
| 336 void InputMethodIBus::OnFocus() { | 299 void InputMethodIBus::OnFocus() { |
| 337 DCHECK(!widget_focused()); | |
| 338 InputMethodBase::OnFocus(); | 300 InputMethodBase::OnFocus(); |
| 339 UpdateContextFocusState(); | 301 UpdateContextFocusState(); |
| 340 } | 302 } |
| 341 | 303 |
| 342 void InputMethodIBus::OnBlur() { | 304 void InputMethodIBus::OnBlur() { |
| 343 DCHECK(widget_focused()); | |
| 344 ConfirmCompositionText(); | 305 ConfirmCompositionText(); |
| 345 InputMethodBase::OnBlur(); | 306 InputMethodBase::OnBlur(); |
| 346 UpdateContextFocusState(); | 307 UpdateContextFocusState(); |
| 347 } | 308 } |
| 348 | 309 |
| 349 void InputMethodIBus::Init(Widget* widget) { | 310 void InputMethodIBus::Init(const base::NativeWindow& system_toplevel_window) { |
| 350 // Initializes the connection to ibus daemon. It may happen asynchronously, | 311 // Initializes the connection to ibus daemon. It may happen asynchronously, |
| 351 // and as soon as the connection is established, the |context_| will be | 312 // and as soon as the connection is established, the |context_| will be |
| 352 // created automatically. | 313 // created automatically. |
| 353 IBusBus* bus = GetIBus(); | 314 IBusBus* bus = GetIBus(); |
| 354 | 315 |
| 355 // connect bus signals | 316 // connect bus signals |
| 356 g_signal_connect(bus, "connected", | 317 g_signal_connect(bus, "connected", |
| 357 G_CALLBACK(OnIBusConnectedThunk), this); | 318 G_CALLBACK(OnIBusConnectedThunk), this); |
| 358 g_signal_connect(bus, "disconnected", | 319 g_signal_connect(bus, "disconnected", |
| 359 G_CALLBACK(OnIBusDisconnectedThunk), this); | 320 G_CALLBACK(OnIBusDisconnectedThunk), this); |
| 360 | 321 |
| 361 // Creates the |context_| if the connection is already established. In such | 322 // Creates the |context_| if the connection is already established. In such |
| 362 // case, we will not get "connected" signal. | 323 // case, we will not get "connected" signal. |
| 363 if (ibus_bus_is_connected(bus)) | 324 if (ibus_bus_is_connected(bus)) |
| 364 CreateContext(); | 325 CreateContext(); |
| 365 | 326 |
| 366 InputMethodBase::Init(widget); | 327 InputMethodBase::Init(system_toplevel_window); |
| 367 } | 328 } |
| 368 | 329 |
| 369 void InputMethodIBus::DispatchKeyEvent(const KeyEvent& key) { | 330 void InputMethodIBus::DispatchKeyEvent(const base::NativeEvent& native_event) { |
| 370 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); | 331 DCHECK(native_event && (native_event->type == KeyPress || |
| 371 DCHECK(widget_focused()); | 332 native_event->type == KeyRelease)); |
| 333 DCHECK(system_toplevel_window_focused()); | |
| 372 | 334 |
| 373 guint32 ibus_keyval = 0; | 335 guint32 ibus_keyval = 0; |
| 374 guint32 ibus_keycode = 0; | 336 guint32 ibus_keycode = 0; |
| 375 guint32 ibus_state = 0; | 337 guint32 ibus_state = 0; |
| 376 IBusKeyEventFromViewsKeyEvent(key, &ibus_keyval, &ibus_keycode, &ibus_state); | 338 IBusKeyEventFromNativeKeyEvent( |
| 339 native_event, &ibus_keyval, &ibus_keycode, &ibus_state); | |
| 377 | 340 |
| 378 // If |context_| is not usable, then we can only dispatch the key event as is. | 341 // If |context_| is not usable, then we can only dispatch the key event as is. |
| 379 // We also dispatch the key event directly if the current text input type is | 342 // We also dispatch the key event directly if the current text input type is |
| 380 // ui::TEXT_INPUT_TYPE_PASSWORD, to bypass the input method. | 343 // TEXT_INPUT_TYPE_PASSWORD, to bypass the input method. |
| 381 // Note: We need to send the key event to ibus even if the |context_| is not | 344 // Note: We need to send the key event to ibus even if the |context_| is not |
| 382 // enabled, so that ibus can have a chance to enable the |context_|. | 345 // enabled, so that ibus can have a chance to enable the |context_|. |
| 383 if (!context_focused_ || | 346 if (!context_focused_ || |
| 384 GetTextInputType() == ui::TEXT_INPUT_TYPE_PASSWORD) { | 347 GetTextInputType() == TEXT_INPUT_TYPE_PASSWORD) { |
| 385 if (key.type() == ui::ET_KEY_PRESSED) | 348 if (native_event->type == KeyPress) |
| 386 ProcessUnfilteredKeyPressEvent(key, ibus_keyval); | 349 ProcessUnfilteredKeyPressEvent(native_event, ibus_keyval); |
| 387 else | 350 else |
| 388 DispatchKeyEventPostIME(key); | 351 DispatchKeyEventPostIME(native_event); |
| 389 return; | 352 return; |
| 390 } | 353 } |
| 391 | 354 |
| 392 PendingKeyEvent* pending_key = new PendingKeyEvent(this, key, ibus_keyval); | 355 PendingKeyEvent* pending_key = |
| 356 new PendingKeyEvent(this, native_event, ibus_keyval); | |
| 393 pending_key_events_.insert(pending_key); | 357 pending_key_events_.insert(pending_key); |
| 394 | 358 |
| 395 // Note: | 359 // Note: |
| 396 // 1. We currently set timeout to -1, because ibus doesn't have a mechanism to | 360 // 1. We currently set timeout to -1, because ibus doesn't have a mechanism to |
| 397 // associate input method results to corresponding key event, thus there is | 361 // associate input method results to corresponding key event, thus there is |
| 398 // actually no way to abandon results generated by a specific key event. So we | 362 // actually no way to abandon results generated by a specific key event. So we |
| 399 // actually cannot abandon a specific key event and its result but accept | 363 // actually cannot abandon a specific key event and its result but accept |
| 400 // following key events and their results. So a timeout to abandon a key event | 364 // following key events and their results. So a timeout to abandon a key event |
| 401 // will not work. | 365 // will not work. |
| 402 // 2. We set GCancellable to NULL, because the operation of cancelling a async | 366 // 2. We set GCancellable to NULL, because the operation of cancelling a async |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 413 context_, | 377 context_, |
| 414 ibus_keyval, ibus_keycode, ibus_state, -1, NULL, | 378 ibus_keyval, ibus_keycode, ibus_state, -1, NULL, |
| 415 reinterpret_cast<GAsyncReadyCallback>(ProcessKeyEventDone), | 379 reinterpret_cast<GAsyncReadyCallback>(ProcessKeyEventDone), |
| 416 pending_key); | 380 pending_key); |
| 417 | 381 |
| 418 // We don't want to suppress the result generated by this key event, but it | 382 // We don't want to suppress the result generated by this key event, but it |
| 419 // may cause problem. See comment in ResetContext() method. | 383 // may cause problem. See comment in ResetContext() method. |
| 420 suppress_next_result_ = false; | 384 suppress_next_result_ = false; |
| 421 } | 385 } |
| 422 | 386 |
| 423 void InputMethodIBus::OnTextInputTypeChanged(View* view) { | 387 void InputMethodIBus::OnTextInputTypeChanged(const TextInputClient* client) { |
| 424 if (context_ && IsViewFocused(view)) { | 388 if (context_ && IsTextInputClientFocused(client)) { |
| 425 ResetContext(); | 389 ResetContext(); |
| 426 UpdateContextFocusState(); | 390 UpdateContextFocusState(); |
| 427 } | 391 } |
| 428 InputMethodBase::OnTextInputTypeChanged(view); | 392 InputMethodBase::OnTextInputTypeChanged(client); |
| 429 } | 393 } |
| 430 | 394 |
| 431 void InputMethodIBus::OnCaretBoundsChanged(View* view) { | 395 void InputMethodIBus::OnCaretBoundsChanged(const TextInputClient* client) { |
| 432 if (!context_focused_ || !IsViewFocused(view)) | 396 if (!context_focused_ || !IsTextInputClientFocused(client)) |
| 433 return; | 397 return; |
| 434 | 398 |
| 435 // The current text input type should not be NONE if |context_| is focused. | 399 // The current text input type should not be NONE if |context_| is focused. |
| 436 DCHECK(!IsTextInputTypeNone()); | 400 DCHECK(!IsTextInputTypeNone()); |
| 437 | 401 const gfx::Rect rect = GetTextInputClient()->GetCaretBounds(); |
| 438 gfx::Rect rect = GetTextInputClient()->GetCaretBounds(); | |
| 439 gfx::Point origin = rect.origin(); | |
| 440 gfx::Point end = gfx::Point(rect.right(), rect.bottom()); | |
| 441 | |
| 442 // We need to convert the origin and end points separately, in case the View | |
| 443 // is scaled. | |
| 444 View::ConvertPointToScreen(view, &origin); | |
| 445 View::ConvertPointToScreen(view, &end); | |
| 446 | 402 |
| 447 // This function runs asynchronously. | 403 // This function runs asynchronously. |
| 448 ibus_input_context_set_cursor_location( | 404 ibus_input_context_set_cursor_location( |
| 449 context_, origin.x(), origin.y(), | 405 context_, rect.x(), rect.y(), rect.width(), rect.height()); |
| 450 end.x() - origin.x(), end.y() - origin.y()); | |
| 451 } | 406 } |
| 452 | 407 |
| 453 void InputMethodIBus::CancelComposition(View* view) { | 408 void InputMethodIBus::CancelComposition(const TextInputClient* client) { |
| 454 if (context_focused_ && IsViewFocused(view)) | 409 if (context_focused_ && IsTextInputClientFocused(client)) |
| 455 ResetContext(); | 410 ResetContext(); |
| 456 } | 411 } |
| 457 | 412 |
| 458 std::string InputMethodIBus::GetInputLocale() { | 413 std::string InputMethodIBus::GetInputLocale() { |
| 459 // Not supported. | 414 // Not supported. |
| 460 return std::string(""); | 415 return ""; |
| 461 } | 416 } |
| 462 | 417 |
| 463 base::i18n::TextDirection InputMethodIBus::GetInputTextDirection() { | 418 base::i18n::TextDirection InputMethodIBus::GetInputTextDirection() { |
| 464 // Not supported. | 419 // Not supported. |
| 465 return base::i18n::UNKNOWN_DIRECTION; | 420 return base::i18n::UNKNOWN_DIRECTION; |
| 466 } | 421 } |
| 467 | 422 |
| 468 bool InputMethodIBus::IsActive() { | 423 bool InputMethodIBus::IsActive() { |
| 469 return true; | 424 return true; |
| 470 } | 425 } |
| 471 | 426 |
| 472 // static | |
| 473 bool InputMethodIBus::IsInputMethodIBusEnabled() { | |
| 474 #if defined(TOUCH_UI) | |
| 475 return true; | |
| 476 #else | |
| 477 return inputmethod_ibus_enabled || | |
| 478 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 479 kEnableInputMethodIBusSwitch); | |
| 480 #endif | |
| 481 } | |
| 482 | |
| 483 // static | |
| 484 void InputMethodIBus::SetEnableInputMethodIBus(bool enabled) { | |
| 485 inputmethod_ibus_enabled = enabled; | |
| 486 } | |
| 487 | |
| 488 void InputMethodIBus::OnWillChangeFocus(View* focused_before, View* focused) { | |
| 489 ConfirmCompositionText(); | |
| 490 } | |
| 491 | |
| 492 void InputMethodIBus::OnDidChangeFocus(View* focused_before, View* focused) { | |
| 493 UpdateContextFocusState(); | |
| 494 | |
| 495 // Force to update caret bounds, in case the View thinks that the caret | |
| 496 // bounds has not changed. | |
| 497 if (context_focused_) | |
| 498 OnCaretBoundsChanged(GetFocusedView()); | |
| 499 } | |
| 500 | |
| 501 void InputMethodIBus::CreateContext() { | 427 void InputMethodIBus::CreateContext() { |
| 502 DCHECK(!context_); | 428 DCHECK(!context_); |
| 503 DCHECK(GetIBus()); | 429 DCHECK(GetIBus()); |
| 504 DCHECK(ibus_bus_is_connected(GetIBus())); | 430 DCHECK(ibus_bus_is_connected(GetIBus())); |
| 505 DCHECK(!pending_create_ic_request_); | 431 DCHECK(!pending_create_ic_request_); |
| 506 | 432 |
| 507 // Creates the input context asynchronously. | 433 // Creates the input context asynchronously. |
| 508 pending_create_ic_request_ = new PendingCreateICRequest( | 434 pending_create_ic_request_ = new PendingCreateICRequest( |
| 509 this, &pending_create_ic_request_); | 435 this, &pending_create_ic_request_); |
| 510 ibus_bus_create_input_context_async( | 436 ibus_bus_create_input_context_async( |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 537 ibus_input_context_set_capabilities(ic, caps); | 463 ibus_input_context_set_capabilities(ic, caps); |
| 538 | 464 |
| 539 UpdateContextFocusState(); | 465 UpdateContextFocusState(); |
| 540 OnInputMethodChanged(); | 466 OnInputMethodChanged(); |
| 541 } | 467 } |
| 542 | 468 |
| 543 void InputMethodIBus::DestroyContext() { | 469 void InputMethodIBus::DestroyContext() { |
| 544 if (pending_create_ic_request_) { | 470 if (pending_create_ic_request_) { |
| 545 DCHECK(!context_); | 471 DCHECK(!context_); |
| 546 // |pending_create_ic_request_| will be deleted in CreateInputContextDone(). | 472 // |pending_create_ic_request_| will be deleted in CreateInputContextDone(). |
| 547 pending_create_ic_request_->abandon(); | 473 pending_create_ic_request_->Abandon(); |
| 548 pending_create_ic_request_ = NULL; | 474 pending_create_ic_request_ = NULL; |
| 549 } else if (context_) { | 475 } else if (context_) { |
| 550 // ibus_proxy_destroy() will not really release the resource of |context_| | 476 // ibus_proxy_destroy() will not really release the resource of |context_| |
| 551 // object. We still need to handle "destroy" signal and call | 477 // object. We still need to handle "destroy" signal and call |
| 552 // g_object_unref() there. | 478 // g_object_unref() there. |
| 553 ibus_proxy_destroy(reinterpret_cast<IBusProxy *>(context_)); | 479 ibus_proxy_destroy(reinterpret_cast<IBusProxy *>(context_)); |
| 554 DCHECK(!context_); | 480 DCHECK(!context_); |
| 555 } | 481 } |
| 556 } | 482 } |
| 557 | 483 |
| 558 void InputMethodIBus::ConfirmCompositionText() { | 484 void InputMethodIBus::ConfirmCompositionText() { |
| 559 ui::TextInputClient* client = GetTextInputClient(); | 485 TextInputClient* client = GetTextInputClient(); |
| 560 if (client && client->HasCompositionText()) | 486 if (client && client->HasCompositionText()) |
| 561 client->ConfirmCompositionText(); | 487 client->ConfirmCompositionText(); |
| 562 | 488 |
| 563 ResetContext(); | 489 ResetContext(); |
| 564 } | 490 } |
| 565 | 491 |
| 566 void InputMethodIBus::ResetContext() { | 492 void InputMethodIBus::ResetContext() { |
| 567 if (!context_focused_ || !GetTextInputClient()) | 493 if (!context_focused_ || !GetTextInputClient()) |
| 568 return; | 494 return; |
| 569 | 495 |
| 570 DCHECK(widget_focused()); | 496 DCHECK(system_toplevel_window_focused()); |
| 571 DCHECK(GetFocusedView()); | |
| 572 | 497 |
| 573 // Because ibus runs in asynchronous mode, the input method may still send us | 498 // Because ibus runs in asynchronous mode, the input method may still send us |
| 574 // results after sending out the reset request, so we use a flag to discard | 499 // results after sending out the reset request, so we use a flag to discard |
| 575 // all results generated by previous key events. But because ibus does not | 500 // all results generated by previous key events. But because ibus does not |
| 576 // have a mechanism to identify each key event and corresponding results, this | 501 // have a mechanism to identify each key event and corresponding results, this |
| 577 // approach will not work for some corner cases. For example if the user types | 502 // approach will not work for some corner cases. For example if the user types |
| 578 // very fast, then the next key event may come in before the |context_| is | 503 // very fast, then the next key event may come in before the |context_| is |
| 579 // really reset. Then we actually cannot know whether or not the next | 504 // really reset. Then we actually cannot know whether or not the next |
| 580 // result should be discard. | 505 // result should be discard. |
| 581 suppress_next_result_ = true; | 506 suppress_next_result_ = true; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 601 | 526 |
| 602 void InputMethodIBus::UpdateContextFocusState() { | 527 void InputMethodIBus::UpdateContextFocusState() { |
| 603 if (!context_) { | 528 if (!context_) { |
| 604 context_focused_ = false; | 529 context_focused_ = false; |
| 605 return; | 530 return; |
| 606 } | 531 } |
| 607 | 532 |
| 608 const bool old_context_focused = context_focused_; | 533 const bool old_context_focused = context_focused_; |
| 609 // Use switch here in case we are going to add more text input types. | 534 // Use switch here in case we are going to add more text input types. |
| 610 switch (GetTextInputType()) { | 535 switch (GetTextInputType()) { |
| 611 case ui::TEXT_INPUT_TYPE_NONE: | 536 case TEXT_INPUT_TYPE_NONE: |
| 612 case ui::TEXT_INPUT_TYPE_PASSWORD: | 537 case TEXT_INPUT_TYPE_PASSWORD: |
| 613 context_focused_ = false; | 538 context_focused_ = false; |
| 614 break; | 539 break; |
| 615 default: | 540 default: |
| 616 context_focused_ = true; | 541 context_focused_ = true; |
| 617 break; | 542 break; |
| 618 } | 543 } |
| 619 | 544 |
| 620 // We only focus in |context_| when the focus is in a normal textfield. | 545 // We only focus in |context_| when the focus is in a normal textfield. |
| 621 // ibus_input_context_focus_{in|out}() run asynchronously. | 546 // ibus_input_context_focus_{in|out}() run asynchronously. |
| 622 if (old_context_focused && !context_focused_) | 547 if (old_context_focused && !context_focused_) |
| 623 ibus_input_context_focus_out(context_); | 548 ibus_input_context_focus_out(context_); |
| 624 else if (!old_context_focused && context_focused_) | 549 else if (!old_context_focused && context_focused_) |
| 625 ibus_input_context_focus_in(context_); | 550 ibus_input_context_focus_in(context_); |
| 626 } | 551 } |
| 627 | 552 |
| 628 void InputMethodIBus::ProcessKeyEventPostIME(const KeyEvent& key, | 553 void InputMethodIBus::ProcessKeyEventPostIME( |
| 629 guint32 ibus_keyval, | 554 const base::NativeEvent& native_event, |
| 630 bool handled) { | 555 guint32 ibus_keyval, |
| 631 // If we get here without a focused text input client, then it means the key | 556 bool handled) { |
| 632 // event is sent to the global ibus input context. | 557 TextInputClient* client = GetTextInputClient(); |
| 633 if (!GetTextInputClient()) { | 558 |
| 634 DispatchKeyEventPostIME(key); | 559 if (!client) { |
| 560 // As ibus works asynchronously, there is a chance that the focused client | |
| 561 // loses focus before this method gets called. | |
| 562 DispatchKeyEventPostIME(native_event); | |
| 635 return; | 563 return; |
| 636 } | 564 } |
| 637 | 565 |
| 638 const View* old_focused_view = GetFocusedView(); | 566 if (native_event->type == KeyPress && handled) |
| 639 | 567 ProcessFilteredKeyPressEvent(native_event); |
| 640 // Same reason as above DCHECK. | |
| 641 DCHECK(old_focused_view); | |
| 642 | |
| 643 if (key.type() == ui::ET_KEY_PRESSED && handled) | |
| 644 ProcessFilteredKeyPressEvent(key); | |
| 645 | 568 |
| 646 // In case the focus was changed by the key event. The |context_| should have | 569 // In case the focus was changed by the key event. The |context_| should have |
| 647 // been reset when the focused view changed. | 570 // been reset when the focused window changed. |
| 648 if (old_focused_view != GetFocusedView()) | 571 if (client != GetTextInputClient()) |
| 649 return; | 572 return; |
| 650 | 573 |
| 651 if (HasInputMethodResult()) | 574 if (HasInputMethodResult()) |
| 652 ProcessInputMethodResult(key, handled); | 575 ProcessInputMethodResult(native_event, handled); |
| 653 | 576 |
| 654 // In case the focus was changed when sending input method results to the | 577 // In case the focus was changed when sending input method results to the |
| 655 // focused View. | 578 // focused window. |
| 656 if (old_focused_view != GetFocusedView()) | 579 if (client != GetTextInputClient()) |
| 657 return; | 580 return; |
| 658 | 581 |
| 659 if (key.type() == ui::ET_KEY_PRESSED && !handled) | 582 if (native_event->type == KeyPress && !handled) |
| 660 ProcessUnfilteredKeyPressEvent(key, ibus_keyval); | 583 ProcessUnfilteredKeyPressEvent(native_event, ibus_keyval); |
| 661 else if (key.type() == ui::ET_KEY_RELEASED) | 584 else if (native_event->type == KeyRelease) |
| 662 DispatchKeyEventPostIME(key); | 585 DispatchKeyEventPostIME(native_event); |
| 663 } | 586 } |
| 664 | 587 |
| 665 void InputMethodIBus::ProcessFilteredKeyPressEvent(const KeyEvent& key) { | 588 void InputMethodIBus::ProcessFilteredKeyPressEvent( |
| 666 if (NeedInsertChar()) { | 589 const base::NativeEvent& native_event) { |
| 667 DispatchKeyEventPostIME(key); | 590 if (NeedInsertChar()) |
| 668 } else { | 591 DispatchKeyEventPostIME(native_event); |
| 669 KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, key.flags()); | 592 else |
| 670 DispatchKeyEventPostIME(key); | 593 DispatchFabricatedKeyEventPostIME( |
| 671 } | 594 ET_KEY_PRESSED, |
| 595 VKEY_PROCESSKEY, | |
| 596 EventFlagsFromXFlags(GetKeyEvent(native_event)->state)); | |
| 672 } | 597 } |
| 673 | 598 |
| 674 void InputMethodIBus::ProcessUnfilteredKeyPressEvent(const KeyEvent& key, | 599 void InputMethodIBus::ProcessUnfilteredKeyPressEvent( |
| 675 guint32 ibus_keyval) { | 600 const base::NativeEvent& native_event, guint32 ibus_keyval) { |
| 676 const View* old_focused_view = GetFocusedView(); | 601 // For a fabricated event, ProcessUnfilteredFabricatedKeyPressEvent should be |
| 677 DispatchKeyEventPostIME(key); | 602 // called instead. |
| 603 DCHECK(native_event); | |
| 678 | 604 |
| 679 // We shouldn't dispatch the character anymore if the key event caused focus | 605 TextInputClient* client = GetTextInputClient(); |
| 680 // change. | 606 DispatchKeyEventPostIME(native_event); |
| 681 if (old_focused_view != GetFocusedView()) | 607 |
| 608 // We shouldn't dispatch the character anymore if the key event dispatch | |
| 609 // caused focus change. For example, in the following scenario, | |
| 610 // 1. visit a web page which has a <textarea>. | |
| 611 // 2. click Omnibox. | |
| 612 // 3. enable Korean IME, press A, then press Tab to move the focus to the web | |
| 613 // page. | |
| 614 // We should return here not to send the Tab key event to RWHV. | |
| 615 if (client != GetTextInputClient()) | |
| 682 return; | 616 return; |
| 683 | 617 |
| 684 // Process compose and dead keys | 618 // Process compose and dead keys |
| 685 if (character_composer_.FilterKeyPress(ibus_keyval)) { | 619 if (character_composer_.FilterKeyPress(ibus_keyval)) { |
| 686 string16 composed = character_composer_.composed_character(); | 620 string16 composed = character_composer_.composed_character(); |
| 687 if (!composed.empty()) { | 621 if (!composed.empty()) { |
| 688 ui::TextInputClient* client = GetTextInputClient(); | 622 client = GetTextInputClient(); |
| 689 if (client) | 623 if (client) |
| 690 client->InsertText(composed); | 624 client->InsertText(composed); |
| 691 } | 625 } |
| 692 return; | 626 return; |
| 693 } | 627 } |
| 628 | |
| 694 // If a key event was not filtered by |context_| and |character_composer_|, | 629 // If a key event was not filtered by |context_| and |character_composer_|, |
| 695 // then it means the key event didn't generate any result text. So we need | 630 // then it means the key event didn't generate any result text. So we need |
| 696 // to send corresponding character to the focused text input client. | 631 // to send corresponding character to the focused text input client. |
| 632 client = GetTextInputClient(); | |
| 697 | 633 |
| 698 ui::TextInputClient* client = GetTextInputClient(); | 634 const uint32 state = |
| 699 char16 ch = key.GetCharacter(); | 635 EventFlagsFromXFlags(GetKeyEvent(native_event)->state); |
| 700 if (ch && client) | 636 uint16 ch = ui::DefaultSymbolFromXEvent(native_event); |
| 701 client->InsertChar(ch, key.flags()); | 637 if (!ch) |
| 638 ch = ui::GetCharacterFromKeyCode( | |
| 639 ui::KeyboardCodeFromNative(native_event), state); | |
|
James Su
2011/11/29 04:25:53
need {}
Yusuke Sato
2011/11/29 09:06:36
Done.
| |
| 640 | |
| 641 if (client && ch) | |
| 642 client->InsertChar(ch, state); | |
| 702 } | 643 } |
| 703 | 644 |
| 704 void InputMethodIBus::ProcessInputMethodResult(const KeyEvent& key, | 645 void InputMethodIBus::ProcessUnfilteredFabricatedKeyPressEvent( |
| 705 bool handled) { | 646 EventType type, KeyboardCode key_code, int flags, guint32 ibus_keyval) { |
| 706 ui::TextInputClient* client = GetTextInputClient(); | 647 TextInputClient* client = GetTextInputClient(); |
| 648 DispatchFabricatedKeyEventPostIME(type, key_code, flags); | |
| 649 | |
| 650 if (client != GetTextInputClient()) | |
| 651 return; | |
| 652 | |
| 653 if (character_composer_.FilterKeyPress(ibus_keyval)) { | |
| 654 string16 composed = character_composer_.composed_character(); | |
| 655 if (!composed.empty()) { | |
| 656 client = GetTextInputClient(); | |
| 657 if (client) | |
| 658 client->InsertText(composed); | |
| 659 } | |
| 660 return; | |
| 661 } | |
| 662 | |
| 663 client = GetTextInputClient(); | |
| 664 const uint16 ch = ui::GetCharacterFromKeyCode(key_code, flags); | |
| 665 if (client && ch) | |
| 666 client->InsertChar(ch, flags); | |
| 667 } | |
| 668 | |
| 669 void InputMethodIBus::ProcessInputMethodResult( | |
| 670 const base::NativeEvent& native_event, bool handled) { | |
| 671 TextInputClient* client = GetTextInputClient(); | |
| 707 DCHECK(client); | 672 DCHECK(client); |
| 708 | 673 |
| 709 if (result_text_.length()) { | 674 if (result_text_.length()) { |
| 710 if (handled && NeedInsertChar()) { | 675 if (handled && NeedInsertChar()) { |
| 676 const uint32 state = | |
| 677 EventFlagsFromXFlags(GetKeyEvent(native_event)->state); | |
| 711 for (string16::const_iterator i = result_text_.begin(); | 678 for (string16::const_iterator i = result_text_.begin(); |
| 712 i != result_text_.end(); ++i) { | 679 i != result_text_.end(); ++i) { |
| 713 client->InsertChar(*i, key.flags()); | 680 client->InsertChar(*i, state); |
| 714 } | 681 } |
| 715 } else { | 682 } else { |
| 716 client->InsertText(result_text_); | 683 client->InsertText(result_text_); |
| 717 composing_text_ = false; | 684 composing_text_ = false; |
| 718 } | 685 } |
| 719 } | 686 } |
| 720 | 687 |
| 721 if (composition_changed_ && !IsTextInputTypeNone()) { | 688 if (composition_changed_ && !IsTextInputTypeNone()) { |
| 722 if (composition_.text.length()) { | 689 if (composition_.text.length()) { |
| 723 composing_text_ = true; | 690 composing_text_ = true; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 737 return GetTextInputClient() && | 704 return GetTextInputClient() && |
| 738 (IsTextInputTypeNone() || | 705 (IsTextInputTypeNone() || |
| 739 (!composing_text_ && result_text_.length() == 1)); | 706 (!composing_text_ && result_text_.length() == 1)); |
| 740 } | 707 } |
| 741 | 708 |
| 742 bool InputMethodIBus::HasInputMethodResult() const { | 709 bool InputMethodIBus::HasInputMethodResult() const { |
| 743 return result_text_.length() || composition_changed_; | 710 return result_text_.length() || composition_changed_; |
| 744 } | 711 } |
| 745 | 712 |
| 746 void InputMethodIBus::SendFakeProcessKeyEvent(bool pressed) const { | 713 void InputMethodIBus::SendFakeProcessKeyEvent(bool pressed) const { |
| 747 KeyEvent key(pressed ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, | 714 DispatchFabricatedKeyEventPostIME(pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED, |
| 748 ui::VKEY_PROCESSKEY, 0); | 715 VKEY_PROCESSKEY, |
| 749 DispatchKeyEventPostIME(key); | 716 0); |
| 750 } | 717 } |
| 751 | 718 |
| 752 void InputMethodIBus::FinishPendingKeyEvent(PendingKeyEvent* pending_key) { | 719 void InputMethodIBus::FinishPendingKeyEvent(PendingKeyEvent* pending_key) { |
| 753 DCHECK(pending_key_events_.count(pending_key)); | 720 DCHECK(pending_key_events_.count(pending_key)); |
| 754 | 721 |
| 755 // |pending_key| will be deleted in ProcessKeyEventDone(). | 722 // |pending_key| will be deleted in ProcessKeyEventDone(). |
| 756 pending_key_events_.erase(pending_key); | 723 pending_key_events_.erase(pending_key); |
| 757 } | 724 } |
| 758 | 725 |
| 759 void InputMethodIBus::AbandonAllPendingKeyEvents() { | 726 void InputMethodIBus::AbandonAllPendingKeyEvents() { |
| 760 for (std::set<PendingKeyEvent*>::iterator i = pending_key_events_.begin(); | 727 for (std::set<PendingKeyEvent*>::iterator i = pending_key_events_.begin(); |
| 761 i != pending_key_events_.end(); ++i) { | 728 i != pending_key_events_.end(); ++i) { |
| 762 // The object will be deleted in ProcessKeyEventDone(). | 729 // The object will be deleted in ProcessKeyEventDone(). |
| 763 (*i)->abandon(); | 730 (*i)->Abandon(); |
| 764 } | 731 } |
| 765 pending_key_events_.clear(); | 732 pending_key_events_.clear(); |
| 766 } | 733 } |
| 767 | 734 |
| 768 void InputMethodIBus::OnCommitText(IBusInputContext* context, IBusText* text) { | 735 void InputMethodIBus::OnCommitText( |
| 736 IBusInputContext* context, IBusText* text) { | |
| 769 DCHECK_EQ(context_, context); | 737 DCHECK_EQ(context_, context); |
| 770 if (suppress_next_result_ || !text || !text->text) | 738 if (suppress_next_result_ || !text || !text->text) |
| 771 return; | 739 return; |
| 772 | 740 |
| 773 // We need to receive input method result even if the text input type is | 741 // We need to receive input method result even if the text input type is |
| 774 // ui::TEXT_INPUT_TYPE_NONE, to make sure we can always send correct | 742 // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct |
| 775 // character for each key event to the focused text input client. | 743 // character for each key event to the focused text input client. |
| 776 if (!GetTextInputClient()) | 744 if (!GetTextInputClient()) |
| 777 return; | 745 return; |
| 778 | 746 |
| 779 string16 utf16_text(UTF8ToUTF16(text->text)); | 747 string16 utf16_text(UTF8ToUTF16(text->text)); |
| 780 | 748 |
| 781 // Append the text to the buffer, because commit signal might be fired | 749 // Append the text to the buffer, because commit signal might be fired |
| 782 // multiple times when processing a key event. | 750 // multiple times when processing a key event. |
| 783 result_text_.append(utf16_text); | 751 result_text_.append(utf16_text); |
| 784 | 752 |
| 785 // If we are not handling key event, do not bother sending text result if the | 753 // If we are not handling key event, do not bother sending text result if the |
| 786 // focused text input client does not support text input. | 754 // focused text input client does not support text input. |
| 787 if (pending_key_events_.empty() && !IsTextInputTypeNone()) { | 755 if (pending_key_events_.empty() && !IsTextInputTypeNone()) { |
| 788 SendFakeProcessKeyEvent(true); | 756 SendFakeProcessKeyEvent(true); |
| 789 GetTextInputClient()->InsertText(utf16_text); | 757 GetTextInputClient()->InsertText(utf16_text); |
| 790 SendFakeProcessKeyEvent(false); | 758 SendFakeProcessKeyEvent(false); |
| 791 result_text_.clear(); | 759 result_text_.clear(); |
| 792 } | 760 } |
| 793 } | 761 } |
| 794 | 762 |
| 795 void InputMethodIBus::OnForwardKeyEvent(IBusInputContext* context, | 763 void InputMethodIBus::OnForwardKeyEvent(IBusInputContext* context, |
| 796 guint keyval, | 764 guint keyval, |
| 797 guint keycode, | 765 guint keycode, |
| 798 guint state) { | 766 guint state) { |
| 799 DCHECK_EQ(context_, context); | 767 DCHECK_EQ(context_, context); |
| 800 | 768 |
| 801 ui::KeyboardCode key_code = ui::VKEY_UNKNOWN; | 769 KeyboardCode ui_key_code = KeyboardCodeFromXKeysym(keyval); |
| 802 #if defined(USE_AURA) | 770 if (!ui_key_code) |
| 803 key_code = ui::KeyboardCodeFromXKeysym(keyval); | |
| 804 #elif defined(TOOLKIT_USES_GTK) | |
| 805 key_code = ui::WindowsKeyCodeForGdkKeyCode(keyval); | |
| 806 #endif | |
| 807 | |
| 808 if (!key_code) | |
| 809 return; | 771 return; |
| 810 | 772 |
| 811 KeyEvent key(state & IBUS_RELEASE_MASK ? | 773 const EventType event = |
| 812 ui::ET_KEY_RELEASED : ui::ET_KEY_PRESSED, | 774 (state & IBUS_RELEASE_MASK) ? ET_KEY_RELEASED : ET_KEY_PRESSED; |
| 813 key_code, EventFlagsFromIBusState(state)); | 775 const int flags = EventFlagsFromIBusState(state); |
| 814 | 776 |
| 815 // It is not clear when the input method will forward us a fake key event. | 777 // It is not clear when the input method will forward us a fake key event. |
| 816 // If there is a pending key event, then we may already received some input | 778 // If there is a pending key event, then we may already received some input |
| 817 // method results, so we dispatch this fake key event directly rather than | 779 // method results, so we dispatch this fake key event directly rather than |
| 818 // calling ProcessKeyEventPostIME(), which will clear pending input method | 780 // calling ProcessKeyEventPostIME(), which will clear pending input method |
| 819 // results. | 781 // results. |
| 820 if (key.type() == ui::ET_KEY_PRESSED) | 782 if (event == ET_KEY_PRESSED) |
| 821 ProcessUnfilteredKeyPressEvent(key, keyval); | 783 ProcessUnfilteredFabricatedKeyPressEvent(event, ui_key_code, flags, keyval); |
| 822 else | 784 else |
| 823 DispatchKeyEventPostIME(key); | 785 DispatchFabricatedKeyEventPostIME(event, ui_key_code, flags); |
| 824 } | 786 } |
| 825 | 787 |
| 826 void InputMethodIBus::OnShowPreeditText(IBusInputContext* context) { | 788 void InputMethodIBus::OnShowPreeditText(IBusInputContext* context) { |
| 827 DCHECK_EQ(context_, context); | 789 DCHECK_EQ(context_, context); |
| 828 if (suppress_next_result_ || IsTextInputTypeNone()) | 790 if (suppress_next_result_ || IsTextInputTypeNone()) |
| 829 return; | 791 return; |
| 830 | 792 |
| 831 composing_text_ = true; | 793 composing_text_ = true; |
| 832 } | 794 } |
| 833 | 795 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 873 void InputMethodIBus::OnHidePreeditText(IBusInputContext* context) { | 835 void InputMethodIBus::OnHidePreeditText(IBusInputContext* context) { |
| 874 DCHECK_EQ(context_, context); | 836 DCHECK_EQ(context_, context); |
| 875 if (composition_.text.empty() || IsTextInputTypeNone()) | 837 if (composition_.text.empty() || IsTextInputTypeNone()) |
| 876 return; | 838 return; |
| 877 | 839 |
| 878 // Intentionally leaves |composing_text_| unchanged. | 840 // Intentionally leaves |composing_text_| unchanged. |
| 879 composition_changed_ = true; | 841 composition_changed_ = true; |
| 880 composition_.Clear(); | 842 composition_.Clear(); |
| 881 | 843 |
| 882 if (pending_key_events_.empty()) { | 844 if (pending_key_events_.empty()) { |
| 883 ui::TextInputClient* client = GetTextInputClient(); | 845 TextInputClient* client = GetTextInputClient(); |
| 884 if (client && client->HasCompositionText()) | 846 if (client && client->HasCompositionText()) |
| 885 client->ClearCompositionText(); | 847 client->ClearCompositionText(); |
| 886 composition_changed_ = false; | 848 composition_changed_ = false; |
| 887 } | 849 } |
| 888 } | 850 } |
| 889 | 851 |
| 890 void InputMethodIBus::OnDestroy(IBusInputContext* context) { | 852 void InputMethodIBus::OnDestroy(IBusInputContext* context) { |
| 891 DCHECK_EQ(context_, context); | 853 DCHECK_EQ(context_, context); |
| 892 g_object_unref(context_); | 854 g_object_unref(context_); |
| 893 context_ = NULL; | 855 context_ = NULL; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 951 PendingCreateICRequest* data) { | 913 PendingCreateICRequest* data) { |
| 952 DCHECK_EQ(GetIBus(), bus); | 914 DCHECK_EQ(GetIBus(), bus); |
| 953 DCHECK(data); | 915 DCHECK(data); |
| 954 IBusInputContext* ic = | 916 IBusInputContext* ic = |
| 955 ibus_bus_create_input_context_async_finish(bus, res, NULL); | 917 ibus_bus_create_input_context_async_finish(bus, res, NULL); |
| 956 if (ic) | 918 if (ic) |
| 957 data->StoreOrAbandonInputContext(ic); | 919 data->StoreOrAbandonInputContext(ic); |
| 958 delete data; | 920 delete data; |
| 959 } | 921 } |
| 960 | 922 |
| 961 } // namespace views | 923 } // namespace ui |
| OLD | NEW |