diff options
-rw-r--r-- | ui/events/cocoa/cocoa_event_utils.mm | 5 | ||||
-rw-r--r-- | ui/events/cocoa/cocoa_event_utils_unittest.mm | 79 | ||||
-rw-r--r-- | ui/events/cocoa/events_mac.mm | 227 | ||||
-rw-r--r-- | ui/events/cocoa/events_mac_unittest.mm | 243 | ||||
-rw-r--r-- | ui/events/event.cc | 45 | ||||
-rw-r--r-- | ui/events/event.h | 4 | ||||
-rw-r--r-- | ui/events/event_utils.h | 9 | ||||
-rw-r--r-- | ui/events/events.gyp | 5 | ||||
-rw-r--r-- | ui/events/events_stub.cc | 11 | ||||
-rw-r--r-- | ui/events/ozone/events_ozone.cc | 7 | ||||
-rw-r--r-- | ui/events/win/events_win.cc | 7 | ||||
-rw-r--r-- | ui/events/x/events_x.cc | 12 |
12 files changed, 522 insertions, 132 deletions
diff --git a/ui/events/cocoa/cocoa_event_utils.mm b/ui/events/cocoa/cocoa_event_utils.mm index dd39845..ade083c 100644 --- a/ui/events/cocoa/cocoa_event_utils.mm +++ b/ui/events/cocoa/cocoa_event_utils.mm @@ -34,11 +34,6 @@ bool IsMiddleButtonEvent(NSEvent* event) { namespace ui { -int EventFlagsFromNative(const base::NativeEvent& event) { - NSUInteger modifiers = [event modifierFlags]; - return EventFlagsFromNSEventWithModifiers(event, modifiers); -} - int EventFlagsFromModifiers(NSUInteger modifiers) { int flags = 0; flags |= (modifiers & NSAlphaShiftKeyMask) ? ui::EF_CAPS_LOCK_DOWN : 0; diff --git a/ui/events/cocoa/cocoa_event_utils_unittest.mm b/ui/events/cocoa/cocoa_event_utils_unittest.mm deleted file mode 100644 index 4e24fd2..0000000 --- a/ui/events/cocoa/cocoa_event_utils_unittest.mm +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "ui/events/cocoa/cocoa_event_utils.h" - -#import <objc/objc-class.h> - -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/platform_test.h" -#include "ui/events/event_constants.h" -#include "ui/events/event_utils.h" -#import "ui/events/test/cocoa_test_event_utils.h" -#import "ui/gfx/test/ui_cocoa_test_helper.h" - -namespace ui { - -namespace { - -class CocoaEventUtilsTest : public CocoaTest { -}; - -TEST_F(CocoaEventUtilsTest, EventFlagsFromNative) { - // Left click. - NSEvent* left = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON, EventFlagsFromNative(left)); - - // Right click. - NSEvent* right = cocoa_test_event_utils::MouseEventWithType(NSRightMouseUp, - 0); - EXPECT_EQ(EF_RIGHT_MOUSE_BUTTON, EventFlagsFromNative(right)); - - // Middle click. - NSEvent* middle = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, - 0); - EXPECT_EQ(EF_MIDDLE_MOUSE_BUTTON, EventFlagsFromNative(middle)); - - // Caps + Left - NSEvent* caps = cocoa_test_event_utils::MouseEventWithType( - NSLeftMouseUp, NSAlphaShiftKeyMask); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_CAPS_LOCK_DOWN, - EventFlagsFromNative(caps)); - - // Shift + Left - NSEvent* shift = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, - NSShiftKeyMask); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_SHIFT_DOWN, EventFlagsFromNative(shift)); - - // Ctrl + Left - NSEvent* ctrl = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, - NSControlKeyMask); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_CONTROL_DOWN, EventFlagsFromNative(ctrl)); - - // Alt + Left - NSEvent* alt = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, - NSAlternateKeyMask); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_ALT_DOWN, EventFlagsFromNative(alt)); - - // Cmd + Left - NSEvent* cmd = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, - NSCommandKeyMask); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_COMMAND_DOWN, EventFlagsFromNative(cmd)); - - // Shift + Ctrl + Left - NSEvent* shiftctrl = cocoa_test_event_utils::MouseEventWithType( - NSLeftMouseUp, NSShiftKeyMask | NSControlKeyMask); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_SHIFT_DOWN | EF_CONTROL_DOWN, - EventFlagsFromNative(shiftctrl)); - - // Cmd + Alt + Right - NSEvent* cmdalt = cocoa_test_event_utils::MouseEventWithType( - NSLeftMouseUp, NSCommandKeyMask | NSAlternateKeyMask); - EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_COMMAND_DOWN | EF_ALT_DOWN, - EventFlagsFromNative(cmdalt)); -} - -} // namespace - -} // namespace ui diff --git a/ui/events/cocoa/events_mac.mm b/ui/events/cocoa/events_mac.mm new file mode 100644 index 0000000..a8d9365 --- /dev/null +++ b/ui/events/cocoa/events_mac.mm @@ -0,0 +1,227 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/events/event_utils.h" + +#include <Cocoa/Cocoa.h> + +#include "base/logging.h" +#include "base/time/time.h" +#include "build/build_config.h" +#include "ui/events/cocoa/cocoa_event_utils.h" +#include "ui/events/event_utils.h" +#import "ui/events/keycodes/keyboard_code_conversion_mac.h" +#include "ui/gfx/point.h" +#include "ui/gfx/vector2d.h" + +namespace ui { + +void UpdateDeviceList() { + NOTIMPLEMENTED(); +} + +EventType EventTypeFromNative(const base::NativeEvent& native_event) { + NSEventType type = [native_event type]; + switch (type) { + case NSKeyDown: + return ET_KEY_PRESSED; + case NSKeyUp: + return ET_KEY_RELEASED; + case NSLeftMouseDown: + case NSRightMouseDown: + case NSOtherMouseDown: + return ET_MOUSE_PRESSED; + case NSLeftMouseUp: + case NSRightMouseUp: + case NSOtherMouseUp: + return ET_MOUSE_RELEASED; + case NSLeftMouseDragged: + case NSRightMouseDragged: + case NSOtherMouseDragged: + return ET_MOUSE_DRAGGED; + case NSMouseMoved: + case NSScrollWheel: + return ET_MOUSEWHEEL; + case NSMouseEntered: + return ET_MOUSE_ENTERED; + case NSMouseExited: + return ET_MOUSE_EXITED; + case NSEventTypeSwipe: + return ET_SCROLL_FLING_START; + case NSFlagsChanged: + case NSAppKitDefined: + case NSSystemDefined: + case NSApplicationDefined: + case NSPeriodic: + case NSCursorUpdate: + case NSTabletPoint: + case NSTabletProximity: + case NSEventTypeGesture: + case NSEventTypeMagnify: + case NSEventTypeRotate: + case NSEventTypeBeginGesture: + case NSEventTypeEndGesture: + NOTIMPLEMENTED() << type; + break; + default: + NOTIMPLEMENTED() << type; + break; + } + return ET_UNKNOWN; +} + +int EventFlagsFromNative(const base::NativeEvent& event) { + NSUInteger modifiers = [event modifierFlags]; + return EventFlagsFromNSEventWithModifiers(event, modifiers); +} + +base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) { + NSTimeInterval since_system_startup = [native_event timestamp]; + // Truncate to extract seconds before doing floating point arithmetic. + int64_t seconds = since_system_startup; + since_system_startup -= seconds; + int64_t microseconds = since_system_startup * 1000000; + return base::TimeDelta::FromSeconds(seconds) + + base::TimeDelta::FromMicroseconds(microseconds); +} + +gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) { + if (![native_event window]) { + NOTIMPLEMENTED(); // Point will be in screen coordinates. + return gfx::Point(); + } + NSPoint location = [native_event locationInWindow]; + return gfx::Point(location.x, + NSHeight([[native_event window] frame]) - location.y); +} + +gfx::Point EventSystemLocationFromNative( + const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return gfx::Point(); +} + +int EventButtonFromNative(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return 0; +} + +int GetChangedMouseButtonFlagsFromNative( + const base::NativeEvent& native_event) { + NSEventType type = [native_event type]; + switch (type) { + case NSLeftMouseDown: + case NSLeftMouseUp: + case NSLeftMouseDragged: + return EF_LEFT_MOUSE_BUTTON; + case NSRightMouseDown: + case NSRightMouseUp: + case NSRightMouseDragged: + return EF_RIGHT_MOUSE_BUTTON; + case NSOtherMouseDown: + case NSOtherMouseUp: + case NSOtherMouseDragged: + return EF_MIDDLE_MOUSE_BUTTON; + } + return 0; +} + +gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& event) { + // Empirically, a value of 0.1 is typical for one mousewheel click. Positive + // values when scrolling up or to the left. Scrolling quickly results in a + // higher delta per click, up to about 15.0. (Quartz documentation suggests + // +/-10). + // Multiply by 1000 to vaguely approximate WHEEL_DELTA on Windows (120). + const CGFloat kWheelDeltaMultiplier = 1000; + return gfx::Vector2d(kWheelDeltaMultiplier * [event deltaX], + kWheelDeltaMultiplier * [event deltaY]); +} + +base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { + return [event copy]; +} + +void ReleaseCopiedNativeEvent(const base::NativeEvent& event) { + [event release]; +} + +void ClearTouchIdIfReleased(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); +} + +int GetTouchId(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return 0; +} + +float GetTouchRadiusX(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return 0.f; +} + +float GetTouchRadiusY(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return 0.f; +} + +float GetTouchAngle(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return 0.f; +} + +float GetTouchForce(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return 0.f; +} + +bool GetScrollOffsets(const base::NativeEvent& native_event, + float* x_offset, + float* y_offset, + float* x_offset_ordinal, + float* y_offset_ordinal, + int* finger_count) { + NOTIMPLEMENTED(); + return false; +} + +bool GetFlingData(const base::NativeEvent& native_event, + float* vx, + float* vy, + float* vx_ordinal, + float* vy_ordinal, + bool* is_cancel) { + NOTIMPLEMENTED(); + return false; +} + +bool GetGestureTimes(const base::NativeEvent& native_event, + double* start_time, + double* end_time) { + NOTIMPLEMENTED(); + return false; +} + +void SetNaturalScroll(bool enabled) { + NOTIMPLEMENTED(); +} + +bool IsNaturalScrollEnabled() { + NOTIMPLEMENTED(); + return false; +} + +bool IsTouchpadEvent(const base::NativeEvent& native_event) { + NOTIMPLEMENTED(); + return false; +} + +KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) { + return KeyboardCodeFromNSEvent(native_event); +} + +const char* CodeFromNative(const base::NativeEvent& native_event) { + return CodeFromNSEvent(native_event); +} + +} // namespace ui diff --git a/ui/events/cocoa/events_mac_unittest.mm b/ui/events/cocoa/events_mac_unittest.mm new file mode 100644 index 0000000..9b93084 --- /dev/null +++ b/ui/events/cocoa/events_mac_unittest.mm @@ -0,0 +1,243 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/events/event_utils.h" + +#import <Cocoa/Cocoa.h> + +#include "base/mac/scoped_cftyperef.h" +#include "base/memory/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/events/event_constants.h" +#import "ui/events/test/cocoa_test_event_utils.h" +#include "ui/gfx/point.h" +#import "ui/gfx/test/ui_cocoa_test_helper.h" + +namespace { + +NSWindow* g_test_window = nil; + +} // namespace + +// Mac APIs for creating test events are frustrating. Quartz APIs have, e.g., +// CGEventCreateMouseEvent() which can't set a window or modifier flags. +// Cocoa APIs have +[NSEvent mouseEventWithType:..] which can't set +// buttonNumber or scroll deltas. To work around this, these tests use some +// Objective C magic to donate member functions to NSEvent temporarily. +@interface MiddleMouseButtonNumberDonor : NSObject +@end + +@interface TestWindowDonor : NSObject +@end + +@implementation MiddleMouseButtonNumberDonor +- (NSUInteger)buttonNumber { return 2; } +@end + +@implementation TestWindowDonor +- (NSWindow*)window { return g_test_window; } +@end + +namespace ui { + +namespace { + +class EventsMacTest : public CocoaTest { + public: + EventsMacTest() {} + + gfx::Point Flip(gfx::Point window_location) { + window_location.set_y( + NSHeight([test_window() frame]) - window_location.y()); + return window_location; + } + + void SwizzleMiddleMouseButton() { + DCHECK(!swizzler_); + swizzler_.reset(new ScopedClassSwizzler( + [NSEvent class], + [MiddleMouseButtonNumberDonor class], + @selector(buttonNumber))); + } + + void SwizzleTestWindow() { + DCHECK(!g_test_window); + DCHECK(!swizzler_); + g_test_window = test_window(); + swizzler_.reset(new ScopedClassSwizzler( + [NSEvent class], + [TestWindowDonor class], + @selector(window))); + } + + void ClearSwizzle() { + swizzler_.reset(); + g_test_window = nil; + } + + NSEvent* TestMouseEvent(NSEventType type, + const gfx::Point &window_location, + NSInteger modifier_flags) { + NSPoint point = NSPointFromCGPoint(Flip(window_location).ToCGPoint()); + return [NSEvent mouseEventWithType:type + location:point + modifierFlags:modifier_flags + timestamp:0 + windowNumber:[test_window() windowNumber] + context:nil + eventNumber:0 + clickCount:0 + pressure:1.0]; + } + + NSEvent* TestScrollEvent(const gfx::Point& window_location, + int32_t delta_x, + int32_t delta_y) { + SwizzleTestWindow(); + base::ScopedCFTypeRef<CGEventRef> scroll( + CGEventCreateScrollWheelEvent(NULL, + kCGScrollEventUnitLine, + 2, + delta_y, + delta_x)); + // CGEvents are always in global display coordinates. These are like screen + // coordinates, but flipped. But first the point needs to be converted out + // of window coordinates (which also requires flipping). + NSPoint window_point = + NSPointFromCGPoint(Flip(window_location).ToCGPoint()); + NSPoint screen_point = [test_window() convertBaseToScreen:window_point]; + CGFloat primary_screen_height = + NSHeight([[[NSScreen screens] objectAtIndex:0] frame]); + screen_point.y = primary_screen_height - screen_point.y; + CGEventSetLocation(scroll, NSPointToCGPoint(screen_point)); + return [NSEvent eventWithCGEvent:scroll]; + } + + private: + scoped_ptr<ScopedClassSwizzler> swizzler_; + + DISALLOW_COPY_AND_ASSIGN(EventsMacTest); +}; + +} // namespace + +TEST_F(EventsMacTest, EventFlagsFromNative) { + // Left click. + NSEvent* left = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON, EventFlagsFromNative(left)); + + // Right click. + NSEvent* right = cocoa_test_event_utils::MouseEventWithType(NSRightMouseUp, + 0); + EXPECT_EQ(EF_RIGHT_MOUSE_BUTTON, EventFlagsFromNative(right)); + + // Middle click. + NSEvent* middle = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, + 0); + EXPECT_EQ(EF_MIDDLE_MOUSE_BUTTON, EventFlagsFromNative(middle)); + + // Caps + Left + NSEvent* caps = cocoa_test_event_utils::MouseEventWithType( + NSLeftMouseUp, NSAlphaShiftKeyMask); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_CAPS_LOCK_DOWN, + EventFlagsFromNative(caps)); + + // Shift + Left + NSEvent* shift = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, + NSShiftKeyMask); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_SHIFT_DOWN, EventFlagsFromNative(shift)); + + // Ctrl + Left + NSEvent* ctrl = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, + NSControlKeyMask); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_CONTROL_DOWN, EventFlagsFromNative(ctrl)); + + // Alt + Left + NSEvent* alt = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, + NSAlternateKeyMask); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_ALT_DOWN, EventFlagsFromNative(alt)); + + // Cmd + Left + NSEvent* cmd = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, + NSCommandKeyMask); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_COMMAND_DOWN, EventFlagsFromNative(cmd)); + + // Shift + Ctrl + Left + NSEvent* shiftctrl = cocoa_test_event_utils::MouseEventWithType( + NSLeftMouseUp, NSShiftKeyMask | NSControlKeyMask); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_SHIFT_DOWN | EF_CONTROL_DOWN, + EventFlagsFromNative(shiftctrl)); + + // Cmd + Alt + Right + NSEvent* cmdalt = cocoa_test_event_utils::MouseEventWithType( + NSLeftMouseUp, NSCommandKeyMask | NSAlternateKeyMask); + EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_COMMAND_DOWN | EF_ALT_DOWN, + EventFlagsFromNative(cmdalt)); +} + +// Tests mouse button presses and mouse wheel events. +TEST_F(EventsMacTest, ButtonEvents) { + gfx::Point location(5, 10); + gfx::Vector2d offset; + + NSEvent* event = TestMouseEvent(NSLeftMouseDown, location, 0); + EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(event)); + EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(event)); + EXPECT_EQ(location, ui::EventLocationFromNative(event)); + + SwizzleMiddleMouseButton(); + event = TestMouseEvent(NSOtherMouseDown, location, NSShiftKeyMask); + EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(event)); + EXPECT_EQ(ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_SHIFT_DOWN, + ui::EventFlagsFromNative(event)); + EXPECT_EQ(location, ui::EventLocationFromNative(event)); + ClearSwizzle(); + + event = TestMouseEvent(NSRightMouseUp, location, 0); + EXPECT_EQ(ui::ET_MOUSE_RELEASED, ui::EventTypeFromNative(event)); + EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, ui::EventFlagsFromNative(event)); + EXPECT_EQ(location, ui::EventLocationFromNative(event)); + + // Scroll up. + event = TestScrollEvent(location, 0, 1); + EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(event)); + EXPECT_EQ(0, ui::EventFlagsFromNative(event)); + EXPECT_EQ(location.ToString(), ui::EventLocationFromNative(event).ToString()); + offset = ui::GetMouseWheelOffset(event); + EXPECT_GT(offset.y(), 0); + EXPECT_EQ(0, offset.x()); + ClearSwizzle(); + + // Scroll down. + event = TestScrollEvent(location, 0, -1); + EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(event)); + EXPECT_EQ(0, ui::EventFlagsFromNative(event)); + EXPECT_EQ(location, ui::EventLocationFromNative(event)); + offset = ui::GetMouseWheelOffset(event); + EXPECT_LT(offset.y(), 0); + EXPECT_EQ(0, offset.x()); + ClearSwizzle(); + + // Scroll left. + event = TestScrollEvent(location, 1, 0); + EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(event)); + EXPECT_EQ(0, ui::EventFlagsFromNative(event)); + EXPECT_EQ(location, ui::EventLocationFromNative(event)); + offset = ui::GetMouseWheelOffset(event); + EXPECT_EQ(0, offset.y()); + EXPECT_GT(offset.x(), 0); + ClearSwizzle(); + + // Scroll right. + event = TestScrollEvent(location, -1, 0); + EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(event)); + EXPECT_EQ(0, ui::EventFlagsFromNative(event)); + EXPECT_EQ(location, ui::EventLocationFromNative(event)); + offset = ui::GetMouseWheelOffset(event); + EXPECT_EQ(0, offset.y()); + EXPECT_LT(offset.x(), 0); + ClearSwizzle(); +} + +} // namespace ui diff --git a/ui/events/event.cc b/ui/events/event.cc index 41f4ca6..13526d9 100644 --- a/ui/events/event.cc +++ b/ui/events/event.cc @@ -29,24 +29,6 @@ namespace { -base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { -#if defined(USE_X11) - if (!event || event->type == GenericEvent) - return NULL; - XEvent* copy = new XEvent; - *copy = *event; - return copy; -#elif defined(OS_WIN) - return event; -#elif defined(USE_OZONE) - return NULL; -#else - NOTREACHED() << - "Don't know how to copy base::NativeEvent for this platform"; - return NULL; -#endif -} - std::string EventTypeName(ui::EventType type) { #define RETURN_IF_TYPE(t) if (type == ui::t) return #t #define CASE_TYPE(t) case ui::t: return #t @@ -119,10 +101,8 @@ namespace ui { // Event Event::~Event() { -#if defined(USE_X11) if (delete_native_event_) - delete native_event_; -#endif + ReleaseCopiedNativeEvent(native_event_); } bool Event::HasNativeEvent() const { @@ -151,9 +131,7 @@ Event::Event(EventType type, base::TimeDelta time_stamp, int flags) : type_(type), time_stamp_(time_stamp), flags_(flags), -#if defined(USE_X11) - native_event_(NULL), -#endif + native_event_(base::NativeEvent()), delete_native_event_(false), cancelable_(true), target_(NULL), @@ -161,7 +139,6 @@ Event::Event(EventType type, base::TimeDelta time_stamp, int flags) result_(ER_UNHANDLED) { if (type_ < ET_LAST) name_ = EventTypeName(type_); - Init(); } Event::Event(const base::NativeEvent& native_event, @@ -170,6 +147,7 @@ Event::Event(const base::NativeEvent& native_event, : type_(type), time_stamp_(EventTimeFromNative(native_event)), flags_(flags), + native_event_(native_event), delete_native_event_(false), cancelable_(true), target_(NULL), @@ -190,7 +168,6 @@ Event::Event(const base::NativeEvent& native_event, 100, base::HistogramBase::kUmaTargetedHistogramFlag); counter_for_type->Add(delta.InMicroseconds()); - InitWithNativeEvent(native_event); } Event::Event(const Event& copy) @@ -198,18 +175,14 @@ Event::Event(const Event& copy) time_stamp_(copy.time_stamp_), latency_(copy.latency_), flags_(copy.flags_), - native_event_(::CopyNativeEvent(copy.native_event_)), - delete_native_event_(false), + native_event_(CopyNativeEvent(copy.native_event_)), + delete_native_event_(true), cancelable_(true), target_(NULL), phase_(EP_PREDISPATCH), result_(ER_UNHANDLED) { if (type_ < ET_LAST) name_ = EventTypeName(type_); -#if defined(USE_X11) - if (native_event_) - delete_native_event_ = true; -#endif } void Event::SetType(EventType type) { @@ -220,14 +193,6 @@ void Event::SetType(EventType type) { name_ = EventTypeName(type_); } -void Event::Init() { - std::memset(&native_event_, 0, sizeof(native_event_)); -} - -void Event::InitWithNativeEvent(const base::NativeEvent& native_event) { - native_event_ = native_event; -} - //////////////////////////////////////////////////////////////////////////////// // CancelModeEvent diff --git a/ui/events/event.h b/ui/events/event.h index 17cad8b..d057a28 100644 --- a/ui/events/event.h +++ b/ui/events/event.h @@ -219,10 +219,6 @@ class EVENTS_EXPORT Event { private: friend class EventTestApi; - // Safely initializes the native event members of this class. - void Init(); - void InitWithNativeEvent(const base::NativeEvent& native_event); - EventType type_; std::string name_; base::TimeDelta time_stamp_; diff --git a/ui/events/event_utils.h b/ui/events/event_utils.h index 9c40ab5..08bbfbc 100644 --- a/ui/events/event_utils.h +++ b/ui/events/event_utils.h @@ -84,6 +84,15 @@ EVENTS_EXPORT int GetChangedMouseButtonFlagsFromNative( EVENTS_EXPORT gfx::Vector2d GetMouseWheelOffset( const base::NativeEvent& native_event); +// Returns a copy of |native_event|. Depending on the platform, this copy may +// need to be deleted with ReleaseCopiedNativeEvent(). +base::NativeEvent CopyNativeEvent( + const base::NativeEvent& native_event); + +// Delete a |native_event| previously created by CopyNativeEvent(). +void ReleaseCopiedNativeEvent( + const base::NativeEvent& native_event); + // Gets the touch id from a native event. EVENTS_EXPORT int GetTouchId(const base::NativeEvent& native_event); diff --git a/ui/events/events.gyp b/ui/events/events.gyp index 1aabbe3..cb796e5 100644 --- a/ui/events/events.gyp +++ b/ui/events/events.gyp @@ -85,6 +85,7 @@ 'sources': [ 'cocoa/cocoa_event_utils.h', 'cocoa/cocoa_event_utils.mm', + 'cocoa/events_mac.mm', 'event.cc', 'event.h', 'event_dispatcher.cc', @@ -166,7 +167,7 @@ 'conditions': [ # We explicitly enumerate the platforms we _do_ provide native cracking # for here. - ['OS=="win" or use_x11==1 or use_ozone==1', { + ['OS=="win" or OS=="mac" or use_x11==1 or use_ozone==1', { 'sources!': [ 'events_stub.cc', ], @@ -337,7 +338,7 @@ 'gesture_detection' ], 'sources': [ - 'cocoa/cocoa_event_utils_unittest.mm', + 'cocoa/events_mac_unittest.mm', 'event_dispatcher_unittest.cc', 'event_processor_unittest.cc', 'event_rewriter_unittest.cc', diff --git a/ui/events/events_stub.cc b/ui/events/events_stub.cc index bece37a..5ab368a 100644 --- a/ui/events/events_stub.cc +++ b/ui/events/events_stub.cc @@ -23,12 +23,10 @@ EventType EventTypeFromNative(const base::NativeEvent& native_event) { return ET_UNKNOWN; } -#if !defined(OS_MACOSX) int EventFlagsFromNative(const base::NativeEvent& native_event) { NOTIMPLEMENTED(); return 0; } -#endif base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) { NOTIMPLEMENTED(); @@ -62,6 +60,15 @@ gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& native_event) { return gfx::Vector2d(); } +base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { + NOTIMPLEMENTED() << + "Don't know how to copy base::NativeEvent for this platform"; + return NULL; +} + +void ReleaseCopiedNativeEvent(const base::NativeEvent& event) { +} + void ClearTouchIdIfReleased(const base::NativeEvent& native_event) { NOTIMPLEMENTED(); } diff --git a/ui/events/ozone/events_ozone.cc b/ui/events/ozone/events_ozone.cc index 2ffc56a..f04824c 100644 --- a/ui/events/ozone/events_ozone.cc +++ b/ui/events/ozone/events_ozone.cc @@ -65,6 +65,13 @@ gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& native_event) { return event->offset(); } +base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { + return NULL; +} + +void ReleaseCopiedNativeEvent(const base::NativeEvent& event) { +} + void ClearTouchIdIfReleased(const base::NativeEvent& xev) { } diff --git a/ui/events/win/events_win.cc b/ui/events/win/events_win.cc index 7b559a6..a192af8 100644 --- a/ui/events/win/events_win.cc +++ b/ui/events/win/events_win.cc @@ -279,6 +279,13 @@ gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& native_event) { return gfx::Vector2d(GET_WHEEL_DELTA_WPARAM(native_event.wParam), 0); } +base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { + return event; +} + +void ReleaseCopiedNativeEvent(const base::NativeEvent& event) { +} + void ClearTouchIdIfReleased(const base::NativeEvent& xev) { NOTIMPLEMENTED(); } diff --git a/ui/events/x/events_x.cc b/ui/events/x/events_x.cc index 04fbf56..59e16cd 100644 --- a/ui/events/x/events_x.cc +++ b/ui/events/x/events_x.cc @@ -559,6 +559,18 @@ gfx::Vector2d GetMouseWheelOffset(const base::NativeEvent& native_event) { } } +base::NativeEvent CopyNativeEvent(const base::NativeEvent& event) { + if (!event || event->type == GenericEvent) + return NULL; + XEvent* copy = new XEvent; + *copy = *event; + return copy; +} + +void ReleaseCopiedNativeEvent(const base::NativeEvent& event) { + delete event; +} + void ClearTouchIdIfReleased(const base::NativeEvent& xev) { ui::EventType type = ui::EventTypeFromNative(xev); if (type == ui::ET_TOUCH_CANCELLED || |