Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1578)

Side by Side Diff: ui/events/platform/x11/x11_hotplug_event_handler.cc

Issue 688163004: Move X11 device processing to worker threads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix-blocking-touchscreen
Patch Set: Add null Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/events/platform/x11/x11_hotplug_event_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_hotplug_event_handler.h" 5 #include "ui/events/platform/x11/x11_hotplug_event_handler.h"
6 6
7 #include <X11/Xatom.h>
7 #include <X11/extensions/XInput.h> 8 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XInput2.h> 9 #include <X11/extensions/XInput2.h>
9 10
10 #include <algorithm> 11 #include <algorithm>
11 #include <cmath> 12 #include <cmath>
12 #include <set> 13 #include <set>
13 #include <string> 14 #include <string>
14 #include <vector> 15 #include <vector>
15 16
17 #include "base/bind.h"
16 #include "base/command_line.h" 18 #include "base/command_line.h"
19 #include "base/location.h"
17 #include "base/logging.h" 20 #include "base/logging.h"
18 #include "base/process/launch.h" 21 #include "base/process/launch.h"
22 #include "base/single_thread_task_runner.h"
19 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
20 #include "base/sys_info.h" 24 #include "base/sys_info.h"
25 #include "base/thread_task_runner_handle.h"
26 #include "base/threading/worker_pool.h"
27 #include "ui/events/devices/device_data_manager.h"
21 #include "ui/events/devices/device_hotplug_event_observer.h" 28 #include "ui/events/devices/device_hotplug_event_observer.h"
22 #include "ui/events/devices/device_util_linux.h" 29 #include "ui/events/devices/device_util_linux.h"
23 #include "ui/events/devices/input_device.h" 30 #include "ui/events/devices/input_device.h"
24 #include "ui/events/devices/keyboard_device.h" 31 #include "ui/events/devices/keyboard_device.h"
25 #include "ui/events/devices/touchscreen_device.h" 32 #include "ui/events/devices/touchscreen_device.h"
26 #include "ui/gfx/x/x11_types.h" 33 #include "ui/gfx/x/x11_types.h"
27 34
28 namespace ui { 35 namespace ui {
29 36
30 namespace { 37 namespace {
31 38
32 // The name of the xinput device corresponding to the AT internal keyboard. 39 // The name of the xinput device corresponding to the AT internal keyboard.
33 const char kATKeyboardName[] = "AT Translated Set 2 keyboard"; 40 const char kATKeyboardName[] = "AT Translated Set 2 keyboard";
34 41
35 // The prefix of xinput devices corresponding to CrOS EC internal keyboards. 42 // The prefix of xinput devices corresponding to CrOS EC internal keyboards.
36 const char kCrosEcKeyboardPrefix[] = "cros-ec"; 43 const char kCrosEcKeyboardPrefix[] = "cros-ec";
37 44
45 const char* kCachedAtomList[] = {
46 "Abs MT Position X",
47 "Abs MT Position Y",
48 NULL,
49 };
50
51 typedef base::Callback<void(const std::vector<KeyboardDevice>&)>
52 KeyboardDeviceCallback;
53
54 typedef base::Callback<void(const std::vector<TouchscreenDevice>&)>
55 TouchscreenDeviceCallback;
56
57 // Used for updating the state on the UI thread once device information is
58 // parsed on helper threads.
59 struct UiCallbacks {
60 KeyboardDeviceCallback keyboard_callback;
61 TouchscreenDeviceCallback touchscreen_callback;
62 };
63
64 // Stores a copy of the XIValuatorClassInfo values so X11 device processing can
65 // happen on a worker thread. This is needed since X11 structs are not copyable.
66 struct ValuatorClassInfo {
67 ValuatorClassInfo(const XIValuatorClassInfo& info)
68 : label(info.label),
69 max(info.max),
70 min(info.min),
71 mode(info.mode),
72 number(info.number) {}
73
74 Atom label;
75 double max;
76 double min;
77 int mode;
78 int number;
79 };
80
81 // Stores a copy of the XITouchClassInfo values so X11 device processing can
82 // happen on a worker thread. This is needed since X11 structs are not copyable.
83 struct TouchClassInfo {
84 TouchClassInfo(const XITouchClassInfo& info)
85 : mode(info.mode) {}
86
87 int mode;
88 };
89
90 struct DeviceInfo {
91 DeviceInfo(const XIDeviceInfo& device, const base::FilePath& path)
92 : id(device.deviceid),
93 name(device.name),
94 use(device.use),
95 enabled(device.enabled),
96 path(path) {
97 for (int i = 0; i < device.num_classes; ++i) {
98 switch (device.classes[i]->type) {
99 case XIValuatorClass:
100 valuator_class_infos.push_back(ValuatorClassInfo(
101 *reinterpret_cast<XIValuatorClassInfo*>(device.classes[i])));
102 break;
103 #if defined(USE_XI2_MT)
104 case XITouchClass:
105 touch_class_infos.push_back(TouchClassInfo(
106 *reinterpret_cast<XITouchClassInfo*>(device.classes[i])));
107 break;
108 #endif
109 default:
110 break;
111 }
112 }
113 }
114
115 // Unique device identifier.
116 int id;
117
118 // Internal device name.
119 std::string name;
120
121 // Device type (ie: XIMasterPointer)
122 int use;
123
124 // Specifies if the device is enabled and can send events.
125 bool enabled;
126
127 // Path to the actual device (ie: /dev/input/eventXX)
128 base::FilePath path;
129
130 std::vector<ValuatorClassInfo> valuator_class_infos;
131
132 #if defined(USE_XI2_MT)
133 std::vector<TouchClassInfo> touch_class_infos;
134 #endif
135 };
136
137 // X11 display cache used on worker threads. This is filled on the UI thread and
138 // passed in to the worker threads.
139 struct DisplayState {
140 Atom mt_position_x;
141 Atom mt_position_y;
142 };
143
38 // Returns true if |name| is the name of a known keyboard device. Note, this may 144 // Returns true if |name| is the name of a known keyboard device. Note, this may
39 // return false negatives. 145 // return false negatives.
40 bool IsKnownKeyboard(const std::string& name) { 146 bool IsKnownKeyboard(const std::string& name) {
41 std::string lower = base::StringToLowerASCII(name); 147 std::string lower = base::StringToLowerASCII(name);
42 return lower.find("keyboard") != std::string::npos; 148 return lower.find("keyboard") != std::string::npos;
43 } 149 }
44 150
45 // Returns true if |name| is the name of a known internal keyboard device. Note, 151 // Returns true if |name| is the name of a known internal keyboard device. Note,
46 // this may return false negatives. 152 // this may return false negatives.
47 bool IsInternalKeyboard(const std::string& name) { 153 bool IsInternalKeyboard(const std::string& name) {
48 // TODO(rsadam@): Come up with a more generic way of identifying internal 154 // TODO(rsadam@): Come up with a more generic way of identifying internal
49 // keyboards. See crbug.com/420728. 155 // keyboards. See crbug.com/420728.
50 if (name == kATKeyboardName) 156 if (name == kATKeyboardName)
51 return true; 157 return true;
52 return name.compare( 158 return name.compare(
53 0u, strlen(kCrosEcKeyboardPrefix), kCrosEcKeyboardPrefix) == 0; 159 0u, strlen(kCrosEcKeyboardPrefix), kCrosEcKeyboardPrefix) == 0;
54 } 160 }
55 161
56 // Returns true if |name| is the name of a known XTEST device. Note, this may 162 // Returns true if |name| is the name of a known XTEST device. Note, this may
57 // return false negatives. 163 // return false negatives.
58 bool IsTestKeyboard(const std::string& name) { 164 bool IsTestKeyboard(const std::string& name) {
59 return name.find("XTEST") != std::string::npos; 165 return name.find("XTEST") != std::string::npos;
60 } 166 }
61 167
62 // We consider the touchscreen to be internal if it is an I2c device. 168 base::FilePath GetDevicePath(XDisplay* dpy, int device_id) {
63 // With the device id, we can query X to get the device's dev input 169 #if !defined(OS_CHROMEOS)
64 // node eventXXX. Then we search all the dev input nodes registered 170 return base::FilePath();
65 // by I2C devices to see if we can find eventXXX.
66 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) {
67 #if !defined(CHROMEOS)
68 return false;
69 #else 171 #else
70 if (!base::SysInfo::IsRunningOnChromeOS()) 172 if (!base::SysInfo::IsRunningOnChromeOS())
71 return false; 173 return base::FilePath();
72 #endif 174 #endif
73 175
74 // Input device has a property "Device Node" pointing to its dev input node, 176 // Input device has a property "Device Node" pointing to its dev input node,
75 // e.g. Device Node (250): "/dev/input/event8" 177 // e.g. Device Node (250): "/dev/input/event8"
76 Atom device_node = XInternAtom(dpy, "Device Node", False); 178 Atom device_node = XInternAtom(dpy, "Device Node", False);
77 if (device_node == None) 179 if (device_node == None)
78 return false; 180 return base::FilePath();
79 181
80 Atom actual_type; 182 Atom actual_type;
81 int actual_format; 183 int actual_format;
82 unsigned long nitems, bytes_after; 184 unsigned long nitems, bytes_after;
83 unsigned char* data; 185 unsigned char* data;
84 XDevice* dev = XOpenDevice(dpy, device_id); 186 XDevice* dev = XOpenDevice(dpy, device_id);
85 if (!dev) 187 if (!dev)
86 return false; 188 return base::FilePath();
87 189
88 if (XGetDeviceProperty(dpy, 190 if (XGetDeviceProperty(dpy,
89 dev, 191 dev,
90 device_node, 192 device_node,
91 0, 193 0,
92 1000, 194 1000,
93 False, 195 False,
94 AnyPropertyType, 196 AnyPropertyType,
95 &actual_type, 197 &actual_type,
96 &actual_format, 198 &actual_format,
97 &nitems, 199 &nitems,
98 &bytes_after, 200 &bytes_after,
99 &data) != Success) { 201 &data) != Success) {
100 XCloseDevice(dpy, dev); 202 XCloseDevice(dpy, dev);
101 return false; 203 return base::FilePath();
102 } 204 }
103 base::FilePath dev_node_path(reinterpret_cast<char*>(data)); 205
206 std::string path;
207 // Make sure the returned value is a string.
208 if (actual_type == XA_STRING && actual_format == 8)
209 path = reinterpret_cast<char*>(data);
210
104 XFree(data); 211 XFree(data);
105 XCloseDevice(dpy, dev); 212 XCloseDevice(dpy, dev);
106 213
107 return ui::IsTouchscreenInternal(dev_node_path); 214 return base::FilePath(path);
108 } 215 }
109 216
110 } // namespace 217 // Helper used to parse keyboard information. When it is done it uses
111 218 // |reply_runner| and |callback| to update the state on the UI thread.
112 X11HotplugEventHandler::X11HotplugEventHandler( 219 void HandleKeyboardDevicesInWorker(
113 DeviceHotplugEventObserver* delegate) 220 const std::vector<DeviceInfo>& device_infos,
114 : delegate_(delegate) { 221 scoped_refptr<base::TaskRunner> reply_runner,
115 } 222 const KeyboardDeviceCallback& callback) {
116
117 X11HotplugEventHandler::~X11HotplugEventHandler() {
118 }
119
120 void X11HotplugEventHandler::OnHotplugEvent() {
121 const XIDeviceList& device_list =
122 DeviceListCacheX11::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay());
123 HandleTouchscreenDevices(device_list);
124 HandleKeyboardDevices(device_list);
125 }
126
127 void X11HotplugEventHandler::HandleKeyboardDevices(
128 const XIDeviceList& x11_devices) {
129 std::vector<KeyboardDevice> devices; 223 std::vector<KeyboardDevice> devices;
130 224
131 for (int i = 0; i < x11_devices.count; i++) { 225 for (const DeviceInfo& device_info : device_infos) {
132 if (!x11_devices[i].enabled || x11_devices[i].use != XISlaveKeyboard) 226 if (!device_info.enabled || device_info.use != XISlaveKeyboard)
133 continue; // Assume all keyboards are keyboard slaves 227 continue; // Assume all keyboards are keyboard slaves
134 std::string device_name(x11_devices[i].name); 228 std::string device_name(device_info.name);
135 base::TrimWhitespaceASCII(device_name, base::TRIM_TRAILING, &device_name); 229 base::TrimWhitespaceASCII(device_name, base::TRIM_TRAILING, &device_name);
136 if (IsTestKeyboard(device_name)) 230 if (IsTestKeyboard(device_name))
137 continue; // Skip test devices. 231 continue; // Skip test devices.
138 InputDeviceType type; 232 InputDeviceType type;
139 if (IsInternalKeyboard(device_name)) { 233 if (IsInternalKeyboard(device_name)) {
140 type = InputDeviceType::INPUT_DEVICE_INTERNAL; 234 type = InputDeviceType::INPUT_DEVICE_INTERNAL;
141 } else if (IsKnownKeyboard(device_name)) { 235 } else if (IsKnownKeyboard(device_name)) {
142 type = InputDeviceType::INPUT_DEVICE_EXTERNAL; 236 type = InputDeviceType::INPUT_DEVICE_EXTERNAL;
143 } else { 237 } else {
144 type = InputDeviceType::INPUT_DEVICE_UNKNOWN; 238 type = InputDeviceType::INPUT_DEVICE_UNKNOWN;
145 } 239 }
146 devices.push_back( 240 devices.push_back(
147 KeyboardDevice(x11_devices[i].deviceid, type, device_name)); 241 KeyboardDevice(device_info.id, type, device_name));
148 } 242 }
149 delegate_->OnKeyboardDevicesUpdated(devices); 243
244 reply_runner->PostTask(FROM_HERE, base::Bind(callback, devices));
150 } 245 }
151 246
152 void X11HotplugEventHandler::HandleTouchscreenDevices( 247 // Helper used to parse touchscreen information. When it is done it uses
153 const XIDeviceList& x11_devices) { 248 // |reply_runner| and |callback| to update the state on the UI thread.
249 void HandleTouchscreenDevicesInWorker(
250 const std::vector<DeviceInfo>& device_infos,
251 const DisplayState& display_state,
252 scoped_refptr<base::TaskRunner> reply_runner,
253 const TouchscreenDeviceCallback& callback) {
154 std::vector<TouchscreenDevice> devices; 254 std::vector<TouchscreenDevice> devices;
155 Display* display = gfx::GetXDisplay(); 255 if (display_state.mt_position_x == None ||
156 Atom valuator_x = XInternAtom(display, "Abs MT Position X", False); 256 display_state.mt_position_y == None)
157 Atom valuator_y = XInternAtom(display, "Abs MT Position Y", False);
158 if (valuator_x == None || valuator_y == None)
159 return; 257 return;
160 258
161 std::set<int> no_match_touchscreen; 259 std::set<int> no_match_touchscreen;
162 for (int i = 0; i < x11_devices.count; i++) { 260 for (const DeviceInfo& device_info : device_infos) {
163 if (!x11_devices[i].enabled || x11_devices[i].use != XIFloatingSlave) 261 if (!device_info.enabled || device_info.use != XIFloatingSlave)
164 continue; // Assume all touchscreens are floating slaves 262 continue; // Assume all touchscreens are floating slaves
165 263
166 double max_x = -1.0; 264 double max_x = -1.0;
167 double max_y = -1.0; 265 double max_y = -1.0;
168 bool is_direct_touch = false; 266 bool is_direct_touch = false;
169 267
170 for (int j = 0; j < x11_devices[i].num_classes; j++) { 268 for (const ValuatorClassInfo& valuator : device_info.valuator_class_infos) {
171 XIAnyClassInfo* class_info = x11_devices[i].classes[j]; 269 if (display_state.mt_position_x == valuator.label) {
172 270 // Ignore X axis valuator with unexpected properties
173 if (class_info->type == XIValuatorClass) { 271 if (valuator.number == 0 && valuator.mode == Absolute &&
174 XIValuatorClassInfo* valuator_info = 272 valuator.min == 0.0) {
175 reinterpret_cast<XIValuatorClassInfo*>(class_info); 273 max_x = valuator.max;
176 274 }
177 if (valuator_x == valuator_info->label) { 275 } else if (display_state.mt_position_y == valuator.label) {
178 // Ignore X axis valuator with unexpected properties 276 // Ignore Y axis valuator with unexpected properties
179 if (valuator_info->number == 0 && valuator_info->mode == Absolute && 277 if (valuator.number == 1 && valuator.mode == Absolute &&
180 valuator_info->min == 0.0) { 278 valuator.min == 0.0) {
181 max_x = valuator_info->max; 279 max_y = valuator.max;
182 }
183 } else if (valuator_y == valuator_info->label) {
184 // Ignore Y axis valuator with unexpected properties
185 if (valuator_info->number == 1 && valuator_info->mode == Absolute &&
186 valuator_info->min == 0.0) {
187 max_y = valuator_info->max;
188 }
189 } 280 }
190 } 281 }
282 }
283
191 #if defined(USE_XI2_MT) 284 #if defined(USE_XI2_MT)
192 if (class_info->type == XITouchClass) { 285 for (const TouchClassInfo& info : device_info.touch_class_infos) {
193 XITouchClassInfo* touch_info = 286 is_direct_touch = info.mode == XIDirectTouch;
194 reinterpret_cast<XITouchClassInfo*>(class_info); 287 }
195 is_direct_touch = touch_info->mode == XIDirectTouch;
196 }
197 #endif 288 #endif
198 }
199 289
200 // Touchscreens should have absolute X and Y axes, and be direct touch 290 // Touchscreens should have absolute X and Y axes, and be direct touch
201 // devices. 291 // devices.
202 if (max_x > 0.0 && max_y > 0.0 && is_direct_touch) { 292 if (max_x > 0.0 && max_y > 0.0 && is_direct_touch) {
203 InputDeviceType type = 293 InputDeviceType type = IsTouchscreenInternal(device_info.path)
204 IsTouchscreenInternal(display, x11_devices[i].deviceid)
205 ? InputDeviceType::INPUT_DEVICE_INTERNAL 294 ? InputDeviceType::INPUT_DEVICE_INTERNAL
206 : InputDeviceType::INPUT_DEVICE_EXTERNAL; 295 : InputDeviceType::INPUT_DEVICE_EXTERNAL;
207 std::string name(x11_devices[i].name);
208 // |max_x| and |max_y| are inclusive values, so we need to add 1 to get 296 // |max_x| and |max_y| are inclusive values, so we need to add 1 to get
209 // the size. 297 // the size.
210 devices.push_back(TouchscreenDevice( 298 devices.push_back(TouchscreenDevice(
211 x11_devices[i].deviceid, 299 device_info.id,
212 type, 300 type,
213 name, 301 device_info.name,
214 gfx::Size(max_x + 1, max_y + 1))); 302 gfx::Size(max_x + 1, max_y + 1)));
215 } 303 }
216 } 304 }
217 305
218 delegate_->OnTouchscreenDevicesUpdated(devices); 306 reply_runner->PostTask(FROM_HERE, base::Bind(callback, devices));
307 }
308
309 // Called on a worker thread to parse the device information.
310 void HandleHotplugEventInWorker(
311 const std::vector<DeviceInfo>& devices,
312 const DisplayState& display_state,
313 scoped_refptr<base::TaskRunner> reply_runner,
314 const UiCallbacks& callbacks) {
315 HandleTouchscreenDevicesInWorker(
316 devices, display_state, reply_runner, callbacks.touchscreen_callback);
317 HandleKeyboardDevicesInWorker(
318 devices, reply_runner, callbacks.keyboard_callback);
319 }
320
321 DeviceHotplugEventObserver* GetHotplugEventObserver() {
322 return DeviceDataManager::GetInstance();
323 }
324
325 void OnKeyboardDevices(const std::vector<KeyboardDevice>& devices) {
326 GetHotplugEventObserver()->OnKeyboardDevicesUpdated(devices);
327 }
328
329 void OnTouchscreenDevices(const std::vector<TouchscreenDevice>& devices) {
330 GetHotplugEventObserver()->OnTouchscreenDevicesUpdated(devices);
331 }
332
333 } // namespace
334
335 X11HotplugEventHandler::X11HotplugEventHandler()
336 : atom_cache_(gfx::GetXDisplay(), kCachedAtomList) {
337 }
338
339 X11HotplugEventHandler::~X11HotplugEventHandler() {
340 }
341
342 void X11HotplugEventHandler::OnHotplugEvent() {
343 const XIDeviceList& device_list =
344 DeviceListCacheX11::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay());
345 Display* display = gfx::GetXDisplay();
346
347 std::vector<DeviceInfo> device_infos;
348 for (int i = 0; i < device_list.count; ++i)
349 device_infos.push_back(DeviceInfo(
350 device_list[i], GetDevicePath(display, device_list[i].deviceid)));
351
352 // X11 is not thread safe, so first get all the required state.
353 DisplayState display_state;
354 display_state.mt_position_x = atom_cache_.GetAtom("Abs MT Position X");
355 display_state.mt_position_y = atom_cache_.GetAtom("Abs MT Position Y");
356
357 UiCallbacks callbacks;
358 callbacks.keyboard_callback = base::Bind(&OnKeyboardDevices);
359 callbacks.touchscreen_callback = base::Bind(&OnTouchscreenDevices);
360
361 // Parsing the device information may block, so delegate the operation to a
362 // worker thread. Once the device information is extracted the parsed devices
363 // will be returned via the callbacks.
364 base::WorkerPool::PostTask(FROM_HERE,
365 base::Bind(&HandleHotplugEventInWorker,
366 device_infos,
367 display_state,
368 base::ThreadTaskRunnerHandle::Get(),
369 callbacks),
370 true /* task_is_slow */);
219 } 371 }
220 372
221 } // namespace ui 373 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/platform/x11/x11_hotplug_event_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698