OLD | NEW |
(Empty) | |
| 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. |
| 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 |
| 31 #include "content/browser/renderer_host/input/web_input_event_builders_mac.h" |
| 32 |
| 33 #import <ApplicationServices/ApplicationServices.h> |
| 34 #import <AvailabilityMacros.h> |
| 35 #import <Cocoa/Cocoa.h> |
| 36 |
| 37 #include "base/mac/sdk_forward_declarations.h" |
| 38 #include "base/strings/string_util.h" |
| 39 #include "content/browser/renderer_host/input/web_input_event_util.h" |
| 40 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 41 #include "ui/events/keycodes/keyboard_code_conversion.h" |
| 42 #include "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| 43 |
| 44 namespace content { |
| 45 |
| 46 namespace { |
| 47 |
| 48 // Return true if the target modifier key is up. OS X has an "official" flag |
| 49 // to test whether either left or right versions of a modifier key are held, |
| 50 // and "unofficial" flags for the left and right versions independently. This |
| 51 // function verifies that |targetKeyMask| and |otherKeyMask| (which should be |
| 52 // the left and right versions of a modifier) are consistent with with the |
| 53 // state of |eitherKeyMask| (which should be the corresponding ""official" |
| 54 // flag). If they are consistent, it tests |targetKeyMask|; otherwise it tests |
| 55 // |eitherKeyMask|. |
| 56 inline bool IsModifierKeyUp(unsigned int flags, |
| 57 unsigned int target_key_mask, |
| 58 unsigned int other_key_mask, |
| 59 unsigned int either_key_mask) { |
| 60 bool either_key_down = (flags & either_key_mask) != 0; |
| 61 bool target_key_down = (flags & target_key_mask) != 0; |
| 62 bool other_key_down = (flags & other_key_mask) != 0; |
| 63 if (either_key_down != (target_key_down || other_key_down)) |
| 64 return !either_key_down; |
| 65 return !target_key_down; |
| 66 } |
| 67 |
| 68 bool IsKeyUpEvent(NSEvent* event) { |
| 69 if ([event type] != NSFlagsChanged) |
| 70 return [event type] == NSKeyUp; |
| 71 |
| 72 // Unofficial bit-masks for left- and right-hand versions of modifier keys. |
| 73 // These values were determined empirically. |
| 74 const unsigned int kLeftControlKeyMask = 1 << 0; |
| 75 const unsigned int kLeftShiftKeyMask = 1 << 1; |
| 76 const unsigned int kRightShiftKeyMask = 1 << 2; |
| 77 const unsigned int kLeftCommandKeyMask = 1 << 3; |
| 78 const unsigned int kRightCommandKeyMask = 1 << 4; |
| 79 const unsigned int kLeftAlternateKeyMask = 1 << 5; |
| 80 const unsigned int kRightAlternateKeyMask = 1 << 6; |
| 81 const unsigned int kRightControlKeyMask = 1 << 13; |
| 82 |
| 83 switch ([event keyCode]) { |
| 84 case 54: // Right Command |
| 85 return IsModifierKeyUp([event modifierFlags], kRightCommandKeyMask, |
| 86 kLeftCommandKeyMask, NSCommandKeyMask); |
| 87 case 55: // Left Command |
| 88 return IsModifierKeyUp([event modifierFlags], kLeftCommandKeyMask, |
| 89 kRightCommandKeyMask, NSCommandKeyMask); |
| 90 |
| 91 case 57: // Capslock |
| 92 return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0; |
| 93 |
| 94 case 56: // Left Shift |
| 95 return IsModifierKeyUp([event modifierFlags], kLeftShiftKeyMask, |
| 96 kRightShiftKeyMask, NSShiftKeyMask); |
| 97 case 60: // Right Shift |
| 98 return IsModifierKeyUp([event modifierFlags], kRightShiftKeyMask, |
| 99 kLeftShiftKeyMask, NSShiftKeyMask); |
| 100 |
| 101 case 58: // Left Alt |
| 102 return IsModifierKeyUp([event modifierFlags], kLeftAlternateKeyMask, |
| 103 kRightAlternateKeyMask, NSAlternateKeyMask); |
| 104 case 61: // Right Alt |
| 105 return IsModifierKeyUp([event modifierFlags], kRightAlternateKeyMask, |
| 106 kLeftAlternateKeyMask, NSAlternateKeyMask); |
| 107 |
| 108 case 59: // Left Ctrl |
| 109 return IsModifierKeyUp([event modifierFlags], kLeftControlKeyMask, |
| 110 kRightControlKeyMask, NSControlKeyMask); |
| 111 case 62: // Right Ctrl |
| 112 return IsModifierKeyUp([event modifierFlags], kRightControlKeyMask, |
| 113 kLeftControlKeyMask, NSControlKeyMask); |
| 114 |
| 115 case 63: // Function |
| 116 return ([event modifierFlags] & NSFunctionKeyMask) == 0; |
| 117 } |
| 118 return false; |
| 119 } |
| 120 |
| 121 inline NSString* TextFromEvent(NSEvent* event) { |
| 122 if ([event type] == NSFlagsChanged) |
| 123 return @""; |
| 124 return [event characters]; |
| 125 } |
| 126 |
| 127 inline NSString* UnmodifiedTextFromEvent(NSEvent* event) { |
| 128 if ([event type] == NSFlagsChanged) |
| 129 return @""; |
| 130 return [event charactersIgnoringModifiers]; |
| 131 } |
| 132 |
| 133 NSString* KeyIdentifierForKeyEvent(NSEvent* event) { |
| 134 if ([event type] == NSFlagsChanged) { |
| 135 switch ([event keyCode]) { |
| 136 case 54: // Right Command |
| 137 case 55: // Left Command |
| 138 return @"Meta"; |
| 139 |
| 140 case 57: // Capslock |
| 141 return @"CapsLock"; |
| 142 |
| 143 case 56: // Left Shift |
| 144 case 60: // Right Shift |
| 145 return @"Shift"; |
| 146 |
| 147 case 58: // Left Alt |
| 148 case 61: // Right Alt |
| 149 return @"Alt"; |
| 150 |
| 151 case 59: // Left Ctrl |
| 152 case 62: // Right Ctrl |
| 153 return @"Control"; |
| 154 |
| 155 // Begin non-Apple addition/modification |
| 156 // -------------------------------------- |
| 157 case 63: // Function |
| 158 return @"Function"; |
| 159 |
| 160 default: // Unknown, but this may be a strange/new keyboard. |
| 161 return @"Unidentified"; |
| 162 // End non-Apple addition/modification |
| 163 // ---------------------------------------- |
| 164 } |
| 165 } |
| 166 |
| 167 NSString* s = [event charactersIgnoringModifiers]; |
| 168 if ([s length] != 1) |
| 169 return @"Unidentified"; |
| 170 |
| 171 unichar c = [s characterAtIndex:0]; |
| 172 switch (c) { |
| 173 // Each identifier listed in the DOM spec is listed here. |
| 174 // Many are simply commented out since they do not appear on standard |
| 175 // Macintosh keyboards |
| 176 // or are on a key that doesn't have a corresponding character. |
| 177 |
| 178 // "Accept" |
| 179 // "AllCandidates" |
| 180 |
| 181 // "Alt" |
| 182 case NSMenuFunctionKey: |
| 183 return @"Alt"; |
| 184 |
| 185 // "Apps" |
| 186 // "BrowserBack" |
| 187 // "BrowserForward" |
| 188 // "BrowserHome" |
| 189 // "BrowserRefresh" |
| 190 // "BrowserSearch" |
| 191 // "BrowserStop" |
| 192 // "CapsLock" |
| 193 |
| 194 // "Clear" |
| 195 case NSClearLineFunctionKey: |
| 196 return @"Clear"; |
| 197 |
| 198 // "CodeInput" |
| 199 // "Compose" |
| 200 // "Control" |
| 201 // "Crsel" |
| 202 // "Convert" |
| 203 // "Copy" |
| 204 // "Cut" |
| 205 |
| 206 // "Down" |
| 207 case NSDownArrowFunctionKey: |
| 208 return @"Down"; |
| 209 // "End" |
| 210 case NSEndFunctionKey: |
| 211 return @"End"; |
| 212 // "Enter" |
| 213 case 0x3: |
| 214 case 0xA: |
| 215 case 0xD: // Macintosh calls the one on the main keyboard Return, but |
| 216 // Windows calls it Enter, so we'll do the same for the DOM |
| 217 return @"Enter"; |
| 218 |
| 219 // "EraseEof" |
| 220 |
| 221 // "Execute" |
| 222 case NSExecuteFunctionKey: |
| 223 return @"Execute"; |
| 224 |
| 225 // "Exsel" |
| 226 |
| 227 // "F1" |
| 228 case NSF1FunctionKey: |
| 229 return @"F1"; |
| 230 // "F2" |
| 231 case NSF2FunctionKey: |
| 232 return @"F2"; |
| 233 // "F3" |
| 234 case NSF3FunctionKey: |
| 235 return @"F3"; |
| 236 // "F4" |
| 237 case NSF4FunctionKey: |
| 238 return @"F4"; |
| 239 // "F5" |
| 240 case NSF5FunctionKey: |
| 241 return @"F5"; |
| 242 // "F6" |
| 243 case NSF6FunctionKey: |
| 244 return @"F6"; |
| 245 // "F7" |
| 246 case NSF7FunctionKey: |
| 247 return @"F7"; |
| 248 // "F8" |
| 249 case NSF8FunctionKey: |
| 250 return @"F8"; |
| 251 // "F9" |
| 252 case NSF9FunctionKey: |
| 253 return @"F9"; |
| 254 // "F10" |
| 255 case NSF10FunctionKey: |
| 256 return @"F10"; |
| 257 // "F11" |
| 258 case NSF11FunctionKey: |
| 259 return @"F11"; |
| 260 // "F12" |
| 261 case NSF12FunctionKey: |
| 262 return @"F12"; |
| 263 // "F13" |
| 264 case NSF13FunctionKey: |
| 265 return @"F13"; |
| 266 // "F14" |
| 267 case NSF14FunctionKey: |
| 268 return @"F14"; |
| 269 // "F15" |
| 270 case NSF15FunctionKey: |
| 271 return @"F15"; |
| 272 // "F16" |
| 273 case NSF16FunctionKey: |
| 274 return @"F16"; |
| 275 // "F17" |
| 276 case NSF17FunctionKey: |
| 277 return @"F17"; |
| 278 // "F18" |
| 279 case NSF18FunctionKey: |
| 280 return @"F18"; |
| 281 // "F19" |
| 282 case NSF19FunctionKey: |
| 283 return @"F19"; |
| 284 // "F20" |
| 285 case NSF20FunctionKey: |
| 286 return @"F20"; |
| 287 // "F21" |
| 288 case NSF21FunctionKey: |
| 289 return @"F21"; |
| 290 // "F22" |
| 291 case NSF22FunctionKey: |
| 292 return @"F22"; |
| 293 // "F23" |
| 294 case NSF23FunctionKey: |
| 295 return @"F23"; |
| 296 // "F24" |
| 297 case NSF24FunctionKey: |
| 298 return @"F24"; |
| 299 |
| 300 // "FinalMode" |
| 301 |
| 302 // "Find" |
| 303 case NSFindFunctionKey: |
| 304 return @"Find"; |
| 305 |
| 306 // "FullWidth" |
| 307 // "HalfWidth" |
| 308 // "HangulMode" |
| 309 // "HanjaMode" |
| 310 |
| 311 // "Help" |
| 312 case NSHelpFunctionKey: |
| 313 return @"Help"; |
| 314 |
| 315 // "Hiragana" |
| 316 |
| 317 // "Home" |
| 318 case NSHomeFunctionKey: |
| 319 return @"Home"; |
| 320 // "Insert" |
| 321 case NSInsertFunctionKey: |
| 322 return @"Insert"; |
| 323 |
| 324 // "JapaneseHiragana" |
| 325 // "JapaneseKatakana" |
| 326 // "JapaneseRomaji" |
| 327 // "JunjaMode" |
| 328 // "KanaMode" |
| 329 // "KanjiMode" |
| 330 // "Katakana" |
| 331 // "LaunchApplication1" |
| 332 // "LaunchApplication2" |
| 333 // "LaunchMail" |
| 334 |
| 335 // "Left" |
| 336 case NSLeftArrowFunctionKey: |
| 337 return @"Left"; |
| 338 |
| 339 // "Meta" |
| 340 // "MediaNextTrack" |
| 341 // "MediaPlayPause" |
| 342 // "MediaPreviousTrack" |
| 343 // "MediaStop" |
| 344 |
| 345 // "ModeChange" |
| 346 case NSModeSwitchFunctionKey: |
| 347 return @"ModeChange"; |
| 348 |
| 349 // "Nonconvert" |
| 350 // "NumLock" |
| 351 |
| 352 // "PageDown" |
| 353 case NSPageDownFunctionKey: |
| 354 return @"PageDown"; |
| 355 // "PageUp" |
| 356 case NSPageUpFunctionKey: |
| 357 return @"PageUp"; |
| 358 |
| 359 // "Paste" |
| 360 |
| 361 // "Pause" |
| 362 case NSPauseFunctionKey: |
| 363 return @"Pause"; |
| 364 |
| 365 // "Play" |
| 366 // "PreviousCandidate" |
| 367 |
| 368 // "PrintScreen" |
| 369 case NSPrintScreenFunctionKey: |
| 370 return @"PrintScreen"; |
| 371 |
| 372 // "Process" |
| 373 // "Props" |
| 374 |
| 375 // "Right" |
| 376 case NSRightArrowFunctionKey: |
| 377 return @"Right"; |
| 378 |
| 379 // "RomanCharacters" |
| 380 |
| 381 // "Scroll" |
| 382 case NSScrollLockFunctionKey: |
| 383 return @"Scroll"; |
| 384 // "Select" |
| 385 case NSSelectFunctionKey: |
| 386 return @"Select"; |
| 387 |
| 388 // "SelectMedia" |
| 389 // "Shift" |
| 390 |
| 391 // "Stop" |
| 392 case NSStopFunctionKey: |
| 393 return @"Stop"; |
| 394 // "Up" |
| 395 case NSUpArrowFunctionKey: |
| 396 return @"Up"; |
| 397 // "Undo" |
| 398 case NSUndoFunctionKey: |
| 399 return @"Undo"; |
| 400 |
| 401 // "VolumeDown" |
| 402 // "VolumeMute" |
| 403 // "VolumeUp" |
| 404 // "Win" |
| 405 // "Zoom" |
| 406 |
| 407 // More function keys, not in the key identifier specification. |
| 408 case NSF25FunctionKey: |
| 409 return @"F25"; |
| 410 case NSF26FunctionKey: |
| 411 return @"F26"; |
| 412 case NSF27FunctionKey: |
| 413 return @"F27"; |
| 414 case NSF28FunctionKey: |
| 415 return @"F28"; |
| 416 case NSF29FunctionKey: |
| 417 return @"F29"; |
| 418 case NSF30FunctionKey: |
| 419 return @"F30"; |
| 420 case NSF31FunctionKey: |
| 421 return @"F31"; |
| 422 case NSF32FunctionKey: |
| 423 return @"F32"; |
| 424 case NSF33FunctionKey: |
| 425 return @"F33"; |
| 426 case NSF34FunctionKey: |
| 427 return @"F34"; |
| 428 case NSF35FunctionKey: |
| 429 return @"F35"; |
| 430 |
| 431 // Turn 0x7F into 0x08, because backspace needs to always be 0x08. |
| 432 case 0x7F: |
| 433 return @"U+0008"; |
| 434 // Standard says that DEL becomes U+007F. |
| 435 case NSDeleteFunctionKey: |
| 436 return @"U+007F"; |
| 437 |
| 438 // Always use 0x09 for tab instead of AppKit's backtab character. |
| 439 case NSBackTabCharacter: |
| 440 return @"U+0009"; |
| 441 |
| 442 case NSBeginFunctionKey: |
| 443 case NSBreakFunctionKey: |
| 444 case NSClearDisplayFunctionKey: |
| 445 case NSDeleteCharFunctionKey: |
| 446 case NSDeleteLineFunctionKey: |
| 447 case NSInsertCharFunctionKey: |
| 448 case NSInsertLineFunctionKey: |
| 449 case NSNextFunctionKey: |
| 450 case NSPrevFunctionKey: |
| 451 case NSPrintFunctionKey: |
| 452 case NSRedoFunctionKey: |
| 453 case NSResetFunctionKey: |
| 454 case NSSysReqFunctionKey: |
| 455 case NSSystemFunctionKey: |
| 456 case NSUserFunctionKey: |
| 457 // FIXME: We should use something other than the vendor-area Unicode values |
| 458 // for the above keys. |
| 459 // For now, just fall through to the default. |
| 460 default: |
| 461 return [NSString stringWithFormat:@"U+%04X", base::ToUpperASCII(c)]; |
| 462 } |
| 463 } |
| 464 |
| 465 // End Apple code. |
| 466 // ---------------------------------------------------------------------------- |
| 467 |
| 468 int ModifiersFromEvent(NSEvent* event) { |
| 469 int modifiers = 0; |
| 470 |
| 471 if ([event modifierFlags] & NSControlKeyMask) |
| 472 modifiers |= blink::WebInputEvent::ControlKey; |
| 473 if ([event modifierFlags] & NSShiftKeyMask) |
| 474 modifiers |= blink::WebInputEvent::ShiftKey; |
| 475 if ([event modifierFlags] & NSAlternateKeyMask) |
| 476 modifiers |= blink::WebInputEvent::AltKey; |
| 477 if ([event modifierFlags] & NSCommandKeyMask) |
| 478 modifiers |= blink::WebInputEvent::MetaKey; |
| 479 if ([event modifierFlags] & NSAlphaShiftKeyMask) |
| 480 modifiers |= blink::WebInputEvent::CapsLockOn; |
| 481 |
| 482 // The return value of 1 << 0 corresponds to the left mouse button, |
| 483 // 1 << 1 corresponds to the right mouse button, |
| 484 // 1 << n, n >= 2 correspond to other mouse buttons. |
| 485 NSUInteger pressed_buttons = [NSEvent pressedMouseButtons]; |
| 486 |
| 487 if (pressed_buttons & (1 << 0)) |
| 488 modifiers |= blink::WebInputEvent::LeftButtonDown; |
| 489 if (pressed_buttons & (1 << 1)) |
| 490 modifiers |= blink::WebInputEvent::RightButtonDown; |
| 491 if (pressed_buttons & (1 << 2)) |
| 492 modifiers |= blink::WebInputEvent::MiddleButtonDown; |
| 493 |
| 494 return modifiers; |
| 495 } |
| 496 |
| 497 void SetWebEventLocationFromEventInView(blink::WebMouseEvent* result, |
| 498 NSEvent* event, |
| 499 NSView* view) { |
| 500 NSPoint window_local = [event locationInWindow]; |
| 501 |
| 502 NSPoint screen_local = [[view window] convertBaseToScreen:window_local]; |
| 503 result->globalX = screen_local.x; |
| 504 // Flip y. |
| 505 NSScreen* primary_screen = ([[NSScreen screens] count] > 0) |
| 506 ? [[NSScreen screens] objectAtIndex:0] |
| 507 : nil; |
| 508 if (primary_screen) |
| 509 result->globalY = [primary_screen frame].size.height - screen_local.y; |
| 510 else |
| 511 result->globalY = screen_local.y; |
| 512 |
| 513 NSPoint content_local = [view convertPoint:window_local fromView:nil]; |
| 514 result->x = content_local.x; |
| 515 result->y = [view frame].size.height - content_local.y; // Flip y. |
| 516 |
| 517 result->windowX = result->x; |
| 518 result->windowY = result->y; |
| 519 |
| 520 result->movementX = [event deltaX]; |
| 521 result->movementY = [event deltaY]; |
| 522 } |
| 523 |
| 524 bool IsSystemKeyEvent(const blink::WebKeyboardEvent& event) { |
| 525 // Windows and Linux set |isSystemKey| if alt is down. Blink looks at this |
| 526 // flag to decide if it should handle a key or not. E.g. alt-left/right |
| 527 // shouldn't be used by Blink to scroll the current page, because we want |
| 528 // to get that key back for it to do history navigation. Hence, the |
| 529 // corresponding situation on OS X is to set this for cmd key presses. |
| 530 // cmd-b and and cmd-i are system wide key bindings that OS X doesn't |
| 531 // handle for us, so the editor handles them. |
| 532 return event.modifiers & blink::WebInputEvent::MetaKey && |
| 533 event.windowsKeyCode != ui::VKEY_B && |
| 534 event.windowsKeyCode != ui::VKEY_I; |
| 535 } |
| 536 |
| 537 blink::WebMouseWheelEvent::Phase PhaseForNSEventPhase( |
| 538 NSEventPhase event_phase) { |
| 539 uint32_t phase = blink::WebMouseWheelEvent::PhaseNone; |
| 540 if (event_phase & NSEventPhaseBegan) |
| 541 phase |= blink::WebMouseWheelEvent::PhaseBegan; |
| 542 if (event_phase & NSEventPhaseStationary) |
| 543 phase |= blink::WebMouseWheelEvent::PhaseStationary; |
| 544 if (event_phase & NSEventPhaseChanged) |
| 545 phase |= blink::WebMouseWheelEvent::PhaseChanged; |
| 546 if (event_phase & NSEventPhaseEnded) |
| 547 phase |= blink::WebMouseWheelEvent::PhaseEnded; |
| 548 if (event_phase & NSEventPhaseCancelled) |
| 549 phase |= blink::WebMouseWheelEvent::PhaseCancelled; |
| 550 if (event_phase & NSEventPhaseMayBegin) |
| 551 phase |= blink::WebMouseWheelEvent::PhaseMayBegin; |
| 552 return static_cast<blink::WebMouseWheelEvent::Phase>(phase); |
| 553 } |
| 554 |
| 555 blink::WebMouseWheelEvent::Phase PhaseForEvent(NSEvent* event) { |
| 556 if (![event respondsToSelector:@selector(phase)]) |
| 557 return blink::WebMouseWheelEvent::PhaseNone; |
| 558 |
| 559 NSEventPhase event_phase = [event phase]; |
| 560 return PhaseForNSEventPhase(event_phase); |
| 561 } |
| 562 |
| 563 blink::WebMouseWheelEvent::Phase MomentumPhaseForEvent(NSEvent* event) { |
| 564 if (![event respondsToSelector:@selector(momentumPhase)]) |
| 565 return blink::WebMouseWheelEvent::PhaseNone; |
| 566 |
| 567 NSEventPhase event_momentum_phase = [event momentumPhase]; |
| 568 return PhaseForNSEventPhase(event_momentum_phase); |
| 569 } |
| 570 |
| 571 } // namespace |
| 572 |
| 573 blink::WebKeyboardEvent WebKeyboardEventBuilder::Build(NSEvent* event) { |
| 574 blink::WebKeyboardEvent result; |
| 575 |
| 576 result.type = IsKeyUpEvent(event) ? blink::WebInputEvent::KeyUp |
| 577 : blink::WebInputEvent::RawKeyDown; |
| 578 |
| 579 result.modifiers = ModifiersFromEvent(event); |
| 580 |
| 581 if (([event type] != NSFlagsChanged) && [event isARepeat]) |
| 582 result.modifiers |= blink::WebInputEvent::IsAutoRepeat; |
| 583 |
| 584 ui::DomCode dom_code = ui::CodeFromNSEvent(event); |
| 585 result.windowsKeyCode = |
| 586 ui::LocatedToNonLocatedKeyboardCode(ui::KeyboardCodeFromNSEvent(event)); |
| 587 result.modifiers |= DomCodeToWebInputEventModifiers(dom_code); |
| 588 result.nativeKeyCode = [event keyCode]; |
| 589 result.domCode = static_cast<int>(dom_code); |
| 590 NSString* text_str = TextFromEvent(event); |
| 591 NSString* unmodified_str = UnmodifiedTextFromEvent(event); |
| 592 NSString* identifier_str = KeyIdentifierForKeyEvent(event); |
| 593 |
| 594 // Begin Apple code, copied from KeyEventMac.mm |
| 595 |
| 596 // Always use 13 for Enter/Return -- we don't want to use AppKit's |
| 597 // different character for Enter. |
| 598 if (result.windowsKeyCode == '\r') { |
| 599 text_str = @"\r"; |
| 600 unmodified_str = @"\r"; |
| 601 } |
| 602 |
| 603 // The adjustments below are only needed in backward compatibility mode, |
| 604 // but we cannot tell what mode we are in from here. |
| 605 |
| 606 // Turn 0x7F into 8, because backspace needs to always be 8. |
| 607 if ([text_str isEqualToString:@"\x7F"]) |
| 608 text_str = @"\x8"; |
| 609 if ([unmodified_str isEqualToString:@"\x7F"]) |
| 610 unmodified_str = @"\x8"; |
| 611 // Always use 9 for tab -- we don't want to use AppKit's different character |
| 612 // for shift-tab. |
| 613 if (result.windowsKeyCode == 9) { |
| 614 text_str = @"\x9"; |
| 615 unmodified_str = @"\x9"; |
| 616 } |
| 617 |
| 618 // End Apple code. |
| 619 |
| 620 if ([text_str length] < blink::WebKeyboardEvent::textLengthCap && |
| 621 [unmodified_str length] < blink::WebKeyboardEvent::textLengthCap) { |
| 622 [text_str getCharacters:&result.text[0]]; |
| 623 [unmodified_str getCharacters:&result.unmodifiedText[0]]; |
| 624 } else |
| 625 NOTIMPLEMENTED(); |
| 626 |
| 627 [identifier_str getCString:&result.keyIdentifier[0] |
| 628 maxLength:sizeof(result.keyIdentifier) |
| 629 encoding:NSASCIIStringEncoding]; |
| 630 |
| 631 result.timeStampSeconds = [event timestamp]; |
| 632 result.isSystemKey = IsSystemKeyEvent(result); |
| 633 |
| 634 return result; |
| 635 } |
| 636 |
| 637 // WebMouseEvent -------------------------------------------------------------- |
| 638 |
| 639 blink::WebMouseEvent WebMouseEventBuilder::Build(NSEvent* event, NSView* view) { |
| 640 blink::WebMouseEvent result; |
| 641 |
| 642 result.clickCount = 0; |
| 643 |
| 644 switch ([event type]) { |
| 645 case NSMouseExited: |
| 646 result.type = blink::WebInputEvent::MouseLeave; |
| 647 result.button = blink::WebMouseEvent::ButtonNone; |
| 648 break; |
| 649 case NSLeftMouseDown: |
| 650 result.type = blink::WebInputEvent::MouseDown; |
| 651 result.clickCount = [event clickCount]; |
| 652 result.button = blink::WebMouseEvent::ButtonLeft; |
| 653 break; |
| 654 case NSOtherMouseDown: |
| 655 result.type = blink::WebInputEvent::MouseDown; |
| 656 result.clickCount = [event clickCount]; |
| 657 result.button = blink::WebMouseEvent::ButtonMiddle; |
| 658 break; |
| 659 case NSRightMouseDown: |
| 660 result.type = blink::WebInputEvent::MouseDown; |
| 661 result.clickCount = [event clickCount]; |
| 662 result.button = blink::WebMouseEvent::ButtonRight; |
| 663 break; |
| 664 case NSLeftMouseUp: |
| 665 result.type = blink::WebInputEvent::MouseUp; |
| 666 result.clickCount = [event clickCount]; |
| 667 result.button = blink::WebMouseEvent::ButtonLeft; |
| 668 break; |
| 669 case NSOtherMouseUp: |
| 670 result.type = blink::WebInputEvent::MouseUp; |
| 671 result.clickCount = [event clickCount]; |
| 672 result.button = blink::WebMouseEvent::ButtonMiddle; |
| 673 break; |
| 674 case NSRightMouseUp: |
| 675 result.type = blink::WebInputEvent::MouseUp; |
| 676 result.clickCount = [event clickCount]; |
| 677 result.button = blink::WebMouseEvent::ButtonRight; |
| 678 break; |
| 679 case NSMouseMoved: |
| 680 case NSMouseEntered: |
| 681 result.type = blink::WebInputEvent::MouseMove; |
| 682 break; |
| 683 case NSLeftMouseDragged: |
| 684 result.type = blink::WebInputEvent::MouseMove; |
| 685 result.button = blink::WebMouseEvent::ButtonLeft; |
| 686 break; |
| 687 case NSOtherMouseDragged: |
| 688 result.type = blink::WebInputEvent::MouseMove; |
| 689 result.button = blink::WebMouseEvent::ButtonMiddle; |
| 690 break; |
| 691 case NSRightMouseDragged: |
| 692 result.type = blink::WebInputEvent::MouseMove; |
| 693 result.button = blink::WebMouseEvent::ButtonRight; |
| 694 break; |
| 695 default: |
| 696 NOTIMPLEMENTED(); |
| 697 } |
| 698 |
| 699 SetWebEventLocationFromEventInView(&result, event, view); |
| 700 |
| 701 result.modifiers = ModifiersFromEvent(event); |
| 702 |
| 703 result.timeStampSeconds = [event timestamp]; |
| 704 |
| 705 return result; |
| 706 } |
| 707 |
| 708 // WebMouseWheelEvent --------------------------------------------------------- |
| 709 |
| 710 blink::WebMouseWheelEvent WebMouseWheelEventBuilder::Build( |
| 711 NSEvent* event, |
| 712 NSView* view, |
| 713 bool can_rubberband_left, |
| 714 bool can_rubberband_right) { |
| 715 blink::WebMouseWheelEvent result; |
| 716 |
| 717 result.type = blink::WebInputEvent::MouseWheel; |
| 718 result.button = blink::WebMouseEvent::ButtonNone; |
| 719 |
| 720 result.modifiers = ModifiersFromEvent(event); |
| 721 |
| 722 SetWebEventLocationFromEventInView(&result, event, view); |
| 723 |
| 724 result.canRubberbandLeft = can_rubberband_left; |
| 725 result.canRubberbandRight = can_rubberband_right; |
| 726 |
| 727 // Of Mice and Men |
| 728 // --------------- |
| 729 // |
| 730 // There are three types of scroll data available on a scroll wheel CGEvent. |
| 731 // Apple's documentation ([1]) is rather vague in their differences, and not |
| 732 // terribly helpful in deciding which to use. This is what's really going on. |
| 733 // |
| 734 // First, these events behave very differently depending on whether a standard |
| 735 // wheel mouse is used (one that scrolls in discrete units) or a |
| 736 // trackpad/Mighty Mouse is used (which both provide continuous scrolling). |
| 737 // You must check to see which was used for the event by testing the |
| 738 // kCGScrollWheelEventIsContinuous field. |
| 739 // |
| 740 // Second, these events refer to "axes". Axis 1 is the y-axis, and axis 2 is |
| 741 // the x-axis. |
| 742 // |
| 743 // Third, there is a concept of mouse acceleration. Scrolling the same amount |
| 744 // of physical distance will give you different results logically depending on |
| 745 // whether you scrolled a little at a time or in one continuous motion. Some |
| 746 // fields account for this while others do not. |
| 747 // |
| 748 // Fourth, for trackpads there is a concept of chunkiness. When scrolling |
| 749 // continuously, events can be delivered in chunks. That is to say, lots of |
| 750 // scroll events with delta 0 will be delivered, and every so often an event |
| 751 // with a non-zero delta will be delivered, containing the accumulated deltas |
| 752 // from all the intermediate moves. [2] |
| 753 // |
| 754 // For notchy wheel mice (kCGScrollWheelEventIsContinuous == 0) |
| 755 // ------------------------------------------------------------ |
| 756 // |
| 757 // kCGScrollWheelEventDeltaAxis* |
| 758 // This is the rawest of raw events. For each mouse notch you get a value of |
| 759 // +1/-1. This does not take acceleration into account and thus is less |
| 760 // useful for building UIs. |
| 761 // |
| 762 // kCGScrollWheelEventPointDeltaAxis* |
| 763 // This is smarter. In general, for each mouse notch you get a value of |
| 764 // +1/-1, but this _does_ take acceleration into account, so you will get |
| 765 // larger values on longer scrolls. This field would be ideal for building |
| 766 // UIs except for one nasty bug: when the shift key is pressed, this set of |
| 767 // fields fails to move the value into the axis2 field (the other two types |
| 768 // of data do). This wouldn't be so bad except for the fact that while the |
| 769 // number of axes is used in the creation of a CGScrollWheelEvent, there is |
| 770 // no way to get that information out of the event once created. |
| 771 // |
| 772 // kCGScrollWheelEventFixedPtDeltaAxis* |
| 773 // This is a fixed value, and for each mouse notch you get a value of |
| 774 // +0.1/-0.1 (but, like above, scaled appropriately for acceleration). This |
| 775 // value takes acceleration into account, and in fact is identical to the |
| 776 // results you get from -[NSEvent delta*]. (That is, if you linked on Tiger |
| 777 // or greater; see [2] for details.) |
| 778 // |
| 779 // A note about continuous devices |
| 780 // ------------------------------- |
| 781 // |
| 782 // There are two devices that provide continuous scrolling events (trackpads |
| 783 // and Mighty Mouses) and they behave rather differently. The Mighty Mouse |
| 784 // behaves a lot like a regular mouse. There is no chunking, and the |
| 785 // FixedPtDelta values are the PointDelta values multiplied by 0.1. With the |
| 786 // trackpad, though, there is chunking. While the FixedPtDelta values are |
| 787 // reasonable (they occur about every fifth event but have values five times |
| 788 // larger than usual) the Delta values are unreasonable. They don't appear to |
| 789 // accumulate properly. |
| 790 // |
| 791 // For continuous devices (kCGScrollWheelEventIsContinuous != 0) |
| 792 // ------------------------------------------------------------- |
| 793 // |
| 794 // kCGScrollWheelEventDeltaAxis* |
| 795 // This provides values with no acceleration. With a trackpad, these values |
| 796 // are chunked but each non-zero value does not appear to be cumulative. |
| 797 // This seems to be a bug. |
| 798 // |
| 799 // kCGScrollWheelEventPointDeltaAxis* |
| 800 // This provides values with acceleration. With a trackpad, these values are |
| 801 // not chunked and are highly accurate. |
| 802 // |
| 803 // kCGScrollWheelEventFixedPtDeltaAxis* |
| 804 // This provides values with acceleration. With a trackpad, these values are |
| 805 // chunked but unlike Delta events are properly cumulative. |
| 806 // |
| 807 // Summary |
| 808 // ------- |
| 809 // |
| 810 // In general the best approach to take is: determine if the event is |
| 811 // continuous. If it is not, then use the FixedPtDelta events (or just stick |
| 812 // with Cocoa events). They provide both acceleration and proper horizontal |
| 813 // scrolling. If the event is continuous, then doing pixel scrolling with the |
| 814 // PointDelta is the way to go. In general, avoid the Delta events. They're |
| 815 // the oldest (dating back to 10.4, before CGEvents were public) but they lack |
| 816 // acceleration and precision, making them useful only in specific edge cases. |
| 817 // |
| 818 // References |
| 819 // ---------- |
| 820 // |
| 821 // [1] |
| 822 // <http://developer.apple.com/documentation/Carbon/Reference/QuartzEventServi
cesRef/Reference/reference.html> |
| 823 // [2] <http://developer.apple.com/releasenotes/Cocoa/AppKitOlderNotes.html> |
| 824 // Scroll to the section headed "NSScrollWheel events". |
| 825 // |
| 826 // P.S. The "smooth scrolling" option in the system preferences is utterly |
| 827 // unrelated to any of this. |
| 828 |
| 829 CGEventRef cg_event = [event CGEvent]; |
| 830 DCHECK(cg_event); |
| 831 |
| 832 // Wheel ticks are supposed to be raw, unaccelerated values, one per physical |
| 833 // mouse wheel notch. The delta event is perfect for this (being a good |
| 834 // "specific edge case" as mentioned above). Trackpads, unfortunately, do |
| 835 // event chunking, and sending mousewheel events with 0 ticks causes some |
| 836 // websites to malfunction. Therefore, for all continuous input devices we use |
| 837 // the point delta data instead, since we cannot distinguish trackpad data |
| 838 // from data from any other continuous device. |
| 839 |
| 840 // Conversion between wheel delta amounts and number of pixels to scroll. |
| 841 static const double kScrollbarPixelsPerCocoaTick = 40.0; |
| 842 |
| 843 if (CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventIsContinuous)) { |
| 844 result.deltaX = CGEventGetIntegerValueField( |
| 845 cg_event, kCGScrollWheelEventPointDeltaAxis2); |
| 846 result.deltaY = CGEventGetIntegerValueField( |
| 847 cg_event, kCGScrollWheelEventPointDeltaAxis1); |
| 848 result.wheelTicksX = result.deltaX / kScrollbarPixelsPerCocoaTick; |
| 849 result.wheelTicksY = result.deltaY / kScrollbarPixelsPerCocoaTick; |
| 850 result.hasPreciseScrollingDeltas = true; |
| 851 } else { |
| 852 result.deltaX = [event deltaX] * kScrollbarPixelsPerCocoaTick; |
| 853 result.deltaY = [event deltaY] * kScrollbarPixelsPerCocoaTick; |
| 854 result.wheelTicksY = |
| 855 CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis1); |
| 856 result.wheelTicksX = |
| 857 CGEventGetIntegerValueField(cg_event, kCGScrollWheelEventDeltaAxis2); |
| 858 } |
| 859 |
| 860 result.timeStampSeconds = [event timestamp]; |
| 861 |
| 862 result.phase = PhaseForEvent(event); |
| 863 result.momentumPhase = MomentumPhaseForEvent(event); |
| 864 |
| 865 return result; |
| 866 } |
| 867 |
| 868 blink::WebGestureEvent WebGestureEventBuilder::Build(NSEvent* event, |
| 869 NSView* view) { |
| 870 blink::WebGestureEvent result; |
| 871 |
| 872 // Use a temporary WebMouseEvent to get the location. |
| 873 blink::WebMouseEvent temp; |
| 874 |
| 875 SetWebEventLocationFromEventInView(&temp, event, view); |
| 876 result.x = temp.x; |
| 877 result.y = temp.y; |
| 878 result.globalX = temp.globalX; |
| 879 result.globalY = temp.globalY; |
| 880 |
| 881 result.modifiers = ModifiersFromEvent(event); |
| 882 result.timeStampSeconds = [event timestamp]; |
| 883 |
| 884 result.sourceDevice = blink::WebGestureDeviceTouchpad; |
| 885 switch ([event type]) { |
| 886 case NSEventTypeMagnify: |
| 887 result.type = blink::WebInputEvent::GesturePinchUpdate; |
| 888 result.data.pinchUpdate.scale = [event magnification] + 1.0; |
| 889 break; |
| 890 case NSEventTypeSmartMagnify: |
| 891 // Map the Cocoa "double-tap with two fingers" zoom gesture to regular |
| 892 // GestureDoubleTap, because the effect is similar to single-finger |
| 893 // double-tap zoom on mobile platforms. Note that tapCount is set to 1 |
| 894 // because the gesture type already encodes that information. |
| 895 result.type = blink::WebInputEvent::GestureDoubleTap; |
| 896 result.data.tap.tapCount = 1; |
| 897 break; |
| 898 case NSEventTypeBeginGesture: |
| 899 case NSEventTypeEndGesture: |
| 900 // The specific type of a gesture is not defined when the gesture begin |
| 901 // and end NSEvents come in. Leave them undefined. The caller will need |
| 902 // to specify them when the gesture is differentiated. |
| 903 result.type = blink::WebInputEvent::Undefined; |
| 904 break; |
| 905 default: |
| 906 NOTIMPLEMENTED(); |
| 907 result.type = blink::WebInputEvent::Undefined; |
| 908 } |
| 909 |
| 910 return result; |
| 911 } |
| 912 |
| 913 } // namespace content |
OLD | NEW |