| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_cont
roller.h" | 5 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_cont
roller.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/touch/ash_touch_transform_controller.h" | 8 #include "ash/touch/ash_touch_transform_controller.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_view
.h" | 11 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_view
.h" |
| 11 #include "ui/display/screen.h" | 12 #include "ui/display/screen.h" |
| 12 #include "ui/events/event.h" | 13 #include "ui/events/event.h" |
| 13 #include "ui/events/event_constants.h" | 14 #include "ui/events/event_constants.h" |
| 14 | 15 |
| 15 namespace chromeos { | 16 namespace chromeos { |
| 16 | 17 |
| 17 // Time interval after a touch event during which all other touch events are | 18 // Time interval after a touch event during which all other touch events are |
| 18 // ignored during calibration. | 19 // ignored during calibration. |
| 19 const base::TimeDelta TouchCalibratorController::kTouchIntervalThreshold = | 20 const base::TimeDelta TouchCalibratorController::kTouchIntervalThreshold = |
| 20 base::TimeDelta::FromMilliseconds(200); | 21 base::TimeDelta::FromMilliseconds(200); |
| 21 | 22 |
| 22 TouchCalibratorController::TouchCalibratorController() | 23 TouchCalibratorController::TouchCalibratorController() |
| 23 : last_touch_timestamp_(base::Time::Now()) {} | 24 : last_touch_timestamp_(base::Time::Now()) {} |
| 24 | 25 |
| 25 TouchCalibratorController::~TouchCalibratorController() { | 26 TouchCalibratorController::~TouchCalibratorController() { |
| 26 touch_calibrator_views_.clear(); | 27 touch_calibrator_views_.clear(); |
| 27 StopCalibration(); | 28 StopCalibration(); |
| 28 } | 29 } |
| 29 | 30 |
| 30 void TouchCalibratorController::OnDisplayConfigurationChanged() { | 31 void TouchCalibratorController::OnDisplayConfigurationChanged() { |
| 31 touch_calibrator_views_.clear(); | 32 touch_calibrator_views_.clear(); |
| 32 StopCalibration(); | 33 StopCalibration(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 void TouchCalibratorController::StartCalibration( | 36 void TouchCalibratorController::StartCalibration( |
| 36 const display::Display& target_display) { | 37 const display::Display& target_display, |
| 38 const TouchCalibratorController::TouchCalibrationCallback& callback) { |
| 37 is_calibrating_ = true; | 39 is_calibrating_ = true; |
| 40 callback_ = callback; |
| 41 |
| 38 ash::Shell::GetInstance()->window_tree_host_manager()->AddObserver(this); | 42 ash::Shell::GetInstance()->window_tree_host_manager()->AddObserver(this); |
| 39 target_display_ = target_display; | 43 target_display_ = target_display; |
| 40 | 44 |
| 41 // Clear all touch calibrator views used in any previous calibration. | 45 // Clear all touch calibrator views used in any previous calibration. |
| 42 touch_calibrator_views_.clear(); | 46 touch_calibrator_views_.clear(); |
| 43 | 47 |
| 44 // Reset the calibration data. | 48 // Reset the calibration data. |
| 45 touch_point_quad_.fill(std::make_pair(gfx::Point(0, 0), gfx::Point(0, 0))); | 49 touch_point_quad_.fill(std::make_pair(gfx::Point(0, 0), gfx::Point(0, 0))); |
| 46 | 50 |
| 47 std::vector<display::Display> displays = | 51 std::vector<display::Display> displays = |
| 48 display::Screen::GetScreen()->GetAllDisplays(); | 52 display::Screen::GetScreen()->GetAllDisplays(); |
| 49 | 53 |
| 50 for (const display::Display& display : displays) { | 54 for (const display::Display& display : displays) { |
| 51 bool is_primary_view = display.id() == target_display_.id(); | 55 bool is_primary_view = display.id() == target_display_.id(); |
| 52 touch_calibrator_views_[display.id()] = | 56 touch_calibrator_views_[display.id()] = |
| 53 base::MakeUnique<TouchCalibratorView>(display, is_primary_view); | 57 base::MakeUnique<TouchCalibratorView>(display, is_primary_view); |
| 54 } | 58 } |
| 55 | 59 |
| 56 ash::Shell::GetInstance()->touch_transformer_controller()->SetForCalibration( | 60 ash::Shell::GetInstance()->touch_transformer_controller()->SetForCalibration( |
| 57 true); | 61 true); |
| 58 | 62 |
| 59 // Add self as an event handler target. | 63 // Add self as an event handler target. |
| 60 ash::Shell::GetInstance()->AddPreTargetHandler(this); | 64 ash::Shell::GetInstance()->AddPreTargetHandler(this); |
| 61 } | 65 } |
| 62 | 66 |
| 63 void TouchCalibratorController::StopCalibration() { | 67 void TouchCalibratorController::StopCalibration() { |
| 64 if (!is_calibrating_) | 68 if (!is_calibrating_) |
| 65 return; | 69 return; |
| 66 is_calibrating_ = false; | 70 is_calibrating_ = false; |
| 71 |
| 67 ash::Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); | 72 ash::Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); |
| 68 | 73 |
| 69 ash::Shell::GetInstance()->touch_transformer_controller()->SetForCalibration( | 74 ash::Shell::GetInstance()->touch_transformer_controller()->SetForCalibration( |
| 70 false); | 75 false); |
| 71 | 76 |
| 72 // Remove self as the event handler. | 77 // Remove self as the event handler. |
| 73 ash::Shell::GetInstance()->RemovePreTargetHandler(this); | 78 ash::Shell::GetInstance()->RemovePreTargetHandler(this); |
| 74 | 79 |
| 75 // Transition all touch calibrator views to their final state for a graceful | 80 // Transition all touch calibrator views to their final state for a graceful |
| 76 // exit. | 81 // exit. |
| 77 for (const auto& it : touch_calibrator_views_) | 82 for (const auto& it : touch_calibrator_views_) |
| 78 it.second->SkipToFinalState(); | 83 it.second->SkipToFinalState(); |
| 84 |
| 85 if (callback_) { |
| 86 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 87 base::Bind(callback_, false)); |
| 88 callback_.Reset(); |
| 89 } |
| 79 } | 90 } |
| 80 | 91 |
| 81 // ui::EventHandler: | 92 // ui::EventHandler: |
| 82 void TouchCalibratorController::OnKeyEvent(ui::KeyEvent* key) { | 93 void TouchCalibratorController::OnKeyEvent(ui::KeyEvent* key) { |
| 83 if (!is_calibrating_) | 94 if (!is_calibrating_) |
| 84 return; | 95 return; |
| 85 // Detect ESC key press. | 96 // Detect ESC key press. |
| 86 if (key->type() == ui::ET_KEY_PRESSED && key->key_code() == ui::VKEY_ESCAPE) | 97 if (key->type() == ui::ET_KEY_PRESSED && key->key_code() == ui::VKEY_ESCAPE) |
| 87 StopCalibration(); | 98 StopCalibration(); |
| 99 |
| 88 key->StopPropagation(); | 100 key->StopPropagation(); |
| 89 } | 101 } |
| 90 | 102 |
| 91 void TouchCalibratorController::OnTouchEvent(ui::TouchEvent* touch) { | 103 void TouchCalibratorController::OnTouchEvent(ui::TouchEvent* touch) { |
| 92 if (!is_calibrating_) | 104 if (!is_calibrating_) |
| 93 return; | 105 return; |
| 94 if (touch->type() != ui::ET_TOUCH_RELEASED) | 106 if (touch->type() != ui::ET_TOUCH_RELEASED) |
| 95 return; | 107 return; |
| 96 if (base::Time::Now() - last_touch_timestamp_ < kTouchIntervalThreshold) | 108 if (base::Time::Now() - last_touch_timestamp_ < kTouchIntervalThreshold) |
| 97 return; | 109 return; |
| 98 last_touch_timestamp_ = base::Time::Now(); | 110 last_touch_timestamp_ = base::Time::Now(); |
| 99 | 111 |
| 100 TouchCalibratorView* target_screen_calibration_view = | 112 TouchCalibratorView* target_screen_calibration_view = |
| 101 touch_calibrator_views_[target_display_.id()].get(); | 113 touch_calibrator_views_[target_display_.id()].get(); |
| 102 | 114 |
| 103 // If this is the final state, then store all calibration data and stop | 115 // If this is the final state, then store all calibration data and stop |
| 104 // calibration. | 116 // calibration. |
| 105 if (target_screen_calibration_view->state() == | 117 if (target_screen_calibration_view->state() == |
| 106 TouchCalibratorView::CALIBRATION_COMPLETE) { | 118 TouchCalibratorView::CALIBRATION_COMPLETE) { |
| 119 if (callback_) { |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 121 FROM_HERE, base::Bind(callback_, true)); |
| 122 callback_.Reset(); |
| 123 } |
| 107 StopCalibration(); | 124 StopCalibration(); |
| 108 ash::Shell::GetInstance()->display_manager()->SetTouchCalibrationData( | 125 ash::Shell::GetInstance()->display_manager()->SetTouchCalibrationData( |
| 109 target_display_.id(), touch_point_quad_, | 126 target_display_.id(), touch_point_quad_, |
| 110 target_screen_calibration_view->size()); | 127 target_screen_calibration_view->size()); |
| 111 return; | 128 return; |
| 112 } | 129 } |
| 113 | 130 |
| 114 int state_index; | 131 int state_index; |
| 115 // Maps the state to an integer value. Assigns a non negative integral value | 132 // Maps the state to an integer value. Assigns a non negative integral value |
| 116 // for a state in which the user can interact with the the interface. | 133 // for a state in which the user can interact with the the interface. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 141 } else { | 158 } else { |
| 142 // TODO(malaykeshav): Display some kind of error for the user. | 159 // TODO(malaykeshav): Display some kind of error for the user. |
| 143 NOTREACHED() << "Touch calibration failed. Could not retrieve location for" | 160 NOTREACHED() << "Touch calibration failed. Could not retrieve location for" |
| 144 " display point. Retry calibration."; | 161 " display point. Retry calibration."; |
| 145 } | 162 } |
| 146 | 163 |
| 147 target_screen_calibration_view->AdvanceToNextState(); | 164 target_screen_calibration_view->AdvanceToNextState(); |
| 148 } | 165 } |
| 149 | 166 |
| 150 } // namespace chromeos | 167 } // namespace chromeos |
| OLD | NEW |