event_utils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (c) 2012 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 UI_EVENTS_EVENT_UTILS_H_
  5. #define UI_EVENTS_EVENT_UTILS_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/strings/string_piece.h"
  10. #include "build/build_config.h"
  11. #include "ui/display/display.h"
  12. #include "ui/events/base_event_utils.h"
  13. #include "ui/events/event.h"
  14. #include "ui/events/event_constants.h"
  15. #include "ui/events/events_export.h"
  16. #include "ui/events/keycodes/keyboard_codes.h"
  17. #include "ui/events/platform_event.h"
  18. #include "ui/events/types/event_type.h"
  19. #include "ui/gfx/native_widget_types.h"
  20. #if BUILDFLAG(IS_WIN)
  21. #include "base/win/windows_types.h"
  22. #endif
  23. namespace gfx {
  24. class Point;
  25. class Vector2d;
  26. } // namespace gfx
  27. namespace base {
  28. class TimeTicks;
  29. }
  30. // Common functions to be used for all platforms except Android.
  31. namespace ui {
  32. class Event;
  33. class MouseEvent;
  34. enum class DomCode;
  35. // Key used to store keyboard 'state' values in Event::Properties.
  36. constexpr char kPropertyKeyboardState[] = "_keyevent_kbd_state_";
  37. // Key used to store keyboard 'group' values in Event::Properties.
  38. constexpr char kPropertyKeyboardGroup[] = "_keyevent_kbd_group_";
  39. // Key used to store 'hardware key code' values in Event::Properties.
  40. constexpr char kPropertyKeyboardHwKeyCode[] = "_keyevent_kbd_hw_keycode_";
  41. // Event::Properties constants for IBus-GTK and fcitx-GTK.
  42. // Both of them in async mode use gtk-specific XKeyEvent::state bits 24 and 25.
  43. // 24 is handled and 25 is ignored.
  44. // Note that they use more bits, but Chrome does not handle it now.
  45. // cf)
  46. // https://github.com/ibus/ibus/blob/dd4cc5b028c35f9bb8fa9d3bdc8f26bcdfc43d40/src/ibustypes.h#L88
  47. // https://github.com/fcitx/fcitx/blob/289b2f674d95651d4e0d0c77a48e3a2f0da40efe/src/lib/fcitx-utils/keysym.h#L47
  48. // https://mail.gnome.org/archives/gtk-devel-list/2013-June/msg00003.html
  49. constexpr char kPropertyKeyboardImeFlag[] = "_keyevent_kbd_ime_flags_";
  50. constexpr unsigned int kPropertyKeyboardImeFlagOffset = 24;
  51. constexpr unsigned int kPropertyKeyboardImeFlagMask = 0x03;
  52. // Ignored is the 25-th bit.
  53. constexpr unsigned int kPropertyKeyboardImeIgnoredFlag =
  54. 1 << (25 - kPropertyKeyboardImeFlagOffset);
  55. // Key used to store mouse event flag telling ET_MOUSE_EXITED must actually be
  56. // interpreted as "crossing intermediate window" in blink context.
  57. constexpr char kPropertyMouseCrossedIntermediateWindow[] =
  58. "_mouseevent_cros_window_";
  59. // Returns a ui::Event wrapping a native event. Ownership of the returned value
  60. // is transferred to the caller.
  61. EVENTS_EXPORT std::unique_ptr<Event> EventFromNative(
  62. const PlatformEvent& native_event);
  63. // Get the EventType from a native event.
  64. EVENTS_EXPORT EventType EventTypeFromNative(const PlatformEvent& native_event);
  65. // Get the EventFlags from a native event.
  66. EVENTS_EXPORT int EventFlagsFromNative(const PlatformEvent& native_event);
  67. // Get the timestamp from a native event.
  68. // Note: This is not a pure function meaning that multiple applications on the
  69. // same native event may return different values.
  70. EVENTS_EXPORT base::TimeTicks EventTimeFromNative(
  71. const PlatformEvent& native_event);
  72. // Get the timestamp to use for latency metrics from |native_event|.
  73. // |current_time| is a timestamp returned by EventTimeForNow which will be
  74. // compared to the |native_event| timestamp to calculate latency. This is
  75. // different from EventTimeFromNative because on some platforms (eg. Windows)
  76. // EventTimeFromNative returns a synthesized timestamp.
  77. EVENTS_EXPORT base::TimeTicks EventLatencyTimeFromNative(
  78. const PlatformEvent& native_event,
  79. base::TimeTicks current_time);
  80. // Get the location from a native event. The coordinate system of the resultant
  81. // |Point| has the origin at top-left of the "root window". The nature of
  82. // this "root window" and how it maps to platform-specific drawing surfaces is
  83. // defined in ui/aura/root_window.* and ui/aura/window_tree_host*.
  84. EVENTS_EXPORT gfx::PointF EventLocationFromNative(
  85. const PlatformEvent& native_event);
  86. // Gets the location in native system coordinate space.
  87. EVENTS_EXPORT gfx::Point EventSystemLocationFromNative(
  88. const PlatformEvent& native_event);
  89. // Returns the KeyboardCode from a native event.
  90. EVENTS_EXPORT KeyboardCode
  91. KeyboardCodeFromNative(const PlatformEvent& native_event);
  92. // Returns the DOM KeyboardEvent code (physical location in the
  93. // keyboard) from a native event.
  94. EVENTS_EXPORT DomCode CodeFromNative(const PlatformEvent& native_event);
  95. // Returns true if the keyboard event is a character event rather than
  96. // a keystroke event.
  97. EVENTS_EXPORT bool IsCharFromNative(const PlatformEvent& native_event);
  98. // Returns the flags of the button that changed during a press/release.
  99. EVENTS_EXPORT int GetChangedMouseButtonFlagsFromNative(
  100. const PlatformEvent& native_event);
  101. // Returns the detailed pointer information for mouse events.
  102. EVENTS_EXPORT PointerDetails
  103. GetMousePointerDetailsFromNative(const PlatformEvent& native_event);
  104. // Returns the movement vector associated with this mouse movement event.
  105. EVENTS_EXPORT const gfx::Vector2dF& GetMouseMovementFromNative(
  106. const PlatformEvent& native_event);
  107. // Gets the mouse wheel offsets from a native event.
  108. EVENTS_EXPORT gfx::Vector2d GetMouseWheelOffset(
  109. const PlatformEvent& native_event);
  110. // Gets the mouse wheel tick counts from a native event, with a value of 120
  111. // representing a whole tick.
  112. EVENTS_EXPORT gfx::Vector2d GetMouseWheelTick120ths(
  113. const PlatformEvent& native_event);
  114. // Returns a copy of |native_event|. Depending on the platform, this copy may
  115. // need to be deleted with ReleaseCopiedNativeEvent().
  116. PlatformEvent CopyNativeEvent(const PlatformEvent& native_event);
  117. // Delete a |native_event| previously created by CopyNativeEvent().
  118. void ReleaseCopiedNativeEvent(const PlatformEvent& native_event);
  119. // Returns the detailed pointer information for touch events.
  120. EVENTS_EXPORT PointerDetails
  121. GetTouchPointerDetailsFromNative(const PlatformEvent& native_event);
  122. // Gets the fling velocity from a native event. is_cancel is set to true if
  123. // this was a tap down, intended to stop an ongoing fling.
  124. EVENTS_EXPORT bool GetFlingData(const PlatformEvent& native_event,
  125. float* vx,
  126. float* vy,
  127. float* vx_ordinal,
  128. float* vy_ordinal,
  129. bool* is_cancel);
  130. // Returns whether this is a scroll event and optionally gets the amount to be
  131. // scrolled. |x_offset|, |y_offset| and |finger_count| can be NULL.
  132. EVENTS_EXPORT bool GetScrollOffsets(const PlatformEvent& native_event,
  133. float* x_offset,
  134. float* y_offset,
  135. float* x_offset_ordinal,
  136. float* y_offset_ordinal,
  137. int* finger_count,
  138. EventMomentumPhase* momentum_phase);
  139. // Returns whether natural scrolling should be used for touchpad.
  140. EVENTS_EXPORT bool ShouldDefaultToNaturalScroll();
  141. // Returns whether or not the internal display produces touch events.
  142. EVENTS_EXPORT display::Display::TouchSupport GetInternalDisplayTouchSupport();
  143. EVENTS_EXPORT void ComputeEventLatencyOS(const PlatformEvent& native_event);
  144. EVENTS_EXPORT void ComputeEventLatencyOS(ui::EventType type,
  145. base::TimeTicks time_stamp,
  146. base::TimeTicks current_time);
  147. #if BUILDFLAG(IS_WIN)
  148. // Like ComputeEventLatencyOS, but for events whose timestamp comes from a
  149. // TOUCHINPUT structure instead of PlatformEvent.
  150. EVENTS_EXPORT void ComputeEventLatencyOSFromTOUCHINPUT(
  151. ui::EventType event_type,
  152. TOUCHINPUT touch_input,
  153. base::TimeTicks current_time);
  154. // Like ComputeEventLatencyOS, but for events whose timestamp comes from a
  155. // POINTER_INFO structure instead of PlatformEvent.
  156. EVENTS_EXPORT void ComputeEventLatencyOSFromPOINTER_INFO(
  157. ui::EventType event_type,
  158. POINTER_INFO pointer_info,
  159. base::TimeTicks current_time);
  160. EVENTS_EXPORT int GetModifiersFromKeyState();
  161. // Returns true if |message| identifies a mouse event that was generated as the
  162. // result of a touch event.
  163. EVENTS_EXPORT bool IsMouseEventFromTouch(UINT message);
  164. // Converts scan code and lParam each other. The scan code
  165. // representing an extended key contains 0xE000 bits.
  166. EVENTS_EXPORT uint16_t GetScanCodeFromLParam(LPARAM lParam);
  167. EVENTS_EXPORT LPARAM GetLParamFromScanCode(uint16_t scan_code);
  168. // Creates a CHROME_MSG from the given KeyEvent if there is no native_event.
  169. EVENTS_EXPORT CHROME_MSG MSGFromKeyEvent(KeyEvent* key_event,
  170. HWND hwnd = nullptr);
  171. EVENTS_EXPORT KeyEvent KeyEventFromMSG(const CHROME_MSG& msg);
  172. EVENTS_EXPORT MouseEvent MouseEventFromMSG(const CHROME_MSG& msg);
  173. EVENTS_EXPORT MouseWheelEvent MouseWheelEventFromMSG(const CHROME_MSG& msg);
  174. #endif // BUILDFLAG(IS_WIN)
  175. // Registers a custom event type.
  176. EVENTS_EXPORT int RegisterCustomEventType();
  177. // Updates the location of |located_event| from |current_window_origin| to be in
  178. // |target_window_origin|'s coordinate system so that it can be dispatched to a
  179. // window based on |target_window_origin|.
  180. EVENTS_EXPORT void ConvertEventLocationToTargetWindowLocation(
  181. const gfx::Point& target_window_origin,
  182. const gfx::Point& current_window_origin,
  183. ui::LocatedEvent* located_event);
  184. // The following utilities are useful for debugging and tracing.
  185. // Returns a string description of an event type.
  186. EVENTS_EXPORT base::StringPiece EventTypeName(EventType type);
  187. // Returns a vector of string representations of EventFlags.
  188. EVENTS_EXPORT std::vector<base::StringPiece> EventFlagsNames(int event_flags);
  189. // Returns a a vector of string representations of KeyEventFlags.
  190. EVENTS_EXPORT std::vector<base::StringPiece> KeyEventFlagsNames(
  191. int event_flags);
  192. // Returns a a vector of string representations of MouseEventFlags.
  193. EVENTS_EXPORT std::vector<base::StringPiece> MouseEventFlagsNames(
  194. int event_flags);
  195. } // namespace ui
  196. #endif // UI_EVENTS_EVENT_UTILS_H_