1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/events/device_data_manager.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/at_exit.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/bind.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/logging.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/events/input_device_event_observer.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/gfx/display.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/gfx/geometry/point3_f.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace ui {
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
17116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDeviceDataManager* DeviceDataManager::instance_ = NULL;
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
19116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDeviceDataManager::DeviceDataManager() {
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CHECK(!instance_) << "Can not create multiple instances of DeviceDataManager";
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  instance_ = this;
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::AtExitManager::RegisterTask(
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      base::Bind(&base::DeletePointer<DeviceDataManager>, this));
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  for (int i = 0; i < kMaxDeviceNum; ++i) {
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    touch_device_to_display_map_[i] = gfx::Display::kInvalidDisplayID;
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    touch_radius_scale_map_[i] = 1.0;
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
32116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDeviceDataManager::~DeviceDataManager() {
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CHECK_EQ(this, instance_);
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  instance_ = NULL;
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
38116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDeviceDataManager* DeviceDataManager::instance() { return instance_; }
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid DeviceDataManager::CreateInstance() {
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (instance())
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  new DeviceDataManager();
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
49116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDeviceDataManager* DeviceDataManager::GetInstance() {
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CHECK(instance_) << "DeviceDataManager was not created.";
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return instance_;
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool DeviceDataManager::HasInstance() {
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return instance_ != NULL;
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid DeviceDataManager::ClearTouchTransformerRecord() {
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (int i = 0; i < kMaxDeviceNum; i++) {
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    touch_device_transformer_map_[i] = gfx::Transform();
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    touch_device_to_display_map_[i] = gfx::Display::kInvalidDisplayID;
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    touch_radius_scale_map_[i] = 1.0;
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool DeviceDataManager::IsTouchDeviceIdValid(int touch_device_id) const {
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return (touch_device_id > 0 && touch_device_id < kMaxDeviceNum);
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid DeviceDataManager::UpdateTouchInfoForDisplay(
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int64_t display_id,
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int touch_device_id,
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const gfx::Transform& touch_transformer) {
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (IsTouchDeviceIdValid(touch_device_id)) {
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    touch_device_to_display_map_[touch_device_id] = display_id;
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    touch_device_transformer_map_[touch_device_id] = touch_transformer;
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void DeviceDataManager::UpdateTouchRadiusScale(int touch_device_id,
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                               double scale) {
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (IsTouchDeviceIdValid(touch_device_id))
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    touch_radius_scale_map_[touch_device_id] = scale;
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void DeviceDataManager::ApplyTouchRadiusScale(int touch_device_id,
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                              double* radius) {
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (IsTouchDeviceIdValid(touch_device_id))
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    *radius = (*radius) * touch_radius_scale_map_[touch_device_id];
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid DeviceDataManager::ApplyTouchTransformer(int touch_device_id,
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                              float* x,
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                              float* y) {
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (IsTouchDeviceIdValid(touch_device_id)) {
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    gfx::Point3F point(*x, *y, 0.0);
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const gfx::Transform& trans =
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        touch_device_transformer_map_[touch_device_id];
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    trans.TransformPoint(&point);
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    *x = point.x();
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    *y = point.y();
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint64_t DeviceDataManager::GetDisplayForTouchDevice(int touch_device_id) const {
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (IsTouchDeviceIdValid(touch_device_id))
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return touch_device_to_display_map_[touch_device_id];
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return gfx::Display::kInvalidDisplayID;
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid DeviceDataManager::OnTouchscreenDevicesUpdated(
1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::vector<TouchscreenDevice>& devices) {
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  touchscreen_devices_ = devices;
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  FOR_EACH_OBSERVER(InputDeviceEventObserver,
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    observers_,
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                    OnInputDeviceConfigurationChanged());
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) {
1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  observers_.AddObserver(observer);
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) {
1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  observers_.RemoveObserver(observer);
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace ui
130