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;
59 case NSApplicationDefined:
63 case NSTabletProximity:
64 case NSEventTypeGesture:
65 case NSEventTypeMagnify:
66 case NSEventTypeRotate:
67 case NSEventTypeBeginGesture:
68 case NSEventTypeEndGesture:
69 NOTIMPLEMENTED() << type;
72 NOTIMPLEMENTED() << type;
78 int EventFlagsFromNative(const base::NativeEvent& event) {
79 NSUInteger modifiers = [event modifierFlags];
80 return EventFlagsFromNSEventWithModifiers(event, modifiers);
83 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
84 NSTimeInterval since_system_startup = [native_event timestamp];
85 // Truncate to extract seconds before doing floating point arithmetic.
86 int64_t seconds = since_system_startup;
87 since_system_startup -= seconds;
88 int64_t microseconds = since_system_startup * 1000000;
89 return base::TimeDelta::FromSeconds(seconds) +
90 base::TimeDelta::FromMicroseconds(microseconds);
93 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
94 NSWindow* window = [native_event window];
96 NOTIMPLEMENTED(); // Point will be in screen coordinates.
99 NSPoint location = [native_event locationInWindow];
100 NSRect content_rect = [window contentRectForFrameRect:[window frame]];
101 return gfx::Point(location.x, NSHeight(content_rect) - location.y);
104 gfx::Point EventSystemLocationFromNative(
105 const base::NativeEvent& native_event) {
110 int EventButtonFromNative(const base::NativeEvent& native_event) {
115 int GetChangedMouseButtonFlagsFromNative(
116 const base::NativeEvent& native_event) {
117 NSEventType type = [native_event type];
119 case NSLeftMouseDown:
121 case NSLeftMouseDragged:
122 return EF_LEFT_MOUSE_BUTTON;
123 case NSRightMouseDown:
125 case NSRightMouseDragged:
126 return EF_RIGHT_MOUSE_BUTTON;
127 case NSOtherMouseDown:
129 case NSOtherMouseDragged:
130 return EF_MIDDLE_MOUSE_BUTTON;
135 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
136 if ([event respondsToSelector:@selector(hasPreciseScrollingDeltas)] &&
137 [event hasPreciseScrollingDeltas]) {
138 // Handle continuous scrolling devices such as a Magic Mouse or a trackpad.
139 // -scrollingDelta{X|Y} have float return types but they return values that
140 // are already rounded to integers.
141 // The values are the same as the values returned from calling
142 // CGEventGetIntegerValueField(kCGScrollWheelEventPointDeltaAxis{1|2}).
143 return gfx::Vector2d([event scrollingDeltaX], [event scrollingDeltaY]);
145 // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
146 // values when scrolling up or to the left. Scrolling quickly results in a
147 // higher delta per click, up to about 15.0. (Quartz documentation suggests
149 // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120).
150 const CGFloat kWheelDeltaMultiplier = 1000;
151 return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
152 kWheelDeltaMultiplier * [event deltaY]);
156 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
160 void ReleaseCopiedNativeEvent(const base::NativeEvent& event) {
164 void ClearTouchIdIfReleased(const base::NativeEvent& native_event) {
168 int GetTouchId(const base::NativeEvent& native_event) {
173 float GetTouchRadiusX(const base::NativeEvent& native_event) {
178 float GetTouchRadiusY(const base::NativeEvent& native_event) {
183 float GetTouchAngle(const base::NativeEvent& native_event) {
188 float GetTouchForce(const base::NativeEvent& native_event) {
193 bool GetScrollOffsets(const base::NativeEvent& native_event,
196 float* x_offset_ordinal,
197 float* y_offset_ordinal,
203 bool GetFlingData(const base::NativeEvent& native_event,
213 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
214 return KeyboardCodeFromNSEvent(native_event);
217 DomCode CodeFromNative(const base::NativeEvent& native_event) {
218 return CodeFromNSEvent(native_event);
221 uint32 PlatformKeycodeFromNative(const base::NativeEvent& native_event) {
222 return native_event.keyCode;
225 uint32 WindowsKeycodeFromNative(const base::NativeEvent& native_event) {
226 return static_cast<uint32>(KeyboardCodeFromNSEvent(native_event));
229 uint16 TextFromNative(const base::NativeEvent& native_event) {
230 NSString* text = @"";
231 if ([native_event type] != NSFlagsChanged)
232 text = [native_event characters];
234 // These exceptions are based on WebInputEventFactoryMac.mm:
235 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
236 if (windows_keycode == '\r')
238 if ([text isEqualToString:@"\x7F"])
240 if (windows_keycode == 9)
244 [text getCharacters:&return_value];
248 uint16 UnmodifiedTextFromNative(const base::NativeEvent& native_event) {
249 NSString* text = @"";
250 if ([native_event type] != NSFlagsChanged)
251 text = [native_event charactersIgnoringModifiers];
253 // These exceptions are based on WebInputEventFactoryMac.mm:
254 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
255 if (windows_keycode == '\r')
257 if ([text isEqualToString:@"\x7F"])
259 if (windows_keycode == 9)
263 [text getCharacters:&return_value];
267 bool IsCharFromNative(const base::NativeEvent& native_event) {