| 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 <libudev.h> | 5 #include <libudev.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" |
| 11 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 12 #include "device/hid/input_service_linux.h" | 13 #include "device/hid/input_service_linux.h" |
| 13 | 14 |
| 14 namespace device { | 15 namespace device { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 const char kSubsystemHid[] = "hid"; | 19 const char kSubsystemHid[] = "hid"; |
| 19 const char kSubsystemInput[] = "input"; | 20 const char kSubsystemInput[] = "input"; |
| 20 const char kTypeBluetooth[] = "bluetooth"; | 21 const char kTypeBluetooth[] = "bluetooth"; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 device, kTypeBluetooth, NULL)) { | 53 device, kTypeBluetooth, NULL)) { |
| 53 return InputServiceLinux::InputDeviceInfo::TYPE_BLUETOOTH; | 54 return InputServiceLinux::InputDeviceInfo::TYPE_BLUETOOTH; |
| 54 } | 55 } |
| 55 if (udev_device_get_parent_with_subsystem_devtype(device, kTypeUsb, NULL)) | 56 if (udev_device_get_parent_with_subsystem_devtype(device, kTypeUsb, NULL)) |
| 56 return InputServiceLinux::InputDeviceInfo::TYPE_USB; | 57 return InputServiceLinux::InputDeviceInfo::TYPE_USB; |
| 57 if (udev_device_get_parent_with_subsystem_devtype(device, kTypeSerio, NULL)) | 58 if (udev_device_get_parent_with_subsystem_devtype(device, kTypeSerio, NULL)) |
| 58 return InputServiceLinux::InputDeviceInfo::TYPE_SERIO; | 59 return InputServiceLinux::InputDeviceInfo::TYPE_SERIO; |
| 59 return InputServiceLinux::InputDeviceInfo::TYPE_UNKNOWN; | 60 return InputServiceLinux::InputDeviceInfo::TYPE_UNKNOWN; |
| 60 } | 61 } |
| 61 | 62 |
| 63 std::string GetParentDeviceName(udev_device* device, const char* subsystem) { |
| 64 udev_device* parent = |
| 65 udev_device_get_parent_with_subsystem_devtype(device, subsystem, NULL); |
| 66 if (!parent) |
| 67 return std::string(); |
| 68 const char* name = udev_device_get_property_value(parent, "NAME"); |
| 69 if (!name) |
| 70 return std::string(); |
| 71 std::string result; |
| 72 base::TrimString(name, "\"", &result); |
| 73 return result; |
| 74 } |
| 75 |
| 62 class InputServiceLinuxImpl : public InputServiceLinux, | 76 class InputServiceLinuxImpl : public InputServiceLinux, |
| 63 public DeviceMonitorLinux::Observer { | 77 public DeviceMonitorLinux::Observer { |
| 64 public: | 78 public: |
| 65 // Implements DeviceMonitorLinux::Observer: | 79 // Implements DeviceMonitorLinux::Observer: |
| 66 virtual void OnDeviceAdded(udev_device* device) OVERRIDE; | 80 virtual void OnDeviceAdded(udev_device* device) OVERRIDE; |
| 67 virtual void OnDeviceRemoved(udev_device* device) OVERRIDE; | 81 virtual void OnDeviceRemoved(udev_device* device) OVERRIDE; |
| 68 | 82 |
| 69 private: | 83 private: |
| 70 friend class InputServiceLinux; | 84 friend class InputServiceLinux; |
| 71 | 85 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 83 | 97 |
| 84 InputServiceLinuxImpl::~InputServiceLinuxImpl() { | 98 InputServiceLinuxImpl::~InputServiceLinuxImpl() { |
| 85 if (DeviceMonitorLinux::HasInstance()) | 99 if (DeviceMonitorLinux::HasInstance()) |
| 86 DeviceMonitorLinux::GetInstance()->RemoveObserver(this); | 100 DeviceMonitorLinux::GetInstance()->RemoveObserver(this); |
| 87 } | 101 } |
| 88 | 102 |
| 89 void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) { | 103 void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) { |
| 90 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 91 if (!device) | 105 if (!device) |
| 92 return; | 106 return; |
| 93 const char* path = udev_device_get_syspath(device); | 107 const char* devnode = udev_device_get_devnode(device); |
| 94 if (!path) | 108 if (!devnode) |
| 95 return; | 109 return; |
| 96 | 110 |
| 97 InputDeviceInfo info; | 111 InputDeviceInfo info; |
| 98 info.id = path; | 112 info.id = devnode; |
| 99 | |
| 100 const char* name = udev_device_get_property_value(device, "NAME"); | |
| 101 if (name) | |
| 102 info.name = name; | |
| 103 | 113 |
| 104 const char* subsystem = udev_device_get_subsystem(device); | 114 const char* subsystem = udev_device_get_subsystem(device); |
| 105 if (!subsystem) | 115 if (!subsystem) |
| 106 return; | 116 return; |
| 107 else if (strcmp(subsystem, kSubsystemHid) == 0) | 117 if (strcmp(subsystem, kSubsystemHid) == 0) { |
| 108 info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_HID; | 118 info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_HID; |
| 109 else if (strcmp(subsystem, kSubsystemInput) == 0) | 119 info.name = GetParentDeviceName(device, kSubsystemHid); |
| 120 } else if (strcmp(subsystem, kSubsystemInput) == 0) { |
| 110 info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_INPUT; | 121 info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_INPUT; |
| 111 else | 122 info.name = GetParentDeviceName(device, kSubsystemInput); |
| 123 } else { |
| 112 return; | 124 return; |
| 125 } |
| 113 | 126 |
| 114 info.type = GetDeviceType(device); | 127 info.type = GetDeviceType(device); |
| 115 | 128 |
| 116 info.is_accelerometer = GetBoolProperty(device, kIdInputAccelerometer); | 129 info.is_accelerometer = GetBoolProperty(device, kIdInputAccelerometer); |
| 117 info.is_joystick = GetBoolProperty(device, kIdInputJoystick); | 130 info.is_joystick = GetBoolProperty(device, kIdInputJoystick); |
| 118 info.is_key = GetBoolProperty(device, kIdInputKey); | 131 info.is_key = GetBoolProperty(device, kIdInputKey); |
| 119 info.is_keyboard = GetBoolProperty(device, kIdInputKeyboard); | 132 info.is_keyboard = GetBoolProperty(device, kIdInputKeyboard); |
| 120 info.is_mouse = GetBoolProperty(device, kIdInputMouse); | 133 info.is_mouse = GetBoolProperty(device, kIdInputMouse); |
| 121 info.is_tablet = GetBoolProperty(device, kIdInputTablet); | 134 info.is_tablet = GetBoolProperty(device, kIdInputTablet); |
| 122 info.is_touchpad = GetBoolProperty(device, kIdInputTouchpad); | 135 info.is_touchpad = GetBoolProperty(device, kIdInputTouchpad); |
| 123 info.is_touchscreen = GetBoolProperty(device, kIdInputTouchscreen); | 136 info.is_touchscreen = GetBoolProperty(device, kIdInputTouchscreen); |
| 124 | 137 |
| 125 AddDevice(info); | 138 AddDevice(info); |
| 126 } | 139 } |
| 127 | 140 |
| 128 void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) { | 141 void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) { |
| 129 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
| 130 if (!device) | 143 if (!device) |
| 131 return; | 144 return; |
| 132 const char* path = udev_device_get_syspath(device); | 145 const char* devnode = udev_device_get_devnode(device); |
| 133 if (!path) | 146 if (devnode) |
| 134 return; | 147 RemoveDevice(devnode); |
| 135 RemoveDevice(path); | |
| 136 } | 148 } |
| 137 | 149 |
| 138 } // namespace | 150 } // namespace |
| 139 | 151 |
| 140 InputServiceLinux::InputDeviceInfo::InputDeviceInfo() | 152 InputServiceLinux::InputDeviceInfo::InputDeviceInfo() |
| 141 : subsystem(SUBSYSTEM_UNKNOWN), | 153 : subsystem(SUBSYSTEM_UNKNOWN), |
| 142 type(TYPE_UNKNOWN), | 154 type(TYPE_UNKNOWN), |
| 143 is_accelerometer(false), | 155 is_accelerometer(false), |
| 144 is_joystick(false), | 156 is_joystick(false), |
| 145 is_key(false), | 157 is_key(false), |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 void InputServiceLinux::RemoveDevice(const std::string& id) { | 231 void InputServiceLinux::RemoveDevice(const std::string& id) { |
| 220 devices_.erase(id); | 232 devices_.erase(id); |
| 221 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); | 233 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); |
| 222 } | 234 } |
| 223 | 235 |
| 224 bool InputServiceLinux::CalledOnValidThread() const { | 236 bool InputServiceLinux::CalledOnValidThread() const { |
| 225 return thread_checker_.CalledOnValidThread(); | 237 return thread_checker_.CalledOnValidThread(); |
| 226 } | 238 } |
| 227 | 239 |
| 228 } // namespace device | 240 } // namespace device |
| OLD | NEW |