Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / events / devices / device_data_manager.cc
blob2bdbac9847424c6533133f812afa04285b5a7ac3
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"
8 #include "base/bind.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 namespace ui {
16 namespace {
18 bool InputDeviceEquals(const ui::InputDevice& a, const ui::InputDevice& b) {
19 return a.id == b.id;
22 } // namespace
24 // static
25 DeviceDataManager* DeviceDataManager::instance_ = NULL;
27 DeviceDataManager::DeviceDataManager() {
28 CHECK(!instance_) << "Can not create multiple instances of DeviceDataManager";
29 instance_ = this;
31 base::AtExitManager::RegisterTask(
32 base::Bind(&base::DeletePointer<DeviceDataManager>, this));
34 ClearTouchDeviceAssociations();
37 DeviceDataManager::~DeviceDataManager() {
38 CHECK_EQ(this, instance_);
39 instance_ = NULL;
42 // static
43 DeviceDataManager* DeviceDataManager::instance() { return instance_; }
45 // static
46 void DeviceDataManager::CreateInstance() {
47 if (instance())
48 return;
50 new DeviceDataManager();
53 // static
54 DeviceDataManager* DeviceDataManager::GetInstance() {
55 CHECK(instance_) << "DeviceDataManager was not created.";
56 return instance_;
59 // static
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,
88 double scale) {
89 if (IsTouchDeviceIdValid(touch_device_id))
90 touch_radius_scale_map_[touch_device_id] = scale;
93 void DeviceDataManager::ApplyTouchRadiusScale(unsigned int touch_device_id,
94 double* radius) {
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,
100 float* x,
101 float* y) {
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);
107 *x = point.x();
108 *y = point.y();
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(),
123 devices.end(),
124 touchscreen_devices_.begin(),
125 InputDeviceEquals)) {
126 return;
128 touchscreen_devices_ = devices;
129 FOR_EACH_OBSERVER(InputDeviceEventObserver,
130 observers_,
131 OnTouchscreenDeviceConfigurationChanged());
134 void DeviceDataManager::OnKeyboardDevicesUpdated(
135 const std::vector<KeyboardDevice>& devices) {
136 if (devices.size() == keyboard_devices_.size() &&
137 std::equal(devices.begin(),
138 devices.end(),
139 keyboard_devices_.begin(),
140 InputDeviceEquals)) {
141 return;
143 keyboard_devices_ = devices;
144 FOR_EACH_OBSERVER(InputDeviceEventObserver,
145 observers_,
146 OnKeyboardDeviceConfigurationChanged());
149 void DeviceDataManager::OnMouseDevicesUpdated(
150 const std::vector<InputDevice>& devices) {
151 if (devices.size() == mouse_devices_.size() &&
152 std::equal(devices.begin(),
153 devices.end(),
154 mouse_devices_.begin(),
155 InputDeviceEquals)) {
156 return;
158 mouse_devices_ = devices;
159 FOR_EACH_OBSERVER(InputDeviceEventObserver,
160 observers_,
161 OnMouseDeviceConfigurationChanged());
164 void DeviceDataManager::OnTouchpadDevicesUpdated(
165 const std::vector<InputDevice>& devices) {
166 if (devices.size() == touchpad_devices_.size() &&
167 std::equal(devices.begin(),
168 devices.end(),
169 touchpad_devices_.begin(),
170 InputDeviceEquals)) {
171 return;
173 touchpad_devices_ = devices;
174 FOR_EACH_OBSERVER(InputDeviceEventObserver,
175 observers_,
176 OnTouchpadDeviceConfigurationChanged());
179 void DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) {
180 observers_.AddObserver(observer);
183 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) {
184 observers_.RemoveObserver(observer);
187 } // namespace ui