message_pump_glib_x.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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 "base/message_pump_glib_x.h" 6 7#include <gdk/gdkx.h> 8#if defined(HAVE_XINPUT2) 9#include <X11/extensions/XInput2.h> 10#else 11#include <X11/Xlib.h> 12#endif 13 14#include "base/message_pump_glib_x_dispatch.h" 15 16namespace { 17 18gboolean PlaceholderDispatch(GSource* source, 19 GSourceFunc cb, 20 gpointer data) { 21 return TRUE; 22} 23 24#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 35 // TODO(sad): Do we need to set a flag on |window| to make sure we don't 36 // select for the same GdkWindow multiple times? Does it matter? 37 msgpump->SetupXInput2ForXWindow(GDK_WINDOW_XID(window)); 38 39 return true; 40} 41 42// We need to capture all the GDK windows that get created, and start 43// listening for XInput2 events. So we setup a callback to the 'realize' 44// signal for GTK+ widgets, so that whenever the signal triggers for any 45// GtkWidget, which means the GtkWidget should now have a GdkWindow, we can 46// setup XInput2 events for the GdkWindow. 47void SetupGtkWidgetRealizeNotifier(base::MessagePumpGlibX* msgpump) { 48 guint signal_id; 49 gpointer klass = g_type_class_ref(GTK_TYPE_WIDGET); 50 51 g_signal_parse_name("realize", GTK_TYPE_WIDGET, &signal_id, NULL, FALSE); 52 g_signal_add_emission_hook(signal_id, 0, GtkWidgetRealizeCallback, 53 static_cast<gpointer>(msgpump), NULL); 54 55 g_type_class_unref(klass); 56} 57 58#endif // HAVE_XINPUT2 59 60} // namespace 61 62namespace base { 63 64MessagePumpGlibX::MessagePumpGlibX() : base::MessagePumpForUI(), 65#if defined(HAVE_XINPUT2) 66 xiopcode_(-1), 67 masters_(), 68 slaves_(), 69#endif 70 gdksource_(NULL), 71 dispatching_event_(false), 72 capture_x_events_(0), 73 capture_gdk_events_(0) { 74 gdk_event_handler_set(&EventDispatcherX, this, NULL); 75 76#if defined(HAVE_XINPUT2) 77 InitializeXInput2(); 78#endif 79 InitializeEventsToCapture(); 80} 81 82MessagePumpGlibX::~MessagePumpGlibX() { 83} 84 85bool MessagePumpGlibX::RunOnce(GMainContext* context, bool block) { 86 GdkDisplay* gdisp = gdk_display_get_default(); 87 if (!gdisp) 88 return MessagePumpForUI::RunOnce(context, block); 89 90 Display* display = GDK_DISPLAY_XDISPLAY(gdisp); 91 bool should_quit = false; 92 93 if (XPending(display)) { 94 XEvent xev; 95 XPeekEvent(display, &xev); 96 if (capture_x_events_[xev.type] 97#if defined(HAVE_XINPUT2) 98 && (xev.type != GenericEvent || xev.xcookie.extension == xiopcode_) 99#endif 100 ) { 101 XNextEvent(display, &xev); 102 103#if defined(HAVE_XINPUT2) 104 bool have_cookie = false; 105 if (xev.type == GenericEvent && 106 XGetEventData(xev.xgeneric.display, &xev.xcookie)) { 107 have_cookie = true; 108 } 109#endif 110 111 MessagePumpGlibXDispatcher::DispatchStatus status = 112 static_cast<MessagePumpGlibXDispatcher*> 113 (GetDispatcher())->Dispatch(&xev); 114 115 if (status == MessagePumpGlibXDispatcher::EVENT_QUIT) { 116 should_quit = true; 117 Quit(); 118 } else if (status == MessagePumpGlibXDispatcher::EVENT_IGNORED) { 119 DLOG(WARNING) << "Event (" << xev.type << ") not handled."; 120 121 // TODO(sad): It is necessary to put back the event so that the default 122 // GDK events handler can take care of it. Without this, it is 123 // impossible to use the omnibox at the moment. However, this will 124 // eventually be removed once the omnibox code is updated for touchui. 125 XPutBackEvent(display, &xev); 126 if (gdksource_) 127 gdksource_->source_funcs->dispatch = gdkdispatcher_; 128 g_main_context_iteration(context, FALSE); 129 } 130 131#if defined(HAVE_XINPUT2) 132 if (have_cookie) { 133 XFreeEventData(xev.xgeneric.display, &xev.xcookie); 134 } 135#endif 136 } else { 137 // TODO(sad): A couple of extra events can still sneak in during this. 138 // Those should be sent back to the X queue from the dispatcher 139 // EventDispatcherX. 140 if (gdksource_) 141 gdksource_->source_funcs->dispatch = gdkdispatcher_; 142 g_main_context_iteration(context, FALSE); 143 } 144 } 145 146 if (should_quit) 147 return true; 148 149 bool retvalue; 150 if (gdksource_) { 151 // Replace the dispatch callback of the GDK event source temporarily so that 152 // it doesn't read events from X. 153 gboolean (*cb)(GSource*, GSourceFunc, void*) = 154 gdksource_->source_funcs->dispatch; 155 gdksource_->source_funcs->dispatch = PlaceholderDispatch; 156 157 dispatching_event_ = true; 158 retvalue = g_main_context_iteration(context, block); 159 dispatching_event_ = false; 160 161 gdksource_->source_funcs->dispatch = cb; 162 } else { 163 retvalue = g_main_context_iteration(context, block); 164 } 165 166 return retvalue; 167} 168 169void MessagePumpGlibX::InitializeEventsToCapture(void) { 170 // TODO(sad): Decide which events we want to capture and update the tables 171 // accordingly. 172 capture_x_events_[KeyPress] = true; 173 capture_gdk_events_[GDK_KEY_PRESS] = true; 174 175 capture_x_events_[KeyRelease] = true; 176 capture_gdk_events_[GDK_KEY_RELEASE] = true; 177 178 capture_x_events_[ButtonPress] = true; 179 capture_gdk_events_[GDK_BUTTON_PRESS] = true; 180 181 capture_x_events_[ButtonRelease] = true; 182 capture_gdk_events_[GDK_BUTTON_RELEASE] = true; 183 184 capture_x_events_[MotionNotify] = true; 185 capture_gdk_events_[GDK_MOTION_NOTIFY] = true; 186 187#if defined(HAVE_XINPUT2) 188 capture_x_events_[GenericEvent] = true; 189#endif 190} 191 192#if defined(HAVE_XINPUT2) 193void MessagePumpGlibX::InitializeXInput2(void) { 194 GdkDisplay* display = gdk_display_get_default(); 195 if (!display) 196 return; 197 198 Display* xdisplay = GDK_DISPLAY_XDISPLAY(display); 199 int event, err; 200 201 if (!XQueryExtension(xdisplay, "XInputExtension", &xiopcode_, &event, &err)) { 202 DLOG(WARNING) << "X Input extension not available."; 203 xiopcode_ = -1; 204 return; 205 } 206 207 int major = 2, minor = 0; 208 if (XIQueryVersion(xdisplay, &major, &minor) == BadRequest) { 209 DLOG(WARNING) << "XInput2 not supported in the server."; 210 xiopcode_ = -1; 211 return; 212 } 213 214 // TODO(sad): Here, we only setup so that the X windows created by GTK+ are 215 // setup for XInput2 events. We need a way to listen for XInput2 events for X 216 // windows created by other means (e.g. for context menus). 217 SetupGtkWidgetRealizeNotifier(this); 218 219 // Instead of asking X for the list of devices all the time, let's maintain a 220 // list of slave (physical) and master (virtual) pointer devices. 221 int count = 0; 222 XIDeviceInfo* devices = XIQueryDevice(xdisplay, XIAllDevices, &count); 223 for (int i = 0; i < count; i++) { 224 XIDeviceInfo* devinfo = devices + i; 225 if (devinfo->use == XISlavePointer) { 226 slaves_.insert(devinfo->deviceid); 227 } else if (devinfo->use == XIMasterPointer) { 228 masters_.insert(devinfo->deviceid); 229 } 230 // We do not need to care about XIFloatingSlave, because the callback for 231 // XI_HierarchyChanged event will take care of it. 232 } 233 XIFreeDeviceInfo(devices); 234 235 // TODO(sad): Select on root for XI_HierarchyChanged so that slaves_ and 236 // masters_ can be kept up-to-date. This is a relatively rare event, so we can 237 // put it off for a later time. 238 // Note: It is not necessary to listen for XI_DeviceChanged events. 239} 240 241void MessagePumpGlibX::SetupXInput2ForXWindow(Window xwindow) { 242 Display* xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); 243 244 // Setup mask for mouse events. 245 unsigned char mask[(XI_LASTEVENT + 7)/8]; 246 memset(mask, 0, sizeof(mask)); 247 248 XISetMask(mask, XI_ButtonPress); 249 XISetMask(mask, XI_ButtonRelease); 250 XISetMask(mask, XI_Motion); 251 252 // It is necessary to select only for the master devices. XInput2 provides 253 // enough information to the event callback to decide which slave device 254 // triggered the event, thus decide whether the 'pointer event' is a 'mouse 255 // event' or a 'touch event'. So it is not necessary to select for the slave 256 // devices here. 257 XIEventMask evmasks[masters_.size()]; 258 int count = 0; 259 for (std::set<int>::const_iterator iter = masters_.begin(); 260 iter != masters_.end(); 261 ++iter, ++count) { 262 evmasks[count].deviceid = *iter; 263 evmasks[count].mask_len = sizeof(mask); 264 evmasks[count].mask = mask; 265 } 266 267 XISelectEvents(xdisplay, xwindow, evmasks, masters_.size()); 268 269 // TODO(sad): Setup masks for keyboard events. 270 271 XFlush(xdisplay); 272} 273 274#endif // HAVE_XINPUT2 275 276void MessagePumpGlibX::EventDispatcherX(GdkEvent* event, gpointer data) { 277 MessagePumpGlibX* pump_x = reinterpret_cast<MessagePumpGlibX*>(data); 278 279 if (!pump_x->gdksource_) { 280 pump_x->gdksource_ = g_main_current_source(); 281 pump_x->gdkdispatcher_ = pump_x->gdksource_->source_funcs->dispatch; 282 } else if (!pump_x->IsDispatchingEvent()) { 283 if (event->type != GDK_NOTHING && 284 pump_x->capture_gdk_events_[event->type]) { 285 // TODO(sad): An X event is caught by the GDK handler. Put it back in the 286 // X queue so that we catch it in the next iteration. When done, the 287 // following DLOG statement will be removed. 288 DLOG(WARNING) << "GDK received an event it shouldn't have"; 289 } 290 } 291 292 pump_x->DispatchEvents(event); 293} 294 295} // namespace base 296