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 5 #ifndef UI_BASE_EVENTS_EVENT_UTILS_H_ 6 #define UI_BASE_EVENTS_EVENT_UTILS_H_ 7 8 #include "base/event_types.h" 9 #include "ui/base/events/event_constants.h" 10 #include "ui/base/keycodes/keyboard_codes.h" 11 #include "ui/gfx/native_widget_types.h" 12 13 #if defined(OS_WIN) 14 #include <windows.h> 15 #endif 16 17 namespace gfx { 18 class Point; 19 class Vector2d; 20 } 21 22 namespace base { 23 class TimeDelta; 24 } 25 26 namespace ui { 27 28 class Event; 29 30 // Updates the list of devices for cached properties. 31 UI_EXPORT void UpdateDeviceList(); 32 33 // Get the EventType from a native event. 34 UI_EXPORT EventType EventTypeFromNative(const base::NativeEvent& native_event); 35 36 // Get the EventFlags from a native event. 37 UI_EXPORT int EventFlagsFromNative(const base::NativeEvent& native_event); 38 39 // Get the timestamp from a native event. 40 UI_EXPORT base::TimeDelta EventTimeFromNative( 41 const base::NativeEvent& native_event); 42 43 // Create a timestamp based on the current time. 44 UI_EXPORT base::TimeDelta EventTimeForNow(); 45 46 // Get the location from a native event. The coordinate system of the resultant 47 // |Point| has the origin at top-left of the "root window". The nature of 48 // this "root window" and how it maps to platform-specific drawing surfaces is 49 // defined in ui/aura/root_window.* and ui/aura/root_window_host*. 50 UI_EXPORT gfx::Point EventLocationFromNative( 51 const base::NativeEvent& native_event); 52 53 // Gets the location in native system coordinate space. 54 UI_EXPORT gfx::Point EventSystemLocationFromNative( 55 const base::NativeEvent& native_event); 56 57 #if defined(USE_X11) 58 // Returns the 'real' button for an event. The button reported in slave events 59 // does not take into account any remapping (e.g. using xmodmap), while the 60 // button reported in master events do. This is a utility function to always 61 // return the mapped button. 62 UI_EXPORT int EventButtonFromNative(const base::NativeEvent& native_event); 63 #endif 64 65 // Returns the KeyboardCode from a native event. 66 UI_EXPORT KeyboardCode KeyboardCodeFromNative( 67 const base::NativeEvent& native_event); 68 69 // Returns true if the message is a mouse event. 70 UI_EXPORT bool IsMouseEvent(const base::NativeEvent& native_event); 71 72 // Returns the flags of the button that changed during a press/release. 73 UI_EXPORT int GetChangedMouseButtonFlagsFromNative( 74 const base::NativeEvent& native_event); 75 76 // Gets the mouse wheel offsets from a native event. 77 UI_EXPORT gfx::Vector2d GetMouseWheelOffset( 78 const base::NativeEvent& native_event); 79 80 // Gets the touch id from a native event. 81 UI_EXPORT int GetTouchId(const base::NativeEvent& native_event); 82 83 // Clear the touch id from bookkeeping if it is a release/cancel event. 84 UI_EXPORT void ClearTouchIdIfReleased(const base::NativeEvent& native_event); 85 86 // Gets the radius along the X/Y axis from a native event. Default is 1.0. 87 UI_EXPORT float GetTouchRadiusX(const base::NativeEvent& native_event); 88 UI_EXPORT float GetTouchRadiusY(const base::NativeEvent& native_event); 89 90 // Gets the angle of the major axis away from the X axis. Default is 0.0. 91 UI_EXPORT float GetTouchAngle(const base::NativeEvent& native_event); 92 93 // Gets the force from a native_event. Normalized to be [0, 1]. Default is 0.0. 94 UI_EXPORT float GetTouchForce(const base::NativeEvent& native_event); 95 96 // Gets the fling velocity from a native event. is_cancel is set to true if 97 // this was a tap down, intended to stop an ongoing fling. 98 UI_EXPORT bool GetFlingData(const base::NativeEvent& native_event, 99 float* vx, 100 float* vy, 101 float* vx_ordinal, 102 float* vy_ordinal, 103 bool* is_cancel); 104 105 // Returns whether this is a scroll event and optionally gets the amount to be 106 // scrolled. |x_offset|, |y_offset| and |finger_count| can be NULL. 107 UI_EXPORT bool GetScrollOffsets(const base::NativeEvent& native_event, 108 float* x_offset, 109 float* y_offset, 110 float* x_offset_ordinal, 111 float* y_offset_ordinal, 112 int* finger_count); 113 114 UI_EXPORT bool GetGestureTimes(const base::NativeEvent& native_event, 115 double* start_time, 116 double* end_time); 117 118 // Enable/disable natural scrolling for touchpads. 119 UI_EXPORT void SetNaturalScroll(bool enabled); 120 121 // In natural scrolling enabled for touchpads? 122 UI_EXPORT bool IsNaturalScrollEnabled(); 123 124 // Was this event generated by a touchpad device? 125 // The caller is responsible for ensuring that this is a mouse/touchpad event 126 // before calling this function. 127 UI_EXPORT bool IsTouchpadEvent(const base::NativeEvent& event); 128 129 // Returns true if event is noop. 130 UI_EXPORT bool IsNoopEvent(const base::NativeEvent& event); 131 132 // Creates and returns no-op event. 133 UI_EXPORT base::NativeEvent CreateNoopEvent(); 134 135 #if defined(OS_WIN) 136 UI_EXPORT int GetModifiersFromACCEL(const ACCEL& accel); 137 UI_EXPORT int GetModifiersFromKeyState(); 138 139 // Returns true if |message| identifies a mouse event that was generated as the 140 // result of a touch event. 141 UI_EXPORT bool IsMouseEventFromTouch(UINT message); 142 #endif 143 144 // Returns true if default post-target handling was canceled for |event| after 145 // its dispatch to its target. 146 UI_EXPORT bool EventCanceledDefaultHandling(const Event& event); 147 148 // Registers a custom event type. 149 UI_EXPORT int RegisterCustomEventType(); 150 151 } // namespace ui 152 153 #endif // UI_BASE_EVENTS_EVENT_UTILS_H_ 154