Add ICU message format support
[chromium-blink-merge.git] / ui / events / devices / device_data_manager.cc
blob8df3f37d0c42e650186c9e1023532b6eefeda2e4
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 // 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_, \
18 observer_method()); \
21 namespace ui {
23 namespace {
25 bool InputDeviceEquals(const ui::InputDevice& a, const ui::InputDevice& b) {
26 return a.id == b.id;
29 } // namespace
31 // static
32 DeviceDataManager* DeviceDataManager::instance_ = NULL;
34 DeviceDataManager::DeviceDataManager() {
35 ClearTouchDeviceAssociations();
38 DeviceDataManager::~DeviceDataManager() {
41 // static
42 DeviceDataManager* DeviceDataManager::instance() { return instance_; }
44 void DeviceDataManager::set_instance(DeviceDataManager* instance) {
45 DCHECK(instance)
46 << "Must reset the DeviceDataManager using DeleteInstance().";
47 DCHECK(!instance_) "Can not set multiple instances of DeviceDataManager.";
48 instance_ = instance;
51 // static
52 void DeviceDataManager::CreateInstance() {
53 if (instance())
54 return;
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() {
63 if (instance_) {
64 delete instance_;
65 instance_ = NULL;
69 // static
70 DeviceDataManager* DeviceDataManager::GetInstance() {
71 CHECK(instance_) << "DeviceDataManager was not created.";
72 return instance_;
75 // static
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,
95 int touch_device_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,
104 double scale) {
105 if (IsTouchDeviceIdValid(touch_device_id))
106 touch_radius_scale_map_[touch_device_id] = scale;
109 void DeviceDataManager::ApplyTouchRadiusScale(int touch_device_id,
110 double* radius) {
111 if (IsTouchDeviceIdValid(touch_device_id))
112 *radius = (*radius) * touch_radius_scale_map_[touch_device_id];
115 void DeviceDataManager::ApplyTouchTransformer(int touch_device_id,
116 float* x,
117 float* y) {
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);
123 *x = point.x();
124 *y = point.y();
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(),
139 devices.end(),
140 touchscreen_devices_.begin(),
141 InputDeviceEquals)) {
142 return;
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(),
152 devices.end(),
153 keyboard_devices_.begin(),
154 InputDeviceEquals)) {
155 return;
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(),
165 devices.end(),
166 mouse_devices_.begin(),
167 InputDeviceEquals)) {
168 return;
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(),
178 devices.end(),
179 touchpad_devices_.begin(),
180 InputDeviceEquals)) {
181 return;
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);
216 } // namespace ui