Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /* |
| 6 | * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. |
| 7 | * Copyright (C) 2006-2009 Google Inc. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 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. |
| 17 | * |
| 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. |
| 29 | */ |
| 30 | |
Kartar Singh | 74660652 | 2024-05-29 09:34:30 | [diff] [blame] | 31 | #include "components/input/web_input_event_builders_mac.h" |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 32 | |
| 33 | #import <ApplicationServices/ApplicationServices.h> |
| 34 | #import <Cocoa/Cocoa.h> |
| 35 | |
avi | b533f5d | 2015-12-25 03:11:15 | [diff] [blame] | 36 | #include <stdint.h> |
| 37 | |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 38 | #include "base/apple/owned_objc.h" |
James Howard | a07e149 | 2020-06-26 15:34:08 | [diff] [blame] | 39 | #include "base/mac/mac_util.h" |
Annie Sullivan | 14243de | 2020-01-31 21:47:57 | [diff] [blame] | 40 | #include "base/metrics/histogram_macros.h" |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 41 | #include "base/strings/string_util.h" |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 42 | #include "base/time/time.h" |
Dave Tapuska | 129cef8 | 2019-12-19 16:36:48 | [diff] [blame] | 43 | #include "third_party/blink/public/common/input/web_input_event.h" |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 44 | #include "ui/events/base_event_utils.h" |
jonross | 2addcb7 | 2016-08-18 21:48:54 | [diff] [blame] | 45 | #include "ui/events/blink/blink_event_util.h" |
tapted | a3d3da13 | 2016-08-11 06:04:40 | [diff] [blame] | 46 | #import "ui/events/cocoa/cocoa_event_utils.h" |
Annie Sullivan | 14243de | 2020-01-31 21:47:57 | [diff] [blame] | 47 | #include "ui/events/event_utils.h" |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 48 | #include "ui/events/keycodes/keyboard_code_conversion.h" |
| 49 | #include "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 50 | |
Kartar Singh | 5c8e0b2 | 2024-05-30 10:32:14 | [diff] [blame] | 51 | namespace input { |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 52 | |
| 53 | namespace { |
| 54 | |
chongz | c8bec73 | 2015-12-21 21:35:05 | [diff] [blame] | 55 | inline NSString* FilterSpecialCharacter(NSString* str) { |
| 56 | if ([str length] != 1) |
| 57 | return str; |
| 58 | unichar c = [str characterAtIndex:0]; |
| 59 | NSString* result = str; |
| 60 | if (c == 0x7F) { |
| 61 | // Backspace should be 8 |
| 62 | result = @"\x8"; |
| 63 | } else if (c >= 0xF700 && c <= 0xF7FF) { |
| 64 | // Mac private use characters should be @"\0" (@"" won't work) |
| 65 | // NSDeleteFunctionKey will also go into here |
| 66 | // Use the range 0xF700~0xF7FF to match |
| 67 | // http://www.opensource.apple.com/source/WebCore/WebCore-7601.1.55/platform/mac/KeyEventMac.mm |
| 68 | result = @"\0"; |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 73 | inline NSString* TextFromEvent(NSEvent* event) { |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 74 | if ([event type] == NSEventTypeFlagsChanged) |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 75 | return @""; |
chongz | c8bec73 | 2015-12-21 21:35:05 | [diff] [blame] | 76 | return FilterSpecialCharacter([event characters]); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | inline NSString* UnmodifiedTextFromEvent(NSEvent* event) { |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 80 | if ([event type] == NSEventTypeFlagsChanged) |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 81 | return @""; |
chongz | c8bec73 | 2015-12-21 21:35:05 | [diff] [blame] | 82 | return FilterSpecialCharacter([event charactersIgnoringModifiers]); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 83 | } |
| 84 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 85 | // End Apple code. |
| 86 | // ---------------------------------------------------------------------------- |
| 87 | |
| 88 | int ModifiersFromEvent(NSEvent* event) { |
| 89 | int modifiers = 0; |
Jayson Adams | 085b2b41 | 2022-06-23 16:47:27 | [diff] [blame] | 90 | NSEventModifierFlags modifier_flags = [event modifierFlags]; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 91 | |
Jayson Adams | 085b2b41 | 2022-06-23 16:47:27 | [diff] [blame] | 92 | if (modifier_flags & NSEventModifierFlagControl) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 93 | modifiers |= blink::WebInputEvent::kControlKey; |
Jayson Adams | 085b2b41 | 2022-06-23 16:47:27 | [diff] [blame] | 94 | if (modifier_flags & NSEventModifierFlagShift) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 95 | modifiers |= blink::WebInputEvent::kShiftKey; |
Jayson Adams | 085b2b41 | 2022-06-23 16:47:27 | [diff] [blame] | 96 | if (modifier_flags & NSEventModifierFlagOption) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 97 | modifiers |= blink::WebInputEvent::kAltKey; |
Jayson Adams | 085b2b41 | 2022-06-23 16:47:27 | [diff] [blame] | 98 | if (modifier_flags & NSEventModifierFlagCommand) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 99 | modifiers |= blink::WebInputEvent::kMetaKey; |
Jayson Adams | 085b2b41 | 2022-06-23 16:47:27 | [diff] [blame] | 100 | if (modifier_flags & NSEventModifierFlagCapsLock) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 101 | modifiers |= blink::WebInputEvent::kCapsLockOn; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 102 | |
| 103 | // The return value of 1 << 0 corresponds to the left mouse button, |
| 104 | // 1 << 1 corresponds to the right mouse button, |
| 105 | // 1 << n, n >= 2 correspond to other mouse buttons. |
| 106 | NSUInteger pressed_buttons = [NSEvent pressedMouseButtons]; |
| 107 | |
| 108 | if (pressed_buttons & (1 << 0)) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 109 | modifiers |= blink::WebInputEvent::kLeftButtonDown; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 110 | if (pressed_buttons & (1 << 1)) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 111 | modifiers |= blink::WebInputEvent::kRightButtonDown; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 112 | if (pressed_buttons & (1 << 2)) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 113 | modifiers |= blink::WebInputEvent::kMiddleButtonDown; |
Lan Wei | 2bddb8c | 2019-08-09 15:39:26 | [diff] [blame] | 114 | if (pressed_buttons & (1 << 3)) |
| 115 | modifiers |= blink::WebInputEvent::kBackButtonDown; |
| 116 | if (pressed_buttons & (1 << 4)) |
| 117 | modifiers |= blink::WebInputEvent::kForwardButtonDown; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 118 | |
| 119 | return modifiers; |
| 120 | } |
| 121 | |
| 122 | void SetWebEventLocationFromEventInView(blink::WebMouseEvent* result, |
| 123 | NSEvent* event, |
James Howard | a07e149 | 2020-06-26 15:34:08 | [diff] [blame] | 124 | NSView* view, |
| 125 | bool unacceleratedMovement = false) { |
Avi Drissman | ceeb0f2 | 2023-03-17 16:44:44 | [diff] [blame] | 126 | NSPoint screen_local = |
| 127 | [view.window convertPointToScreen:event.locationInWindow]; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 128 | NSScreen* primary_screen = ([[NSScreen screens] count] > 0) |
ccameron | 741debc | 2015-10-07 19:42:18 | [diff] [blame] | 129 | ? [[NSScreen screens] firstObject] |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 130 | : nil; |
Avi Drissman | ceeb0f2 | 2023-03-17 16:44:44 | [diff] [blame] | 131 | |
mustaq | c51f3aab | 2017-04-05 15:43:11 | [diff] [blame] | 132 | // Flip y conditionally. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 133 | result->SetPositionInScreen( |
mustaq | c51f3aab | 2017-04-05 15:43:11 | [diff] [blame] | 134 | screen_local.x, primary_screen |
| 135 | ? [primary_screen frame].size.height - screen_local.y |
| 136 | : screen_local.y); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 137 | |
erikchen | 51f7d244 | 2016-03-11 05:19:17 | [diff] [blame] | 138 | NSPoint content_local = |
| 139 | [view convertPoint:[event locationInWindow] fromView:nil]; |
mustaq | c51f3aab | 2017-04-05 15:43:11 | [diff] [blame] | 140 | // Flip y. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 141 | result->SetPositionInWidget(content_local.x, |
mustaq | c51f3aab | 2017-04-05 15:43:11 | [diff] [blame] | 142 | [view frame].size.height - content_local.y); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 143 | |
James Howard | a07e149 | 2020-06-26 15:34:08 | [diff] [blame] | 144 | CGEventRef cgEvent = nullptr; |
| 145 | if (unacceleratedMovement && (cgEvent = [event CGEvent]) != nullptr) { |
James Howard | a07e149 | 2020-06-26 15:34:08 | [diff] [blame] | 146 | result->movement_x = CGEventGetIntegerValueField( |
| 147 | cgEvent, kCGEventUnacceleratedPointerMovementX); |
| 148 | result->movement_y = CGEventGetIntegerValueField( |
| 149 | cgEvent, kCGEventUnacceleratedPointerMovementY); |
| 150 | result->is_raw_movement_event = true; |
| 151 | } else { |
| 152 | result->movement_x = [event deltaX]; |
| 153 | result->movement_y = [event deltaY]; |
| 154 | } |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | bool IsSystemKeyEvent(const blink::WebKeyboardEvent& event) { |
| 158 | // Windows and Linux set |isSystemKey| if alt is down. Blink looks at this |
| 159 | // flag to decide if it should handle a key or not. E.g. alt-left/right |
| 160 | // shouldn't be used by Blink to scroll the current page, because we want |
| 161 | // to get that key back for it to do history navigation. Hence, the |
| 162 | // corresponding situation on OS X is to set this for cmd key presses. |
joleksy | 914cede | 2015-09-29 19:40:00 | [diff] [blame] | 163 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 164 | // cmd-b and and cmd-i are system wide key bindings that OS X doesn't |
| 165 | // handle for us, so the editor handles them. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 166 | int modifiers = event.GetModifiers() & blink::WebInputEvent::kInputModifiers; |
| 167 | if (modifiers == blink::WebInputEvent::kMetaKey && |
| 168 | event.windows_key_code == ui::VKEY_B) |
joleksy | 914cede | 2015-09-29 19:40:00 | [diff] [blame] | 169 | return false; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 170 | if (modifiers == blink::WebInputEvent::kMetaKey && |
| 171 | event.windows_key_code == ui::VKEY_I) |
joleksy | 914cede | 2015-09-29 19:40:00 | [diff] [blame] | 172 | return false; |
| 173 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 174 | return event.GetModifiers() & blink::WebInputEvent::kMetaKey; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | blink::WebMouseWheelEvent::Phase PhaseForNSEventPhase( |
| 178 | NSEventPhase event_phase) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 179 | uint32_t phase = blink::WebMouseWheelEvent::kPhaseNone; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 180 | if (event_phase & NSEventPhaseBegan) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 181 | phase |= blink::WebMouseWheelEvent::kPhaseBegan; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 182 | if (event_phase & NSEventPhaseStationary) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 183 | phase |= blink::WebMouseWheelEvent::kPhaseStationary; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 184 | if (event_phase & NSEventPhaseChanged) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 185 | phase |= blink::WebMouseWheelEvent::kPhaseChanged; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 186 | if (event_phase & NSEventPhaseEnded) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 187 | phase |= blink::WebMouseWheelEvent::kPhaseEnded; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 188 | if (event_phase & NSEventPhaseCancelled) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 189 | phase |= blink::WebMouseWheelEvent::kPhaseCancelled; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 190 | if (event_phase & NSEventPhaseMayBegin) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 191 | phase |= blink::WebMouseWheelEvent::kPhaseMayBegin; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 192 | return static_cast<blink::WebMouseWheelEvent::Phase>(phase); |
| 193 | } |
| 194 | |
| 195 | blink::WebMouseWheelEvent::Phase PhaseForEvent(NSEvent* event) { |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 196 | NSEventPhase event_phase = [event phase]; |
| 197 | return PhaseForNSEventPhase(event_phase); |
| 198 | } |
| 199 | |
| 200 | blink::WebMouseWheelEvent::Phase MomentumPhaseForEvent(NSEvent* event) { |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 201 | NSEventPhase event_momentum_phase = [event momentumPhase]; |
| 202 | return PhaseForNSEventPhase(event_momentum_phase); |
| 203 | } |
| 204 | |
dtapuska | dee64e4 | 2015-09-24 02:47:07 | [diff] [blame] | 205 | ui::DomKey DomKeyFromEvent(NSEvent* event) { |
| 206 | ui::DomKey key = ui::DomKeyFromNSEvent(event); |
| 207 | if (key != ui::DomKey::NONE) |
| 208 | return key; |
| 209 | return ui::DomKey::UNIDENTIFIED; |
| 210 | } |
| 211 | |
Ella Ge | f2599af | 2017-09-06 13:27:17 | [diff] [blame] | 212 | blink::WebMouseEvent::Button ButtonFromPressedMouseButtons() { |
| 213 | NSUInteger pressed_buttons = [NSEvent pressedMouseButtons]; |
| 214 | |
| 215 | if (pressed_buttons & (1 << 0)) |
| 216 | return blink::WebMouseEvent::Button::kLeft; |
| 217 | if (pressed_buttons & (1 << 1)) |
| 218 | return blink::WebMouseEvent::Button::kRight; |
| 219 | if (pressed_buttons & (1 << 2)) |
| 220 | return blink::WebMouseEvent::Button::kMiddle; |
Lan Wei | 2bddb8c | 2019-08-09 15:39:26 | [diff] [blame] | 221 | if (pressed_buttons & (1 << 3)) |
| 222 | return blink::WebMouseEvent::Button::kBack; |
| 223 | if (pressed_buttons & (1 << 4)) |
| 224 | return blink::WebMouseEvent::Button::kForward; |
| 225 | return blink::WebMouseEvent::Button::kNoButton; |
| 226 | } |
| 227 | blink::WebMouseEvent::Button ButtonFromButtonNumber(NSEvent* event) { |
| 228 | NSUInteger button_number = [event buttonNumber]; |
| 229 | |
| 230 | if (button_number == 1) |
| 231 | return blink::WebMouseEvent::Button::kRight; |
| 232 | if (button_number == 2) |
| 233 | return blink::WebMouseEvent::Button::kMiddle; |
| 234 | if (button_number == 3) |
| 235 | return blink::WebMouseEvent::Button::kBack; |
| 236 | if (button_number == 4) |
| 237 | return blink::WebMouseEvent::Button::kForward; |
Ella Ge | f2599af | 2017-09-06 13:27:17 | [diff] [blame] | 238 | return blink::WebMouseEvent::Button::kNoButton; |
| 239 | } |
| 240 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 241 | } // namespace |
| 242 | |
Joe Mason | 74bec92 | 2022-03-16 09:25:19 | [diff] [blame] | 243 | blink::WebKeyboardEvent WebKeyboardEventBuilder::Build(NSEvent* event) { |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 244 | ui::ComputeEventLatencyOS(base::apple::OwnedNSEvent(event)); |
David Bokan | 87b556b1 | 2020-07-21 19:14:04 | [diff] [blame] | 245 | |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 246 | ui::DomCode dom_code = ui::DomCodeFromNSEvent(event); |
| 247 | int modifiers = |
| 248 | ModifiersFromEvent(event) | ui::DomCodeToWebInputEventModifiers(dom_code); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 249 | |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 250 | if ((event.type != NSEventTypeFlagsChanged) && event.ARepeat) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 251 | modifiers |= blink::WebInputEvent::kIsAutoRepeat; |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 252 | } |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 253 | |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 254 | blink::WebKeyboardEvent result( |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 255 | ui::IsKeyUpEvent(event) ? blink::WebInputEvent::Type::kKeyUp |
| 256 | : blink::WebInputEvent::Type::kRawKeyDown, |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 257 | modifiers, ui::EventTimeStampFromSeconds(event.timestamp)); |
Jayson Adams | e1325a1e | 2022-08-08 17:36:19 | [diff] [blame] | 258 | |
| 259 | // Some keys have the same meaning but different locations on the keyboard: |
| 260 | // the left and right shift keys; the numeric keypad keys and their |
| 261 | // counterparts in the number row; etc. A "located" keyboard code lets us |
| 262 | // distinguish between keys with the same meaning. For example, VKEY_LSHIFT |
| 263 | // and VKEY_RSHIFT are located keyboard codes and VKEY_SHIFT is their non- |
| 264 | // located representation. |
| 265 | // |
| 266 | // When determining the windows_key_code, we want to use the non-located code |
| 267 | // for some keys (Shift, etc.). We call ui::LocatedToNonLocatedKeyboardCode() |
| 268 | // to perform this conversion. However, ui::LocatedToNonLocatedKeyboardCode() |
| 269 | // converts more keys than we'd like. In particular, it returns the |
| 270 | // non-located representations of number pad key codes. If we use these as |
| 271 | // windows key codes, key presses in the number row and the number pad will be |
| 272 | // indistinguishable (see https://crbug.com/1282730). To avoid this, when we |
| 273 | // encounter a number pad key, we'll use the located key_code itself rather |
| 274 | // than its non-located counterpart. |
| 275 | ui::KeyboardCode key_code = ui::KeyboardCodeFromNSEvent(event); |
| 276 | bool is_numeric_keypad_keycode = |
| 277 | key_code >= ui::VKEY_NUMPAD0 && key_code <= ui::VKEY_NUMPAD9; |
| 278 | result.windows_key_code = is_numeric_keypad_keycode |
| 279 | ? key_code |
| 280 | : ui::LocatedToNonLocatedKeyboardCode(key_code); |
| 281 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 282 | result.native_key_code = [event keyCode]; |
| 283 | result.dom_code = static_cast<int>(dom_code); |
| 284 | result.dom_key = DomKeyFromEvent(event); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 285 | NSString* text_str = TextFromEvent(event); |
| 286 | NSString* unmodified_str = UnmodifiedTextFromEvent(event); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 287 | |
| 288 | // Begin Apple code, copied from KeyEventMac.mm |
| 289 | |
| 290 | // Always use 13 for Enter/Return -- we don't want to use AppKit's |
| 291 | // different character for Enter. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 292 | if (result.windows_key_code == '\r') { |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 293 | text_str = @"\r"; |
| 294 | unmodified_str = @"\r"; |
| 295 | } |
| 296 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 297 | // Always use 9 for tab -- we don't want to use AppKit's different character |
| 298 | // for shift-tab. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 299 | if (result.windows_key_code == 9) { |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 300 | text_str = @"\x9"; |
| 301 | unmodified_str = @"\x9"; |
| 302 | } |
| 303 | |
| 304 | // End Apple code. |
| 305 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 306 | if ([text_str length] < blink::WebKeyboardEvent::kTextLengthCap && |
| 307 | [unmodified_str length] < blink::WebKeyboardEvent::kTextLengthCap) { |
Jan Wilken Dörrie | f5b33f9 | 2020-12-17 22:19:20 | [diff] [blame] | 308 | [text_str getCharacters:reinterpret_cast<UniChar*>(&result.text[0])]; |
| 309 | [unmodified_str |
| 310 | getCharacters:reinterpret_cast<UniChar*>(&result.unmodified_text[0])]; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 311 | } else |
| 312 | NOTIMPLEMENTED(); |
| 313 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 314 | result.is_system_key = IsSystemKeyEvent(result); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 315 | |
| 316 | return result; |
| 317 | } |
| 318 | |
| 319 | // WebMouseEvent -------------------------------------------------------------- |
| 320 | |
lanwei | e97eb529 | 2017-02-07 21:59:42 | [diff] [blame] | 321 | blink::WebMouseEvent WebMouseEventBuilder::Build( |
| 322 | NSEvent* event, |
| 323 | NSView* view, |
James Howard | a07e149 | 2020-06-26 15:34:08 | [diff] [blame] | 324 | blink::WebPointerProperties::PointerType pointerType, |
| 325 | bool unacceleratedMovement) { |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 326 | ui::ComputeEventLatencyOS(base::apple::OwnedNSEvent(event)); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 327 | blink::WebInputEvent::Type event_type = |
| 328 | blink::WebInputEvent::Type::kUndefined; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 329 | int click_count = 0; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 330 | blink::WebMouseEvent::Button button = blink::WebMouseEvent::Button::kNoButton; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 331 | |
lanwei | 74a646d | 2016-06-08 19:16:55 | [diff] [blame] | 332 | NSEventType type = [event type]; |
| 333 | switch (type) { |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 334 | case NSEventTypeMouseExited: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 335 | event_type = blink::WebInputEvent::Type::kMouseLeave; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 336 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 337 | case NSEventTypeLeftMouseDown: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 338 | event_type = blink::WebInputEvent::Type::kMouseDown; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 339 | click_count = [event clickCount]; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 340 | button = blink::WebMouseEvent::Button::kLeft; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 341 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 342 | case NSEventTypeOtherMouseDown: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 343 | event_type = blink::WebInputEvent::Type::kMouseDown; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 344 | click_count = [event clickCount]; |
Lan Wei | 2bddb8c | 2019-08-09 15:39:26 | [diff] [blame] | 345 | button = ButtonFromButtonNumber(event); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 346 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 347 | case NSEventTypeRightMouseDown: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 348 | event_type = blink::WebInputEvent::Type::kMouseDown; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 349 | click_count = [event clickCount]; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 350 | button = blink::WebMouseEvent::Button::kRight; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 351 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 352 | case NSEventTypeLeftMouseUp: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 353 | event_type = blink::WebInputEvent::Type::kMouseUp; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 354 | click_count = [event clickCount]; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 355 | button = blink::WebMouseEvent::Button::kLeft; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 356 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 357 | case NSEventTypeOtherMouseUp: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 358 | event_type = blink::WebInputEvent::Type::kMouseUp; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 359 | click_count = [event clickCount]; |
Lan Wei | 2bddb8c | 2019-08-09 15:39:26 | [diff] [blame] | 360 | button = ButtonFromButtonNumber(event); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 361 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 362 | case NSEventTypeRightMouseUp: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 363 | event_type = blink::WebInputEvent::Type::kMouseUp; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 364 | click_count = [event clickCount]; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 365 | button = blink::WebMouseEvent::Button::kRight; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 366 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 367 | case NSEventTypeMouseMoved: |
| 368 | case NSEventTypeMouseEntered: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 369 | event_type = blink::WebInputEvent::Type::kMouseMove; |
Ella Ge | f2599af | 2017-09-06 13:27:17 | [diff] [blame] | 370 | button = ButtonFromPressedMouseButtons(); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 371 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 372 | case NSEventTypeLeftMouseDragged: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 373 | event_type = blink::WebInputEvent::Type::kMouseMove; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 374 | button = blink::WebMouseEvent::Button::kLeft; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 375 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 376 | case NSEventTypeOtherMouseDragged: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 377 | event_type = blink::WebInputEvent::Type::kMouseMove; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 378 | button = blink::WebMouseEvent::Button::kMiddle; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 379 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 380 | case NSEventTypeRightMouseDragged: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 381 | event_type = blink::WebInputEvent::Type::kMouseMove; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 382 | button = blink::WebMouseEvent::Button::kRight; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 383 | break; |
| 384 | default: |
| 385 | NOTIMPLEMENTED(); |
| 386 | } |
| 387 | |
Ella Ge | 6038027 | 2017-08-25 17:59:27 | [diff] [blame] | 388 | // Set id = 0 for all mouse events, disable multi-pen on mac for now. |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 389 | // NSEventTypeMouseExited and NSEventTypeMouseEntered events don't have |
| 390 | // deviceID. Therefore pen exit and enter events can't get correct id. |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 391 | blink::WebMouseEvent result(event_type, ModifiersFromEvent(event), |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 392 | ui::EventTimeStampFromSeconds([event timestamp]), |
| 393 | 0); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 394 | result.click_count = click_count; |
dtapuska | 8c4dae1 | 2017-01-13 00:23:06 | [diff] [blame] | 395 | result.button = button; |
James Howard | a07e149 | 2020-06-26 15:34:08 | [diff] [blame] | 396 | SetWebEventLocationFromEventInView(&result, event, view, |
| 397 | unacceleratedMovement); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 398 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 399 | result.pointer_type = pointerType; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 400 | if ((type == NSEventTypeMouseExited || type == NSEventTypeMouseEntered) || |
| 401 | ([event subtype] != NSEventSubtypeTabletPoint && |
| 402 | [event subtype] != NSEventSubtypeTabletProximity)) { |
lanwei | d0d3d88 | 2016-09-21 20:56:59 | [diff] [blame] | 403 | return result; |
lanwei | 74a646d | 2016-06-08 19:16:55 | [diff] [blame] | 404 | } |
lanwei | d0d3d88 | 2016-09-21 20:56:59 | [diff] [blame] | 405 | |
| 406 | // Set stylus properties for events with a subtype of |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 407 | // NSEventSubtypeTabletPoint. |
lanwei | 45def6b | 2017-03-23 19:01:11 | [diff] [blame] | 408 | NSEventSubtype subtype = [event subtype]; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 409 | if (subtype == NSEventSubtypeTabletPoint) { |
lanwei | d0d3d88 | 2016-09-21 20:56:59 | [diff] [blame] | 410 | result.force = [event pressure]; |
| 411 | NSPoint tilt = [event tilt]; |
Randolf Jung | 52ec474 | 2023-10-11 18:18:01 | [diff] [blame] | 412 | result.tilt_x = tilt.x * 90.0f; |
Liviu Tinta | 12a1ad20 | 2020-08-11 20:33:50 | [diff] [blame] | 413 | // Pointer Events specification states that tiltY is positive when the |
| 414 | // pen is tilted towards the user. |
| 415 | // By default, in MacOS, the Y coordinate increases going up, |
| 416 | // while in Chromium the Y coordinate increases going down. |
| 417 | // https://developer.apple.com/library/archive/documentation/General/Conceptual/Devpedia-CocoaApp/CoordinateSystem.html |
| 418 | // In this case (if the coordinate system is not flipped) tiltY needs to |
| 419 | // be reversed to match Chromium's expectation that tiltY is positive |
| 420 | // towards the user |
Randolf Jung | 52ec474 | 2023-10-11 18:18:01 | [diff] [blame] | 421 | result.tilt_y = ([view isFlipped] ? 1.0 : (-1.0)) * tilt.y * 90.0f; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 422 | result.tangential_pressure = [event tangentialPressure]; |
lanwei | dcc9534 | 2017-01-19 18:28:28 | [diff] [blame] | 423 | // NSEvent spec doesn't specify the range of rotation, we make sure that |
| 424 | // this value is in the range of [0,359]. |
| 425 | int twist = (int)[event rotation]; |
| 426 | twist = twist % 360; |
| 427 | if (twist < 0) |
| 428 | twist += 360; |
| 429 | result.twist = twist; |
lanwei | e97eb529 | 2017-02-07 21:59:42 | [diff] [blame] | 430 | } else { |
| 431 | event_type = [event isEnteringProximity] |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 432 | ? blink::WebInputEvent::Type::kMouseMove |
| 433 | : blink::WebInputEvent::Type::kMouseLeave; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 434 | result.SetType(event_type); |
lanwei | d0d3d88 | 2016-09-21 20:56:59 | [diff] [blame] | 435 | } |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 436 | return result; |
| 437 | } |
| 438 | |
| 439 | // WebMouseWheelEvent --------------------------------------------------------- |
| 440 | |
| 441 | blink::WebMouseWheelEvent WebMouseWheelEventBuilder::Build( |
| 442 | NSEvent* event, |
wjmaclean | 0e5881c | 2016-08-11 00:27:30 | [diff] [blame] | 443 | NSView* view) { |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 444 | ui::ComputeEventLatencyOS(base::apple::OwnedNSEvent(event)); |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 445 | blink::WebMouseWheelEvent result( |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 446 | blink::WebInputEvent::Type::kMouseWheel, ModifiersFromEvent(event), |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 447 | ui::EventTimeStampFromSeconds([event timestamp])); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 448 | result.button = blink::WebMouseEvent::Button::kNoButton; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 449 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 450 | SetWebEventLocationFromEventInView(&result, event, view); |
| 451 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 452 | // Of Mice and Men |
| 453 | // --------------- |
| 454 | // |
| 455 | // There are three types of scroll data available on a scroll wheel CGEvent. |
| 456 | // Apple's documentation ([1]) is rather vague in their differences, and not |
| 457 | // terribly helpful in deciding which to use. This is what's really going on. |
| 458 | // |
| 459 | // First, these events behave very differently depending on whether a standard |
| 460 | // wheel mouse is used (one that scrolls in discrete units) or a |
| 461 | // trackpad/Mighty Mouse is used (which both provide continuous scrolling). |
| 462 | // You must check to see which was used for the event by testing the |
| 463 | // kCGScrollWheelEventIsContinuous field. |
| 464 | // |
| 465 | // Second, these events refer to "axes". Axis 1 is the y-axis, and axis 2 is |
| 466 | // the x-axis. |
| 467 | // |
| 468 | // Third, there is a concept of mouse acceleration. Scrolling the same amount |
| 469 | // of physical distance will give you different results logically depending on |
| 470 | // whether you scrolled a little at a time or in one continuous motion. Some |
| 471 | // fields account for this while others do not. |
| 472 | // |
| 473 | // Fourth, for trackpads there is a concept of chunkiness. When scrolling |
| 474 | // continuously, events can be delivered in chunks. That is to say, lots of |
| 475 | // scroll events with delta 0 will be delivered, and every so often an event |
| 476 | // with a non-zero delta will be delivered, containing the accumulated deltas |
| 477 | // from all the intermediate moves. [2] |
| 478 | // |
| 479 | // For notchy wheel mice (kCGScrollWheelEventIsContinuous == 0) |
| 480 | // ------------------------------------------------------------ |
| 481 | // |
| 482 | // kCGScrollWheelEventDeltaAxis* |
| 483 | // This is the rawest of raw events. For each mouse notch you get a value of |
| 484 | // +1/-1. This does not take acceleration into account and thus is less |
| 485 | // useful for building UIs. |
| 486 | // |
| 487 | // kCGScrollWheelEventPointDeltaAxis* |
| 488 | // This is smarter. In general, for each mouse notch you get a value of |
| 489 | // +1/-1, but this _does_ take acceleration into account, so you will get |
| 490 | // larger values on longer scrolls. This field would be ideal for building |
| 491 | // UIs except for one nasty bug: when the shift key is pressed, this set of |
| 492 | // fields fails to move the value into the axis2 field (the other two types |
| 493 | // of data do). This wouldn't be so bad except for the fact that while the |
| 494 | // number of axes is used in the creation of a CGScrollWheelEvent, there is |
| 495 | // no way to get that information out of the event once created. |
| 496 | // |
| 497 | // kCGScrollWheelEventFixedPtDeltaAxis* |
| 498 | // This is a fixed value, and for each mouse notch you get a value of |
| 499 | // +0.1/-0.1 (but, like above, scaled appropriately for acceleration). This |
| 500 | // value takes acceleration into account, and in fact is identical to the |
| 501 | // results you get from -[NSEvent delta*]. (That is, if you linked on Tiger |
| 502 | // or greater; see [2] for details.) |
| 503 | // |
| 504 | // A note about continuous devices |
| 505 | // ------------------------------- |
| 506 | // |
| 507 | // There are two devices that provide continuous scrolling events (trackpads |
| 508 | // and Mighty Mouses) and they behave rather differently. The Mighty Mouse |
| 509 | // behaves a lot like a regular mouse. There is no chunking, and the |
| 510 | // FixedPtDelta values are the PointDelta values multiplied by 0.1. With the |
| 511 | // trackpad, though, there is chunking. While the FixedPtDelta values are |
| 512 | // reasonable (they occur about every fifth event but have values five times |
| 513 | // larger than usual) the Delta values are unreasonable. They don't appear to |
| 514 | // accumulate properly. |
| 515 | // |
| 516 | // For continuous devices (kCGScrollWheelEventIsContinuous != 0) |
| 517 | // ------------------------------------------------------------- |
| 518 | // |
| 519 | // kCGScrollWheelEventDeltaAxis* |
| 520 | // This provides values with no acceleration. With a trackpad, these values |
| 521 | // are chunked but each non-zero value does not appear to be cumulative. |
| 522 | // This seems to be a bug. |
| 523 | // |
| 524 | // kCGScrollWheelEventPointDeltaAxis* |
| 525 | // This provides values with acceleration. With a trackpad, these values are |
| 526 | // not chunked and are highly accurate. |
| 527 | // |
| 528 | // kCGScrollWheelEventFixedPtDeltaAxis* |
| 529 | // This provides values with acceleration. With a trackpad, these values are |
| 530 | // chunked but unlike Delta events are properly cumulative. |
| 531 | // |
| 532 | // Summary |
| 533 | // ------- |
| 534 | // |
| 535 | // In general the best approach to take is: determine if the event is |
| 536 | // continuous. If it is not, then use the FixedPtDelta events (or just stick |
| 537 | // with Cocoa events). They provide both acceleration and proper horizontal |
| 538 | // scrolling. If the event is continuous, then doing pixel scrolling with the |
| 539 | // PointDelta is the way to go. In general, avoid the Delta events. They're |
| 540 | // the oldest (dating back to 10.4, before CGEvents were public) but they lack |
| 541 | // acceleration and precision, making them useful only in specific edge cases. |
| 542 | // |
| 543 | // References |
| 544 | // ---------- |
| 545 | // |
| 546 | // [1] |
| 547 | // <http://developer.apple.com/documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html> |
| 548 | // [2] <http://developer.apple.com/releasenotes/Cocoa/AppKitOlderNotes.html> |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 549 | // Scroll to the section headed "NSEventTypeScrollWheel events". |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 550 | // |
| 551 | // P.S. The "smooth scrolling" option in the system preferences is utterly |
| 552 | // unrelated to any of this. |
| 553 | |
| 554 | CGEventRef cg_event = [event CGEvent]; |
| 555 | DCHECK(cg_event); |
| 556 | |
| 557 | // Wheel ticks are supposed to be raw, unaccelerated values, one per physical |
| 558 | // mouse wheel notch. The delta event is perfect for this (being a good |
| 559 | // "specific edge case" as mentioned above). Trackpads, unfortunately, do |
| 560 | // event chunking, and sending mousewheel events with 0 ticks causes some |
| 561 | // websites to malfunction. Therefore, for all continuous input devices we use |
| 562 | // the point delta data instead, since we cannot distinguish trackpad data |
| 563 | // from data from any other continuous device. |
| 564 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 565 | if (CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventIsContinuous)) { |
Mohsen Izadi | ffcbc61f1 | 2020-02-09 06:31:27 | [diff] [blame] | 566 | result.delta_units = ui::ScrollGranularity::kScrollByPrecisePixel; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 567 | result.delta_x = CGEventGetIntegerValueField( |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 568 | cg_event, kCGScrollWheelEventPointDeltaAxis2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 569 | result.delta_y = CGEventGetIntegerValueField( |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 570 | cg_event, kCGScrollWheelEventPointDeltaAxis1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 571 | result.wheel_ticks_x = result.delta_x / ui::kScrollbarPixelsPerCocoaTick; |
| 572 | result.wheel_ticks_y = result.delta_y / ui::kScrollbarPixelsPerCocoaTick; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 573 | } else { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 574 | result.delta_x = [event deltaX] * ui::kScrollbarPixelsPerCocoaTick; |
| 575 | result.delta_y = [event deltaY] * ui::kScrollbarPixelsPerCocoaTick; |
| 576 | result.wheel_ticks_y = |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 577 | CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 578 | result.wheel_ticks_x = |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 579 | CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis2); |
| 580 | } |
| 581 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 582 | result.phase = PhaseForEvent(event); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 583 | result.momentum_phase = MomentumPhaseForEvent(event); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 584 | |
| 585 | return result; |
| 586 | } |
| 587 | |
| 588 | blink::WebGestureEvent WebGestureEventBuilder::Build(NSEvent* event, |
| 589 | NSView* view) { |
| 590 | blink::WebGestureEvent result; |
| 591 | |
| 592 | // Use a temporary WebMouseEvent to get the location. |
| 593 | blink::WebMouseEvent temp; |
| 594 | |
| 595 | SetWebEventLocationFromEventInView(&temp, event, view); |
Ella Ge | 1116059d | 2018-03-21 02:06:13 | [diff] [blame] | 596 | result.SetPositionInWidget(temp.PositionInWidget()); |
| 597 | result.SetPositionInScreen(temp.PositionInScreen()); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 598 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 599 | result.SetModifiers(ModifiersFromEvent(event)); |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 600 | result.SetTimeStamp(ui::EventTimeStampFromSeconds([event timestamp])); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 601 | |
Daniel Cheng | 7f9ec90 | 2019-04-18 05:07:00 | [diff] [blame] | 602 | result.SetSourceDevice(blink::WebGestureDevice::kTouchpad); |
Ella Ge | 1116059d | 2018-03-21 02:06:13 | [diff] [blame] | 603 | |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 604 | switch ([event type]) { |
| 605 | case NSEventTypeMagnify: |
Kevin McNee | 297980d | 2018-06-04 21:32:14 | [diff] [blame] | 606 | // We don't need to set the type based on |[event phase]| as the caller |
| 607 | // must set the begin and end types in order to support older Mac |
| 608 | // versions. |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 609 | result.SetType(blink::WebInputEvent::Type::kGesturePinchUpdate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 610 | result.data.pinch_update.scale = [event magnification] + 1.0; |
Kevin McNee | 297980d | 2018-06-04 21:32:14 | [diff] [blame] | 611 | result.SetNeedsWheelEvent(true); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 612 | break; |
| 613 | case NSEventTypeSmartMagnify: |
| 614 | // Map the Cocoa "double-tap with two fingers" zoom gesture to regular |
| 615 | // GestureDoubleTap, because the effect is similar to single-finger |
| 616 | // double-tap zoom on mobile platforms. Note that tapCount is set to 1 |
| 617 | // because the gesture type already encodes that information. |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 618 | result.SetType(blink::WebInputEvent::Type::kGestureDoubleTap); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 619 | result.data.tap.tap_count = 1; |
Kevin McNee | 1a7d3096 | 2018-10-12 15:30:46 | [diff] [blame] | 620 | result.SetNeedsWheelEvent(true); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 621 | break; |
| 622 | case NSEventTypeBeginGesture: |
| 623 | case NSEventTypeEndGesture: |
| 624 | // The specific type of a gesture is not defined when the gesture begin |
| 625 | // and end NSEvents come in. Leave them undefined. The caller will need |
| 626 | // to specify them when the gesture is differentiated. |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 627 | break; |
Erik Chen | f253afd | 2017-11-02 19:46:13 | [diff] [blame] | 628 | case NSEventTypeScrollWheel: |
| 629 | // When building against the 10.11 SDK or later, and running on macOS |
| 630 | // 10.11+, Cocoa no longer sends separate Begin/End gestures for scroll |
| 631 | // events. However, it's convenient to use the same path as the older |
| 632 | // OSes, to avoid logic duplication. We just need to support building a |
| 633 | // dummy WebGestureEvent. |
| 634 | break; |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 635 | default: |
| 636 | NOTIMPLEMENTED(); |
dtapuska | edc082c | 2015-09-17 12:17:01 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | return result; |
| 640 | } |
| 641 | |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 642 | // WebTouchEvent -------------------------------------------------------------- |
| 643 | |
| 644 | blink::WebTouchEvent WebTouchEventBuilder::Build(NSEvent* event, NSView* view) { |
| 645 | blink::WebInputEvent::Type event_type = |
| 646 | blink::WebInputEvent::Type::kUndefined; |
| 647 | NSEventType type = [event type]; |
Dave Tapuska | 22124157 | 2020-04-27 15:59:47 | [diff] [blame] | 648 | blink::WebTouchPoint::State state = |
| 649 | blink::WebTouchPoint::State::kStateUndefined; |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 650 | switch (type) { |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 651 | case NSEventTypeLeftMouseDown: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 652 | event_type = blink::WebInputEvent::Type::kTouchStart; |
Dave Tapuska | 22124157 | 2020-04-27 15:59:47 | [diff] [blame] | 653 | state = blink::WebTouchPoint::State::kStatePressed; |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 654 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 655 | case NSEventTypeLeftMouseUp: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 656 | event_type = blink::WebInputEvent::Type::kTouchEnd; |
Dave Tapuska | 22124157 | 2020-04-27 15:59:47 | [diff] [blame] | 657 | state = blink::WebTouchPoint::State::kStateReleased; |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 658 | break; |
Avi Drissman | 8be8111 | 2022-05-11 01:12:42 | [diff] [blame] | 659 | case NSEventTypeLeftMouseDragged: |
| 660 | case NSEventTypeRightMouseDragged: |
| 661 | case NSEventTypeOtherMouseDragged: |
| 662 | case NSEventTypeMouseMoved: |
| 663 | case NSEventTypeRightMouseDown: |
| 664 | case NSEventTypeOtherMouseDown: |
| 665 | case NSEventTypeRightMouseUp: |
| 666 | case NSEventTypeOtherMouseUp: |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 667 | event_type = blink::WebInputEvent::Type::kTouchMove; |
Dave Tapuska | 22124157 | 2020-04-27 15:59:47 | [diff] [blame] | 668 | state = blink::WebTouchPoint::State::kStateMoved; |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 669 | break; |
| 670 | default: |
Peter Boström | 0af5ffa1 | 2024-11-08 11:47:04 | [diff] [blame] | 671 | NOTREACHED() << "Invalid types for touch events." << type; |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | blink::WebTouchEvent result(event_type, ModifiersFromEvent(event), |
| 675 | ui::EventTimeStampFromSeconds([event timestamp])); |
Avi Drissman | 9306606f | 2023-05-26 22:43:50 | [diff] [blame] | 676 | ui::ComputeEventLatencyOS(base::apple::OwnedNSEvent(event)); |
Dave Tapuska | b5a72eb | 2020-04-21 22:12:56 | [diff] [blame] | 677 | result.hovering = event_type == blink::WebInputEvent::Type::kTouchEnd; |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 678 | result.unique_touch_event_id = ui::GetNextTouchEventId(); |
| 679 | result.touches_length = 1; |
| 680 | |
| 681 | // Use a temporary WebMouseEvent to get the location. |
| 682 | blink::WebMouseEvent temp; |
| 683 | SetWebEventLocationFromEventInView(&temp, event, view); |
| 684 | result.touches[0].SetPositionInWidget(temp.PositionInWidget()); |
| 685 | result.touches[0].SetPositionInScreen(temp.PositionInScreen()); |
| 686 | result.touches[0].movement_x = temp.movement_x; |
| 687 | result.touches[0].movement_y = temp.movement_y; |
| 688 | |
| 689 | result.touches[0].state = state; |
| 690 | result.touches[0].pointer_type = |
| 691 | blink::WebPointerProperties::PointerType::kPen; |
| 692 | result.touches[0].id = [event pointingDeviceID]; |
| 693 | result.touches[0].force = [event pressure]; |
| 694 | NSPoint tilt = [event tilt]; |
Randolf Jung | 52ec474 | 2023-10-11 18:18:01 | [diff] [blame] | 695 | result.touches[0].tilt_x = tilt.x * 90.0f; |
| 696 | result.touches[0].tilt_y = tilt.y * 90.0f; |
Lan Wei | 404244f | 2018-08-01 14:10:22 | [diff] [blame] | 697 | result.touches[0].tangential_pressure = [event tangentialPressure]; |
| 698 | // NSEvent spec doesn't specify the range of rotation, we make sure that |
| 699 | // this value is in the range of [0,359]. |
| 700 | int twist = (int)[event rotation]; |
| 701 | twist = twist % 360; |
| 702 | if (twist < 0) |
| 703 | twist += 360; |
| 704 | result.touches[0].twist = twist; |
| 705 | float rotation_angle = twist % 180; |
| 706 | if (rotation_angle > 90) |
| 707 | rotation_angle = 180.f - rotation_angle; |
| 708 | result.touches[0].rotation_angle = rotation_angle; |
| 709 | return result; |
| 710 | } |
| 711 | |
Kartar Singh | 5c8e0b2 | 2024-05-30 10:32:14 | [diff] [blame] | 712 | } // namespace input |