blob: cb3a81ea0fe7d130a05cb4294a989867cb62e003 [file] [log] [blame]
malaykeshav9fe4c53f2016-12-22 21:17:031// 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
F#m6b86d4b2017-09-06 23:46:235#ifndef ASH_DISPLAY_TOUCH_CALIBRATOR_TOUCH_CALIBRATOR_CONTROLLER_H_
6#define ASH_DISPLAY_TOUCH_CALIBRATOR_TOUCH_CALIBRATOR_CONTROLLER_H_
malaykeshav9fe4c53f2016-12-22 21:17:037
8#include <map>
9
F#m6b86d4b2017-09-06 23:46:2310#include "ash/ash_export.h"
malaykeshav9fe4c53f2016-12-22 21:17:0311#include "ash/display/window_tree_host_manager.h"
12#include "base/time/time.h"
13#include "ui/display/display.h"
14#include "ui/display/manager/managed_display_info.h"
F#m02490122017-09-28 22:47:2415#include "ui/events/devices/touchscreen_device.h"
malaykeshav9fe4c53f2016-12-22 21:17:0316#include "ui/events/event_handler.h"
17
18namespace ui {
19class KeyEvent;
20class TouchEvent;
F#m6b86d4b2017-09-06 23:46:2321} // namespace ui
malaykeshav9fe4c53f2016-12-22 21:17:0322
F#m6b86d4b2017-09-06 23:46:2323namespace ash {
malaykeshav9fe4c53f2016-12-22 21:17:0324
25class TouchCalibratorView;
26
F#m02490122017-09-28 22:47:2427// TouchCalibratorController is responsible for managing the touch calibration
28// process. In case of native touch calibration it is also responsible for
29// collecting the touch calibration associated data from the user. It
30// instantiates TouchCalibratorView classes to present the native UX interface
31// the user can interact with for calibration.
32// This controller ensures that only one instance of calibration is running at
33// any given time.
F#m6b86d4b2017-09-06 23:46:2334class ASH_EXPORT TouchCalibratorController
35 : public ui::EventHandler,
36 public WindowTreeHostManager::Observer {
malaykeshav9fe4c53f2016-12-22 21:17:0337 public:
38 using CalibrationPointPairQuad =
39 display::TouchCalibrationData::CalibrationPointPairQuad;
F#m02490122017-09-28 22:47:2440 using TouchCalibrationCallback = base::OnceCallback<void(bool)>;
malaykeshav9fe4c53f2016-12-22 21:17:0341
42 static const base::TimeDelta kTouchIntervalThreshold;
43
44 TouchCalibratorController();
45 ~TouchCalibratorController() override;
46
47 // ui::EventHandler
48 void OnKeyEvent(ui::KeyEvent* event) override;
49 void OnTouchEvent(ui::TouchEvent* event) override;
50
51 // WindowTreeHostManager::Observer
52 void OnDisplayConfigurationChanged() override;
53
54 // Starts the calibration process for the given |target_display|.
F#m02490122017-09-28 22:47:2455 // |opt_callback| is an optional callback that if provided is executed
56 // with the success or failure of the calibration as a boolean argument.
malaykeshav5f64df432017-01-20 02:09:4257 void StartCalibration(const display::Display& target_display,
F#m02490122017-09-28 22:47:2458 bool is_custom_calibration,
59 TouchCalibrationCallback opt_callback);
malaykeshav9fe4c53f2016-12-22 21:17:0360
F#m02490122017-09-28 22:47:2461 // Stops any ongoing calibration process. This is a hard stop which does not
62 // save any calibration data. Call CompleteCalibration() if you wish to save
63 // calibration data.
64 void StopCalibrationAndResetParams();
malaykeshav9fe4c53f2016-12-22 21:17:0365
F#m02490122017-09-28 22:47:2466 // Completes the touch calibration by storing the calibration data for the
67 // display.
68 void CompleteCalibration(const CalibrationPointPairQuad& pairs,
69 const gfx::Size& display_size);
70
71 // Returns true if any type of touch calibration is active.
72 bool IsCalibrating() const;
malaykeshav9fe4c53f2016-12-22 21:17:0373
74 private:
75 friend class TouchCalibratorControllerTest;
malaykeshav9fe4c53f2016-12-22 21:17:0376 FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, TouchThreshold);
F#m02490122017-09-28 22:47:2477 FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, CustomCalibration);
78 FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest,
79 CustomCalibrationInvalidTouchId);
F#mad6eceaf2017-11-17 10:58:0180 FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest,
81 InternalTouchDeviceIsRejected);
F#m02490122017-09-28 22:47:2482
83 enum class CalibrationState {
84 // Indicates that the touch calibration is currently active with the built
85 // in native UX.
86 kNativeCalibration = 0,
87
88 // Indicates that the touch calibration is currently active with a custom
89 // UX via the extensions API.
90 kCustomCalibration,
91
92 // Indicates that touch calibration is currently inactive.
93 kInactive
94 };
95 CalibrationState state_ = CalibrationState::kInactive;
malaykeshav9fe4c53f2016-12-22 21:17:0396
97 // A map for TouchCalibrator view with the key as display id of the display
98 // it is present in.
99 std::map<int64_t, std::unique_ptr<TouchCalibratorView>>
100 touch_calibrator_views_;
101
102 // The display which is being calibrated by the touch calibrator controller.
103 // This is valid only if |is_calibrating| is set to true.
104 display::Display target_display_;
105
106 // During calibration this stores the timestamp when the previous touch event
107 // was received.
108 base::Time last_touch_timestamp_;
109
F#m02490122017-09-28 22:47:24110 // This is populated during calibration, based on the source id of the device
111 // the events are originating from.
112 int touch_device_id_ = ui::InputDevice::kInvalidId;
malaykeshav9fe4c53f2016-12-22 21:17:03113
F#m3e506f502017-10-03 05:35:33114 // A set of ids that belong to touch devices associated with the internal
F#mad6eceaf2017-11-17 10:58:01115 // display and are of type |ui::InputDeviceType::INPUT_DEVICE_INTERNAL|. This
116 // is only valid when |state_| is not |kInactive|.
F#m3e506f502017-10-03 05:35:33117 std::set<int> internal_touch_device_ids_;
118
malaykeshav9fe4c53f2016-12-22 21:17:03119 // An array of Calibration point pairs. This stores all the 4 display and
120 // touch input point pairs that will be used for calibration.
121 CalibrationPointPairQuad touch_point_quad_;
122
malaykeshav5f64df432017-01-20 02:09:42123 // A callback to be called when touch calibration completes.
F#m02490122017-09-28 22:47:24124 TouchCalibrationCallback opt_callback_;
malaykeshav5f64df432017-01-20 02:09:42125
F#mad6eceaf2017-11-17 10:58:01126 // The touch device under calibration may be re-associated to another display
127 // during calibration. In such a case, the events originating from the touch
128 // device are tranformed based on parameters of the previous display it was
129 // linked to. We need to undo these transformations before recording the event
130 // locations.
131 gfx::Transform event_transformer_;
132
malaykeshav9fe4c53f2016-12-22 21:17:03133 DISALLOW_COPY_AND_ASSIGN(TouchCalibratorController);
134};
135
F#m6b86d4b2017-09-06 23:46:23136} // namespace ash
137#endif // ASH_DISPLAY_TOUCH_CALIBRATOR_TOUCH_CALIBRATOR_CONTROLLER_H_