1 // Copyright 2015 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.
6 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
7 * Copyright (C) 2006-2009 Google Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "content/browser/renderer_host/input/web_input_event_builders_mac.h"
33 #import <ApplicationServices/ApplicationServices.h>
34 #import <Cocoa/Cocoa.h>
36 #include "base/mac/sdk_forward_declarations.h"
37 #include "base/strings/string_util.h"
38 #include "content/browser/renderer_host/input/web_input_event_util.h"
39 #include "third_party/WebKit/public/web/WebInputEvent.h"
40 #include "ui/events/keycodes/keyboard_code_conversion.h"
41 #include "ui/events/keycodes/keyboard_code_conversion_mac.h"
47 // Return true if the target modifier key is up. OS X has an "official" flag
48 // to test whether either left or right versions of a modifier key are held,
49 // and "unofficial" flags for the left and right versions independently. This
50 // function verifies that |target_key_mask| and |otherKeyMask| (which should be
51 // the left and right versions of a modifier) are consistent with with the
52 // state of |eitherKeyMask| (which should be the corresponding ""official"
53 // flag). If they are consistent, it tests |target_key_mask|; otherwise it tests
55 inline bool IsModifierKeyUp(unsigned int flags,
56 unsigned int target_key_mask,
57 unsigned int other_key_mask,
58 unsigned int either_key_mask) {
59 bool either_key_down = (flags & either_key_mask) != 0;
60 bool target_key_down = (flags & target_key_mask) != 0;
61 bool other_key_down = (flags & other_key_mask) != 0;
62 if (either_key_down != (target_key_down || other_key_down))
63 return !either_key_down;
64 return !target_key_down;
67 bool IsKeyUpEvent(NSEvent* event) {
68 if ([event type] != NSFlagsChanged)
69 return [event type] == NSKeyUp;
71 // Unofficial bit-masks for left- and right-hand versions of modifier keys.
72 // These values were determined empirically.
73 const unsigned int kLeftControlKeyMask = 1 << 0;
74 const unsigned int kLeftShiftKeyMask = 1 << 1;
75 const unsigned int kRightShiftKeyMask = 1 << 2;
76 const unsigned int kLeftCommandKeyMask = 1 << 3;
77 const unsigned int kRightCommandKeyMask = 1 << 4;
78 const unsigned int kLeftAlternateKeyMask = 1 << 5;
79 const unsigned int kRightAlternateKeyMask = 1 << 6;
80 const unsigned int kRightControlKeyMask = 1 << 13;
82 switch ([event keyCode]) {
83 case 54: // Right Command
84 return IsModifierKeyUp([event modifierFlags], kRightCommandKeyMask,
85 kLeftCommandKeyMask, NSCommandKeyMask);
86 case 55: // Left Command
87 return IsModifierKeyUp([event modifierFlags], kLeftCommandKeyMask,
88 kRightCommandKeyMask, NSCommandKeyMask);
91 return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
93 case 56: // Left Shift
94 return IsModifierKeyUp([event modifierFlags], kLeftShiftKeyMask,
95 kRightShiftKeyMask, NSShiftKeyMask);
96 case 60: // Right Shift
97 return IsModifierKeyUp([event modifierFlags], kRightShiftKeyMask,
98 kLeftShiftKeyMask, NSShiftKeyMask);
101 return IsModifierKeyUp([event modifierFlags], kLeftAlternateKeyMask,
102 kRightAlternateKeyMask, NSAlternateKeyMask);
103 case 61: // Right Alt
104 return IsModifierKeyUp([event modifierFlags], kRightAlternateKeyMask,
105 kLeftAlternateKeyMask, NSAlternateKeyMask);
107 case 59: // Left Ctrl
108 return IsModifierKeyUp([event modifierFlags], kLeftControlKeyMask,
109 kRightControlKeyMask, NSControlKeyMask);
110 case 62: // Right Ctrl
111 return IsModifierKeyUp([event modifierFlags], kRightControlKeyMask,
112 kLeftControlKeyMask, NSControlKeyMask);
115 return ([event modifierFlags] & NSFunctionKeyMask) == 0;
120 inline NSString* TextFromEvent(NSEvent* event) {
121 if ([event type] == NSFlagsChanged)
123 return [event characters];
126 inline NSString* UnmodifiedTextFromEvent(NSEvent* event) {
127 if ([event type] == NSFlagsChanged)
129 return [event charactersIgnoringModifiers];
132 NSString* KeyIdentifierForKeyEvent(NSEvent* event) {
133 if ([event type] == NSFlagsChanged) {
134 switch ([event keyCode]) {
135 case 54: // Right Command
136 case 55: // Left Command
142 case 56: // Left Shift
143 case 60: // Right Shift
147 case 61: // Right Alt
150 case 59: // Left Ctrl
151 case 62: // Right Ctrl
154 // Begin non-Apple addition/modification
155 // --------------------------------------
159 default: // Unknown, but this may be a strange/new keyboard.
160 return @"Unidentified";
161 // End non-Apple addition/modification
162 // ----------------------------------------
166 NSString* s = [event charactersIgnoringModifiers];
168 return @"Unidentified";
170 unichar c = [s characterAtIndex:0];
172 // Each identifier listed in the DOM spec is listed here.
173 // Many are simply commented out since they do not appear on standard
174 // Macintosh keyboards
175 // or are on a key that doesn't have a corresponding character.
181 case NSMenuFunctionKey:
194 case NSClearLineFunctionKey:
206 case NSDownArrowFunctionKey:
209 case NSEndFunctionKey:
214 case 0xD: // Macintosh calls the one on the main keyboard Return, but
215 // Windows calls it Enter, so we'll do the same for the DOM
221 case NSExecuteFunctionKey:
227 case NSF1FunctionKey:
230 case NSF2FunctionKey:
233 case NSF3FunctionKey:
236 case NSF4FunctionKey:
239 case NSF5FunctionKey:
242 case NSF6FunctionKey:
245 case NSF7FunctionKey:
248 case NSF8FunctionKey:
251 case NSF9FunctionKey:
254 case NSF10FunctionKey:
257 case NSF11FunctionKey:
260 case NSF12FunctionKey:
263 case NSF13FunctionKey:
266 case NSF14FunctionKey:
269 case NSF15FunctionKey:
272 case NSF16FunctionKey:
275 case NSF17FunctionKey:
278 case NSF18FunctionKey:
281 case NSF19FunctionKey:
284 case NSF20FunctionKey:
287 case NSF21FunctionKey:
290 case NSF22FunctionKey:
293 case NSF23FunctionKey:
296 case NSF24FunctionKey:
302 case NSFindFunctionKey:
311 case NSHelpFunctionKey:
317 case NSHomeFunctionKey:
320 case NSInsertFunctionKey:
323 // "JapaneseHiragana"
324 // "JapaneseKatakana"
330 // "LaunchApplication1"
331 // "LaunchApplication2"
335 case NSLeftArrowFunctionKey:
341 // "MediaPreviousTrack"
345 case NSModeSwitchFunctionKey:
346 return @"ModeChange";
352 case NSPageDownFunctionKey:
355 case NSPageUpFunctionKey:
361 case NSPauseFunctionKey:
365 // "PreviousCandidate"
368 case NSPrintScreenFunctionKey:
369 return @"PrintScreen";
375 case NSRightArrowFunctionKey:
381 case NSScrollLockFunctionKey:
384 case NSSelectFunctionKey:
391 case NSStopFunctionKey:
394 case NSUpArrowFunctionKey:
397 case NSUndoFunctionKey:
406 // More function keys, not in the key identifier specification.
407 case NSF25FunctionKey:
409 case NSF26FunctionKey:
411 case NSF27FunctionKey:
413 case NSF28FunctionKey:
415 case NSF29FunctionKey:
417 case NSF30FunctionKey:
419 case NSF31FunctionKey:
421 case NSF32FunctionKey:
423 case NSF33FunctionKey:
425 case NSF34FunctionKey:
427 case NSF35FunctionKey:
430 // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
433 // Standard says that DEL becomes U+007F.
434 case NSDeleteFunctionKey:
437 // Always use 0x09 for tab instead of AppKit's backtab character.
438 case NSBackTabCharacter:
441 case NSBeginFunctionKey:
442 case NSBreakFunctionKey:
443 case NSClearDisplayFunctionKey:
444 case NSDeleteCharFunctionKey:
445 case NSDeleteLineFunctionKey:
446 case NSInsertCharFunctionKey:
447 case NSInsertLineFunctionKey:
448 case NSNextFunctionKey:
449 case NSPrevFunctionKey:
450 case NSPrintFunctionKey:
451 case NSRedoFunctionKey:
452 case NSResetFunctionKey:
453 case NSSysReqFunctionKey:
454 case NSSystemFunctionKey:
455 case NSUserFunctionKey:
456 // FIXME: We should use something other than the vendor-area Unicode values
457 // for the above keys.
458 // For now, just fall through to the default.
460 return [NSString stringWithFormat:@"U+%04X", base::ToUpperASCII(c)];
465 // ----------------------------------------------------------------------------
467 int ModifiersFromEvent(NSEvent* event) {
470 if ([event modifierFlags] & NSControlKeyMask)
471 modifiers |= blink::WebInputEvent::ControlKey;
472 if ([event modifierFlags] & NSShiftKeyMask)
473 modifiers |= blink::WebInputEvent::ShiftKey;
474 if ([event modifierFlags] & NSAlternateKeyMask)
475 modifiers |= blink::WebInputEvent::AltKey;
476 if ([event modifierFlags] & NSCommandKeyMask)
477 modifiers |= blink::WebInputEvent::MetaKey;
478 if ([event modifierFlags] & NSAlphaShiftKeyMask)
479 modifiers |= blink::WebInputEvent::CapsLockOn;
481 // The return value of 1 << 0 corresponds to the left mouse button,
482 // 1 << 1 corresponds to the right mouse button,
483 // 1 << n, n >= 2 correspond to other mouse buttons.
484 NSUInteger pressed_buttons = [NSEvent pressedMouseButtons];
486 if (pressed_buttons & (1 << 0))
487 modifiers |= blink::WebInputEvent::LeftButtonDown;
488 if (pressed_buttons & (1 << 1))
489 modifiers |= blink::WebInputEvent::RightButtonDown;
490 if (pressed_buttons & (1 << 2))
491 modifiers |= blink::WebInputEvent::MiddleButtonDown;
496 void SetWebEventLocationFromEventInView(blink::WebMouseEvent* result,
499 NSPoint window_local = [event locationInWindow];
501 NSPoint screen_local = [[view window] convertBaseToScreen:window_local];
502 result->globalX = screen_local.x;
504 NSScreen* primary_screen = ([[NSScreen screens] count] > 0)
505 ? [[NSScreen screens] objectAtIndex:0]
508 result->globalY = [primary_screen frame].size.height - screen_local.y;
510 result->globalY = screen_local.y;
512 NSPoint content_local = [view convertPoint:window_local fromView:nil];
513 result->x = content_local.x;
514 result->y = [view frame].size.height - content_local.y; // Flip y.
516 result->windowX = result->x;
517 result->windowY = result->y;
519 result->movementX = [event deltaX];
520 result->movementY = [event deltaY];
523 bool IsSystemKeyEvent(const blink::WebKeyboardEvent& event) {
524 // Windows and Linux set |isSystemKey| if alt is down. Blink looks at this
525 // flag to decide if it should handle a key or not. E.g. alt-left/right
526 // shouldn't be used by Blink to scroll the current page, because we want
527 // to get that key back for it to do history navigation. Hence, the
528 // corresponding situation on OS X is to set this for cmd key presses.
529 // cmd-b and and cmd-i are system wide key bindings that OS X doesn't
530 // handle for us, so the editor handles them.
531 return event.modifiers & blink::WebInputEvent::MetaKey &&
532 event.windowsKeyCode != ui::VKEY_B &&
533 event.windowsKeyCode != ui::VKEY_I;
536 blink::WebMouseWheelEvent::Phase PhaseForNSEventPhase(
537 NSEventPhase event_phase) {
538 uint32_t phase = blink::WebMouseWheelEvent::PhaseNone;
539 if (event_phase & NSEventPhaseBegan)
540 phase |= blink::WebMouseWheelEvent::PhaseBegan;
541 if (event_phase & NSEventPhaseStationary)
542 phase |= blink::WebMouseWheelEvent::PhaseStationary;
543 if (event_phase & NSEventPhaseChanged)
544 phase |= blink::WebMouseWheelEvent::PhaseChanged;
545 if (event_phase & NSEventPhaseEnded)
546 phase |= blink::WebMouseWheelEvent::PhaseEnded;
547 if (event_phase & NSEventPhaseCancelled)
548 phase |= blink::WebMouseWheelEvent::PhaseCancelled;
549 if (event_phase & NSEventPhaseMayBegin)
550 phase |= blink::WebMouseWheelEvent::PhaseMayBegin;
551 return static_cast<blink::WebMouseWheelEvent::Phase>(phase);
554 blink::WebMouseWheelEvent::Phase PhaseForEvent(NSEvent* event) {
555 if (![event respondsToSelector:@selector(phase)])
556 return blink::WebMouseWheelEvent::PhaseNone;
558 NSEventPhase event_phase = [event phase];
559 return PhaseForNSEventPhase(event_phase);
562 blink::WebMouseWheelEvent::Phase MomentumPhaseForEvent(NSEvent* event) {
563 if (![event respondsToSelector:@selector(momentumPhase)])
564 return blink::WebMouseWheelEvent::PhaseNone;
566 NSEventPhase event_momentum_phase = [event momentumPhase];
567 return PhaseForNSEventPhase(event_momentum_phase);
572 blink::WebKeyboardEvent WebKeyboardEventBuilder::Build(NSEvent* event) {
573 blink::WebKeyboardEvent result;
575 result.type = IsKeyUpEvent(event) ? blink::WebInputEvent::KeyUp
576 : blink::WebInputEvent::RawKeyDown;
578 result.modifiers = ModifiersFromEvent(event);
580 if (([event type] != NSFlagsChanged) && [event isARepeat])
581 result.modifiers |= blink::WebInputEvent::IsAutoRepeat;
583 ui::DomCode dom_code = ui::CodeFromNSEvent(event);
584 result.windowsKeyCode =
585 ui::LocatedToNonLocatedKeyboardCode(ui::KeyboardCodeFromNSEvent(event));
586 result.modifiers |= DomCodeToWebInputEventModifiers(dom_code);
587 result.nativeKeyCode = [event keyCode];
588 result.domCode = static_cast<int>(dom_code);
589 NSString* text_str = TextFromEvent(event);
590 NSString* unmodified_str = UnmodifiedTextFromEvent(event);
591 NSString* identifier_str = KeyIdentifierForKeyEvent(event);
593 // Begin Apple code, copied from KeyEventMac.mm
595 // Always use 13 for Enter/Return -- we don't want to use AppKit's
596 // different character for Enter.
597 if (result.windowsKeyCode == '\r') {
599 unmodified_str = @"\r";
602 // The adjustments below are only needed in backward compatibility mode,
603 // but we cannot tell what mode we are in from here.
605 // Turn 0x7F into 8, because backspace needs to always be 8.
606 if ([text_str isEqualToString:@"\x7F"])
608 if ([unmodified_str isEqualToString:@"\x7F"])
609 unmodified_str = @"\x8";
610 // Always use 9 for tab -- we don't want to use AppKit's different character
612 if (result.windowsKeyCode == 9) {
614 unmodified_str = @"\x9";
619 if ([text_str length] < blink::WebKeyboardEvent::textLengthCap &&
620 [unmodified_str length] < blink::WebKeyboardEvent::textLengthCap) {
621 [text_str getCharacters:&result.text[0]];
622 [unmodified_str getCharacters:&result.unmodifiedText[0]];
626 [identifier_str getCString:&result.keyIdentifier[0]
627 maxLength:sizeof(result.keyIdentifier)
628 encoding:NSASCIIStringEncoding];
630 result.timeStampSeconds = [event timestamp];
631 result.isSystemKey = IsSystemKeyEvent(result);
636 // WebMouseEvent --------------------------------------------------------------
638 blink::WebMouseEvent WebMouseEventBuilder::Build(NSEvent* event, NSView* view) {
639 blink::WebMouseEvent result;
641 result.clickCount = 0;
643 switch ([event type]) {
645 result.type = blink::WebInputEvent::MouseLeave;
646 result.button = blink::WebMouseEvent::ButtonNone;
648 case NSLeftMouseDown:
649 result.type = blink::WebInputEvent::MouseDown;
650 result.clickCount = [event clickCount];
651 result.button = blink::WebMouseEvent::ButtonLeft;
653 case NSOtherMouseDown:
654 result.type = blink::WebInputEvent::MouseDown;
655 result.clickCount = [event clickCount];
656 result.button = blink::WebMouseEvent::ButtonMiddle;
658 case NSRightMouseDown:
659 result.type = blink::WebInputEvent::MouseDown;
660 result.clickCount = [event clickCount];
661 result.button = blink::WebMouseEvent::ButtonRight;
664 result.type = blink::WebInputEvent::MouseUp;
665 result.clickCount = [event clickCount];
666 result.button = blink::WebMouseEvent::ButtonLeft;
669 result.type = blink::WebInputEvent::MouseUp;
670 result.clickCount = [event clickCount];
671 result.button = blink::WebMouseEvent::ButtonMiddle;
674 result.type = blink::WebInputEvent::MouseUp;
675 result.clickCount = [event clickCount];
676 result.button = blink::WebMouseEvent::ButtonRight;
680 result.type = blink::WebInputEvent::MouseMove;
682 case NSLeftMouseDragged:
683 result.type = blink::WebInputEvent::MouseMove;
684 result.button = blink::WebMouseEvent::ButtonLeft;
686 case NSOtherMouseDragged:
687 result.type = blink::WebInputEvent::MouseMove;
688 result.button = blink::WebMouseEvent::ButtonMiddle;
690 case NSRightMouseDragged:
691 result.type = blink::WebInputEvent::MouseMove;
692 result.button = blink::WebMouseEvent::ButtonRight;
698 SetWebEventLocationFromEventInView(&result, event, view);
700 result.modifiers = ModifiersFromEvent(event);
702 result.timeStampSeconds = [event timestamp];
707 // WebMouseWheelEvent ---------------------------------------------------------
709 blink::WebMouseWheelEvent WebMouseWheelEventBuilder::Build(
712 bool can_rubberband_left,
713 bool can_rubberband_right) {
714 blink::WebMouseWheelEvent result;
716 result.type = blink::WebInputEvent::MouseWheel;
717 result.button = blink::WebMouseEvent::ButtonNone;
719 result.modifiers = ModifiersFromEvent(event);
721 SetWebEventLocationFromEventInView(&result, event, view);
723 result.canRubberbandLeft = can_rubberband_left;
724 result.canRubberbandRight = can_rubberband_right;
729 // There are three types of scroll data available on a scroll wheel CGEvent.
730 // Apple's documentation ([1]) is rather vague in their differences, and not
731 // terribly helpful in deciding which to use. This is what's really going on.
733 // First, these events behave very differently depending on whether a standard
734 // wheel mouse is used (one that scrolls in discrete units) or a
735 // trackpad/Mighty Mouse is used (which both provide continuous scrolling).
736 // You must check to see which was used for the event by testing the
737 // kCGScrollWheelEventIsContinuous field.
739 // Second, these events refer to "axes". Axis 1 is the y-axis, and axis 2 is
742 // Third, there is a concept of mouse acceleration. Scrolling the same amount
743 // of physical distance will give you different results logically depending on
744 // whether you scrolled a little at a time or in one continuous motion. Some
745 // fields account for this while others do not.
747 // Fourth, for trackpads there is a concept of chunkiness. When scrolling
748 // continuously, events can be delivered in chunks. That is to say, lots of
749 // scroll events with delta 0 will be delivered, and every so often an event
750 // with a non-zero delta will be delivered, containing the accumulated deltas
751 // from all the intermediate moves. [2]
753 // For notchy wheel mice (kCGScrollWheelEventIsContinuous == 0)
754 // ------------------------------------------------------------
756 // kCGScrollWheelEventDeltaAxis*
757 // This is the rawest of raw events. For each mouse notch you get a value of
758 // +1/-1. This does not take acceleration into account and thus is less
759 // useful for building UIs.
761 // kCGScrollWheelEventPointDeltaAxis*
762 // This is smarter. In general, for each mouse notch you get a value of
763 // +1/-1, but this _does_ take acceleration into account, so you will get
764 // larger values on longer scrolls. This field would be ideal for building
765 // UIs except for one nasty bug: when the shift key is pressed, this set of
766 // fields fails to move the value into the axis2 field (the other two types
767 // of data do). This wouldn't be so bad except for the fact that while the
768 // number of axes is used in the creation of a CGScrollWheelEvent, there is
769 // no way to get that information out of the event once created.
771 // kCGScrollWheelEventFixedPtDeltaAxis*
772 // This is a fixed value, and for each mouse notch you get a value of
773 // +0.1/-0.1 (but, like above, scaled appropriately for acceleration). This
774 // value takes acceleration into account, and in fact is identical to the
775 // results you get from -[NSEvent delta*]. (That is, if you linked on Tiger
776 // or greater; see [2] for details.)
778 // A note about continuous devices
779 // -------------------------------
781 // There are two devices that provide continuous scrolling events (trackpads
782 // and Mighty Mouses) and they behave rather differently. The Mighty Mouse
783 // behaves a lot like a regular mouse. There is no chunking, and the
784 // FixedPtDelta values are the PointDelta values multiplied by 0.1. With the
785 // trackpad, though, there is chunking. While the FixedPtDelta values are
786 // reasonable (they occur about every fifth event but have values five times
787 // larger than usual) the Delta values are unreasonable. They don't appear to
788 // accumulate properly.
790 // For continuous devices (kCGScrollWheelEventIsContinuous != 0)
791 // -------------------------------------------------------------
793 // kCGScrollWheelEventDeltaAxis*
794 // This provides values with no acceleration. With a trackpad, these values
795 // are chunked but each non-zero value does not appear to be cumulative.
796 // This seems to be a bug.
798 // kCGScrollWheelEventPointDeltaAxis*
799 // This provides values with acceleration. With a trackpad, these values are
800 // not chunked and are highly accurate.
802 // kCGScrollWheelEventFixedPtDeltaAxis*
803 // This provides values with acceleration. With a trackpad, these values are
804 // chunked but unlike Delta events are properly cumulative.
809 // In general the best approach to take is: determine if the event is
810 // continuous. If it is not, then use the FixedPtDelta events (or just stick
811 // with Cocoa events). They provide both acceleration and proper horizontal
812 // scrolling. If the event is continuous, then doing pixel scrolling with the
813 // PointDelta is the way to go. In general, avoid the Delta events. They're
814 // the oldest (dating back to 10.4, before CGEvents were public) but they lack
815 // acceleration and precision, making them useful only in specific edge cases.
821 // <http://developer.apple.com/documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html>
822 // [2] <http://developer.apple.com/releasenotes/Cocoa/AppKitOlderNotes.html>
823 // Scroll to the section headed "NSScrollWheel events".
825 // P.S. The "smooth scrolling" option in the system preferences is utterly
826 // unrelated to any of this.
828 CGEventRef cg_event = [event CGEvent];
831 // Wheel ticks are supposed to be raw, unaccelerated values, one per physical
832 // mouse wheel notch. The delta event is perfect for this (being a good
833 // "specific edge case" as mentioned above). Trackpads, unfortunately, do
834 // event chunking, and sending mousewheel events with 0 ticks causes some
835 // websites to malfunction. Therefore, for all continuous input devices we use
836 // the point delta data instead, since we cannot distinguish trackpad data
837 // from data from any other continuous device.
839 // Conversion between wheel delta amounts and number of pixels to scroll.
840 static const double kScrollbarPixelsPerCocoaTick = 40.0;
842 if (CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventIsContinuous)) {
843 result.deltaX = CGEventGetIntegerValueField(
844 cg_event, kCGScrollWheelEventPointDeltaAxis2);
845 result.deltaY = CGEventGetIntegerValueField(
846 cg_event, kCGScrollWheelEventPointDeltaAxis1);
847 result.wheelTicksX = result.deltaX / kScrollbarPixelsPerCocoaTick;
848 result.wheelTicksY = result.deltaY / kScrollbarPixelsPerCocoaTick;
849 result.hasPreciseScrollingDeltas = true;
851 result.deltaX = [event deltaX] * kScrollbarPixelsPerCocoaTick;
852 result.deltaY = [event deltaY] * kScrollbarPixelsPerCocoaTick;
854 CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis1);
856 CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis2);
859 result.timeStampSeconds = [event timestamp];
861 result.phase = PhaseForEvent(event);
862 result.momentumPhase = MomentumPhaseForEvent(event);
867 blink::WebGestureEvent WebGestureEventBuilder::Build(NSEvent* event,
869 blink::WebGestureEvent result;
871 // Use a temporary WebMouseEvent to get the location.
872 blink::WebMouseEvent temp;
874 SetWebEventLocationFromEventInView(&temp, event, view);
877 result.globalX = temp.globalX;
878 result.globalY = temp.globalY;
880 result.modifiers = ModifiersFromEvent(event);
881 result.timeStampSeconds = [event timestamp];
883 result.sourceDevice = blink::WebGestureDeviceTouchpad;
884 switch ([event type]) {
885 case NSEventTypeMagnify:
886 result.type = blink::WebInputEvent::GesturePinchUpdate;
887 result.data.pinchUpdate.scale = [event magnification] + 1.0;
889 case NSEventTypeSmartMagnify:
890 // Map the Cocoa "double-tap with two fingers" zoom gesture to regular
891 // GestureDoubleTap, because the effect is similar to single-finger
892 // double-tap zoom on mobile platforms. Note that tapCount is set to 1
893 // because the gesture type already encodes that information.
894 result.type = blink::WebInputEvent::GestureDoubleTap;
895 result.data.tap.tapCount = 1;
897 case NSEventTypeBeginGesture:
898 case NSEventTypeEndGesture:
899 // The specific type of a gesture is not defined when the gesture begin
900 // and end NSEvents come in. Leave them undefined. The caller will need
901 // to specify them when the gesture is differentiated.
902 result.type = blink::WebInputEvent::Undefined;
906 result.type = blink::WebInputEvent::Undefined;
912 } // namespace content