OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "ui/events/platform/x11/x11_event_source.h" | 5 #include "ui/events/platform/x11/x11_event_source.h" |
6 | 6 |
7 #include <X11/extensions/XInput2.h> | 7 #include <X11/XKBlib.h> |
8 #include <X11/X.h> | |
9 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
10 #include <X11/XKBlib.h> | |
11 | 9 |
12 #include "base/logging.h" | 10 #include "base/logging.h" |
13 #include "ui/events/devices/x11/device_data_manager_x11.h" | 11 #include "ui/events/devices/x11/device_data_manager_x11.h" |
14 #include "ui/events/event_utils.h" | 12 #include "ui/events/event_utils.h" |
15 #include "ui/events/platform/platform_event_dispatcher.h" | 13 #include "ui/events/platform/platform_event_dispatcher.h" |
16 #include "ui/events/platform/x11/x11_hotplug_event_handler.h" | 14 #include "ui/events/platform/x11/x11_hotplug_event_handler.h" |
17 #include "ui/gfx/x/x11_types.h" | |
18 | 15 |
19 namespace ui { | 16 namespace ui { |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 int g_xinput_opcode = -1; | |
24 | |
25 bool InitializeXInput2(XDisplay* display) { | |
26 if (!display) | |
27 return false; | |
28 | |
29 int event, err; | |
30 | |
31 int xiopcode; | |
32 if (!XQueryExtension(display, "XInputExtension", &xiopcode, &event, &err)) { | |
33 DVLOG(1) << "X Input extension not available."; | |
34 return false; | |
35 } | |
36 g_xinput_opcode = xiopcode; | |
37 | |
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; | |
42 } | |
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; | |
47 } | |
48 | |
49 return true; | |
50 } | |
51 | |
52 bool InitializeXkb(XDisplay* display) { | 20 bool InitializeXkb(XDisplay* display) { |
53 if (!display) | 21 if (!display) |
54 return false; | 22 return false; |
55 | 23 |
56 int opcode, event, error; | 24 int opcode, event, error; |
57 int major = XkbMajorVersion; | 25 int major = XkbMajorVersion; |
58 int minor = XkbMinorVersion; | 26 int minor = XkbMinorVersion; |
59 if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) { | 27 if (!XkbQueryExtension(display, &opcode, &event, &error, &major, &minor)) { |
60 DVLOG(1) << "Xkb extension not available."; | 28 DVLOG(1) << "Xkb extension not available."; |
61 return false; | 29 return false; |
62 } | 30 } |
63 | 31 |
64 // Ask the server not to send KeyRelease event when the user holds down a key. | 32 // Ask the server not to send KeyRelease event when the user holds down a key. |
65 // crbug.com/138092 | 33 // crbug.com/138092 |
66 Bool supported_return; | 34 Bool supported_return; |
67 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) { | 35 if (!XkbSetDetectableAutoRepeat(display, True, &supported_return)) { |
68 DVLOG(1) << "XKB not supported in the server."; | 36 DVLOG(1) << "XKB not supported in the server."; |
69 return false; | 37 return false; |
70 } | 38 } |
71 | 39 |
72 return true; | 40 return true; |
73 } | 41 } |
74 | 42 |
75 } // namespace | 43 } // namespace |
76 | 44 |
77 X11EventSource::X11EventSource(XDisplay* display) | 45 X11EventSource* X11EventSource::instance_ = nullptr; |
78 : display_(display), | 46 |
79 continue_stream_(true) { | 47 X11EventSource::X11EventSource(X11EventSourceDelegate* delegate, |
80 CHECK(display_); | 48 XDisplay* display) |
| 49 : delegate_(delegate), display_(display), continue_stream_(true) { |
| 50 DCHECK(!instance_); |
| 51 instance_ = this; |
| 52 |
| 53 DCHECK(delegate_); |
| 54 DCHECK(display_); |
81 DeviceDataManagerX11::CreateInstance(); | 55 DeviceDataManagerX11::CreateInstance(); |
82 InitializeXInput2(display_); | |
83 InitializeXkb(display_); | 56 InitializeXkb(display_); |
84 } | 57 } |
85 | 58 |
86 X11EventSource::~X11EventSource() { | 59 X11EventSource::~X11EventSource() { |
| 60 DCHECK_EQ(this, instance_); |
| 61 instance_ = nullptr; |
87 } | 62 } |
88 | 63 |
89 // static | 64 // static |
90 X11EventSource* X11EventSource::GetInstance() { | 65 X11EventSource* X11EventSource::GetInstance() { |
91 return static_cast<X11EventSource*>(PlatformEventSource::GetInstance()); | 66 DCHECK(instance_); |
| 67 return instance_; |
92 } | 68 } |
93 | 69 |
94 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
95 // X11EventSource, public | 71 // X11EventSource, public |
96 | 72 |
97 void X11EventSource::DispatchXEvents() { | 73 void X11EventSource::DispatchXEvents() { |
98 DCHECK(display_); | 74 DCHECK(display_); |
99 // Handle all pending events. | 75 // Handle all pending events. |
100 // It may be useful to eventually align this event dispatch with vsync, but | 76 // It may be useful to eventually align this event dispatch with vsync, but |
101 // not yet. | 77 // not yet. |
102 continue_stream_ = true; | 78 continue_stream_ = true; |
103 while (XPending(display_) && continue_stream_) { | 79 while (XPending(display_) && continue_stream_) { |
104 XEvent xevent; | 80 XEvent xevent; |
105 XNextEvent(display_, &xevent); | 81 XNextEvent(display_, &xevent); |
106 ExtractCookieDataDispatchEvent(&xevent); | 82 ExtractCookieDataDispatchEvent(&xevent); |
107 } | 83 } |
108 } | 84 } |
109 | 85 |
110 void X11EventSource::BlockUntilWindowMapped(XID window) { | 86 void X11EventSource::BlockUntilWindowMapped(XID window) { |
111 XEvent event; | 87 XEvent event; |
112 do { | 88 do { |
113 // Block until there's a message of |event_mask| type on |w|. Then remove | 89 // Block until there's a message of |event_mask| type on |w|. Then remove |
114 // it from the queue and stuff it in |event|. | 90 // it from the queue and stuff it in |event|. |
115 XWindowEvent(display_, window, StructureNotifyMask, &event); | 91 XWindowEvent(display_, window, StructureNotifyMask, &event); |
116 ExtractCookieDataDispatchEvent(&event); | 92 ExtractCookieDataDispatchEvent(&event); |
117 } while (event.type != MapNotify); | 93 } while (event.type != MapNotify); |
118 } | 94 } |
119 | 95 |
120 //////////////////////////////////////////////////////////////////////////////// | 96 //////////////////////////////////////////////////////////////////////////////// |
121 // X11EventSource, private | 97 // X11EventSource, protected |
122 | 98 |
123 uint32_t X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { | 99 void X11EventSource::ExtractCookieDataDispatchEvent(XEvent* xevent) { |
124 bool have_cookie = false; | 100 bool have_cookie = false; |
125 if (xevent->type == GenericEvent && | 101 if (xevent->type == GenericEvent && |
126 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { | 102 XGetEventData(xevent->xgeneric.display, &xevent->xcookie)) { |
127 have_cookie = true; | 103 have_cookie = true; |
128 } | 104 } |
129 uint32_t action = DispatchEvent(xevent); | 105 delegate_->ProcessXEvent(xevent); |
| 106 PostDispatchEvent(xevent); |
130 if (have_cookie) | 107 if (have_cookie) |
131 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); | 108 XFreeEventData(xevent->xgeneric.display, &xevent->xcookie); |
132 return action; | |
133 } | 109 } |
134 | 110 |
135 uint32_t X11EventSource::DispatchEvent(XEvent* xevent) { | 111 void X11EventSource::PostDispatchEvent(XEvent* xevent) { |
136 uint32_t action = PlatformEventSource::DispatchEvent(xevent); | |
137 if (xevent->type == GenericEvent && | 112 if (xevent->type == GenericEvent && |
138 (xevent->xgeneric.evtype == XI_HierarchyChanged || | 113 (xevent->xgeneric.evtype == XI_HierarchyChanged || |
139 xevent->xgeneric.evtype == XI_DeviceChanged)) { | 114 xevent->xgeneric.evtype == XI_DeviceChanged)) { |
140 ui::UpdateDeviceList(); | 115 ui::UpdateDeviceList(); |
141 hotplug_event_handler_->OnHotplugEvent(); | 116 hotplug_event_handler_->OnHotplugEvent(); |
142 } | 117 } |
143 | 118 |
144 if (xevent->type == EnterNotify && | 119 if (xevent->type == EnterNotify && |
145 xevent->xcrossing.detail != NotifyInferior && | 120 xevent->xcrossing.detail != NotifyInferior && |
146 xevent->xcrossing.mode != NotifyUngrab) { | 121 xevent->xcrossing.mode != NotifyUngrab) { |
147 // Clear stored scroll data | 122 // Clear stored scroll data |
148 ui::DeviceDataManagerX11::GetInstance()->InvalidateScrollClasses(); | 123 ui::DeviceDataManagerX11::GetInstance()->InvalidateScrollClasses(); |
149 } | 124 } |
150 return action; | |
151 } | 125 } |
152 | 126 |
153 void X11EventSource::StopCurrentEventStream() { | 127 void X11EventSource::StopCurrentEventStream() { |
154 continue_stream_ = false; | 128 continue_stream_ = false; |
155 } | 129 } |
156 | 130 |
157 void X11EventSource::OnDispatcherListChanged() { | 131 void X11EventSource::OnDispatcherListChanged() { |
158 if (!hotplug_event_handler_) { | 132 if (!hotplug_event_handler_) { |
159 hotplug_event_handler_.reset(new X11HotplugEventHandler()); | 133 hotplug_event_handler_.reset(new X11HotplugEventHandler()); |
160 // Force the initial device query to have an update list of active devices. | 134 // Force the initial device query to have an update list of active devices. |
161 hotplug_event_handler_->OnHotplugEvent(); | 135 hotplug_event_handler_->OnHotplugEvent(); |
162 } | 136 } |
163 } | 137 } |
164 | 138 |
165 } // namespace ui | 139 } // namespace ui |
OLD | NEW |