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"
14 // This macro provides the implementation for the observer notification methods.
15 #define NOTIFY_OBSERVERS(method_name, observer_method) \
16 void DeviceDataManager::method_name() { \
17 FOR_EACH_OBSERVER(InputDeviceEventObserver, observers_, \
25 bool InputDeviceEquals(const ui::InputDevice
& a
, const ui::InputDevice
& b
) {
32 DeviceDataManager
* DeviceDataManager::instance_
= NULL
;
34 DeviceDataManager::DeviceDataManager() {
35 ClearTouchDeviceAssociations();
38 DeviceDataManager::~DeviceDataManager() {
42 DeviceDataManager
* DeviceDataManager::instance() { return instance_
; }
44 void DeviceDataManager::set_instance(DeviceDataManager
* instance
) {
46 << "Must reset the DeviceDataManager using DeleteInstance().";
47 DCHECK(!instance_
) << "Can not set multiple instances of DeviceDataManager.";
52 void DeviceDataManager::CreateInstance() {
56 set_instance(new DeviceDataManager());
58 // TODO(bruthig): Replace the DeleteInstance callbacks with explicit calls.
59 base::AtExitManager::RegisterTask(base::Bind(DeleteInstance
));
62 void DeviceDataManager::DeleteInstance() {
70 DeviceDataManager
* DeviceDataManager::GetInstance() {
71 CHECK(instance_
) << "DeviceDataManager was not created.";
76 bool DeviceDataManager::HasInstance() {
77 return instance_
!= NULL
;
80 void DeviceDataManager::ClearTouchDeviceAssociations() {
81 for (int i
= 0; i
< kMaxDeviceNum
; i
++) {
82 touch_device_transformer_map_
[i
] = gfx::Transform();
83 touch_device_to_target_display_map_
[i
] = gfx::Display::kInvalidDisplayID
;
84 touch_radius_scale_map_
[i
] = 1.0;
88 bool DeviceDataManager::IsTouchDeviceIdValid(
89 int touch_device_id
) const {
90 return (touch_device_id
> 0 && touch_device_id
< kMaxDeviceNum
);
93 void DeviceDataManager::UpdateTouchInfoForDisplay(
94 int64_t target_display_id
,
96 const gfx::Transform
& touch_transformer
) {
97 if (IsTouchDeviceIdValid(touch_device_id
)) {
98 touch_device_to_target_display_map_
[touch_device_id
] = target_display_id
;
99 touch_device_transformer_map_
[touch_device_id
] = touch_transformer
;
103 void DeviceDataManager::UpdateTouchRadiusScale(int touch_device_id
,
105 if (IsTouchDeviceIdValid(touch_device_id
))
106 touch_radius_scale_map_
[touch_device_id
] = scale
;
109 void DeviceDataManager::ApplyTouchRadiusScale(int touch_device_id
,
111 if (IsTouchDeviceIdValid(touch_device_id
))
112 *radius
= (*radius
) * touch_radius_scale_map_
[touch_device_id
];
115 void DeviceDataManager::ApplyTouchTransformer(int touch_device_id
,
118 if (IsTouchDeviceIdValid(touch_device_id
)) {
119 gfx::Point3F
point(*x
, *y
, 0.0);
120 const gfx::Transform
& trans
=
121 touch_device_transformer_map_
[touch_device_id
];
122 trans
.TransformPoint(&point
);
128 int64_t DeviceDataManager::GetTargetDisplayForTouchDevice(
129 int touch_device_id
) const {
130 if (IsTouchDeviceIdValid(touch_device_id
))
131 return touch_device_to_target_display_map_
[touch_device_id
];
132 return gfx::Display::kInvalidDisplayID
;
135 void DeviceDataManager::OnTouchscreenDevicesUpdated(
136 const std::vector
<TouchscreenDevice
>& devices
) {
137 if (devices
.size() == touchscreen_devices_
.size() &&
138 std::equal(devices
.begin(),
140 touchscreen_devices_
.begin(),
141 InputDeviceEquals
)) {
144 touchscreen_devices_
= devices
;
145 NotifyObserversTouchscreenDeviceConfigurationChanged();
148 void DeviceDataManager::OnKeyboardDevicesUpdated(
149 const std::vector
<KeyboardDevice
>& devices
) {
150 if (devices
.size() == keyboard_devices_
.size() &&
151 std::equal(devices
.begin(),
153 keyboard_devices_
.begin(),
154 InputDeviceEquals
)) {
157 keyboard_devices_
= devices
;
158 NotifyObserversKeyboardDeviceConfigurationChanged();
161 void DeviceDataManager::OnMouseDevicesUpdated(
162 const std::vector
<InputDevice
>& devices
) {
163 if (devices
.size() == mouse_devices_
.size() &&
164 std::equal(devices
.begin(),
166 mouse_devices_
.begin(),
167 InputDeviceEquals
)) {
170 mouse_devices_
= devices
;
171 NotifyObserversMouseDeviceConfigurationChanged();
174 void DeviceDataManager::OnTouchpadDevicesUpdated(
175 const std::vector
<InputDevice
>& devices
) {
176 if (devices
.size() == touchpad_devices_
.size() &&
177 std::equal(devices
.begin(),
179 touchpad_devices_
.begin(),
180 InputDeviceEquals
)) {
183 touchpad_devices_
= devices
;
184 NotifyObserversTouchpadDeviceConfigurationChanged();
187 void DeviceDataManager::OnDeviceListsComplete() {
188 if (!device_lists_complete_
) {
189 device_lists_complete_
= true;
190 NotifyObserversDeviceListsComplete();
194 NOTIFY_OBSERVERS(NotifyObserversTouchscreenDeviceConfigurationChanged
,
195 OnTouchscreenDeviceConfigurationChanged
);
197 NOTIFY_OBSERVERS(NotifyObserversKeyboardDeviceConfigurationChanged
,
198 OnKeyboardDeviceConfigurationChanged
);
200 NOTIFY_OBSERVERS(NotifyObserversMouseDeviceConfigurationChanged
,
201 OnMouseDeviceConfigurationChanged
);
203 NOTIFY_OBSERVERS(NotifyObserversTouchpadDeviceConfigurationChanged
,
204 OnTouchpadDeviceConfigurationChanged
);
206 NOTIFY_OBSERVERS(NotifyObserversDeviceListsComplete
, OnDeviceListsComplete
);
208 void DeviceDataManager::AddObserver(InputDeviceEventObserver
* observer
) {
209 observers_
.AddObserver(observer
);
212 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver
* observer
) {
213 observers_
.RemoveObserver(observer
);