Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / events / device_data_manager.cc
blobe7eb1147c8b339b741a0b6838ab8e87d93106621
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/gfx/display.h"
11 #include "ui/gfx/geometry/point3_f.h"
13 namespace ui {
15 // static
16 DeviceDataManager* DeviceDataManager::instance_ = NULL;
18 DeviceDataManager::DeviceDataManager() {
19 CHECK(!instance_) << "Can not create multiple instances of DeviceDataManager";
20 instance_ = this;
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_);
33 instance_ = NULL;
36 // static
37 DeviceDataManager* DeviceDataManager::instance() { return instance_; }
39 // static
40 void DeviceDataManager::CreateInstance() {
41 if (instance())
42 return;
44 new DeviceDataManager();
47 // static
48 DeviceDataManager* DeviceDataManager::GetInstance() {
49 CHECK(instance_) << "DeviceDataManager was not created.";
50 return instance_;
53 // static
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(
71 int64_t display_id,
72 int touch_device_id,
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,
81 double scale) {
82 if (IsTouchDeviceIdValid(touch_device_id))
83 touch_radius_scale_map_[touch_device_id] = scale;
86 void DeviceDataManager::ApplyTouchRadiusScale(int touch_device_id,
87 double* radius) {
88 if (IsTouchDeviceIdValid(touch_device_id))
89 *radius = (*radius) * touch_radius_scale_map_[touch_device_id];
92 void DeviceDataManager::ApplyTouchTransformer(int touch_device_id,
93 float* x,
94 float* y) {
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);
100 *x = point.x();
101 *y = point.y();
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;
111 } // namespace ui