1 // Copyright 2014 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.
5 #include "ui/events/event_utils.h"
7 #include <Cocoa/Cocoa.h>
9 #include "base/logging.h"
10 #import "base/mac/mac_util.h"
11 #import "base/mac/sdk_forward_declarations.h"
12 #include "base/time/time.h"
13 #include "build/build_config.h"
14 #include "ui/events/cocoa/cocoa_event_utils.h"
15 #include "ui/events/event_utils.h"
16 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
17 #include "ui/gfx/geometry/point.h"
18 #include "ui/gfx/geometry/vector2d.h"
22 void UpdateDeviceList() {
26 EventType EventTypeFromNative(const base::NativeEvent& native_event) {
27 NSEventType type = [native_event type];
30 return ET_KEY_PRESSED;
32 return ET_KEY_RELEASED;
34 case NSRightMouseDown:
35 case NSOtherMouseDown:
36 return ET_MOUSE_PRESSED;
40 return ET_MOUSE_RELEASED;
41 case NSLeftMouseDragged:
42 case NSRightMouseDragged:
43 case NSOtherMouseDragged:
44 return ET_MOUSE_DRAGGED;
46 return ET_MOUSE_MOVED;
50 return ET_MOUSE_ENTERED;
52 return ET_MOUSE_EXITED;
53 case NSEventTypeSwipe:
54 return ET_SCROLL_FLING_START;
58 case NSApplicationDefined:
62 case NSTabletProximity:
63 case NSEventTypeGesture:
64 case NSEventTypeMagnify:
65 case NSEventTypeRotate:
66 case NSEventTypeBeginGesture:
67 case NSEventTypeEndGesture:
68 NOTIMPLEMENTED() << type;
71 NOTIMPLEMENTED() << type;
77 int EventFlagsFromNative(const base::NativeEvent& event) {
78 NSUInteger modifiers = [event modifierFlags];
79 return EventFlagsFromNSEventWithModifiers(event, modifiers);
82 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
83 NSTimeInterval since_system_startup = [native_event timestamp];
84 // Truncate to extract seconds before doing floating point arithmetic.
85 int64_t seconds = since_system_startup;
86 since_system_startup -= seconds;
87 int64_t microseconds = since_system_startup * 1000000;
88 return base::TimeDelta::FromSeconds(seconds) +
89 base::TimeDelta::FromMicroseconds(microseconds);
92 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
93 NSWindow* window = [native_event window];
95 NOTIMPLEMENTED(); // Point will be in screen coordinates.
98 NSPoint location = [native_event locationInWindow];
99 NSRect content_rect = [window contentRectForFrameRect:[window frame]];
100 return gfx::Point(location.x, NSHeight(content_rect) - location.y);
103 gfx::Point EventSystemLocationFromNative(
104 const base::NativeEvent& native_event) {
109 int EventButtonFromNative(const base::NativeEvent& native_event) {
114 int GetChangedMouseButtonFlagsFromNative(
115 const base::NativeEvent& native_event) {
116 NSEventType type = [native_event type];
118 case NSLeftMouseDown:
120 case NSLeftMouseDragged:
121 return EF_LEFT_MOUSE_BUTTON;
122 case NSRightMouseDown:
124 case NSRightMouseDragged:
125 return EF_RIGHT_MOUSE_BUTTON;
126 case NSOtherMouseDown:
128 case NSOtherMouseDragged:
129 return EF_MIDDLE_MOUSE_BUTTON;
134 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
135 if ([event respondsToSelector:@selector(hasPreciseScrollingDeltas)] &&
136 [event hasPreciseScrollingDeltas]) {
137 // Handle continuous scrolling devices such as a Magic Mouse or a trackpad.
138 // -scrollingDelta{X|Y} have float return types but they return values that
139 // are already rounded to integers.
140 // The values are the same as the values returned from calling
141 // CGEventGetIntegerValueField(kCGScrollWheelEventPointDeltaAxis{1|2}).
142 return gfx::Vector2d([event scrollingDeltaX], [event scrollingDeltaY]);
144 // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
145 // values when scrolling up or to the left. Scrolling quickly results in a
146 // higher delta per click, up to about 15.0. (Quartz documentation suggests
148 // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120).
149 const CGFloat kWheelDeltaMultiplier = 1000;
150 return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
151 kWheelDeltaMultiplier * [event deltaY]);
155 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
159 void ReleaseCopiedNativeEvent(const base::NativeEvent& event) {
163 void IncrementTouchIdRefCount(const base::NativeEvent& native_event) {
167 void ClearTouchIdIfReleased(const base::NativeEvent& native_event) {
171 int GetTouchId(const base::NativeEvent& native_event) {
176 float GetTouchRadiusX(const base::NativeEvent& native_event) {
181 float GetTouchRadiusY(const base::NativeEvent& native_event) {
186 float GetTouchAngle(const base::NativeEvent& native_event) {
191 float GetTouchForce(const base::NativeEvent& native_event) {
196 bool GetScrollOffsets(const base::NativeEvent& native_event,
199 float* x_offset_ordinal,
200 float* y_offset_ordinal,
206 bool GetFlingData(const base::NativeEvent& native_event,
216 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
217 return KeyboardCodeFromNSEvent(native_event);
220 DomCode CodeFromNative(const base::NativeEvent& native_event) {
221 return CodeFromNSEvent(native_event);
224 uint32 PlatformKeycodeFromNative(const base::NativeEvent& native_event) {
225 return native_event.keyCode;
228 uint32 WindowsKeycodeFromNative(const base::NativeEvent& native_event) {
229 return static_cast<uint32>(KeyboardCodeFromNSEvent(native_event));
232 uint16 TextFromNative(const base::NativeEvent& native_event) {
233 NSString* text = @"";
234 if ([native_event type] != NSFlagsChanged)
235 text = [native_event characters];
237 // These exceptions are based on WebInputEventFactoryMac.mm:
238 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
239 if (windows_keycode == '\r')
241 if ([text isEqualToString:@"\x7F"])
243 if (windows_keycode == 9)
247 [text getCharacters:&return_value];
251 uint16 UnmodifiedTextFromNative(const base::NativeEvent& native_event) {
252 NSString* text = @"";
253 if ([native_event type] != NSFlagsChanged)
254 text = [native_event charactersIgnoringModifiers];
256 // These exceptions are based on WebInputEventFactoryMac.mm:
257 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
258 if (windows_keycode == '\r')
260 if ([text isEqualToString:@"\x7F"])
262 if (windows_keycode == 9)
266 [text getCharacters:&return_value];
270 bool IsCharFromNative(const base::NativeEvent& native_event) {