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/device_data_manager.h"
7 #include "base/at_exit.h"
9 #include "base/logging.h"
10 #include "ui/events/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 for (int i
= 0; i
< kMaxDeviceNum
; ++i
) {
35 touch_device_to_display_map_
[i
] = gfx::Display::kInvalidDisplayID
;
36 touch_radius_scale_map_
[i
] = 1.0;
40 DeviceDataManager::~DeviceDataManager() {
41 CHECK_EQ(this, instance_
);
46 DeviceDataManager
* DeviceDataManager::instance() { return instance_
; }
49 void DeviceDataManager::CreateInstance() {
53 new DeviceDataManager();
57 DeviceDataManager
* DeviceDataManager::GetInstance() {
58 CHECK(instance_
) << "DeviceDataManager was not created.";
63 bool DeviceDataManager::HasInstance() {
64 return instance_
!= NULL
;
67 void DeviceDataManager::ClearTouchTransformerRecord() {
68 for (int i
= 0; i
< kMaxDeviceNum
; i
++) {
69 touch_device_transformer_map_
[i
] = gfx::Transform();
70 touch_device_to_display_map_
[i
] = gfx::Display::kInvalidDisplayID
;
71 touch_radius_scale_map_
[i
] = 1.0;
75 bool DeviceDataManager::IsTouchDeviceIdValid(
76 unsigned int touch_device_id
) const {
77 return (touch_device_id
> 0 && touch_device_id
< kMaxDeviceNum
);
80 void DeviceDataManager::UpdateTouchInfoForDisplay(
82 unsigned int touch_device_id
,
83 const gfx::Transform
& touch_transformer
) {
84 if (IsTouchDeviceIdValid(touch_device_id
)) {
85 touch_device_to_display_map_
[touch_device_id
] = display_id
;
86 touch_device_transformer_map_
[touch_device_id
] = touch_transformer
;
90 void DeviceDataManager::UpdateTouchRadiusScale(unsigned int touch_device_id
,
92 if (IsTouchDeviceIdValid(touch_device_id
))
93 touch_radius_scale_map_
[touch_device_id
] = scale
;
96 void DeviceDataManager::ApplyTouchRadiusScale(unsigned int touch_device_id
,
98 if (IsTouchDeviceIdValid(touch_device_id
))
99 *radius
= (*radius
) * touch_radius_scale_map_
[touch_device_id
];
102 void DeviceDataManager::ApplyTouchTransformer(unsigned int touch_device_id
,
105 if (IsTouchDeviceIdValid(touch_device_id
)) {
106 gfx::Point3F
point(*x
, *y
, 0.0);
107 const gfx::Transform
& trans
=
108 touch_device_transformer_map_
[touch_device_id
];
109 trans
.TransformPoint(&point
);
115 int64_t DeviceDataManager::GetDisplayForTouchDevice(
116 unsigned int touch_device_id
) const {
117 if (IsTouchDeviceIdValid(touch_device_id
))
118 return touch_device_to_display_map_
[touch_device_id
];
119 return gfx::Display::kInvalidDisplayID
;
122 void DeviceDataManager::OnTouchscreenDevicesUpdated(
123 const std::vector
<TouchscreenDevice
>& devices
) {
124 if (devices
.size() == touchscreen_devices_
.size() &&
125 std::equal(devices
.begin(),
127 touchscreen_devices_
.begin(),
128 InputDeviceEquals
)) {
131 touchscreen_devices_
= devices
;
132 FOR_EACH_OBSERVER(InputDeviceEventObserver
,
134 OnTouchscreenDeviceConfigurationChanged());
137 void DeviceDataManager::OnKeyboardDevicesUpdated(
138 const std::vector
<KeyboardDevice
>& devices
) {
139 if (devices
.size() == keyboard_devices_
.size() &&
140 std::equal(devices
.begin(),
142 keyboard_devices_
.begin(),
143 InputDeviceEquals
)) {
146 keyboard_devices_
= devices
;
147 FOR_EACH_OBSERVER(InputDeviceEventObserver
,
149 OnKeyboardDeviceConfigurationChanged());
152 void DeviceDataManager::AddObserver(InputDeviceEventObserver
* observer
) {
153 observers_
.AddObserver(observer
);
156 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver
* observer
) {
157 observers_
.RemoveObserver(observer
);