pointer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2015 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 COMPONENTS_EXO_POINTER_H_
  5. #define COMPONENTS_EXO_POINTER_H_
  6. #include <memory>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/unguessable_token.h"
  9. #include "components/exo/surface_observer.h"
  10. #include "components/exo/surface_tree_host.h"
  11. #include "components/exo/wm_helper.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "third_party/skia/include/core/SkBitmap.h"
  14. #include "ui/aura/client/cursor_client_observer.h"
  15. #include "ui/aura/client/drag_drop_client_observer.h"
  16. #include "ui/aura/client/focus_change_observer.h"
  17. #include "ui/base/cursor/cursor.h"
  18. #include "ui/base/cursor/mojom/cursor_type.mojom-forward.h"
  19. #include "ui/events/event_handler.h"
  20. #include "ui/events/types/event_type.h"
  21. #include "ui/gfx/geometry/point.h"
  22. #include "ui/gfx/geometry/point_f.h"
  23. #include "ui/gfx/native_widget_types.h"
  24. namespace viz {
  25. class CopyOutputResult;
  26. }
  27. namespace ui {
  28. class LocatedEvent;
  29. class MouseEvent;
  30. } // namespace ui
  31. namespace exo {
  32. class PointerConstraintDelegate;
  33. class PointerDelegate;
  34. class PointerGesturePinchDelegate;
  35. class PointerStylusDelegate;
  36. class RelativePointerDelegate;
  37. class Seat;
  38. class Surface;
  39. class SurfaceTreeHost;
  40. // This class implements a client pointer that represents one or more input
  41. // devices, such as mice, which control the pointer location and pointer focus.
  42. class Pointer : public SurfaceTreeHost,
  43. public SurfaceObserver,
  44. public ui::EventHandler,
  45. public aura::client::DragDropClientObserver,
  46. public aura::client::CursorClientObserver,
  47. public aura::client::FocusChangeObserver {
  48. public:
  49. Pointer(PointerDelegate* delegate, Seat* seat);
  50. Pointer(const Pointer&) = delete;
  51. Pointer& operator=(const Pointer&) = delete;
  52. ~Pointer() override;
  53. PointerDelegate* delegate() const { return delegate_; }
  54. // Set the pointer surface, i.e., the surface that contains the pointer image
  55. // (cursor). The |hotspot| argument defines the position of the pointer
  56. // surface relative to the pointer location. Its top-left corner is always at
  57. // (x, y) - (hotspot.x, hotspot.y), where (x, y) are the coordinates of the
  58. // pointer location, in surface local coordinates.
  59. void SetCursor(Surface* surface, const gfx::Point& hotspot);
  60. // Set the pointer cursor type. This is similar to SetCursor, but this method
  61. // accepts ui::mojom::CursorType instead of the surface for the pointer image.
  62. void SetCursorType(ui::mojom::CursorType cursor_type);
  63. // Set delegate for pinch events.
  64. void SetGesturePinchDelegate(PointerGesturePinchDelegate* delegate);
  65. // Overridden from SurfaceDelegate:
  66. void OnSurfaceCommit() override;
  67. // Overridden from SurfaceObserver:
  68. void OnSurfaceDestroying(Surface* surface) override;
  69. // Overridden from ui::EventHandler:
  70. void OnMouseEvent(ui::MouseEvent* event) override;
  71. void OnScrollEvent(ui::ScrollEvent* event) override;
  72. void OnGestureEvent(ui::GestureEvent* event) override;
  73. // aura::client::DragDropClientObserver:
  74. void OnDragStarted() override;
  75. void OnDragCompleted(const ui::DropTargetEvent& event) override;
  76. // Overridden from aura::client::CursorClientObserver:
  77. void OnCursorSizeChanged(ui::CursorSize cursor_size) override;
  78. void OnCursorDisplayChanged(const display::Display& display) override;
  79. // Overridden from aura::client::FocusChangeObserver;
  80. void OnWindowFocused(aura::Window* gained_focus,
  81. aura::Window* lost_focus) override;
  82. // Relative motion registration.
  83. void RegisterRelativePointerDelegate(RelativePointerDelegate* delegate);
  84. void UnregisterRelativePointerDelegate(RelativePointerDelegate* delegate);
  85. // Enable the pointer constraint on the given surface. Returns true if the
  86. // lock was granted, false otherwise.
  87. //
  88. // The delegate must call OnPointerConstraintDelegateDestroying() upon/before
  89. // being destroyed, regardless of the return value of ConstrainPointer(),
  90. // unless PointerConstraintDelegate::OnDefunct() is called first.
  91. //
  92. // TODO(crbug.com/957455): For legacy reasons, locking the pointer will also
  93. // hide the cursor.
  94. bool ConstrainPointer(PointerConstraintDelegate* delegate);
  95. // Notifies that |delegate| is being destroyed.
  96. void OnPointerConstraintDelegateDestroying(
  97. PointerConstraintDelegate* delegate);
  98. // Disable the pointer constraint, notify the delegate, and do not permit
  99. // the constraint to be re-established until the user acts on the surface
  100. // (by clicking on it).
  101. //
  102. // Designed to be called by client code, on behalf of a user action to break
  103. // the constraint.
  104. //
  105. // Returns true if an active pointer constraint was disabled.
  106. bool UnconstrainPointerByUserAction();
  107. // Set the stylus delegate for handling stylus events.
  108. void SetStylusDelegate(PointerStylusDelegate* delegate);
  109. bool HasStylusDelegate() const;
  110. private:
  111. // Remove |delegate| from |constraints_|.
  112. void RemoveConstraintDelegate(PointerConstraintDelegate* delegate);
  113. // Disable the pointer constraint and notify the delegate.
  114. void UnconstrainPointer();
  115. // Try to reactivate a pointer constraint previously requested for the given
  116. // surface, if any.
  117. void MaybeReactivatePointerConstraint(Surface* surface);
  118. // Capture the pointer for the given surface. Returns true iff the capture
  119. // succeeded.
  120. bool EnablePointerCapture(Surface* capture_surface);
  121. // Remove the currently active pointer capture (if there is one).
  122. void DisablePointerCapture();
  123. // Returns the effective target for |event| and the event's location converted
  124. // to the target's coordinates.
  125. Surface* GetEffectiveTargetForEvent(const ui::LocatedEvent* event,
  126. gfx::PointF* location_in_target) const;
  127. // Change pointer focus to |surface|.
  128. void SetFocus(Surface* surface,
  129. const gfx::PointF& location,
  130. int button_flags);
  131. // Updates the root_surface in |SurfaceTreeHost| from which the cursor
  132. // is captured.
  133. void UpdatePointerSurface(Surface* surface);
  134. // Asynchronously update the cursor by capturing a snapshot of
  135. // |SurfaceTreeHost::root_surface()|.
  136. void CaptureCursor(const gfx::Point& hotspot);
  137. // Called when cursor snapshot has been captured.
  138. void OnCursorCaptured(const gfx::Point& hotspot,
  139. std::unique_ptr<viz::CopyOutputResult> result);
  140. // Update |cursor_| to |cursor_bitmap_| transformed for the current display.
  141. void UpdateCursor();
  142. // Convert the given |location_in_target| to coordinates in the root window.
  143. gfx::PointF GetLocationInRoot(Surface* target,
  144. gfx::PointF location_in_target);
  145. // Called to check if cursor should be moved to the center of the window when
  146. // sending relative movements.
  147. bool ShouldMoveToCenter();
  148. // Moves the cursor to center of the active display.
  149. void MoveCursorToCenterOfActiveDisplay();
  150. // Process the delta for relative pointer motion. Returns true if relative
  151. // motion was sent to the delegate, false otherwise. If |ordinal_motion| is
  152. // supplied, it will be used for determining physical motion, otherwise
  153. // physical motion will be the relative delta.
  154. bool HandleRelativePointerMotion(
  155. base::TimeTicks time_stamp,
  156. gfx::PointF location_in_target,
  157. const absl::optional<gfx::Vector2dF>& ordinal_motion);
  158. // Whether this Pointer should observe the given |surface|.
  159. bool ShouldObserveSurface(Surface* surface);
  160. // Stop observing |surface| if it's no longer relevant.
  161. void MaybeRemoveSurfaceObserver(Surface* surface);
  162. // The delegate instance that all events are dispatched to.
  163. PointerDelegate* const delegate_;
  164. Seat* const seat_;
  165. // The delegate instance that all pinch related events are dispatched to.
  166. PointerGesturePinchDelegate* pinch_delegate_ = nullptr;
  167. // The delegate instance that relative movement events are dispatched to.
  168. RelativePointerDelegate* relative_pointer_delegate_ = nullptr;
  169. // Delegate that owns the currently granted pointer lock, if any.
  170. PointerConstraintDelegate* pointer_constraint_delegate_ = nullptr;
  171. // All delegates currently requesting a pointer locks, whether granted or
  172. // not. Only one such request may exist per surface; others will be denied.
  173. base::flat_map<Surface*, PointerConstraintDelegate*> constraints_;
  174. // The delegate instance that stylus/pen events are dispatched to.
  175. PointerStylusDelegate* stylus_delegate_ = nullptr;
  176. // The current focus surface for the pointer.
  177. Surface* focus_surface_ = nullptr;
  178. // The location of the pointer in the current focus surface.
  179. gfx::PointF location_;
  180. // The location of the pointer when pointer capture is first enabled.
  181. absl::optional<gfx::Point> location_when_pointer_capture_enabled_;
  182. // If this is not nullptr, a synthetic move was sent and this points to the
  183. // location of a generated move that was sent which should not be forwarded.
  184. absl::optional<gfx::Point> location_synthetic_move_;
  185. // The window with pointer capture. Pointer capture is enabled if and only if
  186. // this is not null.
  187. aura::Window* capture_window_ = nullptr;
  188. // True if this pointer is permitted to be captured.
  189. //
  190. // Set false when a user action (except focus loss) breaks pointer capture.
  191. // Set true when the user clicks in any Exo window.
  192. bool capture_permitted_ = true;
  193. // The position of the pointer surface relative to the pointer location.
  194. gfx::Point hotspot_;
  195. // Latest cursor snapshot.
  196. SkBitmap cursor_bitmap_;
  197. // The current cursor.
  198. ui::Cursor cursor_;
  199. // Hotspot to use with latest cursor snapshot.
  200. gfx::Point cursor_hotspot_;
  201. // Scale at which cursor snapshot is captured.
  202. float capture_scale_;
  203. // Source used for cursor capture copy output requests.
  204. const base::UnguessableToken cursor_capture_source_id_;
  205. // Last received event type.
  206. ui::EventType last_event_type_ = ui::ET_UNKNOWN;
  207. // Last reported stylus values.
  208. ui::EventPointerType last_pointer_type_ = ui::EventPointerType::kUnknown;
  209. float last_force_ = std::numeric_limits<float>::quiet_NaN();
  210. gfx::Vector2dF last_tilt_;
  211. // Weak pointer factory used for cursor capture callbacks.
  212. base::WeakPtrFactory<Pointer> cursor_capture_weak_ptr_factory_{this};
  213. };
  214. } // namespace exo
  215. #endif // COMPONENTS_EXO_POINTER_H_