1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/hid/input_service_linux.h"
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "device/udev_linux/udev.h"
namespace device {
namespace {
const char kSubsystemHid[] = "hid";
const char kSubsystemInput[] = "input";
const char kTypeBluetooth[] = "bluetooth";
const char kTypeUsb[] = "usb";
const char kTypeSerio[] = "serio";
const char kIdInputAccelerometer[] = "ID_INPUT_ACCELEROMETER";
const char kIdInputJoystick[] = "ID_INPUT_JOYSTICK";
const char kIdInputKey[] = "ID_INPUT_KEY";
const char kIdInputKeyboard[] = "ID_INPUT_KEYBOARD";
const char kIdInputMouse[] = "ID_INPUT_MOUSE";
const char kIdInputTablet[] = "ID_INPUT_TABLET";
const char kIdInputTouchpad[] = "ID_INPUT_TOUCHPAD";
const char kIdInputTouchscreen[] = "ID_INPUT_TOUCHSCREEN";
// The instance will be reset when message loop destroys.
base::LazyInstance<scoped_ptr<InputServiceLinux> >::Leaky
g_input_service_linux_ptr = LAZY_INSTANCE_INITIALIZER;
bool GetBoolProperty(udev_device* device, const char* key) {
CHECK(device);
CHECK(key);
const char* property = udev_device_get_property_value(device, key);
if (!property)
return false;
int value;
if (!base::StringToInt(property, &value)) {
LOG(ERROR) << "Not an integer value for " << key << " property";
return false;
}
return (value != 0);
}
InputServiceLinux::InputDeviceInfo::Type GetDeviceType(udev_device* device) {
if (udev_device_get_parent_with_subsystem_devtype(
device, kTypeBluetooth, NULL)) {
return InputServiceLinux::InputDeviceInfo::TYPE_BLUETOOTH;
}
if (udev_device_get_parent_with_subsystem_devtype(device, kTypeUsb, NULL))
return InputServiceLinux::InputDeviceInfo::TYPE_USB;
if (udev_device_get_parent_with_subsystem_devtype(device, kTypeSerio, NULL))
return InputServiceLinux::InputDeviceInfo::TYPE_SERIO;
return InputServiceLinux::InputDeviceInfo::TYPE_UNKNOWN;
}
std::string GetParentDeviceName(udev_device* device, const char* subsystem) {
udev_device* parent =
udev_device_get_parent_with_subsystem_devtype(device, subsystem, NULL);
if (!parent)
return std::string();
const char* name = udev_device_get_property_value(parent, "NAME");
if (!name)
return std::string();
std::string result;
base::TrimString(name, "\"", &result);
return result;
}
class InputServiceLinuxImpl : public InputServiceLinux,
public DeviceMonitorLinux::Observer {
public:
// Implements DeviceMonitorLinux::Observer:
void OnDeviceAdded(udev_device* device) override;
void OnDeviceRemoved(udev_device* device) override;
private:
friend class InputServiceLinux;
InputServiceLinuxImpl();
~InputServiceLinuxImpl() override;
DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxImpl);
};
InputServiceLinuxImpl::InputServiceLinuxImpl() {
base::ThreadRestrictions::AssertIOAllowed();
base::MessageLoop::current()->AddDestructionObserver(this);
DeviceMonitorLinux::GetInstance()->AddObserver(this);
DeviceMonitorLinux::GetInstance()->Enumerate(base::Bind(
&InputServiceLinuxImpl::OnDeviceAdded, base::Unretained(this)));
}
InputServiceLinuxImpl::~InputServiceLinuxImpl() {
if (DeviceMonitorLinux::HasInstance())
DeviceMonitorLinux::GetInstance()->RemoveObserver(this);
base::MessageLoop::current()->RemoveDestructionObserver(this);
}
void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) {
DCHECK(CalledOnValidThread());
if (!device)
return;
const char* devnode = udev_device_get_devnode(device);
if (!devnode)
return;
InputDeviceInfo info;
info.id = devnode;
const char* subsystem = udev_device_get_subsystem(device);
if (!subsystem)
return;
if (strcmp(subsystem, kSubsystemHid) == 0) {
info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_HID;
info.name = GetParentDeviceName(device, kSubsystemHid);
} else if (strcmp(subsystem, kSubsystemInput) == 0) {
info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_INPUT;
info.name = GetParentDeviceName(device, kSubsystemInput);
} else {
return;
}
info.type = GetDeviceType(device);
info.is_accelerometer = GetBoolProperty(device, kIdInputAccelerometer);
info.is_joystick = GetBoolProperty(device, kIdInputJoystick);
info.is_key = GetBoolProperty(device, kIdInputKey);
info.is_keyboard = GetBoolProperty(device, kIdInputKeyboard);
info.is_mouse = GetBoolProperty(device, kIdInputMouse);
info.is_tablet = GetBoolProperty(device, kIdInputTablet);
info.is_touchpad = GetBoolProperty(device, kIdInputTouchpad);
info.is_touchscreen = GetBoolProperty(device, kIdInputTouchscreen);
AddDevice(info);
}
void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) {
DCHECK(CalledOnValidThread());
if (!device)
return;
const char* devnode = udev_device_get_devnode(device);
if (devnode)
RemoveDevice(devnode);
}
} // namespace
InputServiceLinux::InputDeviceInfo::InputDeviceInfo()
: subsystem(SUBSYSTEM_UNKNOWN),
type(TYPE_UNKNOWN),
is_accelerometer(false),
is_joystick(false),
is_key(false),
is_keyboard(false),
is_mouse(false),
is_tablet(false),
is_touchpad(false),
is_touchscreen(false) {}
InputServiceLinux::InputServiceLinux() {
}
InputServiceLinux::~InputServiceLinux() {
DCHECK(CalledOnValidThread());
}
// static
InputServiceLinux* InputServiceLinux::GetInstance() {
if (!HasInstance())
g_input_service_linux_ptr.Get().reset(new InputServiceLinuxImpl());
return g_input_service_linux_ptr.Get().get();
}
// static
bool InputServiceLinux::HasInstance() {
return g_input_service_linux_ptr.Get().get();
}
// static
void InputServiceLinux::SetForTesting(InputServiceLinux* service) {
g_input_service_linux_ptr.Get().reset(service);
}
void InputServiceLinux::AddObserver(Observer* observer) {
DCHECK(CalledOnValidThread());
if (observer)
observers_.AddObserver(observer);
}
void InputServiceLinux::RemoveObserver(Observer* observer) {
DCHECK(CalledOnValidThread());
if (observer)
observers_.RemoveObserver(observer);
}
void InputServiceLinux::GetDevices(std::vector<InputDeviceInfo>* devices) {
DCHECK(CalledOnValidThread());
for (DeviceMap::iterator it = devices_.begin(), ie = devices_.end(); it != ie;
++it) {
devices->push_back(it->second);
}
}
bool InputServiceLinux::GetDeviceInfo(const std::string& id,
InputDeviceInfo* info) const {
DCHECK(CalledOnValidThread());
DeviceMap::const_iterator it = devices_.find(id);
if (it == devices_.end())
return false;
*info = it->second;
return true;
}
void InputServiceLinux::WillDestroyCurrentMessageLoop() {
DCHECK(CalledOnValidThread());
g_input_service_linux_ptr.Get().reset(NULL);
}
void InputServiceLinux::AddDevice(const InputDeviceInfo& info) {
devices_[info.id] = info;
FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info));
}
void InputServiceLinux::RemoveDevice(const std::string& id) {
devices_.erase(id);
FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id));
}
bool InputServiceLinux::CalledOnValidThread() const {
return thread_checker_.CalledOnValidThread();
}
} // namespace device
|