[email protected] | 60e102b8c | 2011-03-23 17:49:02 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 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 "base/message_pump_glib_x.h" |
| 6 | |
| 7 | #include <gdk/gdkx.h> |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 8 | #if defined(HAVE_XINPUT2) |
| 9 | #include <X11/extensions/XInput2.h> |
| 10 | #else |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 11 | #include <X11/Xlib.h> |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 12 | #endif |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 13 | |
[email protected] | b2f7ac4 | 2010-10-26 18:43:18 | [diff] [blame] | 14 | #include "base/message_pump_glib_x_dispatch.h" |
| 15 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | gboolean PlaceholderDispatch(GSource* source, |
| 19 | GSourceFunc cb, |
| 20 | gpointer data) { |
| 21 | return TRUE; |
| 22 | } |
| 23 | |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 24 | #if defined(HAVE_XINPUT2) |
| 25 | |
| 26 | // Setup XInput2 select for the GtkWidget. |
| 27 | gboolean 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] | 0cbe5a1 | 2011-03-01 23:38:23 | [diff] [blame] | 35 | 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 40 | // 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] | 4b9baf9d | 2011-03-02 22:56:11 | [diff] [blame] | 52 | static guint realize_signal_id = 0; |
| 53 | static guint realize_hook_id = 0; |
| 54 | |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 55 | void SetupGtkWidgetRealizeNotifier(base::MessagePumpGlibX* msgpump) { |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 56 | gpointer klass = g_type_class_ref(GTK_TYPE_WIDGET); |
| 57 | |
[email protected] | 4b9baf9d | 2011-03-02 22:56:11 | [diff] [blame] | 58 | 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 62 | |
| 63 | g_type_class_unref(klass); |
| 64 | } |
| 65 | |
[email protected] | 4b9baf9d | 2011-03-02 22:56:11 | [diff] [blame] | 66 | void 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 73 | #endif // HAVE_XINPUT2 |
| 74 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 75 | } // namespace |
| 76 | |
| 77 | namespace base { |
| 78 | |
| 79 | MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(), |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 80 | #if defined(HAVE_XINPUT2) |
| 81 | xiopcode_(-1), |
[email protected] | 009d54e9 | 2011-03-25 22:52:54 | [diff] [blame] | 82 | pointer_devices_(), |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 83 | #endif |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 84 | 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 90 | #if defined(HAVE_XINPUT2) |
| 91 | InitializeXInput2(); |
| 92 | #endif |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 93 | InitializeEventsToCapture(); |
| 94 | } |
| 95 | |
| 96 | MessagePumpGlibX::~MessagePumpGlibX() { |
[email protected] | 4b9baf9d | 2011-03-02 22:56:11 | [diff] [blame] | 97 | #if defined(HAVE_XINPUT2) |
| 98 | RemoveGtkWidgetRealizeNotifier(); |
| 99 | #endif |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 102 | #if defined(HAVE_XINPUT2) |
| 103 | void 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] | 009d54e9 | 2011-03-25 22:52:54 | [diff] [blame] | 114 | XIEventMask evmasks[pointer_devices_.size()]; |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 115 | int count = 0; |
[email protected] | 009d54e9 | 2011-03-25 22:52:54 | [diff] [blame] | 116 | for (std::set<int>::const_iterator iter = pointer_devices_.begin(); |
| 117 | iter != pointer_devices_.end(); |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 118 | ++iter, ++count) { |
| 119 | evmasks[count].deviceid = *iter; |
| 120 | evmasks[count].mask_len = sizeof(mask); |
| 121 | evmasks[count].mask = mask; |
| 122 | } |
| 123 | |
[email protected] | 009d54e9 | 2011-03-25 22:52:54 | [diff] [blame] | 124 | XISelectEvents(xdisplay, xwindow, evmasks, pointer_devices_.size()); |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 125 | |
| 126 | // TODO(sad): Setup masks for keyboard events. |
| 127 | |
| 128 | XFlush(xdisplay); |
| 129 | } |
| 130 | #endif // HAVE_XINPUT2 |
| 131 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 132 | bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) { |
| 133 | GdkDisplay* gdisp = gdk_display_get_default(); |
[email protected] | 4b9baf9d | 2011-03-02 22:56:11 | [diff] [blame] | 134 | if (!gdisp || !GetDispatcher()) |
[email protected] | 81516d6ea | 2010-12-30 23:51:15 | [diff] [blame] | 135 | return MessagePumpForUI::RunOnce(context, block); |
| 136 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 137 | Display* display = GDK_DISPLAY_XDISPLAY(gdisp); |
[email protected] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 138 | bool should_quit = false; |
| 139 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 140 | if (XPending(display)) { |
| 141 | XEvent xev; |
| 142 | XPeekEvent(display, &xev); |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 143 | if (capture_x_events_[xev.type] |
| 144 | #if defined(HAVE_XINPUT2) |
| 145 | && (xev.type != GenericEvent || xev.xcookie.extension == xiopcode_) |
| 146 | #endif |
| 147 | ) { |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 148 | XNextEvent(display, &xev); |
| 149 | |
[email protected] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 150 | #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] | 0c304949 | 2011-02-11 16:02:40 | [diff] [blame] | 160 | (GetDispatcher())->DispatchX(&xev); |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 161 | |
[email protected] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 162 | if (status == MessagePumpGlibXDispatcher::EVENT_QUIT) { |
| 163 | should_quit = true; |
| 164 | Quit(); |
| 165 | } else if (status == MessagePumpGlibXDispatcher::EVENT_IGNORED) { |
[email protected] | b2f7ac4 | 2010-10-26 18:43:18 | [diff] [blame] | 166 | DLOG(WARNING) << "Event (" << xev.type << ") not handled."; |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 167 | |
| 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] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 173 | if (gdksource_) |
| 174 | gdksource_->source_funcs->dispatch = gdkdispatcher_; |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 175 | g_main_context_iteration(context, FALSE); |
[email protected] | b2f7ac4 | 2010-10-26 18:43:18 | [diff] [blame] | 176 | } |
[email protected] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 177 | |
| 178 | #if defined(HAVE_XINPUT2) |
| 179 | if (have_cookie) { |
| 180 | XFreeEventData(xev.xgeneric.display, &xev.xcookie); |
| 181 | } |
| 182 | #endif |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 183 | } else { |
[email protected] | b2f7ac4 | 2010-10-26 18:43:18 | [diff] [blame] | 184 | // 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] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 187 | if (gdksource_) |
| 188 | gdksource_->source_funcs->dispatch = gdkdispatcher_; |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 189 | g_main_context_iteration(context, FALSE); |
| 190 | } |
| 191 | } |
| 192 | |
[email protected] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 193 | if (should_quit) |
| 194 | return true; |
| 195 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 196 | 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] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 216 | void 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] | 60e102b8c | 2011-03-23 17:49:02 | [diff] [blame] | 221 | if (pump_x->gdksource_) |
| 222 | pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch; |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 223 | } 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] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 236 | void 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] | b2f7ac4 | 2010-10-26 18:43:18 | [diff] [blame] | 244 | |
| 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 253 | |
| 254 | #if defined(HAVE_XINPUT2) |
| 255 | capture_x_events_[GenericEvent] = true; |
| 256 | #endif |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 259 | #if defined(HAVE_XINPUT2) |
| 260 | void MessagePumpGlibX::InitializeXInput2(void) { |
| 261 | GdkDisplay* display = gdk_display_get_default(); |
[email protected] | 81516d6ea | 2010-12-30 23:51:15 | [diff] [blame] | 262 | if (!display) |
| 263 | return; |
| 264 | |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 265 | 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] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 281 | // 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 284 | SetupGtkWidgetRealizeNotifier(this); |
| 285 | |
| 286 | // Instead of asking X for the list of devices all the time, let's maintain a |
[email protected] | 009d54e9 | 2011-03-25 22:52:54 | [diff] [blame] | 287 | // 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 296 | 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] | 009d54e9 | 2011-03-25 22:52:54 | [diff] [blame] | 300 | if (devinfo->use == XIFloatingSlave || devinfo->use == XIMasterPointer) |
| 301 | pointer_devices_.insert(devinfo->deviceid); |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 302 | } |
| 303 | XIFreeDeviceInfo(devices); |
| 304 | |
[email protected] | 60e102b8c | 2011-03-23 17:49:02 | [diff] [blame] | 305 | // TODO(sad): Select on root for XI_HierarchyChanged so that floats_ and |
[email protected] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 306 | // 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] | 130eeb3 | 2010-11-12 17:42:30 | [diff] [blame] | 310 | #endif // HAVE_XINPUT2 |
| 311 | |
[email protected] | 71ad9c6f | 2010-10-22 16:17:47 | [diff] [blame] | 312 | } // namespace base |