blob: 34313e5a6bb098d63f18930ac4a3ecfa9548bb08 [file] [log] [blame]
[email protected]60e102b8c2011-03-23 17:49:021// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]71ad9c6f2010-10-22 16:17:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/message_pump_glib_x.h"
6
7#include <gdk/gdkx.h>
[email protected]130eeb32010-11-12 17:42:308#if defined(HAVE_XINPUT2)
9#include <X11/extensions/XInput2.h>
10#else
[email protected]71ad9c6f2010-10-22 16:17:4711#include <X11/Xlib.h>
[email protected]130eeb32010-11-12 17:42:3012#endif
[email protected]71ad9c6f2010-10-22 16:17:4713
[email protected]b2f7ac42010-10-26 18:43:1814#include "base/message_pump_glib_x_dispatch.h"
15
[email protected]71ad9c6f2010-10-22 16:17:4716namespace {
17
18gboolean PlaceholderDispatch(GSource* source,
19 GSourceFunc cb,
20 gpointer data) {
21 return TRUE;
22}
23
[email protected]130eeb32010-11-12 17:42:3024#if defined(HAVE_XINPUT2)
25
26// Setup XInput2 select for the GtkWidget.
27gboolean GtkWidgetRealizeCallback(GSignalInvocationHint* hint, guint nparams,
28 const GValue* pvalues, gpointer data) {
29 GtkWidget* widget = GTK_WIDGET(g_value_get_object(pvalues));
30 GdkWindow* window = widget->window;
31 base::MessagePumpGlibX* msgpump = static_cast<base::MessagePumpGlibX*>(data);
32
33 DCHECK(window); // TODO(sad): Remove once determined if necessary.
34
[email protected]0cbe5a12011-03-01 23:38:2335 if (GDK_WINDOW_TYPE(window) != GDK_WINDOW_TOPLEVEL &&
36 GDK_WINDOW_TYPE(window) != GDK_WINDOW_CHILD &&
37 GDK_WINDOW_TYPE(window) != GDK_WINDOW_DIALOG)
38 return true;
39
[email protected]130eeb32010-11-12 17:42:3040 // TODO(sad): Do we need to set a flag on |window| to make sure we don't
41 // select for the same GdkWindow multiple times? Does it matter?
42 msgpump->SetupXInput2ForXWindow(GDK_WINDOW_XID(window));
43
44 return true;
45}
46
47// We need to capture all the GDK windows that get created, and start
48// listening for XInput2 events. So we setup a callback to the 'realize'
49// signal for GTK+ widgets, so that whenever the signal triggers for any
50// GtkWidget, which means the GtkWidget should now have a GdkWindow, we can
51// setup XInput2 events for the GdkWindow.
[email protected]4b9baf9d2011-03-02 22:56:1152static guint realize_signal_id = 0;
53static guint realize_hook_id = 0;
54
[email protected]130eeb32010-11-12 17:42:3055void SetupGtkWidgetRealizeNotifier(base::MessagePumpGlibX* msgpump) {
[email protected]130eeb32010-11-12 17:42:3056 gpointer klass = g_type_class_ref(GTK_TYPE_WIDGET);
57
[email protected]4b9baf9d2011-03-02 22:56:1158 g_signal_parse_name("realize", GTK_TYPE_WIDGET,
59 &realize_signal_id, NULL, FALSE);
60 realize_hook_id = g_signal_add_emission_hook(realize_signal_id, 0,
61 GtkWidgetRealizeCallback, static_cast<gpointer>(msgpump), NULL);
[email protected]130eeb32010-11-12 17:42:3062
63 g_type_class_unref(klass);
64}
65
[email protected]4b9baf9d2011-03-02 22:56:1166void RemoveGtkWidgetRealizeNotifier() {
67 if (realize_signal_id != 0)
68 g_signal_remove_emission_hook(realize_signal_id, realize_hook_id);
69 realize_signal_id = 0;
70 realize_hook_id = 0;
71}
72
[email protected]130eeb32010-11-12 17:42:3073#endif // HAVE_XINPUT2
74
[email protected]71ad9c6f2010-10-22 16:17:4775} // namespace
76
77namespace base {
78
79MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(),
[email protected]130eeb32010-11-12 17:42:3080#if defined(HAVE_XINPUT2)
81 xiopcode_(-1),
[email protected]009d54e92011-03-25 22:52:5482 pointer_devices_(),
[email protected]130eeb32010-11-12 17:42:3083#endif
[email protected]71ad9c6f2010-10-22 16:17:4784 gdksource_(NULL),
85 dispatching_event_(false),
86 capture_x_events_(0),
87 capture_gdk_events_(0) {
88 gdk_event_handler_set(&EventDispatcherX, this, NULL);
89
[email protected]130eeb32010-11-12 17:42:3090#if defined(HAVE_XINPUT2)
91 InitializeXInput2();
92#endif
[email protected]71ad9c6f2010-10-22 16:17:4793 InitializeEventsToCapture();
94}
95
96MessagePumpGlibX::~MessagePumpGlibX() {
[email protected]4b9baf9d2011-03-02 22:56:1197#if defined(HAVE_XINPUT2)
98 RemoveGtkWidgetRealizeNotifier();
99#endif
[email protected]71ad9c6f2010-10-22 16:17:47100}
101
[email protected]eae9c062011-01-11 00:50:59102#if defined(HAVE_XINPUT2)
103void MessagePumpGlibX::SetupXInput2ForXWindow(Window xwindow) {
104 Display* xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
105
106 // Setup mask for mouse events.
107 unsigned char mask[(XI_LASTEVENT + 7)/8];
108 memset(mask, 0, sizeof(mask));
109
110 XISetMask(mask, XI_ButtonPress);
111 XISetMask(mask, XI_ButtonRelease);
112 XISetMask(mask, XI_Motion);
113
[email protected]009d54e92011-03-25 22:52:54114 XIEventMask evmasks[pointer_devices_.size()];
[email protected]eae9c062011-01-11 00:50:59115 int count = 0;
[email protected]009d54e92011-03-25 22:52:54116 for (std::set<int>::const_iterator iter = pointer_devices_.begin();
117 iter != pointer_devices_.end();
[email protected]eae9c062011-01-11 00:50:59118 ++iter, ++count) {
119 evmasks[count].deviceid = *iter;
120 evmasks[count].mask_len = sizeof(mask);
121 evmasks[count].mask = mask;
122 }
123
[email protected]009d54e92011-03-25 22:52:54124 XISelectEvents(xdisplay, xwindow, evmasks, pointer_devices_.size());
[email protected]eae9c062011-01-11 00:50:59125
126 // TODO(sad): Setup masks for keyboard events.
127
128 XFlush(xdisplay);
129}
130#endif // HAVE_XINPUT2
131
[email protected]71ad9c6f2010-10-22 16:17:47132bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) {
133 GdkDisplay* gdisp = gdk_display_get_default();
[email protected]4b9baf9d2011-03-02 22:56:11134 if (!gdisp || !GetDispatcher())
[email protected]81516d6ea2010-12-30 23:51:15135 return MessagePumpForUI::RunOnce(context, block);
136
[email protected]71ad9c6f2010-10-22 16:17:47137 Display* display = GDK_DISPLAY_XDISPLAY(gdisp);
[email protected]74434942010-12-05 04:00:59138 bool should_quit = false;
139
[email protected]71ad9c6f2010-10-22 16:17:47140 if (XPending(display)) {
141 XEvent xev;
142 XPeekEvent(display, &xev);
[email protected]130eeb32010-11-12 17:42:30143 if (capture_x_events_[xev.type]
144#if defined(HAVE_XINPUT2)
145 && (xev.type != GenericEvent || xev.xcookie.extension == xiopcode_)
146#endif
147 ) {
[email protected]71ad9c6f2010-10-22 16:17:47148 XNextEvent(display, &xev);
149
[email protected]74434942010-12-05 04:00:59150#if defined(HAVE_XINPUT2)
151 bool have_cookie = false;
152 if (xev.type == GenericEvent &&
153 XGetEventData(xev.xgeneric.display, &xev.xcookie)) {
154 have_cookie = true;
155 }
156#endif
157
158 MessagePumpGlibXDispatcher::DispatchStatus status =
159 static_cast<MessagePumpGlibXDispatcher*>
[email protected]0c3049492011-02-11 16:02:40160 (GetDispatcher())->DispatchX(&xev);
[email protected]71ad9c6f2010-10-22 16:17:47161
[email protected]74434942010-12-05 04:00:59162 if (status == MessagePumpGlibXDispatcher::EVENT_QUIT) {
163 should_quit = true;
164 Quit();
165 } else if (status == MessagePumpGlibXDispatcher::EVENT_IGNORED) {
[email protected]b2f7ac42010-10-26 18:43:18166 DLOG(WARNING) << "Event (" << xev.type << ") not handled.";
[email protected]130eeb32010-11-12 17:42:30167
168 // TODO(sad): It is necessary to put back the event so that the default
169 // GDK events handler can take care of it. Without this, it is
170 // impossible to use the omnibox at the moment. However, this will
171 // eventually be removed once the omnibox code is updated for touchui.
172 XPutBackEvent(display, &xev);
[email protected]74434942010-12-05 04:00:59173 if (gdksource_)
174 gdksource_->source_funcs->dispatch = gdkdispatcher_;
[email protected]130eeb32010-11-12 17:42:30175 g_main_context_iteration(context, FALSE);
[email protected]b2f7ac42010-10-26 18:43:18176 }
[email protected]74434942010-12-05 04:00:59177
178#if defined(HAVE_XINPUT2)
179 if (have_cookie) {
180 XFreeEventData(xev.xgeneric.display, &xev.xcookie);
181 }
182#endif
[email protected]71ad9c6f2010-10-22 16:17:47183 } else {
[email protected]b2f7ac42010-10-26 18:43:18184 // TODO(sad): A couple of extra events can still sneak in during this.
185 // Those should be sent back to the X queue from the dispatcher
186 // EventDispatcherX.
[email protected]74434942010-12-05 04:00:59187 if (gdksource_)
188 gdksource_->source_funcs->dispatch = gdkdispatcher_;
[email protected]71ad9c6f2010-10-22 16:17:47189 g_main_context_iteration(context, FALSE);
190 }
191 }
192
[email protected]74434942010-12-05 04:00:59193 if (should_quit)
194 return true;
195
[email protected]71ad9c6f2010-10-22 16:17:47196 bool retvalue;
197 if (gdksource_) {
198 // Replace the dispatch callback of the GDK event source temporarily so that
199 // it doesn't read events from X.
200 gboolean (*cb)(GSource*, GSourceFunc, void*) =
201 gdksource_->source_funcs->dispatch;
202 gdksource_->source_funcs->dispatch = PlaceholderDispatch;
203
204 dispatching_event_ = true;
205 retvalue = g_main_context_iteration(context, block);
206 dispatching_event_ = false;
207
208 gdksource_->source_funcs->dispatch = cb;
209 } else {
210 retvalue = g_main_context_iteration(context, block);
211 }
212
213 return retvalue;
214}
215
[email protected]eae9c062011-01-11 00:50:59216void MessagePumpGlibX::EventDispatcherX(GdkEvent* event, gpointer data) {
217 MessagePumpGlibX* pump_x = reinterpret_cast<MessagePumpGlibX*>(data);
218
219 if (!pump_x->gdksource_) {
220 pump_x->gdksource_ = g_main_current_source();
[email protected]60e102b8c2011-03-23 17:49:02221 if (pump_x->gdksource_)
222 pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch;
[email protected]eae9c062011-01-11 00:50:59223 } else if (!pump_x->IsDispatchingEvent()) {
224 if (event->type != GDK_NOTHING &&
225 pump_x->capture_gdk_events_[event->type]) {
226 // TODO(sad): An X event is caught by the GDK handler. Put it back in the
227 // X queue so that we catch it in the next iteration. When done, the
228 // following DLOG statement will be removed.
229 DLOG(WARNING) << "GDK received an event it shouldn't have";
230 }
231 }
232
233 pump_x->DispatchEvents(event);
234}
235
[email protected]71ad9c6f2010-10-22 16:17:47236void MessagePumpGlibX::InitializeEventsToCapture(void) {
237 // TODO(sad): Decide which events we want to capture and update the tables
238 // accordingly.
239 capture_x_events_[KeyPress] = true;
240 capture_gdk_events_[GDK_KEY_PRESS] = true;
241
242 capture_x_events_[KeyRelease] = true;
243 capture_gdk_events_[GDK_KEY_RELEASE] = true;
[email protected]b2f7ac42010-10-26 18:43:18244
245 capture_x_events_[ButtonPress] = true;
246 capture_gdk_events_[GDK_BUTTON_PRESS] = true;
247
248 capture_x_events_[ButtonRelease] = true;
249 capture_gdk_events_[GDK_BUTTON_RELEASE] = true;
250
251 capture_x_events_[MotionNotify] = true;
252 capture_gdk_events_[GDK_MOTION_NOTIFY] = true;
[email protected]130eeb32010-11-12 17:42:30253
254#if defined(HAVE_XINPUT2)
255 capture_x_events_[GenericEvent] = true;
256#endif
[email protected]71ad9c6f2010-10-22 16:17:47257}
258
[email protected]130eeb32010-11-12 17:42:30259#if defined(HAVE_XINPUT2)
260void MessagePumpGlibX::InitializeXInput2(void) {
261 GdkDisplay* display = gdk_display_get_default();
[email protected]81516d6ea2010-12-30 23:51:15262 if (!display)
263 return;
264
[email protected]130eeb32010-11-12 17:42:30265 Display* xdisplay = GDK_DISPLAY_XDISPLAY(display);
266 int event, err;
267
268 if (!XQueryExtension(xdisplay, "XInputExtension", &xiopcode_, &event, &err)) {
269 DLOG(WARNING) << "X Input extension not available.";
270 xiopcode_ = -1;
271 return;
272 }
273
274 int major = 2, minor = 0;
275 if (XIQueryVersion(xdisplay, &major, &minor) == BadRequest) {
276 DLOG(WARNING) << "XInput2 not supported in the server.";
277 xiopcode_ = -1;
278 return;
279 }
280
[email protected]74434942010-12-05 04:00:59281 // TODO(sad): Here, we only setup so that the X windows created by GTK+ are
282 // setup for XInput2 events. We need a way to listen for XInput2 events for X
283 // windows created by other means (e.g. for context menus).
[email protected]130eeb32010-11-12 17:42:30284 SetupGtkWidgetRealizeNotifier(this);
285
286 // Instead of asking X for the list of devices all the time, let's maintain a
[email protected]009d54e92011-03-25 22:52:54287 // list of pointer devices we care about.
288 // It is not necessary to select for slave devices. XInput2 provides enough
289 // information to the event callback to decide which slave device triggered
290 // the event, thus decide whether the 'pointer event' is a 'mouse event' or a
291 // 'touch event'.
292 // If the touch device has 'GrabDevice' set and 'SendCoreEvents' unset (which
293 // is possible), then the device is detected as a floating device, and a
294 // floating device is not connected to a master device. So it is necessary to
295 // also select on the floating devices.
[email protected]130eeb32010-11-12 17:42:30296 int count = 0;
297 XIDeviceInfo* devices = XIQueryDevice(xdisplay, XIAllDevices, &count);
298 for (int i = 0; i < count; i++) {
299 XIDeviceInfo* devinfo = devices + i;
[email protected]009d54e92011-03-25 22:52:54300 if (devinfo->use == XIFloatingSlave || devinfo->use == XIMasterPointer)
301 pointer_devices_.insert(devinfo->deviceid);
[email protected]130eeb32010-11-12 17:42:30302 }
303 XIFreeDeviceInfo(devices);
304
[email protected]60e102b8c2011-03-23 17:49:02305 // TODO(sad): Select on root for XI_HierarchyChanged so that floats_ and
[email protected]130eeb32010-11-12 17:42:30306 // masters_ can be kept up-to-date. This is a relatively rare event, so we can
307 // put it off for a later time.
308 // Note: It is not necessary to listen for XI_DeviceChanged events.
309}
[email protected]130eeb32010-11-12 17:42:30310#endif // HAVE_XINPUT2
311
[email protected]71ad9c6f2010-10-22 16:17:47312} // namespace base