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;
47 return ET_MOUSE_ENTERED;
49 return ET_MOUSE_EXITED;
50 case NSEventTypeSwipe:
51 return ET_SCROLL_FLING_START;
55 case NSApplicationDefined:
59 case NSTabletProximity:
60 case NSEventTypeGesture:
61 case NSEventTypeMagnify:
62 case NSEventTypeRotate:
63 case NSEventTypeBeginGesture:
64 case NSEventTypeEndGesture:
65 NOTIMPLEMENTED() << type;
68 NOTIMPLEMENTED() << type;
74 int EventFlagsFromNative(const base::NativeEvent& event) {
75 NSUInteger modifiers = [event modifierFlags];
76 return EventFlagsFromNSEventWithModifiers(event, modifiers);
79 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
80 NSTimeInterval since_system_startup = [native_event timestamp];
81 // Truncate to extract seconds before doing floating point arithmetic.
82 int64_t seconds = since_system_startup;
83 since_system_startup -= seconds;
84 int64_t microseconds = since_system_startup * 1000000;
85 return base::TimeDelta::FromSeconds(seconds) +
86 base::TimeDelta::FromMicroseconds(microseconds);
89 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
90 if (![native_event window]) {
91 NOTIMPLEMENTED(); // Point will be in screen coordinates.
94 NSPoint location = [native_event locationInWindow];
95 return gfx::Point(location.x,
96 NSHeight([[native_event window] frame]) - location.y);
99 gfx::Point EventSystemLocationFromNative(
100 const base::NativeEvent& native_event) {
105 int EventButtonFromNative(const base::NativeEvent& native_event) {
110 int GetChangedMouseButtonFlagsFromNative(
111 const base::NativeEvent& native_event) {
112 NSEventType type = [native_event type];
114 case NSLeftMouseDown:
116 case NSLeftMouseDragged:
117 return EF_LEFT_MOUSE_BUTTON;
118 case NSRightMouseDown:
120 case NSRightMouseDragged:
121 return EF_RIGHT_MOUSE_BUTTON;
122 case NSOtherMouseDown:
124 case NSOtherMouseDragged:
125 return EF_MIDDLE_MOUSE_BUTTON;
130 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) {
131 // Empirically, a value of 0.1 is typical for one mousewheel click. Positive
132 // values when scrolling up or to the left. Scrolling quickly results in a
133 // higher delta per click, up to about 15.0. (Quartz documentation suggests
135 // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120).
136 const CGFloat kWheelDeltaMultiplier = 1000;
137 return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX],
138 kWheelDeltaMultiplier * [event deltaY]);
141 base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) {
145 void ReleaseCopiedNativeEvent(const base::NativeEvent& event) {
149 void ClearTouchIdIfReleased(const base::NativeEvent& native_event) {
153 int GetTouchId(const base::NativeEvent& native_event) {
158 float GetTouchRadiusX(const base::NativeEvent& native_event) {
163 float GetTouchRadiusY(const base::NativeEvent& native_event) {
168 float GetTouchAngle(const base::NativeEvent& native_event) {
173 float GetTouchForce(const base::NativeEvent& native_event) {
178 bool GetScrollOffsets(const base::NativeEvent& native_event,
181 float* x_offset_ordinal,
182 float* y_offset_ordinal,
188 bool GetFlingData(const base::NativeEvent& native_event,
198 bool GetGestureTimes(const base::NativeEvent& native_event,
205 void SetNaturalScroll(bool enabled) {
209 bool IsNaturalScrollEnabled() {
214 bool IsTouchpadEvent(const base::NativeEvent& native_event) {
219 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
220 return KeyboardCodeFromNSEvent(native_event);
223 const char* CodeFromNative(const base::NativeEvent& native_event) {
224 return CodeFromNSEvent(native_event);