Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_controller.cc

Issue 2596553003: Implements Touch calibrator controller and a skeleton for touch calibrator view. (Closed)
Patch Set: Implements Touch calibrator controller and a skeleton for touch calibrator view. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_cont roller.h"
6
7 #include "ash/shell.h"
8 #include "ash/touch/touch_transformer_controller.h"
9 #include "base/memory/ptr_util.h"
10 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_view .h"
11 #include "ui/display/screen.h"
12 #include "ui/events/event.h"
13 #include "ui/events/event_constants.h"
14
15 namespace chromeos {
16 namespace {
17
18 // Time interval after a touch event during which all other touch events are
19 // ignored during calibration.
20 // If modified also update the value in touch_calibrator_controller_unittest.cc.
21 const auto kTouchIntervalThreshold = base::TimeDelta::FromMilliseconds(200);
stevenjb 2016/12/22 01:14:20 If this is used in a test it should really be a st
malaykeshav 2016/12/22 11:50:07 Done.
22
23 // Returns the index corresponding to the state the touch point is in currently.
24 // Will return -1 if the touch point is not in a state where it is ready for
25 // input(eg. will return -1 if the touch point is being animated or if the touch
26 // point is currently not visible,)
27 int GetDisplayPointStateIndex(TouchCalibratorView::State state) {
28 switch (state) {
29 case TouchCalibratorView::DISPLAY_POINT_1:
30 return 0;
31 case TouchCalibratorView::DISPLAY_POINT_2:
32 return 1;
33 case TouchCalibratorView::DISPLAY_POINT_3:
34 return 2;
35 case TouchCalibratorView::DISPLAY_POINT_4:
36 return 3;
37 default:
38 return -1;
39 }
40 }
41
42 // Returns true if the touch event needs to be processed for the given touch
43 // calibration view state.
44 bool CanTouch(TouchCalibratorView::State state) {
45 if (GetDisplayPointStateIndex(state) + 1)
stevenjb 2016/12/22 01:14:20 nit: This is confusing, just use if (GetDisplayPoi
malaykeshav 2016/12/22 11:50:07 Done
46 return true;
47 if (state == TouchCalibratorView::CALIBRATION_COMPLETE)
48 return true;
stevenjb 2016/12/22 01:14:20 nit: Test this first (simpler test)
malaykeshav 2016/12/22 11:50:07 Done
49 return false;
50 }
51
52 } // namespace
53
54 TouchCalibratorController::TouchCalibratorController()
55 : last_touch_timestamp_(base::Time::Now()) {
56 ash::Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
57 }
58
59 TouchCalibratorController::~TouchCalibratorController() {
60 ash::Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
61 touch_calibrator_views_.clear();
62 StopCalibration();
63 }
64
65 void TouchCalibratorController::OnDisplayConfigurationChanged() {
66 touch_calibrator_views_.clear();
67 StopCalibration();
68 }
69
70 void TouchCalibratorController::StartCalibration(
71 const display::Display& target_display) {
72 is_calibrating_ = true;
73 target_display_ = target_display;
74
75 // Clear all touch calibrator views used in any previous calibration.
76 touch_calibrator_views_.clear();
77
78 // Reset the calibration data.
79 touch_point_quad_.fill(std::make_pair(gfx::Point(0, 0), gfx::Point(0, 0)));
80
81 std::vector<display::Display> displays =
82 display::Screen::GetScreen()->GetAllDisplays();
83
84 for (const display::Display& display : displays) {
85 touch_calibrator_views_[display.id()] =
86 base::MakeUnique<TouchCalibratorView>(
87 display, display.id() == target_display_.id());
stevenjb 2016/12/22 01:14:20 nit: Define a local var for display.id() == target
malaykeshav 2016/12/22 11:50:07 Done
88 }
89
90 // TODO(malaykeshav): Uncomment this when this patch lands.
91 // ash::Shell::GetInstance()->touch_transformer_controller()
92 // ->SetForCalibration(true);
stevenjb 2016/12/22 01:14:20 We generally avoid committing commented out code l
malaykeshav 2016/12/22 11:50:07 Done
93
94 // Add self as an event handler target.
95 ash::Shell::GetInstance()->AddPreTargetHandler(this);
96 }
97
98 void TouchCalibratorController::StopCalibration() {
99 if (!is_calibrating_)
100 return;
101 is_calibrating_ = false;
102
103 // TODO(malaykeshav): Uncomment this when this patch lands!
104 // ash::Shell::GetInstance()->touch_transformer_controller()
105 // ->SetForCalibration(false);
stevenjb 2016/12/22 01:14:20 ditto
malaykeshav 2016/12/22 11:50:07 Done
106
107 // Remove self as the event handler.
108 ash::Shell::GetInstance()->RemovePreTargetHandler(this);
109
110 // Transition all touch calibrator views to their final state for a graceful
111 // exit.
112 for (const auto& it : touch_calibrator_views_)
113 it.second->SkipToFinalState();
114 }
115
116 // ui::EventHandler:
117 void TouchCalibratorController::OnKeyEvent(ui::KeyEvent* key) {
118 if (!is_calibrating_)
119 return;
120 // Detect ESC key press.
121 if (key->type() == ui::ET_KEY_PRESSED && key->key_code() == ui::VKEY_ESCAPE)
122 StopCalibration();
123 key->StopPropagation();
124 }
125
126 void TouchCalibratorController::OnTouchEvent(ui::TouchEvent* touch) {
127 LOG(ERROR) << "Received touch event";
stevenjb 2016/12/22 01:14:20 VLOG or remove
malaykeshav 2016/12/22 11:50:07 Remnant from when I was testing. Forgot to remove.
128 if (!is_calibrating_)
129 return;
130 if (touch->type() != ui::ET_TOUCH_RELEASED)
131 return;
132 if (base::Time::Now() - last_touch_timestamp_ < kTouchIntervalThreshold)
133 return;
134
135 last_touch_timestamp_ = base::Time::Now();
136
137 TouchCalibratorView* target_screen_calibration_view =
138 touch_calibrator_views_[target_display_.id()].get();
139
140 TouchCalibratorView::State calibrator_view_state =
141 target_screen_calibration_view->GetState();
142
143 // Check if the touch calibrator view is in a state when touch inputs have to
144 // be ignored.
145 if (!CanTouch(calibrator_view_state))
146 return;
147
148 int state_index = GetDisplayPointStateIndex(calibrator_view_state);
stevenjb 2016/12/22 01:14:20 We also call this in CanTouch, which is only calle
malaykeshav 2016/12/22 11:50:07 Done
149
150 // Store touch points if the state is a display point state.
151 if (state_index + 1) {
stevenjb 2016/12/22 01:14:20 state_index >= 0
malaykeshav 2016/12/22 11:50:07 Removed. Not required.
152 gfx::Point display_point;
153 if (target_screen_calibration_view->GetTouchPointLocation(&display_point))
154 touch_point_quad_[state_index] =
155 std::make_pair(display_point, touch->location());
stevenjb 2016/12/22 01:14:20 {}
malaykeshav 2016/12/22 11:50:07 done
156 }
157
158 // If this is the final state, then store all calibration data and stop
159 // calibration.
160 if (calibrator_view_state == TouchCalibratorView::CALIBRATION_COMPLETE) {
161 ash::Shell::GetInstance()->display_manager()->SetTouchCalibrationData(
162 target_display_.id(), touch_point_quad_,
163 target_screen_calibration_view->size());
164 StopCalibration();
165 return;
166 }
167
168 target_screen_calibration_view->NextState();
169 }
170
171 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698