Chromium Code Reviews| 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 an internal keyboard. | |
| 33 const char* kInternalKeyboardName = "AT Translated Set 2 keyboard"; | |
|
flackr
2014/10/14 19:34:26
Let's name these constants to match the modules pe
rsadam
2014/10/14 22:24:15
Done.
| |
| 34 | |
| 35 // The prefixes of other known xinput devices corresponding to internal | |
| 36 // keyboards. | |
| 37 const char* kInternalKeyboardPrefixes[] = { | |
|
flackr
2014/10/14 19:34:26
nit: Don't use array for a single item.
rsadam
2014/10/14 22:24:15
Done.
| |
| 38 "cros-ec" | |
| 39 }; | |
| 40 | |
| 41 // Returns true if |name| is the name of a known keyboard device. Note, this may | |
| 42 // return false negatives. | |
| 43 bool IsKnownKeyboard(const std::string& name) { | |
| 44 std::string lower(name); | |
| 45 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); | |
| 46 return lower.find("keyboard") != std::string::npos; | |
| 47 } | |
| 48 | |
| 49 // Returns true if |name| is the name of a known internal keyboard device. Note, | |
| 50 // this may return false negatives. | |
| 51 bool IsInternalKeyboard(const std::string& name) { | |
| 52 // TODO(rsadam@): Come up with a more generic way of identifying internal | |
| 53 // keyboards. See crbug.com/420728. | |
| 54 if (name == kInternalKeyboardName) | |
| 55 return true; | |
| 56 for (size_t i = 0; i < arraysize(kInternalKeyboardPrefixes); i++) { | |
| 57 if (name.find(kInternalKeyboardPrefixes[i]) == 0) | |
|
flackr
2014/10/14 19:34:26
nit: Doesn't this need to be 0u (since string::fin
rsadam
2014/10/14 22:24:15
Done.
| |
| 58 return true; | |
| 59 } | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 // Returns true if |name| is the name of a known XTEST device. Note, this may | |
| 64 // return false negatives. | |
| 65 bool IsTestKeyboard(const std::string& name) { | |
| 66 return name.find("XTEST") != std::string::npos; | |
| 67 } | |
| 68 | |
| 29 // We consider the touchscreen to be internal if it is an I2c device. | 69 // 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 | 70 // 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 | 71 // node eventXXX. Then we search all the dev input nodes registered |
| 32 // by I2C devices to see if we can find eventXXX. | 72 // by I2C devices to see if we can find eventXXX. |
| 33 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) { | 73 bool IsTouchscreenInternal(XDisplay* dpy, int device_id) { |
| 34 using base::FileEnumerator; | 74 using base::FileEnumerator; |
| 35 using base::FilePath; | 75 using base::FilePath; |
| 36 | 76 |
| 37 #if !defined(CHROMEOS) | 77 #if !defined(CHROMEOS) |
| 38 return false; | 78 return false; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 : delegate_(delegate) { | 149 : delegate_(delegate) { |
| 110 } | 150 } |
| 111 | 151 |
| 112 HotplugEventHandlerX11::~HotplugEventHandlerX11() { | 152 HotplugEventHandlerX11::~HotplugEventHandlerX11() { |
| 113 } | 153 } |
| 114 | 154 |
| 115 void HotplugEventHandlerX11::OnHotplugEvent() { | 155 void HotplugEventHandlerX11::OnHotplugEvent() { |
| 116 const XIDeviceList& device_list = | 156 const XIDeviceList& device_list = |
| 117 DeviceListCacheX::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay()); | 157 DeviceListCacheX::GetInstance()->GetXI2DeviceList(gfx::GetXDisplay()); |
| 118 HandleTouchscreenDevices(device_list); | 158 HandleTouchscreenDevices(device_list); |
| 159 HandleKeyboardDevices(device_list); | |
| 160 } | |
| 161 | |
| 162 void HotplugEventHandlerX11::HandleKeyboardDevices( | |
| 163 const XIDeviceList& x11_devices) { | |
| 164 std::vector<KeyboardDevice> devices; | |
| 165 | |
| 166 for (int i = 0; i < x11_devices.count; i++) { | |
| 167 if (!x11_devices[i].enabled || x11_devices[i].use != XISlaveKeyboard) | |
| 168 continue; // Assume all keyboards are keyboard slaves | |
| 169 std::string device_name(x11_devices[i].name); | |
| 170 base::TrimWhitespaceASCII(device_name, base::TRIM_TRAILING, | |
| 171 &device_name); | |
| 172 InputDeviceType type; | |
| 173 if (IsTestKeyboard(device_name)) | |
| 174 continue; // Skip test devices. | |
| 175 | |
| 176 if (IsInternalKeyboard(device_name)) { | |
| 177 type = InputDeviceType::INTERNAL; | |
| 178 } else if (IsKnownKeyboard(device_name)) { | |
| 179 type = InputDeviceType::EXTERNAL; | |
| 180 } else { | |
| 181 type = InputDeviceType::UNKNOWN; | |
| 182 } | |
| 183 devices.push_back(KeyboardDevice(x11_devices[i].deviceid, | |
| 184 type, device_name)); | |
| 185 } | |
| 186 delegate_->OnKeyboardDevicesUpdated(devices); | |
| 119 } | 187 } |
| 120 | 188 |
| 121 void HotplugEventHandlerX11::HandleTouchscreenDevices( | 189 void HotplugEventHandlerX11::HandleTouchscreenDevices( |
| 122 const XIDeviceList& x11_devices) { | 190 const XIDeviceList& x11_devices) { |
| 123 std::vector<TouchscreenDevice> devices; | 191 std::vector<TouchscreenDevice> devices; |
| 124 Display* display = gfx::GetXDisplay(); | 192 Display* display = gfx::GetXDisplay(); |
| 125 Atom valuator_x = XInternAtom(display, "Abs MT Position X", False); | 193 Atom valuator_x = XInternAtom(display, "Abs MT Position X", False); |
| 126 Atom valuator_y = XInternAtom(display, "Abs MT Position Y", False); | 194 Atom valuator_y = XInternAtom(display, "Abs MT Position Y", False); |
| 127 if (valuator_x == None || valuator_y == None) | 195 if (valuator_x == None || valuator_y == None) |
| 128 return; | 196 return; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 XITouchClassInfo* touch_info = | 230 XITouchClassInfo* touch_info = |
| 163 reinterpret_cast<XITouchClassInfo*>(class_info); | 231 reinterpret_cast<XITouchClassInfo*>(class_info); |
| 164 is_direct_touch = touch_info->mode == XIDirectTouch; | 232 is_direct_touch = touch_info->mode == XIDirectTouch; |
| 165 } | 233 } |
| 166 #endif | 234 #endif |
| 167 } | 235 } |
| 168 | 236 |
| 169 // Touchscreens should have absolute X and Y axes, and be direct touch | 237 // Touchscreens should have absolute X and Y axes, and be direct touch |
| 170 // devices. | 238 // devices. |
| 171 if (width > 0.0 && height > 0.0 && is_direct_touch) { | 239 if (width > 0.0 && height > 0.0 && is_direct_touch) { |
| 172 bool is_internal = | 240 InputDeviceType type = |
| 173 IsTouchscreenInternal(display, x11_devices[i].deviceid); | 241 IsTouchscreenInternal(display, x11_devices[i].deviceid) ? |
| 242 InputDeviceType::INTERNAL : InputDeviceType::EXTERNAL; | |
| 243 std::string name(x11_devices[i].name); | |
| 174 devices.push_back(TouchscreenDevice( | 244 devices.push_back(TouchscreenDevice( |
| 175 x11_devices[i].deviceid, gfx::Size(width, height), is_internal)); | 245 x11_devices[i].deviceid, type, name, gfx::Size(width, height))); |
| 176 } | 246 } |
| 177 } | 247 } |
| 178 | 248 |
| 179 delegate_->OnTouchscreenDevicesUpdated(devices); | 249 delegate_->OnTouchscreenDevicesUpdated(devices); |
| 180 } | 250 } |
| 181 | 251 |
| 182 } // namespace ui | 252 } // namespace ui |
| OLD | NEW |