OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/libgtk2ui/gtk2_event_loop.h" | |
6 | |
7 #include <gdk/gdk.h> | |
8 #include <gdk/gdkx.h> | |
9 #include <gtk/gtk.h> | |
10 #include <X11/X.h> | |
11 | |
12 #include "base/memory/singleton.h" | |
13 #include "ui/events/platform/x11/x11_event_source.h" | |
14 #include "ui/gfx/x/x11_types.h" | |
15 | |
16 namespace libgtk2ui { | |
17 | |
18 // static | |
19 Gtk2EventLoop* Gtk2EventLoop::GetInstance() { | |
20 return base::Singleton<Gtk2EventLoop>::get(); | |
21 } | |
22 | |
23 Gtk2EventLoop::Gtk2EventLoop() { | |
24 gdk_event_handler_set(DispatchGdkEvent, NULL, NULL); | |
25 } | |
26 | |
27 Gtk2EventLoop::~Gtk2EventLoop() { | |
28 gdk_event_handler_set(reinterpret_cast<GdkEventFunc>(gtk_main_do_event), | |
29 NULL, NULL); | |
30 } | |
31 | |
32 // static | |
33 void Gtk2EventLoop::DispatchGdkEvent(GdkEvent* gdk_event, gpointer) { | |
34 switch (gdk_event->type) { | |
35 case GDK_KEY_PRESS: | |
36 case GDK_KEY_RELEASE: | |
37 ProcessGdkEventKey(gdk_event->key); | |
38 break; | |
39 default: | |
40 break; // Do nothing. | |
41 } | |
42 | |
43 gtk_main_do_event(gdk_event); | |
44 } | |
45 | |
46 // static | |
47 void Gtk2EventLoop::ProcessGdkEventKey(const GdkEventKey& gdk_event_key) { | |
48 // This function translates GdkEventKeys into XKeyEvents and puts them to | |
49 // the X event queue. | |
50 // | |
51 // base::MessagePumpX11 is using the X11 event queue and all key events should | |
52 // be processed there. However, there are cases(*1) that GdkEventKeys are | |
53 // created instead of XKeyEvents. In these cases, we have to translate | |
54 // GdkEventKeys to XKeyEvents and puts them to the X event queue so our main | |
55 // event loop can handle those key events. | |
56 // | |
57 // (*1) At least ibus-gtk in async mode creates a copy of user's key event and | |
58 // pushes it back to the GDK event queue. In this case, there is no | |
59 // corresponding key event in the X event queue. So we have to handle this | |
60 // case. ibus-gtk is used through gtk-immodule to support IMEs. | |
61 | |
62 XEvent x_event = {0}; | |
63 x_event.xkey.type = | |
64 gdk_event_key.type == GDK_KEY_PRESS ? KeyPress : KeyRelease; | |
65 x_event.xkey.send_event = gdk_event_key.send_event; | |
66 x_event.xkey.display = gfx::GetXDisplay(); | |
67 x_event.xkey.window = GDK_WINDOW_XID(gdk_event_key.window); | |
68 x_event.xkey.root = DefaultRootWindow(x_event.xkey.display); | |
69 x_event.xkey.time = gdk_event_key.time; | |
70 x_event.xkey.state = gdk_event_key.state; | |
71 x_event.xkey.keycode = gdk_event_key.hardware_keycode; | |
72 x_event.xkey.same_screen = true; | |
73 | |
74 // We want to process the gtk2 event; mapped to an X11 event immediately | |
75 // otherwise if we put it back on the queue we may get items out of order. | |
76 if (ui::X11EventSource* x11_source = ui::X11EventSource::GetInstance()) | |
77 x11_source->DispatchXEventNow(&x_event); | |
78 else | |
79 XPutBackEvent(x_event.xkey.display, &x_event); | |
80 } | |
81 | |
82 } // namespace libgtk2ui | |
OLD | NEW |