15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#ifndef UI_EVENTS_EVENT_UTILS_H_ 6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define UI_EVENTS_EVENT_UTILS_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/basictypes.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/event_types.h" 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/strings/string16.h" 12d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event_constants.h" 13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/keycodes/keyboard_codes.h" 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/display.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/native_widget_types.h" 1668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/events/events_export.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h> 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Point; 24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class Vector2d; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TimeDelta; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui { 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Event; 345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class MouseEvent; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Updates the list of devices for cached properties. 3768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT void UpdateDeviceList(); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Returns a ui::Event wrapping a native event. Ownership of the returned value 40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// is transferred to the caller. 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)EVENTS_EXPORT scoped_ptr<Event> EventFromNative( 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::NativeEvent& native_event); 43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Get the EventType from a native event. 4568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT EventType EventTypeFromNative( 4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const base::NativeEvent& native_event); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Get the EventFlags from a native event. 4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT int EventFlagsFromNative(const base::NativeEvent& native_event); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get the timestamp from a native event. 5268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT base::TimeDelta EventTimeFromNative( 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::NativeEvent& native_event); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Create a timestamp based on the current time. 5668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT base::TimeDelta EventTimeForNow(); 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Get the location from a native event. The coordinate system of the resultant 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |Point| has the origin at top-left of the "root window". The nature of 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this "root window" and how it maps to platform-specific drawing surfaces is 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// defined in ui/aura/root_window.* and ui/aura/window_tree_host*. 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// TODO(tdresser): Return gfx::PointF here. See crbug.com/337827. 6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT gfx::Point EventLocationFromNative( 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::NativeEvent& native_event); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the location in native system coordinate space. 6768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT gfx::Point EventSystemLocationFromNative( 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::NativeEvent& native_event); 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(USE_X11) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the 'real' button for an event. The button reported in slave events 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// does not take into account any remapping (e.g. using xmodmap), while the 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// button reported in master events do. This is a utility function to always 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// return the mapped button. 7568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT int EventButtonFromNative(const base::NativeEvent& native_event); 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the KeyboardCode from a native event. 7968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT KeyboardCode KeyboardCodeFromNative( 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::NativeEvent& native_event); 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 82a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Returns the DOM KeyboardEvent code (physical location in the 83a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// keyboard) from a native event. The ownership of the return value 84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// is NOT trasferred to the caller. 85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)EVENTS_EXPORT const char* CodeFromNative( 86a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::NativeEvent& native_event); 87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Returns the platform related key code. For X11, it is xksym value. 89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)EVENTS_EXPORT uint32 PlatformKeycodeFromNative( 90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const base::NativeEvent& native_event); 91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 9203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Returns a control character sequences from a |windows_key_code|. 931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciEVENTS_EXPORT base::char16 GetControlCharacterForKeycode(int windows_key_code, 941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool shift); 9503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Returns true if the keyboard event is a character event rather than 975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// a keystroke event. 985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)EVENTS_EXPORT bool IsCharFromNative(const base::NativeEvent& native_event); 995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the flags of the button that changed during a press/release. 10168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT int GetChangedMouseButtonFlagsFromNative( 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::NativeEvent& native_event); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Gets the mouse wheel offsets from a native event. 10568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT gfx::Vector2d GetMouseWheelOffset( 106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const base::NativeEvent& native_event); 1075c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Returns a copy of |native_event|. Depending on the platform, this copy may 1095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// need to be deleted with ReleaseCopiedNativeEvent(). 1105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liubase::NativeEvent CopyNativeEvent( 1115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu const base::NativeEvent& native_event); 1125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Delete a |native_event| previously created by CopyNativeEvent(). 1145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid ReleaseCopiedNativeEvent( 1155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu const base::NativeEvent& native_event); 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the touch id from a native event. 11868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT int GetTouchId(const base::NativeEvent& native_event); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Increases the number of times |ClearTouchIdIfReleased| needs to be called on 1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// an event with a given touch id before it will actually be cleared. 1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)EVENTS_EXPORT void IncrementTouchIdRefCount( 1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const base::NativeEvent& native_event); 1245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 125a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Clear the touch id from bookkeeping if it is a release/cancel event. 12668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT void ClearTouchIdIfReleased( 12768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const base::NativeEvent& native_event); 128a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the radius along the X/Y axis from a native event. Default is 1.0. 13068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT float GetTouchRadiusX(const base::NativeEvent& native_event); 13168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT float GetTouchRadiusY(const base::NativeEvent& native_event); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the angle of the major axis away from the X axis. Default is 0.0. 13468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT float GetTouchAngle(const base::NativeEvent& native_event); 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the force from a native_event. Normalized to be [0, 1]. Default is 0.0. 13768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT float GetTouchForce(const base::NativeEvent& native_event); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the fling velocity from a native event. is_cancel is set to true if 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this was a tap down, intended to stop an ongoing fling. 14168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT bool GetFlingData(const base::NativeEvent& native_event, 14203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* vx, 14303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* vy, 14403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* vx_ordinal, 14503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* vy_ordinal, 14603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool* is_cancel); 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns whether this is a scroll event and optionally gets the amount to be 1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// scrolled. |x_offset|, |y_offset| and |finger_count| can be NULL. 15068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT bool GetScrollOffsets(const base::NativeEvent& native_event, 15103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* x_offset, 15203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* y_offset, 15303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* x_offset_ordinal, 15403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) float* y_offset_ordinal, 15503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) int* finger_count); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Returns whether natural scrolling should be used for touchpad. 1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)EVENTS_EXPORT bool ShouldDefaultToNaturalScroll(); 1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Returns whether or not the internal display produces touch events. 1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)EVENTS_EXPORT gfx::Display::TouchSupport GetInternalDisplayTouchSupport(); 1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN) 16468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT int GetModifiersFromACCEL(const ACCEL& accel); 16568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT int GetModifiersFromKeyState(); 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |message| identifies a mouse event that was generated as the 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// result of a touch event. 16968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT bool IsMouseEventFromTouch(UINT message); 170a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 171a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Converts scan code and lParam each other. The scan code 172a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// representing an extended key contains 0xE000 bits. 173a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)EVENTS_EXPORT uint16 GetScanCodeFromLParam(LPARAM lParam); 174a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)EVENTS_EXPORT LPARAM GetLParamFromScanCode(uint16 scan_code); 17546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if defined(USE_X11) 1795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Update the native X11 event to correspond to the new flags. 1805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)EVENTS_EXPORT void UpdateX11EventForFlags(Event* event); 1815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Update the native X11 event to correspond to the new button flags. 1825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)EVENTS_EXPORT void UpdateX11EventForChangedButtonFlags(MouseEvent* event); 1835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif 1845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Registers a custom event type. 18668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)EVENTS_EXPORT int RegisterCustomEventType(); 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace ui 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 190d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif // UI_EVENTS_EVENT_UTILS_H_ 191