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