| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_pump_aurax11.h" | 5 #include "base/message_pump_aurax11.h" |
| 6 | 6 |
| 7 #include <glib.h> | 7 #include <glib.h> |
| 8 #include <X11/X.h> |
| 8 #include <X11/extensions/XInput2.h> | 9 #include <X11/extensions/XInput2.h> |
| 9 #include <X11/XKBlib.h> | 10 #include <X11/XKBlib.h> |
| 10 | 11 |
| 11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) { | 17 gboolean XSourcePrepare(GSource* source, gint* timeout_ms) { |
| 17 if (XPending(base::MessagePumpAuraX11::GetDefaultXDisplay())) | 18 if (XPending(base::MessagePumpAuraX11::GetDefaultXDisplay())) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 41 | 42 |
| 42 // The connection is essentially a global that's accessed through a static | 43 // The connection is essentially a global that's accessed through a static |
| 43 // method and destroyed whenever ~MessagePumpAuraX11() is called. We do this | 44 // method and destroyed whenever ~MessagePumpAuraX11() is called. We do this |
| 44 // for historical reasons so user code can call | 45 // for historical reasons so user code can call |
| 45 // MessagePumpForUI::GetDefaultXDisplay() where MessagePumpForUI is a typedef | 46 // MessagePumpForUI::GetDefaultXDisplay() where MessagePumpForUI is a typedef |
| 46 // to whatever type in the current build. | 47 // to whatever type in the current build. |
| 47 // | 48 // |
| 48 // TODO(erg): This can be changed to something more sane like | 49 // TODO(erg): This can be changed to something more sane like |
| 49 // MessagePumpAuraX11::Current()->display() once MessagePumpGtk goes away. | 50 // MessagePumpAuraX11::Current()->display() once MessagePumpGtk goes away. |
| 50 Display* g_xdisplay = NULL; | 51 Display* g_xdisplay = NULL; |
| 52 int g_xinput_opcode = -1; |
| 51 | 53 |
| 52 bool InitializeXInput2Internal() { | 54 bool InitializeXInput2Internal() { |
| 53 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay(); | 55 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay(); |
| 54 if (!display) | 56 if (!display) |
| 55 return false; | 57 return false; |
| 56 | 58 |
| 57 int event, err; | 59 int event, err; |
| 58 | 60 |
| 59 int xiopcode; | 61 int xiopcode; |
| 60 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { | 62 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { |
| 61 DVLOG(1) << "X Input extension not available."; | 63 DVLOG(1) << "X Input extension not available."; |
| 62 return false; | 64 return false; |
| 63 } | 65 } |
| 66 g_xinput_opcode = xiopcode; |
| 64 | 67 |
| 65 #if defined(USE_XI2_MT) | 68 #if defined(USE_XI2_MT) |
| 66 // USE_XI2_MT also defines the required XI2 minor minimum version. | 69 // USE_XI2_MT also defines the required XI2 minor minimum version. |
| 67 int major = 2, minor = USE_XI2_MT; | 70 int major = 2, minor = USE_XI2_MT; |
| 68 #else | 71 #else |
| 69 int major = 2, minor = 0; | 72 int major = 2, minor = 0; |
| 70 #endif | 73 #endif |
| 71 if (XIQueryVersion(display, &major, &minor) == BadRequest) { | 74 if (XIQueryVersion(display, &major, &minor) == BadRequest) { |
| 72 DVLOG(1) << "XInput2 not supported in the server."; | 75 DVLOG(1) << "XInput2 not supported in the server."; |
| 73 return false; | 76 return false; |
| 74 } | 77 } |
| 75 #if defined(USE_XI2_MT) | 78 #if defined(USE_XI2_MT) |
| 76 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) { | 79 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) { |
| 77 DVLOG(1) << "XI version on server is " << major << "." << minor << ". " | 80 DVLOG(1) << "XI version on server is " << major << "." << minor << ". " |
| 78 << "But 2." << USE_XI2_MT << " is required."; | 81 << "But 2." << USE_XI2_MT << " is required."; |
| 79 return false; | 82 return false; |
| 80 } | 83 } |
| 81 #endif | 84 #endif |
| 82 | 85 |
| 83 return true; | 86 return true; |
| 84 } | 87 } |
| 85 | 88 |
| 89 Window FindEventTarget(const base::NativeEvent& xev) { |
| 90 Window target = xev->xany.window; |
| 91 if (xev->type == GenericEvent && |
| 92 static_cast<XIEvent*>(xev->xcookie.data)->extension == g_xinput_opcode) { |
| 93 target = static_cast<XIDeviceEvent*>(xev->xcookie.data)->event; |
| 94 } |
| 95 return target; |
| 96 } |
| 97 |
| 86 bool InitializeXInput2() { | 98 bool InitializeXInput2() { |
| 87 static bool xinput2_supported = InitializeXInput2Internal(); | 99 static bool xinput2_supported = InitializeXInput2Internal(); |
| 88 return xinput2_supported; | 100 return xinput2_supported; |
| 89 } | 101 } |
| 90 | 102 |
| 91 bool InitializeXkb() { | 103 bool InitializeXkb() { |
| 92 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay(); | 104 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay(); |
| 93 if (!display) | 105 if (!display) |
| 94 return false; | 106 return false; |
| 95 | 107 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 } | 278 } |
| 267 return false; | 279 return false; |
| 268 } | 280 } |
| 269 | 281 |
| 270 void MessagePumpAuraX11::DidProcessXEvent(XEvent* xevent) { | 282 void MessagePumpAuraX11::DidProcessXEvent(XEvent* xevent) { |
| 271 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent)); | 283 FOR_EACH_OBSERVER(MessagePumpObserver, observers(), DidProcessEvent(xevent)); |
| 272 } | 284 } |
| 273 | 285 |
| 274 MessagePumpDispatcher* MessagePumpAuraX11::GetDispatcherForXEvent( | 286 MessagePumpDispatcher* MessagePumpAuraX11::GetDispatcherForXEvent( |
| 275 const base::NativeEvent& xev) const { | 287 const base::NativeEvent& xev) const { |
| 276 ::Window x_window = xev->xany.window; | 288 ::Window x_window = FindEventTarget(xev); |
| 277 if (xev->type == GenericEvent) { | |
| 278 XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(xev->xcookie.data); | |
| 279 x_window = xievent->event; | |
| 280 } | |
| 281 DispatchersMap::const_iterator it = dispatchers_.find(x_window); | 289 DispatchersMap::const_iterator it = dispatchers_.find(x_window); |
| 282 return it != dispatchers_.end() ? it->second : NULL; | 290 return it != dispatchers_.end() ? it->second : NULL; |
| 283 } | 291 } |
| 284 | 292 |
| 285 bool MessagePumpAuraX11::Dispatch(const base::NativeEvent& xev) { | 293 bool MessagePumpAuraX11::Dispatch(const base::NativeEvent& xev) { |
| 286 // MappingNotify events (meaning that the keyboard or pointer buttons have | 294 // MappingNotify events (meaning that the keyboard or pointer buttons have |
| 287 // been remapped) aren't associated with a window; send them to all | 295 // been remapped) aren't associated with a window; send them to all |
| 288 // dispatchers. | 296 // dispatchers. |
| 289 if (xev->type == MappingNotify) { | 297 if (xev->type == MappingNotify) { |
| 290 for (DispatchersMap::const_iterator it = dispatchers_.begin(); | 298 for (DispatchersMap::const_iterator it = dispatchers_.begin(); |
| 291 it != dispatchers_.end(); ++it) { | 299 it != dispatchers_.end(); ++it) { |
| 292 it->second->Dispatch(xev); | 300 it->second->Dispatch(xev); |
| 293 } | 301 } |
| 294 return true; | 302 return true; |
| 295 } | 303 } |
| 296 if (xev->xany.window == x_root_window_) { | 304 |
| 305 if (FindEventTarget(xev) == x_root_window_) { |
| 297 for (Dispatchers::const_iterator it = root_window_dispatchers_.begin(); | 306 for (Dispatchers::const_iterator it = root_window_dispatchers_.begin(); |
| 298 it != root_window_dispatchers_.end(); | 307 it != root_window_dispatchers_.end(); |
| 299 ++it) { | 308 ++it) { |
| 300 (*it)->Dispatch(xev); | 309 (*it)->Dispatch(xev); |
| 301 } | 310 } |
| 302 return true; | 311 return true; |
| 303 } | 312 } |
| 304 MessagePumpDispatcher* dispatcher = GetDispatcherForXEvent(xev); | 313 MessagePumpDispatcher* dispatcher = GetDispatcherForXEvent(xev); |
| 305 return dispatcher ? dispatcher->Dispatch(xev) : true; | 314 return dispatcher ? dispatcher->Dispatch(xev) : true; |
| 306 } | 315 } |
| 307 | 316 |
| 308 } // namespace base | 317 } // namespace base |
| OLD | NEW |