| OLD | NEW |
| (Empty) |
| 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 namespace { | |
| 17 | |
| 18 bool InputDeviceEquals(const ui::InputDevice& a, const ui::InputDevice& b) { | |
| 19 return a.id == b.id; | |
| 20 } | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 // static | |
| 25 DeviceDataManager* DeviceDataManager::instance_ = NULL; | |
| 26 | |
| 27 DeviceDataManager::DeviceDataManager() { | |
| 28 CHECK(!instance_) << "Can not create multiple instances of DeviceDataManager"; | |
| 29 instance_ = this; | |
| 30 | |
| 31 base::AtExitManager::RegisterTask( | |
| 32 base::Bind(&base::DeletePointer<DeviceDataManager>, this)); | |
| 33 | |
| 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; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 DeviceDataManager::~DeviceDataManager() { | |
| 41 CHECK_EQ(this, instance_); | |
| 42 instance_ = NULL; | |
| 43 } | |
| 44 | |
| 45 // static | |
| 46 DeviceDataManager* DeviceDataManager::instance() { return instance_; } | |
| 47 | |
| 48 // static | |
| 49 void DeviceDataManager::CreateInstance() { | |
| 50 if (instance()) | |
| 51 return; | |
| 52 | |
| 53 new DeviceDataManager(); | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 DeviceDataManager* DeviceDataManager::GetInstance() { | |
| 58 CHECK(instance_) << "DeviceDataManager was not created."; | |
| 59 return instance_; | |
| 60 } | |
| 61 | |
| 62 // static | |
| 63 bool DeviceDataManager::HasInstance() { | |
| 64 return instance_ != NULL; | |
| 65 } | |
| 66 | |
| 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; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 bool DeviceDataManager::IsTouchDeviceIdValid( | |
| 76 unsigned int touch_device_id) const { | |
| 77 return (touch_device_id > 0 && touch_device_id < kMaxDeviceNum); | |
| 78 } | |
| 79 | |
| 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; | |
| 87 } | |
| 88 } | |
| 89 | |
| 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; | |
| 94 } | |
| 95 | |
| 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]; | |
| 100 } | |
| 101 | |
| 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(); | |
| 112 } | |
| 113 } | |
| 114 | |
| 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; | |
| 120 } | |
| 121 | |
| 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; | |
| 130 } | |
| 131 touchscreen_devices_ = devices; | |
| 132 FOR_EACH_OBSERVER(InputDeviceEventObserver, | |
| 133 observers_, | |
| 134 OnTouchscreenDeviceConfigurationChanged()); | |
| 135 } | |
| 136 | |
| 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; | |
| 145 } | |
| 146 keyboard_devices_ = devices; | |
| 147 FOR_EACH_OBSERVER(InputDeviceEventObserver, | |
| 148 observers_, | |
| 149 OnKeyboardDeviceConfigurationChanged()); | |
| 150 } | |
| 151 | |
| 152 void DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) { | |
| 153 observers_.AddObserver(observer); | |
| 154 } | |
| 155 | |
| 156 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) { | |
| 157 observers_.RemoveObserver(observer); | |
| 158 } | |
| 159 | |
| 160 } // namespace ui | |
| OLD | NEW |