touch_calibrator_controller.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef ASH_DISPLAY_TOUCH_CALIBRATOR_CONTROLLER_H_
  5. #define ASH_DISPLAY_TOUCH_CALIBRATOR_CONTROLLER_H_
  6. #include <map>
  7. #include "ash/ash_export.h"
  8. #include "ash/display/window_tree_host_manager.h"
  9. #include "base/gtest_prod_util.h"
  10. #include "base/time/time.h"
  11. #include "ui/display/display.h"
  12. #include "ui/display/manager/managed_display_info.h"
  13. #include "ui/events/devices/touchscreen_device.h"
  14. #include "ui/events/event_handler.h"
  15. #include "ui/gfx/geometry/transform.h"
  16. #include "ui/views/widget/unique_widget_ptr.h"
  17. namespace ui {
  18. class KeyEvent;
  19. class TouchEvent;
  20. } // namespace ui
  21. namespace ash {
  22. class TouchCalibratorView;
  23. // TouchCalibratorController is responsible for managing the touch calibration
  24. // process. In case of native touch calibration it is also responsible for
  25. // collecting the touch calibration associated data from the user. It
  26. // instantiates TouchCalibratorView classes to present the native UX interface
  27. // the user can interact with for calibration.
  28. // This controller ensures that only one instance of calibration is running at
  29. // any given time.
  30. class ASH_EXPORT TouchCalibratorController
  31. : public ui::EventHandler,
  32. public WindowTreeHostManager::Observer {
  33. public:
  34. using CalibrationPointPairQuad =
  35. display::TouchCalibrationData::CalibrationPointPairQuad;
  36. using TouchCalibrationCallback = base::OnceCallback<void(bool)>;
  37. static const base::TimeDelta kTouchIntervalThreshold;
  38. TouchCalibratorController();
  39. TouchCalibratorController(const TouchCalibratorController&) = delete;
  40. TouchCalibratorController& operator=(const TouchCalibratorController&) =
  41. delete;
  42. ~TouchCalibratorController() override;
  43. // ui::EventHandler
  44. void OnKeyEvent(ui::KeyEvent* event) override;
  45. void OnTouchEvent(ui::TouchEvent* event) override;
  46. // WindowTreeHostManager::Observer
  47. void OnDisplayConfigurationChanged() override;
  48. // Starts the calibration process for the given |target_display|.
  49. // |opt_callback| is an optional callback that if provided is executed
  50. // with the success or failure of the calibration as a boolean argument.
  51. void StartCalibration(const display::Display& target_display,
  52. bool is_custom_calibration,
  53. TouchCalibrationCallback opt_callback);
  54. // Stops any ongoing calibration process. This is a hard stop which does not
  55. // save any calibration data. Call CompleteCalibration() if you wish to save
  56. // calibration data.
  57. void StopCalibrationAndResetParams();
  58. // Completes the touch calibration by storing the calibration data for the
  59. // display.
  60. void CompleteCalibration(const CalibrationPointPairQuad& pairs,
  61. const gfx::Size& display_size);
  62. // Returns true if any type of touch calibration is active.
  63. bool IsCalibrating() const;
  64. private:
  65. friend class TouchCalibratorControllerTest;
  66. FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, TouchThreshold);
  67. FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest, CustomCalibration);
  68. FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest,
  69. CustomCalibrationInvalidTouchId);
  70. FRIEND_TEST_ALL_PREFIXES(TouchCalibratorControllerTest,
  71. InternalTouchDeviceIsRejected);
  72. enum class CalibrationState {
  73. // Indicates that the touch calibration is currently active with the built
  74. // in native UX.
  75. kNativeCalibration = 0,
  76. // Indicates that the touch calibration is currently active with a custom
  77. // UX via the extensions API.
  78. kCustomCalibration,
  79. // Indicates that touch calibration is currently inactive.
  80. kInactive
  81. };
  82. CalibrationState state_ = CalibrationState::kInactive;
  83. // A map for TouchCalibrator view with the key as display id of the display
  84. // it is present in.
  85. std::map<int64_t, views::UniqueWidgetPtr> touch_calibrator_widgets_;
  86. // The display which is being calibrated by the touch calibrator controller.
  87. // This is valid only if |is_calibrating| is set to true.
  88. display::Display target_display_;
  89. // During calibration this stores the timestamp when the previous touch event
  90. // was received.
  91. base::Time last_touch_timestamp_;
  92. // This is populated during calibration, based on the source id of the device
  93. // the events are originating from.
  94. int touch_device_id_ = ui::InputDevice::kInvalidId;
  95. // A set of ids that belong to touch devices associated with the internal
  96. // display and are of type |ui::InputDeviceType::INPUT_DEVICE_INTERNAL|. This
  97. // is only valid when |state_| is not |kInactive|.
  98. std::set<int> internal_touch_device_ids_;
  99. // An array of Calibration point pairs. This stores all the 4 display and
  100. // touch input point pairs that will be used for calibration.
  101. CalibrationPointPairQuad touch_point_quad_;
  102. // A callback to be called when touch calibration completes.
  103. TouchCalibrationCallback opt_callback_;
  104. // The touch device under calibration may be re-associated to another display
  105. // during calibration. In such a case, the events originating from the touch
  106. // device are tranformed based on parameters of the previous display it was
  107. // linked to. We need to undo these transformations before recording the event
  108. // locations.
  109. gfx::Transform event_transformer_;
  110. };
  111. } // namespace ash
  112. #endif // ASH_DISPLAY_TOUCH_CALIBRATOR_CONTROLLER_H_