1 // Copyright 2013 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.
5 #include "chrome/browser/ui/libgtk2ui/x11_input_method_context_impl_gtk2.h"
8 #include <gdk/gdkkeysyms.h>
16 #include "base/event_types.h"
17 #include "base/message_loop/message_loop.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "ui/base/ime/composition_text.h"
20 #include "ui/base/ime/composition_text_util_pango.h"
21 #include "ui/base/ime/text_input_client.h"
22 #include "ui/events/event.h"
26 // Constructs a GdkEventKey from a XKeyEvent and returns it. Otherwise,
27 // returns NULL. The returned GdkEvent must be freed by gdk_event_free.
28 GdkEvent* GdkEventFromXKeyEvent(XKeyEvent& xkey, bool is_modifier) {
29 DCHECK(xkey.type == KeyPress || xkey.type == KeyRelease);
32 GdkDisplay* display = gdk_x11_lookup_xdisplay(xkey.display);
34 // Fall back to the default display.
35 display = gdk_display_get_default();
38 LOG(ERROR) << "Cannot get a GdkDisplay for a key event.";
41 // Get a keysym and group.
42 KeySym keysym = NoSymbol;
43 guint8 keyboard_group = 0;
44 XLookupString(&xkey, NULL, 0, &keysym, NULL);
45 GdkKeymap* keymap = gdk_keymap_get_for_display(display);
46 GdkKeymapKey* keys = NULL;
47 guint* keyvals = NULL;
50 gdk_keymap_get_entries_for_keycode(keymap, xkey.keycode,
51 &keys, &keyvals, &n_entries)) {
52 for (gint i = 0; i < n_entries; ++i) {
53 if (keyvals[i] == keysym) {
54 keyboard_group = keys[i].group;
64 GdkWindow* window = gdk_x11_window_lookup_for_display(display, xkey.window);
68 window = gdk_x11_window_foreign_new_for_display(display, xkey.window);
70 LOG(ERROR) << "Cannot get a GdkWindow for a key event.";
75 GdkEventType event_type = xkey.type == KeyPress ?
76 GDK_KEY_PRESS : GDK_KEY_RELEASE;
77 GdkEvent* event = gdk_event_new(event_type);
78 event->key.type = event_type;
79 event->key.window = window;
80 // GdkEventKey and XKeyEvent share the same definition for time and state.
81 event->key.send_event = xkey.send_event;
82 event->key.time = xkey.time;
83 event->key.state = xkey.state;
84 event->key.keyval = keysym;
85 event->key.length = 0;
86 event->key.string = NULL;
87 event->key.hardware_keycode = xkey.keycode;
88 event->key.group = keyboard_group;
89 event->key.is_modifier = is_modifier;
97 X11InputMethodContextImplGtk2::X11InputMethodContextImplGtk2(
98 ui::LinuxInputMethodContextDelegate* delegate)
99 : delegate_(delegate),
100 gtk_context_simple_(NULL),
101 gtk_multicontext_(NULL),
103 gdk_last_set_client_window_(NULL) {
107 XModifierKeymap* keymap = XGetModifierMapping(
108 base::MessagePumpForUI::GetDefaultXDisplay());
109 for (int i = 0; i < 8 * keymap->max_keypermod; ++i) {
110 if (keymap->modifiermap[i])
111 modifier_keycodes_.insert(keymap->modifiermap[i]);
113 XFreeModifiermap(keymap);
116 gtk_context_simple_ = gtk_im_context_simple_new();
117 gtk_multicontext_ = gtk_im_multicontext_new();
119 GtkIMContext* contexts[] = {gtk_context_simple_, gtk_multicontext_};
120 for (size_t i = 0; i < arraysize(contexts); ++i) {
121 g_signal_connect(contexts[i], "commit",
122 G_CALLBACK(OnCommitThunk), this);
123 g_signal_connect(contexts[i], "preedit-changed",
124 G_CALLBACK(OnPreeditChangedThunk), this);
125 g_signal_connect(contexts[i], "preedit-end",
126 G_CALLBACK(OnPreeditEndThunk), this);
127 g_signal_connect(contexts[i], "preedit-start",
128 G_CALLBACK(OnPreeditStartThunk), this);
129 // TODO(yukishiino): Handle operations on surrounding text.
130 // "delete-surrounding" and "retrieve-surrounding" signals should be
135 X11InputMethodContextImplGtk2::~X11InputMethodContextImplGtk2() {
137 if (gtk_context_simple_) {
138 g_object_unref(gtk_context_simple_);
139 gtk_context_simple_ = NULL;
141 if (gtk_multicontext_) {
142 g_object_unref(gtk_multicontext_);
143 gtk_multicontext_ = NULL;
147 // Overriden from ui::LinuxInputMethodContext
149 bool X11InputMethodContextImplGtk2::DispatchKeyEvent(
150 const ui::KeyEvent& key_event) {
151 if (!key_event.HasNativeEvent())
154 // The caller must call Focus() first.
158 // Translate a XKeyEvent to a GdkEventKey.
159 const base::NativeEvent& native_key_event = key_event.native_event();
160 GdkEvent* event = GdkEventFromXKeyEvent(
161 native_key_event->xkey,
162 IsKeycodeModifierKey(native_key_event->xkey.keycode));
164 LOG(ERROR) << "Cannot translate a XKeyEvent to a GdkEvent.";
168 // Set the client window and cursor location.
169 if (event->key.window != gdk_last_set_client_window_) {
170 gtk_im_context_set_client_window(gtk_context_, event->key.window);
171 gdk_last_set_client_window_ = event->key.window;
173 // Convert the last known caret bounds relative to the screen coordinates
174 // to a GdkRectangle relative to the client window.
177 gdk_window_get_origin(event->key.window, &x, &y);
178 GdkRectangle rect = {last_caret_bounds_.x() - x,
179 last_caret_bounds_.y() - y,
180 last_caret_bounds_.width(),
181 last_caret_bounds_.height()};
182 gtk_im_context_set_cursor_location(gtk_context_, &rect);
184 // Let an IME handle the key event.
185 commit_signal_trap_.StartTrap(event->key.keyval);
186 const gboolean handled = gtk_im_context_filter_keypress(gtk_context_,
188 commit_signal_trap_.StopTrap();
189 gdk_event_free(event);
191 return handled && !commit_signal_trap_.IsSignalCaught();
194 void X11InputMethodContextImplGtk2::Reset() {
195 // Reset all the states of the context, not only preedit, caret but also
198 gtk_im_context_reset(gtk_context_simple_);
199 gtk_im_context_reset(gtk_multicontext_);
200 gtk_im_context_focus_out(gtk_context_simple_);
201 gtk_im_context_focus_out(gtk_multicontext_);
202 gdk_last_set_client_window_ = NULL;
205 void X11InputMethodContextImplGtk2::OnTextInputTypeChanged(
206 ui::TextInputType text_input_type) {
207 switch (text_input_type) {
208 case ui::TEXT_INPUT_TYPE_NONE:
209 case ui::TEXT_INPUT_TYPE_PASSWORD:
210 gtk_context_ = gtk_context_simple_;
213 gtk_context_ = gtk_multicontext_;
215 gtk_im_context_focus_in(gtk_context_);
218 void X11InputMethodContextImplGtk2::OnCaretBoundsChanged(
219 const gfx::Rect& caret_bounds) {
220 // Remember the caret bounds so that we can set the cursor location later.
221 // gtk_im_context_set_cursor_location() takes the location relative to the
222 // client window, which is unknown at this point. So we'll call
223 // gtk_im_context_set_cursor_location() later in ProcessKeyEvent() where
224 // (and only where) we know the client window.
225 last_caret_bounds_ = caret_bounds;
230 bool X11InputMethodContextImplGtk2::IsKeycodeModifierKey(
231 unsigned int keycode) const {
232 return modifier_keycodes_.find(keycode) != modifier_keycodes_.end();
235 // GtkIMContext event handlers.
237 void X11InputMethodContextImplGtk2::OnCommit(GtkIMContext* context,
239 if (context != gtk_context_)
242 const base::string16& text_in_utf16 = base::UTF8ToUTF16(text);
243 // If an underlying IME is emitting the "commit" signal to insert a character
244 // for a direct input key event, ignores the insertion of the character at
245 // this point, because we have to call DispatchKeyEventPostIME() for direct
246 // input key events. DispatchKeyEvent() takes care of the trapped character
247 // and calls DispatchKeyEventPostIME().
248 if (commit_signal_trap_.Trap(text_in_utf16))
251 delegate_->OnCommit(text_in_utf16);
254 void X11InputMethodContextImplGtk2::OnPreeditChanged(GtkIMContext* context) {
255 if (context != gtk_context_)
259 PangoAttrList* attrs = NULL;
261 gtk_im_context_get_preedit_string(context, &str, &attrs, &cursor_pos);
262 ui::CompositionText composition_text;
263 ui::ExtractCompositionTextFromGtkPreedit(str, attrs, cursor_pos,
266 pango_attr_list_unref(attrs);
268 delegate_->OnPreeditChanged(composition_text);
271 void X11InputMethodContextImplGtk2::OnPreeditEnd(GtkIMContext* context) {
272 if (context != gtk_context_)
275 delegate_->OnPreeditEnd();
278 void X11InputMethodContextImplGtk2::OnPreeditStart(GtkIMContext* context) {
279 if (context != gtk_context_)
282 delegate_->OnPreeditStart();
285 // GtkCommitSignalTrap
287 X11InputMethodContextImplGtk2::GtkCommitSignalTrap::GtkCommitSignalTrap()
288 : is_trap_enabled_(false),
289 gdk_event_key_keyval_(GDK_KEY_VoidSymbol),
290 is_signal_caught_(false) {}
292 void X11InputMethodContextImplGtk2::GtkCommitSignalTrap::StartTrap(
294 is_signal_caught_ = false;
295 gdk_event_key_keyval_ = keyval;
296 is_trap_enabled_ = true;
299 void X11InputMethodContextImplGtk2::GtkCommitSignalTrap::StopTrap() {
300 is_trap_enabled_ = false;
303 bool X11InputMethodContextImplGtk2::GtkCommitSignalTrap::Trap(
304 const base::string16& text) {
305 DCHECK(!is_signal_caught_);
306 if (is_trap_enabled_ &&
307 text.length() == 1 &&
308 text[0] == gdk_keyval_to_unicode(gdk_event_key_keyval_)) {
309 is_signal_caught_ = true;
316 } // namespace libgtk2ui