Roll src/third_party/skia 591a2ca:b3fe1b8
[chromium-blink-merge.git] / ui / events / platform / x11 / x11_event_source.cc
blob5101f2703a3bff44f677e1c64db1a2ec09532980
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>
8 #include <X11/X.h>
9 #include <X11/Xlib.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"
19 namespace ui {
21 namespace {
23 int g_xinput_opcode = -1;
25 bool InitializeXInput2(XDisplay* display) {
26 if (!display)
27 return false;
29 int event, err;
31 int xiopcode;
32 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) {
33 DVLOG(1) << "X Input extension not available.";
34 return false;
36 g_xinput_opcode = xiopcode;
38 int major = 2, minor = 2;
39 if (XIQueryVersion(display, &major, &minor) == BadRequest) {
40 DVLOG(1) << "XInput2 not supported in the server.";
41 return false;
43 if (major < 2 || (major == 2 && minor < 2)) {
44 DVLOG(1) << "XI version on server is " << major << "." << minor << ". "
45 << "But 2.2 is required.";
46 return false;
49 return true;
52 bool InitializeXkb(XDisplay* display) {
53 if (!display)
54 return false;
56 int opcode, event, error;
57 int major = XkbMajorVersion;
58 int minor = XkbMinorVersion;
59 if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) {
60 DVLOG(1) << "Xkb extension not available.";
61 return false;
64 // Ask the server not to send KeyRelease event when the user holds down a key.
65 // crbug.com/138092
66 Bool supported_return;
67 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) {
68 DVLOG(1) << "XKB not supported in the server.";
69 return false;
72 return true;
75 } // namespace
77 X11EventSource::X11EventSource(XDisplay* display)
78 : display_(display),
79 continue_stream_(true) {
80 CHECK(display_);
81 DeviceDataManagerX11::CreateInstance();
82 InitializeXInput2(display_);
83 InitializeXkb(display_);
86 X11EventSource::~X11EventSource() {
89 // static
90 X11EventSource* X11EventSource::GetInstance() {
91 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance());
94 ////////////////////////////////////////////////////////////////////////////////
95 // X11EventSource, public
97 void X11EventSource::DispatchXEvents() {
98 DCHECK(display_);
99 // Handle all pending events.
100 // It may be useful to eventually align this event dispatch with vsync, but
101 // not yet.
102 continue_stream_ = true;
103 while (XPending(display_) && continue_stream_) {
104 XEvent xevent;
105 XNextEvent(display_, &xevent);
106 DispatchEvent(&xevent);
110 void X11EventSource::BlockUntilWindowMapped(XID window) {
111 XEvent event;
112 do {
113 // Block until there's a message of |event_mask| type on |w|. Then remove
114 // it from the queue and stuff it in |event|.
115 XWindowEvent(display_, window, StructureNotifyMask, &event);
116 DispatchEvent(&event);
117 } while (event.type != MapNotify);
120 ////////////////////////////////////////////////////////////////////////////////
121 // X11EventSource, private
123 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) {
124 bool have_cookie = false;
125 if (xevent->type == GenericEvent &&
126 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) {
127 have_cookie = true;
130 uint32_t action = PlatformEventSource::DispatchEvent(xevent);
131 if (xevent->type == GenericEvent &&
132 xevent->xgeneric.evtype == XI_HierarchyChanged) {
133 ui::UpdateDeviceList();
134 hotplug_event_handler_->OnHotplugEvent();
137 if (have_cookie)
138 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie);
139 return action;
142 void X11EventSource::StopCurrentEventStream() {
143 continue_stream_ = false;
146 void X11EventSource::OnDispatcherListChanged() {
147 if (!hotplug_event_handler_) {
148 hotplug_event_handler_.reset(new X11HotplugEventHandler());
149 // Force the initial device query to have an update list of active devices.
150 hotplug_event_handler_->OnHotplugEvent();
154 } // namespace ui