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/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 name of the xinput device corresponding to the internal keyboard. | |
33 const char kInternalKeyboardName[] = "AT Translated Set 2 keyboard"; | |
34 | |
35 // Filters out all devices that that are not known keyboards. This may return | |
flackr
2014/10/04 00:35:12
nit: This function only determines devices that ar
rsadam
2014/10/04 18:46:27
Done.
| |
36 // false negatives. | |
37 bool IsKnownKeyboard(const std::string& name) { | |
38 std::string lower(name); | |
39 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); | |
40 return lower.find("keyboard") != std::string::npos; | |
41 } | |
42 | |
43 // Filters out all devices that that are XTEST keyboards. | |
flackr
2014/10/04 00:35:12
Ditto.
rsadam
2014/10/04 18:46:27
Done.
| |
44 bool IsTestKeyboard(const std::string& name) { | |
45 return name.find("XTEST") != std::string::npos; | |
46 } | |
47 | |
29 // We consider the touchscreen to be internal if it is an I2c device. | 48 // 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 | 49 // 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 | 50 // node eventXXX. Then we search all the dev input nodes registered |
32 // by I2C devices to see if we can find eventXXX. | 51 // by I2C devices to see if we can find eventXXX. |
33 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) { | 52 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) { |
34 using base::FileEnumerator; | 53 using base::FileEnumerator; |
35 using base::FilePath; | 54 using base::FilePath; |
36 | 55 |
37 #if !defined(CHROMEOS) | 56 #if !defined(CHROMEOS) |
38 return false; | 57 return false; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 : delegate_(delegate) { | 128 : delegate_(delegate) { |
110 } | 129 } |
111 | 130 |
112 HotplugEventHandlerX11::~HotplugEventHandlerX11() { | 131 HotplugEventHandlerX11::~HotplugEventHandlerX11() { |
113 } | 132 } |
114 | 133 |
115 void HotplugEventHandlerX11::OnHotplugEvent() { | 134 void HotplugEventHandlerX11::OnHotplugEvent() { |
116 const XIDeviceList& device_list = | 135 const XIDeviceList& device_list = |
117 DeviceListCacheX::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay()); | 136 DeviceListCacheX::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay()); |
118 HandleTouchscreenDevices(device_list); | 137 HandleTouchscreenDevices(device_list); |
138 HandleKeyboardDevices(device_list); | |
139 } | |
140 | |
141 void HotplugEventHandlerX11::HandleKeyboardDevices( | |
142 const XIDeviceList& x11_devices) { | |
143 std::vector<KeyboardDevice> devices; | |
144 | |
145 for (int i = 0; i < x11_devices.count; i++) { | |
146 if (!x11_devices[i].enabled || x11_devices[i].use != XISlaveKeyboard) | |
147 continue; // Assume all keyboards are keyboard slaves | |
148 std::string device_name(x11_devices[i].name); | |
149 base::TrimWhitespaceASCII(device_name, base::TRIM_TRAILING, | |
150 &device_name); | |
151 InputDeviceType type; | |
152 if (IsTestKeyboard(device_name)) { | |
153 continue; | |
flackr
2014/10/04 00:35:12
No need for else if after a continue. Perhaps put
rsadam
2014/10/04 18:46:27
Done.
| |
154 } else if (device_name == kInternalKeyboardName) { | |
155 type = InputDeviceType::INTERNAL; | |
156 } else if (IsKnownKeyboard(device_name)) { | |
157 type = InputDeviceType::EXTERNAL; | |
158 } else { | |
159 type = InputDeviceType::UNKNOWN; | |
160 } | |
161 devices.push_back(KeyboardDevice(x11_devices[i].deviceid, | |
162 type, device_name)); | |
163 } | |
164 delegate_->OnKeyboardDevicesUpdated(devices); | |
119 } | 165 } |
120 | 166 |
121 void HotplugEventHandlerX11::HandleTouchscreenDevices( | 167 void HotplugEventHandlerX11::HandleTouchscreenDevices( |
122 const XIDeviceList& x11_devices) { | 168 const XIDeviceList& x11_devices) { |
123 std::vector<TouchscreenDevice> devices; | 169 std::vector<TouchscreenDevice> devices; |
124 Display* display = gfx::GetXDisplay(); | 170 Display* display = gfx::GetXDisplay(); |
125 Atom valuator_x = XInternAtom(display, "Abs MT Position X", False); | 171 Atom valuator_x = XInternAtom(display, "Abs MT Position X", False); |
126 Atom valuator_y = XInternAtom(display, "Abs MT Position Y", False); | 172 Atom valuator_y = XInternAtom(display, "Abs MT Position Y", False); |
127 if (valuator_x == None || valuator_y == None) | 173 if (valuator_x == None || valuator_y == None) |
128 return; | 174 return; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 XITouchClassInfo* touch_info = | 208 XITouchClassInfo* touch_info = |
163 reinterpret_cast<XITouchClassInfo*>(class_info); | 209 reinterpret_cast<XITouchClassInfo*>(class_info); |
164 is_direct_touch = touch_info->mode == XIDirectTouch; | 210 is_direct_touch = touch_info->mode == XIDirectTouch; |
165 } | 211 } |
166 #endif | 212 #endif |
167 } | 213 } |
168 | 214 |
169 // Touchscreens should have absolute X and Y axes, and be direct touch | 215 // Touchscreens should have absolute X and Y axes, and be direct touch |
170 // devices. | 216 // devices. |
171 if (width > 0.0 && height > 0.0 && is_direct_touch) { | 217 if (width > 0.0 && height > 0.0 && is_direct_touch) { |
172 bool is_internal = | 218 InputDeviceType type = |
173 IsTouchscreenInternal(display, x11_devices[i].deviceid); | 219 IsTouchscreenInternal(display, x11_devices[i].deviceid) ? |
220 InputDeviceType::INTERNAL : InputDeviceType::EXTERNAL; | |
221 std::string name(x11_devices[i].name); | |
174 devices.push_back(TouchscreenDevice( | 222 devices.push_back(TouchscreenDevice( |
175 x11_devices[i].deviceid, gfx::Size(width, height), is_internal)); | 223 x11_devices[i].deviceid, type, name, gfx::Size(width, height))); |
176 } | 224 } |
177 } | 225 } |
178 | 226 |
179 delegate_->OnTouchscreenDevicesUpdated(devices); | 227 delegate_->OnTouchscreenDevicesUpdated(devices); |
180 } | 228 } |
181 | 229 |
182 } // namespace ui | 230 } // namespace ui |
OLD | NEW |