1 // Copyright 2014 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.
5 #include "ui/events/platform/x11/x11_event_source.h"
7 #include <X11/extensions/XInput2.h>
10 #include <X11/XKBlib.h>
12 #include "base/logging.h"
13 #include "ui/events/devices/x11/device_data_manager_x11.h"
14 #include "ui/events/event_utils.h"
15 #include "ui/events/platform/platform_event_dispatcher.h"
16 #include "ui/events/platform/x11/x11_hotplug_event_handler.h"
17 #include "ui/gfx/x/x11_types.h"
23 int g_xinput_opcode = -1;
25 bool InitializeXInput2(XDisplay* display) {
32 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) {
33 DVLOG(1) << "X Input extension not available.";
36 g_xinput_opcode = xiopcode;
38 #if defined(USE_XI2_MT)
39 // USE_XI2_MT also defines the required XI2 minor minimum version.
40 int major = 2, minor = USE_XI2_MT;
42 int major = 2, minor = 0;
44 if (XIQueryVersion(display, &major, &minor) == BadRequest) {
45 DVLOG(1) << "XInput2 not supported in the server.";
48 #if defined(USE_XI2_MT)
49 if (major < 2 || (major == 2 && minor < USE_XI2_MT)) {
50 DVLOG(1) << "XI version on server is " << major << "." << minor << ". "
51 << "But 2." << USE_XI2_MT << " is required.";
59 bool InitializeXkb(XDisplay* display) {
63 int opcode, event, error;
64 int major = XkbMajorVersion;
65 int minor = XkbMinorVersion;
66 if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) {
67 DVLOG(1) << "Xkb extension not available.";
71 // Ask the server not to send KeyRelease event when the user holds down a key.
73 Bool supported_return;
74 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) {
75 DVLOG(1) << "XKB not supported in the server.";
84 X11EventSource::X11EventSource(XDisplay* display)
86 continue_stream_(true) {
88 DeviceDataManagerX11::CreateInstance();
89 hotplug_event_handler_.reset(
90 new X11HotplugEventHandler(DeviceDataManager::GetInstance()));
91 InitializeXInput2(display_);
92 InitializeXkb(display_);
94 // Force the initial device query to have an update list of active devices.
95 hotplug_event_handler_->OnHotplugEvent();
98 X11EventSource::~X11EventSource() {
102 X11EventSource* X11EventSource::GetInstance() {
103 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance());
106 ////////////////////////////////////////////////////////////////////////////////
107 // X11EventSource, public
109 void X11EventSource::DispatchXEvents() {
111 // Handle all pending events.
112 // It may be useful to eventually align this event dispatch with vsync, but
114 continue_stream_ = true;
115 while (XPending(display_) && continue_stream_) {
117 XNextEvent(display_, &xevent);
118 DispatchEvent(&xevent);
122 void X11EventSource::BlockUntilWindowMapped(XID window) {
125 // Block until there's a message of |event_mask| type on |w|. Then remove
126 // it from the queue and stuff it in |event|.
127 XWindowEvent(display_, window, StructureNotifyMask, &event);
128 DispatchEvent(&event);
129 } while (event.type != MapNotify);
132 ////////////////////////////////////////////////////////////////////////////////
133 // X11EventSource, private
135 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) {
136 bool have_cookie = false;
137 if (xevent->type == GenericEvent &&
138 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) {
142 uint32_t action = PlatformEventSource::DispatchEvent(xevent);
143 if (xevent->type == GenericEvent &&
144 xevent->xgeneric.evtype == XI_HierarchyChanged) {
145 ui::UpdateDeviceList();
146 hotplug_event_handler_->OnHotplugEvent();
150 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie);
154 void X11EventSource::StopCurrentEventStream() {
155 continue_stream_ = false;