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
3 // found in the LICENSE file.
5 #include "ui/events/devices/device_data_manager.h"
7 #include "base/at_exit.h"
9 #include "base/logging.h"
10 #include "ui/events/devices/input_device_event_observer.h"
11 #include "ui/gfx/display.h"
12 #include "ui/gfx/geometry/point3_f.h"
18 bool InputDeviceEquals(const ui::InputDevice
& a
, const ui::InputDevice
& b
) {
25 DeviceDataManager
* DeviceDataManager::instance_
= NULL
;
27 DeviceDataManager::DeviceDataManager() {
28 CHECK(!instance_
) << "Can not create multiple instances of DeviceDataManager";
31 base::AtExitManager::RegisterTask(
32 base::Bind(&base::DeletePointer
<DeviceDataManager
>, this));
34 ClearTouchDeviceAssociations();
37 DeviceDataManager::~DeviceDataManager() {
38 CHECK_EQ(this, instance_
);
43 DeviceDataManager
* DeviceDataManager::instance() { return instance_
; }
46 void DeviceDataManager::CreateInstance() {
50 new DeviceDataManager();
54 DeviceDataManager
* DeviceDataManager::GetInstance() {
55 CHECK(instance_
) << "DeviceDataManager was not created.";
60 bool DeviceDataManager::HasInstance() {
61 return instance_
!= NULL
;
64 void DeviceDataManager::ClearTouchDeviceAssociations() {
65 for (int i
= 0; i
< kMaxDeviceNum
; i
++) {
66 touch_device_transformer_map_
[i
] = gfx::Transform();
67 touch_device_to_target_display_map_
[i
] = gfx::Display::kInvalidDisplayID
;
68 touch_radius_scale_map_
[i
] = 1.0;
72 bool DeviceDataManager::IsTouchDeviceIdValid(
73 unsigned int touch_device_id
) const {
74 return (touch_device_id
> 0 && touch_device_id
< kMaxDeviceNum
);
77 void DeviceDataManager::UpdateTouchInfoForDisplay(
78 int64_t target_display_id
,
79 unsigned int touch_device_id
,
80 const gfx::Transform
& touch_transformer
) {
81 if (IsTouchDeviceIdValid(touch_device_id
)) {
82 touch_device_to_target_display_map_
[touch_device_id
] = target_display_id
;
83 touch_device_transformer_map_
[touch_device_id
] = touch_transformer
;
87 void DeviceDataManager::UpdateTouchRadiusScale(unsigned int touch_device_id
,
89 if (IsTouchDeviceIdValid(touch_device_id
))
90 touch_radius_scale_map_
[touch_device_id
] = scale
;
93 void DeviceDataManager::ApplyTouchRadiusScale(unsigned int touch_device_id
,
95 if (IsTouchDeviceIdValid(touch_device_id
))
96 *radius
= (*radius
) * touch_radius_scale_map_
[touch_device_id
];
99 void DeviceDataManager::ApplyTouchTransformer(unsigned int touch_device_id
,
102 if (IsTouchDeviceIdValid(touch_device_id
)) {
103 gfx::Point3F
point(*x
, *y
, 0.0);
104 const gfx::Transform
& trans
=
105 touch_device_transformer_map_
[touch_device_id
];
106 trans
.TransformPoint(&point
);
112 int64_t DeviceDataManager::GetTargetDisplayForTouchDevice(
113 unsigned int touch_device_id
) const {
114 if (IsTouchDeviceIdValid(touch_device_id
))
115 return touch_device_to_target_display_map_
[touch_device_id
];
116 return gfx::Display::kInvalidDisplayID
;
119 void DeviceDataManager::OnTouchscreenDevicesUpdated(
120 const std::vector
<TouchscreenDevice
>& devices
) {
121 if (devices
.size() == touchscreen_devices_
.size() &&
122 std::equal(devices
.begin(),
124 touchscreen_devices_
.begin(),
125 InputDeviceEquals
)) {
128 touchscreen_devices_
= devices
;
129 FOR_EACH_OBSERVER(InputDeviceEventObserver
,
131 OnTouchscreenDeviceConfigurationChanged());
134 void DeviceDataManager::OnKeyboardDevicesUpdated(
135 const std::vector
<KeyboardDevice
>& devices
) {
136 if (devices
.size() == keyboard_devices_
.size() &&
137 std::equal(devices
.begin(),
139 keyboard_devices_
.begin(),
140 InputDeviceEquals
)) {
143 keyboard_devices_
= devices
;
144 FOR_EACH_OBSERVER(InputDeviceEventObserver
,
146 OnKeyboardDeviceConfigurationChanged());
149 void DeviceDataManager::OnMouseDevicesUpdated(
150 const std::vector
<InputDevice
>& devices
) {
151 if (devices
.size() == mouse_devices_
.size() &&
152 std::equal(devices
.begin(),
154 mouse_devices_
.begin(),
155 InputDeviceEquals
)) {
158 mouse_devices_
= devices
;
159 FOR_EACH_OBSERVER(InputDeviceEventObserver
,
161 OnMouseDeviceConfigurationChanged());
164 void DeviceDataManager::OnTouchpadDevicesUpdated(
165 const std::vector
<InputDevice
>& devices
) {
166 if (devices
.size() == touchpad_devices_
.size() &&
167 std::equal(devices
.begin(),
169 touchpad_devices_
.begin(),
170 InputDeviceEquals
)) {
173 touchpad_devices_
= devices
;
174 FOR_EACH_OBSERVER(InputDeviceEventObserver
,
176 OnTouchpadDeviceConfigurationChanged());
179 void DeviceDataManager::AddObserver(InputDeviceEventObserver
* observer
) {
180 observers_
.AddObserver(observer
);
183 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver
* observer
) {
184 observers_
.RemoveObserver(observer
);