1 // Copyright 2014 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/gtk2_event_loop.h"
12 #include "base/memory/singleton.h"
13 #include "ui/gfx/x/x11_types.h"
18 Gtk2EventLoop
* Gtk2EventLoop::GetInstance() {
19 return Singleton
<Gtk2EventLoop
>::get();
22 Gtk2EventLoop::Gtk2EventLoop() {
23 gdk_event_handler_set(DispatchGdkEvent
, NULL
, NULL
);
26 Gtk2EventLoop::~Gtk2EventLoop() {
27 gdk_event_handler_set(reinterpret_cast<GdkEventFunc
>(gtk_main_do_event
),
32 void Gtk2EventLoop::DispatchGdkEvent(GdkEvent
* gdk_event
, gpointer
) {
33 switch (gdk_event
->type
) {
36 ProcessGdkEventKey(gdk_event
->key
);
42 gtk_main_do_event(gdk_event
);
46 void Gtk2EventLoop::ProcessGdkEventKey(const GdkEventKey
& gdk_event_key
) {
47 // This function translates GdkEventKeys into XKeyEvents and puts them to
50 // base::MessagePumpX11 is using the X11 event queue and all key events should
51 // be processed there. However, there are cases(*1) that GdkEventKeys are
52 // created instead of XKeyEvents. In these cases, we have to translate
53 // GdkEventKeys to XKeyEvents and puts them to the X event queue so our main
54 // event loop can handle those key events.
56 // (*1) At least ibus-gtk in async mode creates a copy of user's key event and
57 // pushes it back to the GDK event queue. In this case, there is no
58 // corresponding key event in the X event queue. So we have to handle this
59 // case. ibus-gtk is used through gtk-immodule to support IMEs.
63 gdk_event_key
.type
== GDK_KEY_PRESS
? KeyPress
: KeyRelease
;
64 x_event
.xkey
.send_event
= gdk_event_key
.send_event
;
65 x_event
.xkey
.display
= gfx::GetXDisplay();
66 x_event
.xkey
.window
= GDK_WINDOW_XID(gdk_event_key
.window
);
67 x_event
.xkey
.root
= DefaultRootWindow(x_event
.xkey
.display
);
68 x_event
.xkey
.time
= gdk_event_key
.time
;
69 x_event
.xkey
.state
= gdk_event_key
.state
;
70 x_event
.xkey
.keycode
= gdk_event_key
.hardware_keycode
;
71 x_event
.xkey
.same_screen
= true;
73 XPutBackEvent(x_event
.xkey
.display
, &x_event
);
76 } // namespace libgtk2ui