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