malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 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 | |
F#m | 6b86d4b | 2017-09-06 23:46:23 | [diff] [blame] | 5 | #ifndef ASH_DISPLAY_TOUCH_CALIBRATOR_TOUCH_CALIBRATOR_CONTROLLER_H_ |
| 6 | #define ASH_DISPLAY_TOUCH_CALIBRATOR_TOUCH_CALIBRATOR_CONTROLLER_H_ |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | |
F#m | 6b86d4b | 2017-09-06 23:46:23 | [diff] [blame] | 10 | #include "ash/ash_export.h" |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 11 | #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#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 15 | #include "ui/events/devices/touchscreen_device.h" |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 16 | #include "ui/events/event_handler.h" |
| 17 | |
| 18 | namespace ui { |
| 19 | class KeyEvent; |
| 20 | class TouchEvent; |
F#m | 6b86d4b | 2017-09-06 23:46:23 | [diff] [blame] | 21 | } // namespace ui |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 22 | |
F#m | 6b86d4b | 2017-09-06 23:46:23 | [diff] [blame] | 23 | namespace ash { |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 24 | |
| 25 | class TouchCalibratorView; |
| 26 | |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 27 | // 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#m | 6b86d4b | 2017-09-06 23:46:23 | [diff] [blame] | 34 | class ASH_EXPORT TouchCalibratorController |
| 35 | : public ui::EventHandler, |
| 36 | public WindowTreeHostManager::Observer { |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 37 | public: |
| 38 | using CalibrationPointPairQuad = |
| 39 | display::TouchCalibrationData::CalibrationPointPairQuad; |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 40 | using TouchCalibrationCallback = base::OnceCallback<void(bool)>; |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 41 | |
| 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#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 55 | // |opt_callback| is an optional callback that if provided is executed |
| 56 | // with the success or failure of the calibration as a boolean argument. |
malaykeshav | 5f64df43 | 2017-01-20 02:09:42 | [diff] [blame] | 57 | void StartCalibration(const display::Display& target_display, |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 58 | bool is_custom_calibration, |
| 59 | TouchCalibrationCallback opt_callback); |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 60 | |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 61 | // 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(); |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 65 | |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 66 | // 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; |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | friend class TouchCalibratorControllerTest; |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 76 | FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, TouchThreshold); |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 77 | FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, CustomCalibration); |
| 78 | FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, |
| 79 | CustomCalibrationInvalidTouchId); |
F#m | ad6eceaf | 2017-11-17 10:58:01 | [diff] [blame] | 80 | FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, |
| 81 | InternalTouchDeviceIsRejected); |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 82 | |
| 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; |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 96 | |
| 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#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 110 | // 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; |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 113 | |
F#m | 3e506f50 | 2017-10-03 05:35:33 | [diff] [blame] | 114 | // A set of ids that belong to touch devices associated with the internal |
F#m | ad6eceaf | 2017-11-17 10:58:01 | [diff] [blame] | 115 | // display and are of type |ui::InputDeviceType::INPUT_DEVICE_INTERNAL|. This |
| 116 | // is only valid when |state_| is not |kInactive|. |
F#m | 3e506f50 | 2017-10-03 05:35:33 | [diff] [blame] | 117 | std::set<int> internal_touch_device_ids_; |
| 118 | |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 119 | // 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 | |
malaykeshav | 5f64df43 | 2017-01-20 02:09:42 | [diff] [blame] | 123 | // A callback to be called when touch calibration completes. |
F#m | 0249012 | 2017-09-28 22:47:24 | [diff] [blame] | 124 | TouchCalibrationCallback opt_callback_; |
malaykeshav | 5f64df43 | 2017-01-20 02:09:42 | [diff] [blame] | 125 | |
F#m | ad6eceaf | 2017-11-17 10:58:01 | [diff] [blame] | 126 | // 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 | |
malaykeshav | 9fe4c53f | 2016-12-22 21:17:03 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(TouchCalibratorController); |
| 134 | }; |
| 135 | |
F#m | 6b86d4b | 2017-09-06 23:46:23 | [diff] [blame] | 136 | } // namespace ash |
| 137 | #endif // ASH_DISPLAY_TOUCH_CALIBRATOR_TOUCH_CALIBRATOR_CONTROLLER_H_ |