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;
137 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
138 if ([event respondsToSelector:@selector(hasPreciseScrollingDeltas)] &&
139 [event hasPreciseScrollingDeltas]) {
140 // Handle continuous scrolling devices such as a Magic Mouse or a trackpad.
141 // -scrollingDelta{X|Y} have float return types but they return values that
142 // are already rounded to integers.
143 // The values are the same as the values returned from calling
144 // CGEventGetIntegerValueField(kCGScrollWheelEventPointDeltaAxis{1|2}).
145 return gfx::Vector2d([event scrollingDeltaX], [event scrollingDeltaY]);
147 // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
148 // values when scrolling up or to the left. Scrolling quickly results in a
149 // higher delta per click, up to about 15.0. (Quartz documentation suggests
151 // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120).
152 const CGFloat kWheelDeltaMultiplier = 1000;
153 return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
154 kWheelDeltaMultiplier * [event deltaY]);
158 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
162 void ReleaseCopiedNativeEvent(const base::NativeEvent& event) {
166 void ClearTouchIdIfReleased(const base::NativeEvent& native_event) {
170 int GetTouchId(const base::NativeEvent& native_event) {
175 float GetTouchRadiusX(const base::NativeEvent& native_event) {
180 float GetTouchRadiusY(const base::NativeEvent& native_event) {
185 float GetTouchAngle(const base::NativeEvent& native_event) {
190 float GetTouchForce(const base::NativeEvent& native_event) {
195 bool GetScrollOffsets(const base::NativeEvent& native_event,
198 float* x_offset_ordinal,
199 float* y_offset_ordinal,
205 bool GetFlingData(const base::NativeEvent& native_event,
215 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
216 return KeyboardCodeFromNSEvent(native_event);
219 DomCode CodeFromNative(const base::NativeEvent& native_event) {
220 return CodeFromNSEvent(native_event);
223 uint32 WindowsKeycodeFromNative(const base::NativeEvent& native_event) {
224 return static_cast<uint32>(KeyboardCodeFromNSEvent(native_event));
227 uint16 TextFromNative(const base::NativeEvent& native_event) {
228 NSString* text = @"";
229 if ([native_event type] != NSFlagsChanged)
230 text = [native_event characters];
232 // These exceptions are based on web_input_event_builders_mac.mm:
233 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
234 if (windows_keycode == '\r')
236 if ([text isEqualToString:@"\x7F"])
238 if (windows_keycode == 9)
242 [text getCharacters:&return_value];
246 uint16 UnmodifiedTextFromNative(const base::NativeEvent& native_event) {
247 NSString* text = @"";
248 if ([native_event type] != NSFlagsChanged)
249 text = [native_event charactersIgnoringModifiers];
251 // These exceptions are based on web_input_event_builders_mac.mm:
252 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
253 if (windows_keycode == '\r')
255 if ([text isEqualToString:@"\x7F"])
257 if (windows_keycode == 9)
261 [text getCharacters:&return_value];
265 bool IsCharFromNative(const base::NativeEvent& native_event) {