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

Side by Side Diff: ui/events/x/hotplug_event_handler_x11.cc

Issue 618283003: Adds special support to the device manager for keyboards devices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add checks for Daisy. Created 6 years, 2 months 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/x/hotplug_event_handler_x11.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/x/hotplug_event_handler_x11.h" 5 #include "ui/events/x/hotplug_event_handler_x11.h"
6 6
7 #include <X11/extensions/XInput.h> 7 #include <X11/extensions/XInput.h>
8 #include <X11/extensions/XInput2.h> 8 #include <X11/extensions/XInput2.h>
9 9
10 #include <algorithm>
10 #include <cmath> 11 #include <cmath>
11 #include <set> 12 #include <set>
12 #include <string> 13 #include <string>
13 #include <vector> 14 #include <vector>
14 15
15 #include "base/command_line.h" 16 #include "base/command_line.h"
16 #include "base/files/file_enumerator.h" 17 #include "base/files/file_enumerator.h"
17 #include "base/logging.h" 18 #include "base/logging.h"
18 #include "base/process/launch.h" 19 #include "base/process/launch.h"
19 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
20 #include "base/sys_info.h" 21 #include "base/sys_info.h"
21 #include "ui/events/device_hotplug_event_observer.h" 22 #include "ui/events/device_hotplug_event_observer.h"
23 #include "ui/events/input_device.h"
24 #include "ui/events/keyboard_device.h"
22 #include "ui/events/touchscreen_device.h" 25 #include "ui/events/touchscreen_device.h"
23 #include "ui/gfx/x/x11_types.h" 26 #include "ui/gfx/x/x11_types.h"
24 27
25 namespace ui { 28 namespace ui {
26 29
27 namespace { 30 namespace {
28 31
32 // The prefixes of the known xinput devices corresponding to internal keyboards.
33 const char* kInternalKeyboardPrefixes[] = {
34 "AT Translated Set 2 keyboard",
35 "cros-ec"
flackr 2014/10/14 17:41:36 Since "cros-ec" is a prefix but "AT Translated Set
rsadam 2014/10/14 17:55:02 Done.
36 };
37
38 // Returns true if |name| is the name of a known keyboard device. Note, this may
39 // return false negatives.
40 bool IsKnownKeyboard(const std::string& name) {
41 std::string lower(name);
42 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
43 return lower.find("keyboard") != std::string::npos;
44 }
45
46 // Returns true if |name| is the name of a known internal keyboard device. Note,
47 // this may return false negatives.
48 bool IsInternalKeyboard(const std::string& name) {
49 // TODO(rsadam@): Come up with a more generic way of identifying internal
50 // keyboards. See crbug.com/420728.
51 for (size_t i = 0; i < arraysize(kInternalKeyboardPrefixes); i++) {
52 if (name.find(kInternalKeyboardPrefixes[i]) != std::string::npos)
flackr 2014/10/14 17:41:36 Only return true if it matches at position 0 (i.e.
rsadam 2014/10/14 17:55:02 Done.
53 return true;
54 }
55 return false;
56 }
57
58 // Returns true if |name| is the name of a known XTEST device. Note, this may
59 // return false negatives.
60 bool IsTestKeyboard(const std::string& name) {
61 return name.find("XTEST") != std::string::npos;
62 }
63
29 // We consider the touchscreen to be internal if it is an I2c device. 64 // We consider the touchscreen to be internal if it is an I2c device.
30 // With the device id, we can query X to get the device's dev input 65 // With the device id, we can query X to get the device's dev input
31 // node eventXXX. Then we search all the dev input nodes registered 66 // node eventXXX. Then we search all the dev input nodes registered
32 // by I2C devices to see if we can find eventXXX. 67 // by I2C devices to see if we can find eventXXX.
33 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) { 68 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) {
34 using base::FileEnumerator; 69 using base::FileEnumerator;
35 using base::FilePath; 70 using base::FilePath;
36 71
37 #if !defined(CHROMEOS) 72 #if !defined(CHROMEOS)
38 return false; 73 return false;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 : delegate_(delegate) { 144 : delegate_(delegate) {
110 } 145 }
111 146
112 HotplugEventHandlerX11::~HotplugEventHandlerX11() { 147 HotplugEventHandlerX11::~HotplugEventHandlerX11() {
113 } 148 }
114 149
115 void HotplugEventHandlerX11::OnHotplugEvent() { 150 void HotplugEventHandlerX11::OnHotplugEvent() {
116 const XIDeviceList& device_list = 151 const XIDeviceList& device_list =
117 DeviceListCacheX::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay()); 152 DeviceListCacheX::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay());
118 HandleTouchscreenDevices(device_list); 153 HandleTouchscreenDevices(device_list);
154 HandleKeyboardDevices(device_list);
155 }
156
157 void HotplugEventHandlerX11::HandleKeyboardDevices(
158 const XIDeviceList& x11_devices) {
159 std::vector<KeyboardDevice> devices;
160
161 for (int i = 0; i < x11_devices.count; i++) {
162 if (!x11_devices[i].enabled || x11_devices[i].use != XISlaveKeyboard)
163 continue; // Assume all keyboards are keyboard slaves
164 std::string device_name(x11_devices[i].name);
165 base::TrimWhitespaceASCII(device_name, base::TRIM_TRAILING,
166 &device_name);
167 InputDeviceType type;
168 if (IsTestKeyboard(device_name))
169 continue; // Skip test devices.
170
171 if (IsInternalKeyboard(device_name)) {
172 type = InputDeviceType::INTERNAL;
173 } else if (IsKnownKeyboard(device_name)) {
174 type = InputDeviceType::EXTERNAL;
175 } else {
176 type = InputDeviceType::UNKNOWN;
177 }
178 devices.push_back(KeyboardDevice(x11_devices[i].deviceid,
179 type, device_name));
180 }
181 delegate_->OnKeyboardDevicesUpdated(devices);
119 } 182 }
120 183
121 void HotplugEventHandlerX11::HandleTouchscreenDevices( 184 void HotplugEventHandlerX11::HandleTouchscreenDevices(
122 const XIDeviceList& x11_devices) { 185 const XIDeviceList& x11_devices) {
123 std::vector<TouchscreenDevice> devices; 186 std::vector<TouchscreenDevice> devices;
124 Display* display = gfx::GetXDisplay(); 187 Display* display = gfx::GetXDisplay();
125 Atom valuator_x = XInternAtom(display, "Abs MT Position X", False); 188 Atom valuator_x = XInternAtom(display, "Abs MT Position X", False);
126 Atom valuator_y = XInternAtom(display, "Abs MT Position Y", False); 189 Atom valuator_y = XInternAtom(display, "Abs MT Position Y", False);
127 if (valuator_x == None || valuator_y == None) 190 if (valuator_x == None || valuator_y == None)
128 return; 191 return;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 XITouchClassInfo* touch_info = 225 XITouchClassInfo* touch_info =
163 reinterpret_cast<XITouchClassInfo*>(class_info); 226 reinterpret_cast<XITouchClassInfo*>(class_info);
164 is_direct_touch = touch_info->mode == XIDirectTouch; 227 is_direct_touch = touch_info->mode == XIDirectTouch;
165 } 228 }
166 #endif 229 #endif
167 } 230 }
168 231
169 // Touchscreens should have absolute X and Y axes, and be direct touch 232 // Touchscreens should have absolute X and Y axes, and be direct touch
170 // devices. 233 // devices.
171 if (width > 0.0 && height > 0.0 && is_direct_touch) { 234 if (width > 0.0 && height > 0.0 && is_direct_touch) {
172 bool is_internal = 235 InputDeviceType type =
173 IsTouchscreenInternal(display, x11_devices[i].deviceid); 236 IsTouchscreenInternal(display, x11_devices[i].deviceid) ?
237 InputDeviceType::INTERNAL : InputDeviceType::EXTERNAL;
238 std::string name(x11_devices[i].name);
174 devices.push_back(TouchscreenDevice( 239 devices.push_back(TouchscreenDevice(
175 x11_devices[i].deviceid, gfx::Size(width, height), is_internal)); 240 x11_devices[i].deviceid, type, name, gfx::Size(width, height)));
176 } 241 }
177 } 242 }
178 243
179 delegate_->OnTouchscreenDevicesUpdated(devices); 244 delegate_->OnTouchscreenDevicesUpdated(devices);
180 } 245 }
181 246
182 } // namespace ui 247 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/x/hotplug_event_handler_x11.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698