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.
4
5 #include "ui/events/device_data_manager.h"
6
7 #include "base/at_exit.h"
8 #include "base/bind.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"
13
14 namespace ui {
15
16 // static
17 DeviceDataManager* DeviceDataManager::instance_ = NULL;
18
DeviceDataManager()19 DeviceDataManager::DeviceDataManager() {
20 CHECK(!instance_) << "Can not create multiple instances of DeviceDataManager";
21 instance_ = this;
22
23 base::AtExitManager::RegisterTask(
24 base::Bind(&base::DeletePointer<DeviceDataManager>, this));
25
26 for (int i = 0; i < kMaxDeviceNum; ++i) {
27 touch_device_to_display_map_[i] = gfx::Display::kInvalidDisplayID;
28 touch_radius_scale_map_[i] = 1.0;
29 }
30 }
31
~DeviceDataManager()32 DeviceDataManager::~DeviceDataManager() {
33 CHECK_EQ(this, instance_);
34 instance_ = NULL;
35 }
36
37 // static
instance()38 DeviceDataManager* DeviceDataManager::instance() { return instance_; }
39
40 // static
CreateInstance()41 void DeviceDataManager::CreateInstance() {
42 if (instance())
43 return;
44
45 new DeviceDataManager();
46 }
47
48 // static
GetInstance()49 DeviceDataManager* DeviceDataManager::GetInstance() {
50 CHECK(instance_) << "DeviceDataManager was not created.";
51 return instance_;
52 }
53
54 // static
HasInstance()55 bool DeviceDataManager::HasInstance() {
56 return instance_ != NULL;
57 }
58
ClearTouchTransformerRecord()59 void DeviceDataManager::ClearTouchTransformerRecord() {
60 for (int i = 0; i < kMaxDeviceNum; i++) {
61 touch_device_transformer_map_[i] = gfx::Transform();
62 touch_device_to_display_map_[i] = gfx::Display::kInvalidDisplayID;
63 touch_radius_scale_map_[i] = 1.0;
64 }
65 }
66
IsTouchDeviceIdValid(int touch_device_id) const67 bool DeviceDataManager::IsTouchDeviceIdValid(int touch_device_id) const {
68 return (touch_device_id > 0 && touch_device_id < kMaxDeviceNum);
69 }
70
UpdateTouchInfoForDisplay(int64_t display_id,int touch_device_id,const gfx::Transform & touch_transformer)71 void DeviceDataManager::UpdateTouchInfoForDisplay(
72 int64_t display_id,
73 int touch_device_id,
74 const gfx::Transform& touch_transformer) {
75 if (IsTouchDeviceIdValid(touch_device_id)) {
76 touch_device_to_display_map_[touch_device_id] = display_id;
77 touch_device_transformer_map_[touch_device_id] = touch_transformer;
78 }
79 }
80
UpdateTouchRadiusScale(int touch_device_id,double scale)81 void DeviceDataManager::UpdateTouchRadiusScale(int touch_device_id,
82 double scale) {
83 if (IsTouchDeviceIdValid(touch_device_id))
84 touch_radius_scale_map_[touch_device_id] = scale;
85 }
86
ApplyTouchRadiusScale(int touch_device_id,double * radius)87 void DeviceDataManager::ApplyTouchRadiusScale(int touch_device_id,
88 double* radius) {
89 if (IsTouchDeviceIdValid(touch_device_id))
90 *radius = (*radius) * touch_radius_scale_map_[touch_device_id];
91 }
92
ApplyTouchTransformer(int touch_device_id,float * x,float * y)93 void DeviceDataManager::ApplyTouchTransformer(int touch_device_id,
94 float* x,
95 float* y) {
96 if (IsTouchDeviceIdValid(touch_device_id)) {
97 gfx::Point3F point(*x, *y, 0.0);
98 const gfx::Transform& trans =
99 touch_device_transformer_map_[touch_device_id];
100 trans.TransformPoint(&point);
101 *x = point.x();
102 *y = point.y();
103 }
104 }
105
GetDisplayForTouchDevice(int touch_device_id) const106 int64_t DeviceDataManager::GetDisplayForTouchDevice(int touch_device_id) const {
107 if (IsTouchDeviceIdValid(touch_device_id))
108 return touch_device_to_display_map_[touch_device_id];
109 return gfx::Display::kInvalidDisplayID;
110 }
111
OnTouchscreenDevicesUpdated(const std::vector<TouchscreenDevice> & devices)112 void DeviceDataManager::OnTouchscreenDevicesUpdated(
113 const std::vector<TouchscreenDevice>& devices) {
114 touchscreen_devices_ = devices;
115
116 FOR_EACH_OBSERVER(InputDeviceEventObserver,
117 observers_,
118 OnInputDeviceConfigurationChanged());
119 }
120
AddObserver(InputDeviceEventObserver * observer)121 void DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) {
122 observers_.AddObserver(observer);
123 }
124
RemoveObserver(InputDeviceEventObserver * observer)125 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) {
126 observers_.RemoveObserver(observer);
127 }
128
129 } // namespace ui
130