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/gfx/display.h"
11 #include "ui/gfx/geometry/point3_f.h"
16 DeviceDataManager
* DeviceDataManager::instance_
= NULL
;
18 DeviceDataManager::DeviceDataManager() {
19 CHECK(!instance_
) << "Can not create multiple instances of DeviceDataManager";
22 base::AtExitManager::RegisterTask(
23 base::Bind(&base::DeletePointer
<DeviceDataManager
>, this));
25 for (int i
= 0; i
< kMaxDeviceNum
; ++i
) {
26 touch_device_to_display_map_
[i
] = gfx::Display::kInvalidDisplayID
;
27 touch_radius_scale_map_
[i
] = 1.0;
31 DeviceDataManager::~DeviceDataManager() {
32 CHECK_EQ(this, instance_
);
37 DeviceDataManager
* DeviceDataManager::instance() { return instance_
; }
40 void DeviceDataManager::CreateInstance() {
44 new DeviceDataManager();
48 DeviceDataManager
* DeviceDataManager::GetInstance() {
49 CHECK(instance_
) << "DeviceDataManager was not created.";
54 bool DeviceDataManager::HasInstance() {
55 return instance_
!= NULL
;
58 void DeviceDataManager::ClearTouchTransformerRecord() {
59 for (int i
= 0; i
< kMaxDeviceNum
; i
++) {
60 touch_device_transformer_map_
[i
] = gfx::Transform();
61 touch_device_to_display_map_
[i
] = gfx::Display::kInvalidDisplayID
;
62 touch_radius_scale_map_
[i
] = 1.0;
66 bool DeviceDataManager::IsTouchDeviceIdValid(int touch_device_id
) const {
67 return (touch_device_id
> 0 && touch_device_id
< kMaxDeviceNum
);
70 void DeviceDataManager::UpdateTouchInfoForDisplay(
73 const gfx::Transform
& touch_transformer
) {
74 if (IsTouchDeviceIdValid(touch_device_id
)) {
75 touch_device_to_display_map_
[touch_device_id
] = display_id
;
76 touch_device_transformer_map_
[touch_device_id
] = touch_transformer
;
80 void DeviceDataManager::UpdateTouchRadiusScale(int touch_device_id
,
82 if (IsTouchDeviceIdValid(touch_device_id
))
83 touch_radius_scale_map_
[touch_device_id
] = scale
;
86 void DeviceDataManager::ApplyTouchRadiusScale(int touch_device_id
,
88 if (IsTouchDeviceIdValid(touch_device_id
))
89 *radius
= (*radius
) * touch_radius_scale_map_
[touch_device_id
];
92 void DeviceDataManager::ApplyTouchTransformer(int touch_device_id
,
95 if (IsTouchDeviceIdValid(touch_device_id
)) {
96 gfx::Point3F
point(*x
, *y
, 0.0);
97 const gfx::Transform
& trans
=
98 touch_device_transformer_map_
[touch_device_id
];
99 trans
.TransformPoint(&point
);
105 int64_t DeviceDataManager::GetDisplayForTouchDevice(int touch_device_id
) const {
106 if (IsTouchDeviceIdValid(touch_device_id
))
107 return touch_device_to_display_map_
[touch_device_id
];
108 return gfx::Display::kInvalidDisplayID
;