1 // Copyright (c) 2012 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 <Cocoa/Cocoa.h>
7 #include "ui/events/event_constants.h"
9 #include "base/event_types.h"
10 #include "base/logging.h"
11 #include "base/time/time.h"
12 #include "ui/events/event_utils.h"
13 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
14 #include "ui/gfx/point.h"
18 EventType EventTypeFromNative(const base::NativeEvent& native_event) {
19 NSEventType native_type = [native_event type];
20 switch (native_type) {
22 case NSRightMouseDown:
23 case NSOtherMouseDown:
24 return ET_MOUSE_PRESSED;
29 return ET_MOUSE_RELEASED;
32 return ET_MOUSE_MOVED;
34 case NSLeftMouseDragged:
35 case NSRightMouseDragged:
36 case NSOtherMouseDragged:
37 return ET_MOUSE_DRAGGED;
40 return ET_MOUSE_ENTERED;
43 return ET_MOUSE_EXITED;
46 return ET_KEY_PRESSED;
49 return ET_KEY_RELEASED;
52 return ET_KEY_PRESSED;
59 case NSApplicationDefined:
63 case NSTabletProximity:
69 int EventFlagsFromNative(const base::NativeEvent& native_event) {
71 NSUInteger modifiers = [native_event modifierFlags];
73 if (modifiers & NSAlphaShiftKeyMask)
74 event_flags = event_flags | EF_CAPS_LOCK_DOWN;
76 if (modifiers & NSShiftKeyMask)
77 event_flags = event_flags | EF_SHIFT_DOWN;
79 if (modifiers & NSControlKeyMask)
80 event_flags = event_flags | EF_CONTROL_DOWN;
82 if (modifiers & NSAlternateKeyMask)
83 event_flags = event_flags | EF_ALT_DOWN;
85 if (modifiers & NSCommandKeyMask)
86 event_flags = event_flags | EF_COMMAND_DOWN;
88 NSEventType type = [native_event type];
90 if (type == NSLeftMouseDown ||
91 type == NSLeftMouseUp ||
92 type == NSLeftMouseDragged) {
93 event_flags = event_flags | EF_LEFT_MOUSE_BUTTON;
96 if (type == NSRightMouseDown ||
97 type == NSRightMouseUp ||
98 type == NSRightMouseDragged) {
99 event_flags = event_flags | EF_RIGHT_MOUSE_BUTTON;
102 if (type == NSOtherMouseDown ||
103 type == NSOtherMouseUp ||
104 type == NSOtherMouseDragged) {
105 event_flags = event_flags | EF_MIDDLE_MOUSE_BUTTON;
111 base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) {
112 return base::TimeDelta::FromMicroseconds(
113 [native_event timestamp] * 1000000.0f);
116 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) {
117 NSWindow* window = [native_event window];
118 NSPoint location = [native_event locationInWindow];
120 // Convert |location| to be relative to coordinate system of |contentView|.
121 // Note: this assumes that ui::Event coordinates are rooted in the top-level
122 // view (with flipped coordinates). A more general (but costly) approach
123 // would be to hit-test the view of the event and use the found view's
124 // coordinate system. Currently there is no need for this generality, and
125 // speed is preferred. Flipped views are not suppported.
126 DCHECK([[window contentView] isFlipped] == NO);
127 location = [[window contentView] convertPoint:location fromView:nil];
128 location.y = [[window contentView] bounds].size.height - location.y;
130 return gfx::Point(NSPointToCGPoint(location));
133 gfx::Point EventSystemLocationFromNative(
134 const base::NativeEvent& native_event) {
135 // TODO(port): Needs to always return screen position here. Returning normal
136 // origin for now since that's obviously wrong.
137 return gfx::Point(0, 0);
140 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) {
141 return ui::KeyboardCodeFromNSEvent(native_event);
144 bool IsMouseEvent(const base::NativeEvent& native_event) {
145 EventType type = EventTypeFromNative(native_event);
146 return type == ET_MOUSE_PRESSED ||
147 type == ET_MOUSE_DRAGGED ||
148 type == ET_MOUSE_RELEASED ||
149 type == ET_MOUSE_MOVED ||
150 type == ET_MOUSE_ENTERED ||
151 type == ET_MOUSE_EXITED;
154 gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& native_event) {
155 // TODO(dhollowa): Come back to this once comparisons can be made with other
157 return gfx::Vector2d([native_event deltaX], [native_event deltaY]);
160 void ClearTouchIdIfReleased(const base::NativeEvent& xev) {
161 // Touch is currently unsupported.
164 int GetTouchId(const base::NativeEvent& native_event) {
165 // Touch is currently unsupported.
169 float GetTouchRadiusX(const base::NativeEvent& native_event) {
170 // Touch is currently unsupported.
174 float GetTouchRadiusY(const base::NativeEvent& native_event) {
175 // Touch is currently unsupported.
179 float GetTouchAngle(const base::NativeEvent& native_event) {
180 // Touch is currently unsupported.
184 float GetTouchForce(const base::NativeEvent& native_event) {
185 // Touch is currently unsupported.
189 bool GetScrollOffsets(const base::NativeEvent& native_event,
196 bool IsNoopEvent(const base::NativeEvent& event) {
197 return ([event type] == NSApplicationDefined && [event subtype] == 0);
200 base::NativeEvent CreateNoopEvent() {
201 return [NSEvent otherEventWithType:NSApplicationDefined
204 timestamp:[NSDate timeIntervalSinceReferenceDate]