MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / ui / events / device_data_manager.cc
bloba9e8578dd1f3d4590020c1b638206a61e02924ad
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"
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"
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 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_);
42 instance_ = NULL;
45 // static
46 DeviceDataManager* DeviceDataManager::instance() { return instance_; }
48 // static
49 void DeviceDataManager::CreateInstance() {
50 if (instance())
51 return;
53 new DeviceDataManager();
56 // static
57 DeviceDataManager* DeviceDataManager::GetInstance() {
58 CHECK(instance_) << "DeviceDataManager was not created.";
59 return instance_;
62 // static
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(
81 int64_t display_id,
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,
91 double scale) {
92 if (IsTouchDeviceIdValid(touch_device_id))
93 touch_radius_scale_map_[touch_device_id] = scale;
96 void DeviceDataManager::ApplyTouchRadiusScale(unsigned int touch_device_id,
97 double* radius) {
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,
103 float* x,
104 float* y) {
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);
110 *x = point.x();
111 *y = point.y();
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(),
126 devices.end(),
127 touchscreen_devices_.begin(),
128 InputDeviceEquals)) {
129 return;
131 touchscreen_devices_ = devices;
132 FOR_EACH_OBSERVER(InputDeviceEventObserver,
133 observers_,
134 OnTouchscreenDeviceConfigurationChanged());
137 void DeviceDataManager::OnKeyboardDevicesUpdated(
138 const std::vector<KeyboardDevice>& devices) {
139 if (devices.size() == keyboard_devices_.size() &&
140 std::equal(devices.begin(),
141 devices.end(),
142 keyboard_devices_.begin(),
143 InputDeviceEquals)) {
144 return;
146 keyboard_devices_ = devices;
147 FOR_EACH_OBSERVER(InputDeviceEventObserver,
148 observers_,
149 OnKeyboardDeviceConfigurationChanged());
152 void DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) {
153 observers_.AddObserver(observer);
156 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) {
157 observers_.RemoveObserver(observer);
160 } // namespace ui