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 #include "base/time/time.h"
11 #include "build/build_config.h"
12 #include "ui/events/cocoa/cocoa_event_utils.h"
13 #include "ui/events/event_utils.h"
14 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
15 #include "ui/gfx/point.h"
16 #include "ui/gfx/vector2d.h"
20 void UpdateDeviceList() {
24 EventType EventTypeFromNative(const base::NativeEvent& native_event) {
25 NSEventType type = [native_event type];
28 return ET_KEY_PRESSED;
30 return ET_KEY_RELEASED;
32 case NSRightMouseDown:
33 case NSOtherMouseDown:
34 return ET_MOUSE_PRESSED;
38 return ET_MOUSE_RELEASED;
39 case NSLeftMouseDragged:
40 case NSRightMouseDragged:
41 case NSOtherMouseDragged:
42 return ET_MOUSE_DRAGGED;
44 return ET_MOUSE_MOVED;
48 return ET_MOUSE_ENTERED;
50 return ET_MOUSE_EXITED;
51 case NSEventTypeSwipe:
52 return ET_SCROLL_FLING_START;
56 case NSApplicationDefined:
60 case NSTabletProximity:
61 case NSEventTypeGesture:
62 case NSEventTypeMagnify:
63 case NSEventTypeRotate:
64 case NSEventTypeBeginGesture:
65 case NSEventTypeEndGesture:
66 NOTIMPLEMENTED() << type;
69 NOTIMPLEMENTED() << type;
75 int EventFlagsFromNative(const base::NativeEvent& event) {
76 NSUInteger modifiers = [event modifierFlags];
77 return EventFlagsFromNSEventWithModifiers(event, modifiers);
80 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
81 NSTimeInterval since_system_startup = [native_event timestamp];
82 // Truncate to extract seconds before doing floating point arithmetic.
83 int64_t seconds = since_system_startup;
84 since_system_startup -= seconds;
85 int64_t microseconds = since_system_startup * 1000000;
86 return base::TimeDelta::FromSeconds(seconds) +
87 base::TimeDelta::FromMicroseconds(microseconds);
90 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
91 NSWindow* window = [native_event window];
93 NOTIMPLEMENTED(); // Point will be in screen coordinates.
96 NSPoint location = [native_event locationInWindow];
97 NSRect content_rect = [window contentRectForFrameRect:[window frame]];
98 return gfx::Point(location.x, NSHeight(content_rect) - location.y);
101 gfx::Point EventSystemLocationFromNative(
102 const base::NativeEvent& native_event) {
107 int EventButtonFromNative(const base::NativeEvent& native_event) {
112 int GetChangedMouseButtonFlagsFromNative(
113 const base::NativeEvent& native_event) {
114 NSEventType type = [native_event type];
116 case NSLeftMouseDown:
118 case NSLeftMouseDragged:
119 return EF_LEFT_MOUSE_BUTTON;
120 case NSRightMouseDown:
122 case NSRightMouseDragged:
123 return EF_RIGHT_MOUSE_BUTTON;
124 case NSOtherMouseDown:
126 case NSOtherMouseDragged:
127 return EF_MIDDLE_MOUSE_BUTTON;
132 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
133 // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
134 // values when scrolling up or to the left. Scrolling quickly results in a
135 // higher delta per click, up to about 15.0. (Quartz documentation suggests
137 // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120).
138 const CGFloat kWheelDeltaMultiplier = 1000;
139 return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
140 kWheelDeltaMultiplier * [event deltaY]);
143 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
147 void ReleaseCopiedNativeEvent(const base::NativeEvent& event) {
151 void IncrementTouchIdRefCount(const base::NativeEvent& native_event) {
155 void ClearTouchIdIfReleased(const base::NativeEvent& native_event) {
159 int GetTouchId(const base::NativeEvent& native_event) {
164 float GetTouchRadiusX(const base::NativeEvent& native_event) {
169 float GetTouchRadiusY(const base::NativeEvent& native_event) {
174 float GetTouchAngle(const base::NativeEvent& native_event) {
179 float GetTouchForce(const base::NativeEvent& native_event) {
184 bool GetScrollOffsets(const base::NativeEvent& native_event,
187 float* x_offset_ordinal,
188 float* y_offset_ordinal,
194 bool GetFlingData(const base::NativeEvent& native_event,
204 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
205 return KeyboardCodeFromNSEvent(native_event);
208 const char* CodeFromNative(const base::NativeEvent& native_event) {
209 return CodeFromNSEvent(native_event);
212 uint32 PlatformKeycodeFromNative(const base::NativeEvent& native_event) {
213 return native_event.keyCode;
216 uint32 WindowsKeycodeFromNative(const base::NativeEvent& native_event) {
217 return static_cast<uint32>(KeyboardCodeFromNSEvent(native_event));
220 uint16 TextFromNative(const base::NativeEvent& native_event) {
221 NSString* text = @"";
222 if ([native_event type] != NSFlagsChanged)
223 text = [native_event characters];
225 // These exceptions are based on WebInputEventFactoryMac.mm:
226 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
227 if (windows_keycode == '\r')
229 if ([text isEqualToString:@"\x7F"])
231 if (windows_keycode == 9)
235 [text getCharacters:&return_value];
239 uint16 UnmodifiedTextFromNative(const base::NativeEvent& native_event) {
240 NSString* text = @"";
241 if ([native_event type] != NSFlagsChanged)
242 text = [native_event charactersIgnoringModifiers];
244 // These exceptions are based on WebInputEventFactoryMac.mm:
245 uint32 windows_keycode = WindowsKeycodeFromNative(native_event);
246 if (windows_keycode == '\r')
248 if ([text isEqualToString:@"\x7F"])
250 if (windows_keycode == 9)
254 [text getCharacters:&return_value];
258 bool IsCharFromNative(const base::NativeEvent& native_event) {