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 int major
= 2, minor
= 2;
39 if (XIQueryVersion(display
, &major
, &minor
) == BadRequest
) {
40 DVLOG(1) << "XInput2 not supported in the server.";
43 if (major
< 2 || (major
== 2 && minor
< 2)) {
44 DVLOG(1) << "XI version on server is " << major
<< "." << minor
<< ". "
45 << "But 2.2 is required.";
52 bool InitializeXkb(XDisplay
* display
) {
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.";
64 // Ask the server not to send KeyRelease event when the user holds down a key.
66 Bool supported_return
;
67 if (!XkbSetDetectableAutoRepeat(display
, True
, &supported_return
)) {
68 DVLOG(1) << "XKB not supported in the server.";
77 X11EventSource::X11EventSource(XDisplay
* display
)
79 continue_stream_(true) {
81 DeviceDataManagerX11::CreateInstance();
82 InitializeXInput2(display_
);
83 InitializeXkb(display_
);
86 X11EventSource::~X11EventSource() {
90 X11EventSource
* X11EventSource::GetInstance() {
91 return static_cast<X11EventSource
*>(PlatformEventSource::GetInstance());
94 ////////////////////////////////////////////////////////////////////////////////
95 // X11EventSource, public
97 void X11EventSource::DispatchXEvents() {
99 // Handle all pending events.
100 // It may be useful to eventually align this event dispatch with vsync, but
102 continue_stream_
= true;
103 while (XPending(display_
) && continue_stream_
) {
105 XNextEvent(display_
, &xevent
);
106 DispatchEvent(&xevent
);
110 void X11EventSource::BlockUntilWindowMapped(XID window
) {
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
)) {
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();
138 XFreeEventData(xevent
->xgeneric
.display
, &xevent
->xcookie
);
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();