forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_data_manager.cc
312 lines (257 loc) · 9.61 KB
/
device_data_manager.cc
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/events/devices/device_data_manager.h"
#include "base/at_exit.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/observer_list.h"
#include "base/ranges/algorithm.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/devices/input_device_event_observer.h"
#include "ui/events/devices/touch_device_transform.h"
#include "ui/events/devices/touchscreen_device.h"
#include "ui/gfx/geometry/point3_f.h"
// This macro provides the implementation for the observer notification methods.
#define NOTIFY_OBSERVERS(method_decl, observer_call) \
void DeviceDataManager::method_decl { \
for (InputDeviceEventObserver& observer : observers_) \
observer.observer_call; \
}
namespace ui {
namespace {
bool InputDeviceEquals(const ui::InputDevice& a, const ui::InputDevice& b) {
return a.id == b.id && a.enabled == b.enabled &&
a.suspected_imposter == b.suspected_imposter;
}
} // namespace
// static
DeviceDataManager* DeviceDataManager::instance_ = nullptr;
DeviceDataManager::DeviceDataManager() {
DCHECK(!instance_);
instance_ = this;
}
DeviceDataManager::~DeviceDataManager() {
instance_ = nullptr;
}
// static
void DeviceDataManager::CreateInstance() {
if (instance_)
return;
new DeviceDataManager();
// TODO(bruthig): Replace the DeleteInstance callbacks with explicit calls.
base::AtExitManager::RegisterTask(base::BindOnce(DeleteInstance));
}
// static
void DeviceDataManager::DeleteInstance() {
delete instance_;
}
// static
DeviceDataManager* DeviceDataManager::GetInstance() {
CHECK(instance_) << "DeviceDataManager was not created.";
return instance_;
}
// static
bool DeviceDataManager::HasInstance() {
return instance_ != nullptr;
}
void DeviceDataManager::ConfigureTouchDevices(
const std::vector<ui::TouchDeviceTransform>& transforms) {
ClearTouchDeviceAssociations();
for (const TouchDeviceTransform& transform : transforms)
UpdateTouchInfoFromTransform(transform);
are_touchscreen_target_displays_valid_ = true;
for (InputDeviceEventObserver& observer : observers_)
observer.OnTouchDeviceAssociationChanged();
}
void DeviceDataManager::ClearTouchDeviceAssociations() {
touch_map_.clear();
for (TouchscreenDevice& touchscreen_device : touchscreen_devices_)
touchscreen_device.target_display_id = display::kInvalidDisplayId;
}
void DeviceDataManager::UpdateTouchInfoFromTransform(
const ui::TouchDeviceTransform& touch_device_transform) {
DCHECK_GE(touch_device_transform.device_id, 0);
touch_map_[touch_device_transform.device_id] = touch_device_transform;
for (TouchscreenDevice& touchscreen_device : touchscreen_devices_) {
if (touchscreen_device.id == touch_device_transform.device_id) {
touchscreen_device.target_display_id = touch_device_transform.display_id;
return;
}
}
}
void DeviceDataManager::UpdateTouchMap() {
// Remove all entries for devices from the |touch_map_| that are not currently
// connected.
auto last_iter = std::remove_if(
touch_map_.begin(), touch_map_.end(),
[this](const std::pair<int, TouchDeviceTransform>& map_entry) {
// Remove the device identified by |map_entry| from |touch_map_| if it
// is not present in the list of currently connected devices.
return !base::Contains(touchscreen_devices_, map_entry.second.device_id,
&TouchscreenDevice::id);
});
touch_map_.erase(last_iter, touch_map_.end());
}
void DeviceDataManager::ApplyTouchRadiusScale(int touch_device_id,
double* radius) {
auto iter = touch_map_.find(touch_device_id);
if (iter != touch_map_.end())
*radius = (*radius) * iter->second.radius_scale;
}
void DeviceDataManager::ApplyTouchTransformer(int touch_device_id,
float* x,
float* y) {
auto iter = touch_map_.find(touch_device_id);
if (iter != touch_map_.end()) {
const gfx::Transform& trans = iter->second.transform;
gfx::PointF point = trans.MapPoint(gfx::PointF(*x, *y));
*x = point.x();
*y = point.y();
}
}
const std::vector<TouchscreenDevice>& DeviceDataManager::GetTouchscreenDevices()
const {
return touchscreen_devices_;
}
const std::vector<InputDevice>& DeviceDataManager::GetKeyboardDevices() const {
return keyboard_devices_;
}
const std::vector<InputDevice>& DeviceDataManager::GetMouseDevices() const {
return mouse_devices_;
}
const std::vector<InputDevice>& DeviceDataManager::GetPointingStickDevices()
const {
return pointing_stick_devices_;
}
const std::vector<InputDevice>& DeviceDataManager::GetTouchpadDevices() const {
return touchpad_devices_;
}
const std::vector<InputDevice>& DeviceDataManager::GetUncategorizedDevices()
const {
return uncategorized_devices_;
}
bool DeviceDataManager::AreDeviceListsComplete() const {
return device_lists_complete_;
}
int64_t DeviceDataManager::GetTargetDisplayForTouchDevice(
int touch_device_id) const {
auto iter = touch_map_.find(touch_device_id);
if (iter != touch_map_.end())
return iter->second.display_id;
return display::kInvalidDisplayId;
}
void DeviceDataManager::OnTouchscreenDevicesUpdated(
const std::vector<TouchscreenDevice>& devices) {
if (base::ranges::equal(devices, touchscreen_devices_, InputDeviceEquals)) {
return;
}
are_touchscreen_target_displays_valid_ = false;
touchscreen_devices_ = devices;
for (TouchscreenDevice& touchscreen_device : touchscreen_devices_) {
touchscreen_device.target_display_id =
GetTargetDisplayForTouchDevice(touchscreen_device.id);
}
UpdateTouchMap();
NotifyObserversTouchscreenDeviceConfigurationChanged();
}
void DeviceDataManager::OnKeyboardDevicesUpdated(
const std::vector<InputDevice>& devices) {
if (base::ranges::equal(devices, keyboard_devices_, InputDeviceEquals)) {
return;
}
keyboard_devices_ = devices;
NotifyObserversKeyboardDeviceConfigurationChanged();
}
void DeviceDataManager::OnMouseDevicesUpdated(
const std::vector<InputDevice>& devices) {
if (base::ranges::equal(devices, mouse_devices_, InputDeviceEquals)) {
return;
}
mouse_devices_ = devices;
NotifyObserversMouseDeviceConfigurationChanged();
}
void DeviceDataManager::OnPointingStickDevicesUpdated(
const std::vector<InputDevice>& devices) {
if (base::ranges::equal(devices, pointing_stick_devices_,
InputDeviceEquals)) {
return;
}
pointing_stick_devices_ = devices;
NotifyObserversPointingStickDeviceConfigurationChanged();
}
void DeviceDataManager::OnTouchpadDevicesUpdated(
const std::vector<InputDevice>& devices) {
if (base::ranges::equal(devices, touchpad_devices_, InputDeviceEquals)) {
return;
}
touchpad_devices_ = devices;
NotifyObserversTouchpadDeviceConfigurationChanged();
}
void DeviceDataManager::OnUncategorizedDevicesUpdated(
const std::vector<InputDevice>& devices) {
if (base::ranges::equal(devices, uncategorized_devices_, InputDeviceEquals)) {
return;
}
uncategorized_devices_ = devices;
NotifyObserversUncategorizedDeviceConfigurationChanged();
}
void DeviceDataManager::OnDeviceListsComplete() {
if (!device_lists_complete_) {
device_lists_complete_ = true;
NotifyObserversDeviceListsComplete();
}
}
void DeviceDataManager::OnStylusStateChanged(StylusState state) {
NotifyObserversStylusStateChanged(state);
}
NOTIFY_OBSERVERS(
NotifyObserversKeyboardDeviceConfigurationChanged(),
OnInputDeviceConfigurationChanged(InputDeviceEventObserver::kKeyboard))
NOTIFY_OBSERVERS(
NotifyObserversMouseDeviceConfigurationChanged(),
OnInputDeviceConfigurationChanged(InputDeviceEventObserver::kMouse))
NOTIFY_OBSERVERS(
NotifyObserversPointingStickDeviceConfigurationChanged(),
OnInputDeviceConfigurationChanged(InputDeviceEventObserver::kPointingStick))
NOTIFY_OBSERVERS(
NotifyObserversTouchpadDeviceConfigurationChanged(),
OnInputDeviceConfigurationChanged(InputDeviceEventObserver::kTouchpad))
NOTIFY_OBSERVERS(
NotifyObserversUncategorizedDeviceConfigurationChanged(),
OnInputDeviceConfigurationChanged(InputDeviceEventObserver::kUncategorized))
NOTIFY_OBSERVERS(
NotifyObserversTouchscreenDeviceConfigurationChanged(),
OnInputDeviceConfigurationChanged(InputDeviceEventObserver::kTouchscreen))
NOTIFY_OBSERVERS(NotifyObserversDeviceListsComplete(), OnDeviceListsComplete())
NOTIFY_OBSERVERS(NotifyObserversStylusStateChanged(StylusState state),
OnStylusStateChanged(state))
void DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) {
observers_.AddObserver(observer);
}
void DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) {
observers_.RemoveObserver(observer);
}
bool DeviceDataManager::HasObserver(InputDeviceEventObserver* observer) {
return observers_.HasObserver(observer);
}
void DeviceDataManager::ResetDeviceListsForTest() {
touchscreen_devices_.clear();
keyboard_devices_.clear();
mouse_devices_.clear();
pointing_stick_devices_.clear();
touchpad_devices_.clear();
uncategorized_devices_.clear();
device_lists_complete_ = false;
}
void DeviceDataManager::SetTouchscreensEnabled(bool enabled) {
touch_screens_enabled_ = enabled;
}
bool DeviceDataManager::AreTouchscreensEnabled() const {
return touch_screens_enabled_;
}
bool DeviceDataManager::AreTouchscreenTargetDisplaysValid() const {
return are_touchscreen_target_displays_valid_;
}
} // namespace ui