event_utils.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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_EVENTS_EVENT_UTILS_H_
6#define UI_EVENTS_EVENT_UTILS_H_
7
8#include "base/event_types.h"
9#include "base/memory/scoped_ptr.h"
10#include "ui/events/event_constants.h"
11#include "ui/events/keycodes/keyboard_codes.h"
12#include "ui/gfx/display.h"
13#include "ui/gfx/native_widget_types.h"
14#include "ui/events/events_export.h"
15
16#if defined(OS_WIN)
17#include <windows.h>
18#endif
19
20namespace gfx {
21class Point;
22class Vector2d;
23}
24
25namespace base {
26class TimeDelta;
27}
28
29namespace ui {
30
31class Event;
32class MouseEvent;
33
34// Updates the list of devices for cached properties.
35EVENTS_EXPORT void UpdateDeviceList();
36
37// Returns a ui::Event wrapping a native event. Ownership of the returned value
38// is transferred to the caller.
39EVENTS_EXPORT scoped_ptr<Event> EventFromNative(
40    const base::NativeEvent& native_event);
41
42// Get the EventType from a native event.
43EVENTS_EXPORT EventType EventTypeFromNative(
44    const base::NativeEvent& native_event);
45
46// Get the EventFlags from a native event.
47EVENTS_EXPORT int EventFlagsFromNative(const base::NativeEvent& native_event);
48
49// Get the timestamp from a native event.
50EVENTS_EXPORT base::TimeDelta EventTimeFromNative(
51    const base::NativeEvent& native_event);
52
53// Create a timestamp based on the current time.
54EVENTS_EXPORT base::TimeDelta EventTimeForNow();
55
56// Get the location from a native event.  The coordinate system of the resultant
57// |Point| has the origin at top-left of the "root window".  The nature of
58// this "root window" and how it maps to platform-specific drawing surfaces is
59// defined in ui/aura/root_window.* and ui/aura/window_tree_host*.
60// TODO(tdresser): Return gfx::PointF here. See crbug.com/337827.
61EVENTS_EXPORT gfx::Point EventLocationFromNative(
62    const base::NativeEvent& native_event);
63
64// Gets the location in native system coordinate space.
65EVENTS_EXPORT gfx::Point EventSystemLocationFromNative(
66    const base::NativeEvent& native_event);
67
68#if defined(USE_X11)
69// Returns the 'real' button for an event. The button reported in slave events
70// does not take into account any remapping (e.g. using xmodmap), while the
71// button reported in master events do. This is a utility function to always
72// return the mapped button.
73EVENTS_EXPORT int EventButtonFromNative(const base::NativeEvent& native_event);
74#endif
75
76// Returns the KeyboardCode from a native event.
77EVENTS_EXPORT KeyboardCode KeyboardCodeFromNative(
78    const base::NativeEvent& native_event);
79
80// Returns the DOM KeyboardEvent code (physical location in the
81// keyboard) from a native event.  The ownership of the return value
82// is NOT trasferred to the caller.
83EVENTS_EXPORT const char* CodeFromNative(
84    const base::NativeEvent& native_event);
85
86// Returns the platform related key code. For X11, it is xksym value.
87EVENTS_EXPORT uint32 PlatformKeycodeFromNative(
88    const base::NativeEvent& native_event);
89
90// Returns the windows key code. Note that this value is different from
91// KeyboardCodeFromNative (there is a divergence between ui::KeyboardCode and
92// windows key codes).
93EVENTS_EXPORT uint32 WindowsKeycodeFromNative(
94    const base::NativeEvent& native_event);
95
96// Returns the text generated by this keystroke. Used in the value we pass to
97// blink.
98EVENTS_EXPORT uint16 TextFromNative(const base::NativeEvent& native_event);
99
100// Returns the unmodified text generated by this keystroke. Used in the value
101// we pass to blink.
102EVENTS_EXPORT uint16 UnmodifiedTextFromNative(
103    const base::NativeEvent& native_event);
104
105#if defined(OS_LINUX)
106// Returns a control character sequences from a |windows_key_code|.
107EVENTS_EXPORT uint16 GetControlCharacterForKeycode(int windows_key_code,
108                                                   bool shift);
109#endif
110
111// Returns true if the keyboard event is a character event rather than
112// a keystroke event.
113EVENTS_EXPORT bool IsCharFromNative(const base::NativeEvent& native_event);
114
115// Returns the flags of the button that changed during a press/release.
116EVENTS_EXPORT int GetChangedMouseButtonFlagsFromNative(
117    const base::NativeEvent& native_event);
118
119// Gets the mouse wheel offsets from a native event.
120EVENTS_EXPORT gfx::Vector2d GetMouseWheelOffset(
121    const base::NativeEvent& native_event);
122
123// Returns a copy of |native_event|. Depending on the platform, this copy may
124// need to be deleted with ReleaseCopiedNativeEvent().
125base::NativeEvent CopyNativeEvent(
126    const base::NativeEvent& native_event);
127
128// Delete a |native_event| previously created by CopyNativeEvent().
129void ReleaseCopiedNativeEvent(
130    const base::NativeEvent& native_event);
131
132// Gets the touch id from a native event.
133EVENTS_EXPORT int GetTouchId(const base::NativeEvent& native_event);
134
135// Increases the number of times |ClearTouchIdIfReleased| needs to be called on
136// an event with a given touch id before it will actually be cleared.
137EVENTS_EXPORT void IncrementTouchIdRefCount(
138    const base::NativeEvent& native_event);
139
140// Clear the touch id from bookkeeping if it is a release/cancel event.
141EVENTS_EXPORT void ClearTouchIdIfReleased(
142    const base::NativeEvent& native_event);
143
144// Gets the radius along the X/Y axis from a native event. Default is 1.0.
145EVENTS_EXPORT float GetTouchRadiusX(const base::NativeEvent& native_event);
146EVENTS_EXPORT float GetTouchRadiusY(const base::NativeEvent& native_event);
147
148// Gets the angle of the major axis away from the X axis. Default is 0.0.
149EVENTS_EXPORT float GetTouchAngle(const base::NativeEvent& native_event);
150
151// Gets the force from a native_event. Normalized to be [0, 1]. Default is 0.0.
152EVENTS_EXPORT float GetTouchForce(const base::NativeEvent& native_event);
153
154// Gets the fling velocity from a native event. is_cancel is set to true if
155// this was a tap down, intended to stop an ongoing fling.
156EVENTS_EXPORT bool GetFlingData(const base::NativeEvent& native_event,
157                                float* vx,
158                                float* vy,
159                                float* vx_ordinal,
160                                float* vy_ordinal,
161                                bool* is_cancel);
162
163// Returns whether this is a scroll event and optionally gets the amount to be
164// scrolled. |x_offset|, |y_offset| and |finger_count| can be NULL.
165EVENTS_EXPORT bool GetScrollOffsets(const base::NativeEvent& native_event,
166                                    float* x_offset,
167                                    float* y_offset,
168                                    float* x_offset_ordinal,
169                                    float* y_offset_ordinal,
170                                    int* finger_count);
171
172// Returns whether natural scrolling should be used for touchpad.
173EVENTS_EXPORT bool ShouldDefaultToNaturalScroll();
174
175// Returns whether or not the internal display produces touch events.
176EVENTS_EXPORT gfx::Display::TouchSupport GetInternalDisplayTouchSupport();
177
178#if defined(OS_WIN)
179EVENTS_EXPORT int GetModifiersFromACCEL(const ACCEL& accel);
180EVENTS_EXPORT int GetModifiersFromKeyState();
181
182// Returns true if |message| identifies a mouse event that was generated as the
183// result of a touch event.
184EVENTS_EXPORT bool IsMouseEventFromTouch(UINT message);
185
186// Converts scan code and lParam each other.  The scan code
187// representing an extended key contains 0xE000 bits.
188EVENTS_EXPORT uint16 GetScanCodeFromLParam(LPARAM lParam);
189EVENTS_EXPORT LPARAM GetLParamFromScanCode(uint16 scan_code);
190
191#endif
192
193#if defined(USE_X11)
194// Update the native X11 event to correspond to the new flags.
195EVENTS_EXPORT void UpdateX11EventForFlags(Event* event);
196// Update the native X11 event to correspond to the new button flags.
197EVENTS_EXPORT void UpdateX11EventForChangedButtonFlags(MouseEvent* event);
198#endif
199
200// Registers a custom event type.
201EVENTS_EXPORT int RegisterCustomEventType();
202
203}  // namespace ui
204
205#endif  // UI_EVENTS_EVENT_UTILS_H_
206