1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "WebEventFactory.h"
28
29#import "WebKitSystemInterface.h"
30#import <wtf/ASCIICType.h>
31#import <WebCore/Scrollbar.h>
32
33using namespace WebCore;
34
35namespace WebKit {
36
37static WebMouseEvent::Button currentMouseButton()
38{
39    NSUInteger pressedMouseButtons = [NSEvent pressedMouseButtons];
40    if (!pressedMouseButtons)
41        return WebMouseEvent::NoButton;
42    if (pressedMouseButtons == 1 << 0)
43        return WebMouseEvent::LeftButton;
44    if (pressedMouseButtons == 1 << 1)
45        return WebMouseEvent::RightButton;
46    return WebMouseEvent::MiddleButton;
47}
48
49static WebMouseEvent::Button mouseButtonForEvent(NSEvent *event)
50{
51    switch ([event type]) {
52        case NSLeftMouseDown:
53        case NSLeftMouseUp:
54        case NSLeftMouseDragged:
55            return WebMouseEvent::LeftButton;
56        case NSRightMouseDown:
57        case NSRightMouseUp:
58        case NSRightMouseDragged:
59            return WebMouseEvent::RightButton;
60        case NSOtherMouseDown:
61        case NSOtherMouseUp:
62        case NSOtherMouseDragged:
63            return WebMouseEvent::MiddleButton;
64        case NSMouseEntered:
65        case NSMouseExited:
66            return currentMouseButton();
67        default:
68            return WebMouseEvent::NoButton;
69    }
70}
71
72static WebEvent::Type mouseEventTypeForEvent(NSEvent* event)
73{
74    switch ([event type]) {
75        case NSLeftMouseDragged:
76        case NSMouseEntered:
77        case NSMouseExited:
78        case NSMouseMoved:
79        case NSOtherMouseDragged:
80        case NSRightMouseDragged:
81            return WebEvent::MouseMove;
82        case NSLeftMouseDown:
83        case NSRightMouseDown:
84        case NSOtherMouseDown:
85            return WebEvent::MouseDown;
86        case NSLeftMouseUp:
87        case NSRightMouseUp:
88        case NSOtherMouseUp:
89            return WebEvent::MouseUp;
90        default:
91            return WebEvent::MouseMove;
92    }
93}
94
95static int clickCountForEvent(NSEvent *event)
96{
97    switch ([event type]) {
98        case NSLeftMouseDown:
99        case NSLeftMouseUp:
100        case NSLeftMouseDragged:
101        case NSRightMouseDown:
102        case NSRightMouseUp:
103        case NSRightMouseDragged:
104        case NSOtherMouseDown:
105        case NSOtherMouseUp:
106        case NSOtherMouseDragged:
107            return [event clickCount];
108        default:
109            return 0;
110    }
111}
112
113static NSScreen *screenForWindow(NSWindow *window)
114{
115    NSScreen *screen = [window screen]; // nil if the window is off-screen
116    if (screen)
117        return screen;
118
119    NSArray *screens = [NSScreen screens];
120    if ([screens count] > 0)
121        return [screens objectAtIndex:0]; // screen containing the menubar
122
123    return nil;
124}
125
126static NSPoint flipScreenPoint(const NSPoint& screenPoint, NSScreen *screen)
127{
128    NSPoint flippedPoint = screenPoint;
129    flippedPoint.y = NSMaxY([screen frame]) - flippedPoint.y;
130    return flippedPoint;
131}
132
133static NSPoint globalPoint(const NSPoint& windowPoint, NSWindow *window)
134{
135    return flipScreenPoint([window convertBaseToScreen:windowPoint], screenForWindow(window));
136}
137
138static NSPoint globalPointForEvent(NSEvent *event)
139{
140    switch ([event type]) {
141        case NSLeftMouseDown:
142        case NSLeftMouseDragged:
143        case NSLeftMouseUp:
144        case NSMouseEntered:
145        case NSMouseExited:
146        case NSMouseMoved:
147        case NSOtherMouseDown:
148        case NSOtherMouseDragged:
149        case NSOtherMouseUp:
150        case NSRightMouseDown:
151        case NSRightMouseDragged:
152        case NSRightMouseUp:
153        case NSScrollWheel:
154            return globalPoint([event locationInWindow], [event window]);
155        default:
156            return NSZeroPoint;
157    }
158}
159
160static NSPoint pointForEvent(NSEvent *event, NSView *windowView)
161{
162    switch ([event type]) {
163        case NSLeftMouseDown:
164        case NSLeftMouseDragged:
165        case NSLeftMouseUp:
166        case NSMouseEntered:
167        case NSMouseExited:
168        case NSMouseMoved:
169        case NSOtherMouseDown:
170        case NSOtherMouseDragged:
171        case NSOtherMouseUp:
172        case NSRightMouseDown:
173        case NSRightMouseDragged:
174        case NSRightMouseUp:
175        case NSScrollWheel: {
176            // Note: This will have its origin at the bottom left of the window unless windowView is flipped.
177            // In those cases, the Y coordinate gets flipped by Widget::convertFromContainingWindow.
178            NSPoint location = [event locationInWindow];
179            if (windowView)
180                location = [windowView convertPoint:location fromView:nil];
181            return location;
182        }
183        default:
184            return NSZeroPoint;
185    }
186}
187
188static WebWheelEvent::Phase phaseForEvent(NSEvent *event)
189{
190#if !defined(BUILDING_ON_SNOW_LEOPARD)
191    uint32_t phase = WebWheelEvent::PhaseNone;
192    if ([event phase] & NSEventPhaseBegan)
193        phase |= WebWheelEvent::PhaseBegan;
194    if ([event phase] & NSEventPhaseStationary)
195        phase |= WebWheelEvent::PhaseStationary;
196    if ([event phase] & NSEventPhaseChanged)
197        phase |= WebWheelEvent::PhaseChanged;
198    if ([event phase] & NSEventPhaseEnded)
199        phase |= WebWheelEvent::PhaseEnded;
200    if ([event phase] & NSEventPhaseCancelled)
201        phase |= WebWheelEvent::PhaseCancelled;
202    return static_cast<WebWheelEvent::Phase>(phase);
203#else
204    return WebWheelEvent::PhaseNone;
205#endif
206}
207
208static WebWheelEvent::Phase momentumPhaseForEvent(NSEvent *event)
209{
210    uint32_t phase = WebWheelEvent::PhaseNone;
211
212#if !defined(BUILDING_ON_SNOW_LEOPARD)
213    if ([event momentumPhase] & NSEventPhaseBegan)
214        phase |= WebWheelEvent::PhaseBegan;
215    if ([event momentumPhase] & NSEventPhaseStationary)
216        phase |= WebWheelEvent::PhaseStationary;
217    if ([event momentumPhase] & NSEventPhaseChanged)
218        phase |= WebWheelEvent::PhaseChanged;
219    if ([event momentumPhase] & NSEventPhaseEnded)
220        phase |= WebWheelEvent::PhaseEnded;
221    if ([event momentumPhase] & NSEventPhaseCancelled)
222        phase |= WebWheelEvent::PhaseCancelled;
223#else
224    switch (WKGetNSEventMomentumPhase(event)) {
225    case WKEventPhaseNone:
226        phase = WebWheelEvent::PhaseNone;
227        break;
228    case WKEventPhaseBegan:
229        phase = WebWheelEvent::PhaseBegan;
230        break;
231    case WKEventPhaseChanged:
232        phase = WebWheelEvent::PhaseChanged;
233        break;
234    case WKEventPhaseEnded:
235        phase = WebWheelEvent::PhaseEnded;
236        break;
237    }
238#endif
239
240    return static_cast<WebWheelEvent::Phase>(phase);
241}
242
243#if ENABLE(GESTURE_EVENTS)
244static WebEvent::Type gestureEventTypeForEvent(NSEvent *event)
245{
246    switch ([event type]) {
247    case NSEventTypeBeginGesture:
248        return WebEvent::GestureScrollBegin;
249    case NSEventTypeEndGesture:
250        return WebEvent::GestureScrollEnd;
251    default:
252        ASSERT_NOT_REACHED();
253        return WebEvent::GestureScrollEnd;
254    }
255}
256#endif
257
258static inline String textFromEvent(NSEvent* event)
259{
260    if ([event type] == NSFlagsChanged)
261        return String("");
262    return String([event characters]);
263}
264
265static inline String unmodifiedTextFromEvent(NSEvent* event)
266{
267    if ([event type] == NSFlagsChanged)
268        return String("");
269    return String([event charactersIgnoringModifiers]);
270}
271
272static String keyIdentifierForKeyEvent(NSEvent* event)
273{
274    if ([event type] == NSFlagsChanged)
275        switch ([event keyCode]) {
276            case 54: // Right Command
277            case 55: // Left Command
278                return String("Meta");
279
280            case 57: // Capslock
281                return String("CapsLock");
282
283            case 56: // Left Shift
284            case 60: // Right Shift
285                return String("Shift");
286
287            case 58: // Left Alt
288            case 61: // Right Alt
289                return String("Alt");
290
291            case 59: // Left Ctrl
292            case 62: // Right Ctrl
293                return String("Control");
294
295            default:
296                ASSERT_NOT_REACHED();
297                return String("");
298        }
299
300    NSString *s = [event charactersIgnoringModifiers];
301    if ([s length] != 1)
302        return String("Unidentified");
303
304    unichar c = [s characterAtIndex:0];
305    switch (c) {
306        // Each identifier listed in the DOM spec is listed here.
307        // Many are simply commented out since they do not appear on standard Macintosh keyboards
308        // or are on a key that doesn't have a corresponding character.
309
310        // "Accept"
311        // "AllCandidates"
312
313        // "Alt"
314        case NSMenuFunctionKey:
315            return String("Alt");
316
317        // "Apps"
318        // "BrowserBack"
319        // "BrowserForward"
320        // "BrowserHome"
321        // "BrowserRefresh"
322        // "BrowserSearch"
323        // "BrowserStop"
324        // "CapsLock"
325
326        // "Clear"
327        case NSClearLineFunctionKey:
328            return String("Clear");
329
330        // "CodeInput"
331        // "Compose"
332        // "Control"
333        // "Crsel"
334        // "Convert"
335        // "Copy"
336        // "Cut"
337
338        // "Down"
339        case NSDownArrowFunctionKey:
340            return String("Down");
341        // "End"
342        case NSEndFunctionKey:
343            return String("End");
344        // "Enter"
345        case 0x3: case 0xA: case 0xD: // Macintosh calls the one on the main keyboard Return, but Windows calls it Enter, so we'll do the same for the DOM
346            return String("Enter");
347
348        // "EraseEof"
349
350        // "Execute"
351        case NSExecuteFunctionKey:
352            return String("Execute");
353
354        // "Exsel"
355
356        // "F1"
357        case NSF1FunctionKey:
358            return String("F1");
359        // "F2"
360        case NSF2FunctionKey:
361            return String("F2");
362        // "F3"
363        case NSF3FunctionKey:
364            return String("F3");
365        // "F4"
366        case NSF4FunctionKey:
367            return String("F4");
368        // "F5"
369        case NSF5FunctionKey:
370            return String("F5");
371        // "F6"
372        case NSF6FunctionKey:
373            return String("F6");
374        // "F7"
375        case NSF7FunctionKey:
376            return String("F7");
377        // "F8"
378        case NSF8FunctionKey:
379            return String("F8");
380        // "F9"
381        case NSF9FunctionKey:
382            return String("F9");
383        // "F10"
384        case NSF10FunctionKey:
385            return String("F10");
386        // "F11"
387        case NSF11FunctionKey:
388            return String("F11");
389        // "F12"
390        case NSF12FunctionKey:
391            return String("F12");
392        // "F13"
393        case NSF13FunctionKey:
394            return String("F13");
395        // "F14"
396        case NSF14FunctionKey:
397            return String("F14");
398        // "F15"
399        case NSF15FunctionKey:
400            return String("F15");
401        // "F16"
402        case NSF16FunctionKey:
403            return String("F16");
404        // "F17"
405        case NSF17FunctionKey:
406            return String("F17");
407        // "F18"
408        case NSF18FunctionKey:
409            return String("F18");
410        // "F19"
411        case NSF19FunctionKey:
412            return String("F19");
413        // "F20"
414        case NSF20FunctionKey:
415            return String("F20");
416        // "F21"
417        case NSF21FunctionKey:
418            return String("F21");
419        // "F22"
420        case NSF22FunctionKey:
421            return String("F22");
422        // "F23"
423        case NSF23FunctionKey:
424            return String("F23");
425        // "F24"
426        case NSF24FunctionKey:
427            return String("F24");
428
429        // "FinalMode"
430
431        // "Find"
432        case NSFindFunctionKey:
433            return String("Find");
434
435        // "FullWidth"
436        // "HalfWidth"
437        // "HangulMode"
438        // "HanjaMode"
439
440        // "Help"
441        case NSHelpFunctionKey:
442            return String("Help");
443
444        // "Hiragana"
445
446        // "Home"
447        case NSHomeFunctionKey:
448            return String("Home");
449        // "Insert"
450        case NSInsertFunctionKey:
451            return String("Insert");
452
453        // "JapaneseHiragana"
454        // "JapaneseKatakana"
455        // "JapaneseRomaji"
456        // "JunjaMode"
457        // "KanaMode"
458        // "KanjiMode"
459        // "Katakana"
460        // "LaunchApplication1"
461        // "LaunchApplication2"
462        // "LaunchMail"
463
464        // "Left"
465        case NSLeftArrowFunctionKey:
466            return String("Left");
467
468        // "Meta"
469        // "MediaNextTrack"
470        // "MediaPlayPause"
471        // "MediaPreviousTrack"
472        // "MediaStop"
473
474        // "ModeChange"
475        case NSModeSwitchFunctionKey:
476            return String("ModeChange");
477
478        // "Nonconvert"
479        // "NumLock"
480
481        // "PageDown"
482        case NSPageDownFunctionKey:
483            return String("PageDown");
484        // "PageUp"
485        case NSPageUpFunctionKey:
486            return String("PageUp");
487
488        // "Paste"
489
490        // "Pause"
491        case NSPauseFunctionKey:
492            return String("Pause");
493
494        // "Play"
495        // "PreviousCandidate"
496
497        // "PrintScreen"
498        case NSPrintScreenFunctionKey:
499            return String("PrintScreen");
500
501        // "Process"
502        // "Props"
503
504        // "Right"
505        case NSRightArrowFunctionKey:
506            return String("Right");
507
508        // "RomanCharacters"
509
510        // "Scroll"
511        case NSScrollLockFunctionKey:
512            return String("Scroll");
513        // "Select"
514        case NSSelectFunctionKey:
515            return String("Select");
516
517        // "SelectMedia"
518        // "Shift"
519
520        // "Stop"
521        case NSStopFunctionKey:
522            return String("Stop");
523        // "Up"
524        case NSUpArrowFunctionKey:
525            return String("Up");
526        // "Undo"
527        case NSUndoFunctionKey:
528            return String("Undo");
529
530        // "VolumeDown"
531        // "VolumeMute"
532        // "VolumeUp"
533        // "Win"
534        // "Zoom"
535
536        // More function keys, not in the key identifier specification.
537        case NSF25FunctionKey:
538            return String("F25");
539        case NSF26FunctionKey:
540            return String("F26");
541        case NSF27FunctionKey:
542            return String("F27");
543        case NSF28FunctionKey:
544            return String("F28");
545        case NSF29FunctionKey:
546            return String("F29");
547        case NSF30FunctionKey:
548            return String("F30");
549        case NSF31FunctionKey:
550            return String("F31");
551        case NSF32FunctionKey:
552            return String("F32");
553        case NSF33FunctionKey:
554            return String("F33");
555        case NSF34FunctionKey:
556            return String("F34");
557        case NSF35FunctionKey:
558            return String("F35");
559
560        // Turn 0x7F into 0x08, because backspace needs to always be 0x08.
561        case 0x7F:
562            return String("U+0008");
563        // Standard says that DEL becomes U+007F.
564        case NSDeleteFunctionKey:
565            return String("U+007F");
566
567        // Always use 0x09 for tab instead of AppKit's backtab character.
568        case NSBackTabCharacter:
569            return String("U+0009");
570
571        case NSBeginFunctionKey:
572        case NSBreakFunctionKey:
573        case NSClearDisplayFunctionKey:
574        case NSDeleteCharFunctionKey:
575        case NSDeleteLineFunctionKey:
576        case NSInsertCharFunctionKey:
577        case NSInsertLineFunctionKey:
578        case NSNextFunctionKey:
579        case NSPrevFunctionKey:
580        case NSPrintFunctionKey:
581        case NSRedoFunctionKey:
582        case NSResetFunctionKey:
583        case NSSysReqFunctionKey:
584        case NSSystemFunctionKey:
585        case NSUserFunctionKey:
586            // FIXME: We should use something other than the vendor-area Unicode values for the above keys.
587            // For now, just fall through to the default.
588        default:
589            return String::format("U+%04X", toASCIIUpper(c));
590    }
591}
592
593static bool isKeypadEvent(NSEvent* event)
594{
595    // Check that this is the type of event that has a keyCode.
596    switch ([event type]) {
597        case NSKeyDown:
598        case NSKeyUp:
599        case NSFlagsChanged:
600            break;
601        default:
602            return false;
603    }
604
605    switch ([event keyCode]) {
606        case 71: // Clear
607        case 81: // =
608        case 75: // /
609        case 67: // *
610        case 78: // -
611        case 69: // +
612        case 76: // Enter
613        case 65: // .
614        case 82: // 0
615        case 83: // 1
616        case 84: // 2
617        case 85: // 3
618        case 86: // 4
619        case 87: // 5
620        case 88: // 6
621        case 89: // 7
622        case 91: // 8
623        case 92: // 9
624            return true;
625     }
626
627     return false;
628}
629
630static int windowsKeyCodeForKeyEvent(NSEvent* event)
631{
632    switch ([event keyCode]) {
633        // VK_TAB (09) TAB key
634        case 48: return 0x09;
635
636        // VK_APPS (5D) Right windows/meta key
637        case 54: // Right Command
638            return 0x5D;
639
640        // VK_LWIN (5B) Left windows/meta key
641        case 55: // Left Command
642            return 0x5B;
643
644        // VK_CAPITAL (14) caps locks key
645        case 57: // Capslock
646            return 0x14;
647
648        // VK_SHIFT (10) either shift key
649        case 56: // Left Shift
650        case 60: // Right Shift
651            return 0x10;
652
653        // VK_MENU (12) either alt key
654        case 58: // Left Alt
655        case 61: // Right Alt
656            return 0x12;
657
658        // VK_CONTROL (11) either ctrl key
659        case 59: // Left Ctrl
660        case 62: // Right Ctrl
661            return 0x11;
662
663        // VK_CLEAR (0C) CLEAR key
664        case 71: return 0x0C;
665
666        // VK_NUMPAD0 (60) Numeric keypad 0 key
667        case 82: return 0x60;
668        // VK_NUMPAD1 (61) Numeric keypad 1 key
669        case 83: return 0x61;
670        // VK_NUMPAD2 (62) Numeric keypad 2 key
671        case 84: return 0x62;
672        // VK_NUMPAD3 (63) Numeric keypad 3 key
673        case 85: return 0x63;
674        // VK_NUMPAD4 (64) Numeric keypad 4 key
675        case 86: return 0x64;
676        // VK_NUMPAD5 (65) Numeric keypad 5 key
677        case 87: return 0x65;
678        // VK_NUMPAD6 (66) Numeric keypad 6 key
679        case 88: return 0x66;
680        // VK_NUMPAD7 (67) Numeric keypad 7 key
681        case 89: return 0x67;
682        // VK_NUMPAD8 (68) Numeric keypad 8 key
683        case 91: return 0x68;
684        // VK_NUMPAD9 (69) Numeric keypad 9 key
685        case 92: return 0x69;
686        // VK_MULTIPLY (6A) Multiply key
687        case 67: return 0x6A;
688        // VK_ADD (6B) Add key
689        case 69: return 0x6B;
690
691        // VK_SUBTRACT (6D) Subtract key
692        case 78: return 0x6D;
693        // VK_DECIMAL (6E) Decimal key
694        case 65: return 0x6E;
695        // VK_DIVIDE (6F) Divide key
696        case 75: return 0x6F;
697     }
698
699    NSString* s = [event charactersIgnoringModifiers];
700    if ([s length] != 1)
701        return 0;
702
703    switch ([s characterAtIndex:0]) {
704        // VK_LBUTTON (01) Left mouse button
705        // VK_RBUTTON (02) Right mouse button
706        // VK_CANCEL (03) Control-break processing
707        // VK_MBUTTON (04) Middle mouse button (three-button mouse)
708        // VK_XBUTTON1 (05)
709        // VK_XBUTTON2 (06)
710
711        // VK_BACK (08) BACKSPACE key
712        case 8: case 0x7F: return 0x08;
713        // VK_TAB (09) TAB key
714        case 9: return 0x09;
715
716        // VK_CLEAR (0C) CLEAR key
717        // handled by key code above
718
719        // VK_RETURN (0D)
720        case 0xD: case 3: return 0x0D;
721
722        // VK_SHIFT (10) SHIFT key
723        // VK_CONTROL (11) CTRL key
724        // VK_MENU (12) ALT key
725
726        // VK_PAUSE (13) PAUSE key
727        case NSPauseFunctionKey: return 0x13;
728
729        // VK_CAPITAL (14) CAPS LOCK key
730        // VK_KANA (15) Input Method Editor (IME) Kana mode
731        // VK_HANGUEL (15) IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
732        // VK_HANGUL (15) IME Hangul mode
733        // VK_JUNJA (17) IME Junja mode
734        // VK_FINAL (18) IME final mode
735        // VK_HANJA (19) IME Hanja mode
736        // VK_KANJI (19) IME Kanji mode
737
738        // VK_ESCAPE (1B) ESC key
739        case 0x1B: return 0x1B;
740
741        // VK_CONVERT (1C) IME convert
742        // VK_NONCONVERT (1D) IME nonconvert
743        // VK_ACCEPT (1E) IME accept
744        // VK_MODECHANGE (1F) IME mode change request
745
746        // VK_SPACE (20) SPACEBAR
747        case ' ': return 0x20;
748        // VK_PRIOR (21) PAGE UP key
749        case NSPageUpFunctionKey: return 0x21;
750        // VK_NEXT (22) PAGE DOWN key
751        case NSPageDownFunctionKey: return 0x22;
752        // VK_END (23) END key
753        case NSEndFunctionKey: return 0x23;
754        // VK_HOME (24) HOME key
755        case NSHomeFunctionKey: return 0x24;
756        // VK_LEFT (25) LEFT ARROW key
757        case NSLeftArrowFunctionKey: return 0x25;
758        // VK_UP (26) UP ARROW key
759        case NSUpArrowFunctionKey: return 0x26;
760        // VK_RIGHT (27) RIGHT ARROW key
761        case NSRightArrowFunctionKey: return 0x27;
762        // VK_DOWN (28) DOWN ARROW key
763        case NSDownArrowFunctionKey: return 0x28;
764        // VK_SELECT (29) SELECT key
765        case NSSelectFunctionKey: return 0x29;
766        // VK_PRINT (2A) PRINT key
767        case NSPrintFunctionKey: return 0x2A;
768        // VK_EXECUTE (2B) EXECUTE key
769        case NSExecuteFunctionKey: return 0x2B;
770        // VK_SNAPSHOT (2C) PRINT SCREEN key
771        case NSPrintScreenFunctionKey: return 0x2C;
772        // VK_INSERT (2D) INS key
773        case NSInsertFunctionKey: case NSHelpFunctionKey: return 0x2D;
774        // VK_DELETE (2E) DEL key
775        case NSDeleteFunctionKey: return 0x2E;
776
777        // VK_HELP (2F) HELP key
778
779        //  (30) 0 key
780        case '0': case ')': return 0x30;
781        //  (31) 1 key
782        case '1': case '!': return 0x31;
783        //  (32) 2 key
784        case '2': case '@': return 0x32;
785        //  (33) 3 key
786        case '3': case '#': return 0x33;
787        //  (34) 4 key
788        case '4': case '$': return 0x34;
789        //  (35) 5 key
790        case '5': case '%': return 0x35;
791        //  (36) 6 key
792        case '6': case '^': return 0x36;
793        //  (37) 7 key
794        case '7': case '&': return 0x37;
795        //  (38) 8 key
796        case '8': case '*': return 0x38;
797        //  (39) 9 key
798        case '9': case '(': return 0x39;
799        //  (41) A key
800        case 'a': case 'A': return 0x41;
801        //  (42) B key
802        case 'b': case 'B': return 0x42;
803        //  (43) C key
804        case 'c': case 'C': return 0x43;
805        //  (44) D key
806        case 'd': case 'D': return 0x44;
807        //  (45) E key
808        case 'e': case 'E': return 0x45;
809        //  (46) F key
810        case 'f': case 'F': return 0x46;
811        //  (47) G key
812        case 'g': case 'G': return 0x47;
813        //  (48) H key
814        case 'h': case 'H': return 0x48;
815        //  (49) I key
816        case 'i': case 'I': return 0x49;
817        //  (4A) J key
818        case 'j': case 'J': return 0x4A;
819        //  (4B) K key
820        case 'k': case 'K': return 0x4B;
821        //  (4C) L key
822        case 'l': case 'L': return 0x4C;
823        //  (4D) M key
824        case 'm': case 'M': return 0x4D;
825        //  (4E) N key
826        case 'n': case 'N': return 0x4E;
827        //  (4F) O key
828        case 'o': case 'O': return 0x4F;
829        //  (50) P key
830        case 'p': case 'P': return 0x50;
831        //  (51) Q key
832        case 'q': case 'Q': return 0x51;
833        //  (52) R key
834        case 'r': case 'R': return 0x52;
835        //  (53) S key
836        case 's': case 'S': return 0x53;
837        //  (54) T key
838        case 't': case 'T': return 0x54;
839        //  (55) U key
840        case 'u': case 'U': return 0x55;
841        //  (56) V key
842        case 'v': case 'V': return 0x56;
843        //  (57) W key
844        case 'w': case 'W': return 0x57;
845        //  (58) X key
846        case 'x': case 'X': return 0x58;
847        //  (59) Y key
848        case 'y': case 'Y': return 0x59;
849        //  (5A) Z key
850        case 'z': case 'Z': return 0x5A;
851
852        // VK_LWIN (5B) Left Windows key (Microsoft Natural keyboard)
853        // VK_RWIN (5C) Right Windows key (Natural keyboard)
854        // VK_APPS (5D) Applications key (Natural keyboard)
855        // VK_SLEEP (5F) Computer Sleep key
856
857        // VK_NUMPAD0 (60) Numeric keypad 0 key
858        // VK_NUMPAD1 (61) Numeric keypad 1 key
859        // VK_NUMPAD2 (62) Numeric keypad 2 key
860        // VK_NUMPAD3 (63) Numeric keypad 3 key
861        // VK_NUMPAD4 (64) Numeric keypad 4 key
862        // VK_NUMPAD5 (65) Numeric keypad 5 key
863        // VK_NUMPAD6 (66) Numeric keypad 6 key
864        // VK_NUMPAD7 (67) Numeric keypad 7 key
865        // VK_NUMPAD8 (68) Numeric keypad 8 key
866        // VK_NUMPAD9 (69) Numeric keypad 9 key
867        // VK_MULTIPLY (6A) Multiply key
868        // VK_ADD (6B) Add key
869        // handled by key code above
870
871        // VK_SEPARATOR (6C) Separator key
872
873        // VK_SUBTRACT (6D) Subtract key
874        // VK_DECIMAL (6E) Decimal key
875        // VK_DIVIDE (6F) Divide key
876        // handled by key code above
877
878        // VK_F1 (70) F1 key
879        case NSF1FunctionKey: return 0x70;
880        // VK_F2 (71) F2 key
881        case NSF2FunctionKey: return 0x71;
882        // VK_F3 (72) F3 key
883        case NSF3FunctionKey: return 0x72;
884        // VK_F4 (73) F4 key
885        case NSF4FunctionKey: return 0x73;
886        // VK_F5 (74) F5 key
887        case NSF5FunctionKey: return 0x74;
888        // VK_F6 (75) F6 key
889        case NSF6FunctionKey: return 0x75;
890        // VK_F7 (76) F7 key
891        case NSF7FunctionKey: return 0x76;
892        // VK_F8 (77) F8 key
893        case NSF8FunctionKey: return 0x77;
894        // VK_F9 (78) F9 key
895        case NSF9FunctionKey: return 0x78;
896        // VK_F10 (79) F10 key
897        case NSF10FunctionKey: return 0x79;
898        // VK_F11 (7A) F11 key
899        case NSF11FunctionKey: return 0x7A;
900        // VK_F12 (7B) F12 key
901        case NSF12FunctionKey: return 0x7B;
902        // VK_F13 (7C) F13 key
903        case NSF13FunctionKey: return 0x7C;
904        // VK_F14 (7D) F14 key
905        case NSF14FunctionKey: return 0x7D;
906        // VK_F15 (7E) F15 key
907        case NSF15FunctionKey: return 0x7E;
908        // VK_F16 (7F) F16 key
909        case NSF16FunctionKey: return 0x7F;
910        // VK_F17 (80H) F17 key
911        case NSF17FunctionKey: return 0x80;
912        // VK_F18 (81H) F18 key
913        case NSF18FunctionKey: return 0x81;
914        // VK_F19 (82H) F19 key
915        case NSF19FunctionKey: return 0x82;
916        // VK_F20 (83H) F20 key
917        case NSF20FunctionKey: return 0x83;
918        // VK_F21 (84H) F21 key
919        case NSF21FunctionKey: return 0x84;
920        // VK_F22 (85H) F22 key
921        case NSF22FunctionKey: return 0x85;
922        // VK_F23 (86H) F23 key
923        case NSF23FunctionKey: return 0x86;
924        // VK_F24 (87H) F24 key
925        case NSF24FunctionKey: return 0x87;
926
927        // VK_NUMLOCK (90) NUM LOCK key
928
929        // VK_SCROLL (91) SCROLL LOCK key
930        case NSScrollLockFunctionKey: return 0x91;
931
932        // VK_LSHIFT (A0) Left SHIFT key
933        // VK_RSHIFT (A1) Right SHIFT key
934        // VK_LCONTROL (A2) Left CONTROL key
935        // VK_RCONTROL (A3) Right CONTROL key
936        // VK_LMENU (A4) Left MENU key
937        // VK_RMENU (A5) Right MENU key
938        // VK_BROWSER_BACK (A6) Windows 2000/XP: Browser Back key
939        // VK_BROWSER_FORWARD (A7) Windows 2000/XP: Browser Forward key
940        // VK_BROWSER_REFRESH (A8) Windows 2000/XP: Browser Refresh key
941        // VK_BROWSER_STOP (A9) Windows 2000/XP: Browser Stop key
942        // VK_BROWSER_SEARCH (AA) Windows 2000/XP: Browser Search key
943        // VK_BROWSER_FAVORITES (AB) Windows 2000/XP: Browser Favorites key
944        // VK_BROWSER_HOME (AC) Windows 2000/XP: Browser Start and Home key
945        // VK_VOLUME_MUTE (AD) Windows 2000/XP: Volume Mute key
946        // VK_VOLUME_DOWN (AE) Windows 2000/XP: Volume Down key
947        // VK_VOLUME_UP (AF) Windows 2000/XP: Volume Up key
948        // VK_MEDIA_NEXT_TRACK (B0) Windows 2000/XP: Next Track key
949        // VK_MEDIA_PREV_TRACK (B1) Windows 2000/XP: Previous Track key
950        // VK_MEDIA_STOP (B2) Windows 2000/XP: Stop Media key
951        // VK_MEDIA_PLAY_PAUSE (B3) Windows 2000/XP: Play/Pause Media key
952        // VK_LAUNCH_MAIL (B4) Windows 2000/XP: Start Mail key
953        // VK_LAUNCH_MEDIA_SELECT (B5) Windows 2000/XP: Select Media key
954        // VK_LAUNCH_APP1 (B6) Windows 2000/XP: Start Application 1 key
955        // VK_LAUNCH_APP2 (B7) Windows 2000/XP: Start Application 2 key
956
957        // VK_OEM_1 (BA) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ';:' key
958        case ';': case ':': return 0xBA;
959        // VK_OEM_PLUS (BB) Windows 2000/XP: For any country/region, the '+' key
960        case '=': case '+': return 0xBB;
961        // VK_OEM_COMMA (BC) Windows 2000/XP: For any country/region, the ',' key
962        case ',': case '<': return 0xBC;
963        // VK_OEM_MINUS (BD) Windows 2000/XP: For any country/region, the '-' key
964        case '-': case '_': return 0xBD;
965        // VK_OEM_PERIOD (BE) Windows 2000/XP: For any country/region, the '.' key
966        case '.': case '>': return 0xBE;
967        // VK_OEM_2 (BF) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '/?' key
968        case '/': case '?': return 0xBF;
969        // VK_OEM_3 (C0) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '`~' key
970        case '`': case '~': return 0xC0;
971        // VK_OEM_4 (DB) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '[{' key
972        case '[': case '{': return 0xDB;
973        // VK_OEM_5 (DC) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the '\|' key
974        case '\\': case '|': return 0xDC;
975        // VK_OEM_6 (DD) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the ']}' key
976        case ']': case '}': return 0xDD;
977        // VK_OEM_7 (DE) Used for miscellaneous characters; it can vary by keyboard. Windows 2000/XP: For the US standard keyboard, the 'single-quote/double-quote' key
978        case '\'': case '"': return 0xDE;
979
980        // VK_OEM_8 (DF) Used for miscellaneous characters; it can vary by keyboard.
981        // VK_OEM_102 (E2) Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
982        // VK_PROCESSKEY (E5) Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
983        // VK_PACKET (E7) Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT,SendInput, WM_KEYDOWN, and WM_KEYUP
984        // VK_ATTN (F6) Attn key
985        // VK_CRSEL (F7) CrSel key
986        // VK_EXSEL (F8) ExSel key
987        // VK_EREOF (F9) Erase EOF key
988        // VK_PLAY (FA) Play key
989        // VK_ZOOM (FB) Zoom key
990        // VK_NONAME (FC) Reserved for future use
991        // VK_PA1 (FD) PA1 key
992        // VK_OEM_CLEAR (FE) Clear key
993    }
994
995    return 0;
996}
997
998static inline bool isKeyUpEvent(NSEvent *event)
999{
1000    if ([event type] != NSFlagsChanged)
1001        return [event type] == NSKeyUp;
1002    // FIXME: This logic fails if the user presses both Shift keys at once, for example:
1003    // we treat releasing one of them as keyDown.
1004    switch ([event keyCode]) {
1005        case 54: // Right Command
1006        case 55: // Left Command
1007            return ([event modifierFlags] & NSCommandKeyMask) == 0;
1008
1009        case 57: // Capslock
1010            return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
1011
1012        case 56: // Left Shift
1013        case 60: // Right Shift
1014            return ([event modifierFlags] & NSShiftKeyMask) == 0;
1015
1016        case 58: // Left Alt
1017        case 61: // Right Alt
1018            return ([event modifierFlags] & NSAlternateKeyMask) == 0;
1019
1020        case 59: // Left Ctrl
1021        case 62: // Right Ctrl
1022            return ([event modifierFlags] & NSControlKeyMask) == 0;
1023
1024        case 63: // Function
1025            return ([event modifierFlags] & NSFunctionKeyMask) == 0;
1026    }
1027    return false;
1028}
1029
1030static inline WebEvent::Modifiers modifiersForEvent(NSEvent *event)
1031{
1032    unsigned modifiers = 0;
1033    if ([event modifierFlags] & NSAlphaShiftKeyMask)
1034        modifiers |= WebEvent::CapsLockKey;
1035    if ([event modifierFlags] & NSShiftKeyMask)
1036        modifiers |= WebEvent::ShiftKey;
1037    if ([event modifierFlags] & NSControlKeyMask)
1038        modifiers |= WebEvent::ControlKey;
1039    if ([event modifierFlags] & NSAlternateKeyMask)
1040        modifiers |= WebEvent::AltKey;
1041    if ([event modifierFlags] & NSCommandKeyMask)
1042        modifiers |= WebEvent::MetaKey;
1043    return (WebEvent::Modifiers)modifiers;
1044}
1045
1046WebMouseEvent WebEventFactory::createWebMouseEvent(NSEvent *event, NSView *windowView)
1047{
1048    NSPoint position = pointForEvent(event, windowView);
1049    NSPoint globalPosition = globalPointForEvent(event);
1050
1051    WebEvent::Type type                     = mouseEventTypeForEvent(event);
1052    WebMouseEvent::Button button            = mouseButtonForEvent(event);
1053    float deltaX                            = [event deltaX];
1054    float deltaY                            = [event deltaY];
1055    float deltaZ                            = [event deltaZ];
1056    int clickCount                          = clickCountForEvent(event);
1057    WebEvent::Modifiers modifiers           = modifiersForEvent(event);
1058    double timestamp                        = [event timestamp];
1059
1060    return WebMouseEvent(type, button, IntPoint(position), IntPoint(globalPosition), deltaX, deltaY, deltaZ, clickCount, modifiers, timestamp);
1061}
1062
1063WebWheelEvent WebEventFactory::createWebWheelEvent(NSEvent *event, NSView *windowView)
1064{
1065    NSPoint position = pointForEvent(event, windowView);
1066    NSPoint globalPosition = globalPointForEvent(event);
1067
1068    WebWheelEvent::Granularity granularity  = WebWheelEvent::ScrollByPixelWheelEvent;
1069
1070    BOOL continuous;
1071    float deltaX = 0;
1072    float deltaY = 0;
1073    float wheelTicksX = 0;
1074    float wheelTicksY = 0;
1075
1076    WKGetWheelEventDeltas(event, &deltaX, &deltaY, &continuous);
1077
1078    if (continuous) {
1079        // smooth scroll events
1080        wheelTicksX = deltaX / static_cast<float>(Scrollbar::pixelsPerLineStep());
1081        wheelTicksY = deltaY / static_cast<float>(Scrollbar::pixelsPerLineStep());
1082    } else {
1083        // plain old wheel events
1084        wheelTicksX = deltaX;
1085        wheelTicksY = deltaY;
1086        deltaX *= static_cast<float>(Scrollbar::pixelsPerLineStep());
1087        deltaY *= static_cast<float>(Scrollbar::pixelsPerLineStep());
1088    }
1089
1090    WebWheelEvent::Phase phase              = phaseForEvent(event);
1091    WebWheelEvent::Phase momentumPhase      = momentumPhaseForEvent(event);
1092    bool hasPreciseScrollingDeltas          = continuous;
1093    WebEvent::Modifiers modifiers           = modifiersForEvent(event);
1094    double timestamp                        = [event timestamp];
1095
1096    return WebWheelEvent(WebEvent::Wheel, IntPoint(position), IntPoint(globalPosition), FloatSize(deltaX, deltaY), FloatSize(wheelTicksX, wheelTicksY), granularity, phase, momentumPhase, hasPreciseScrollingDeltas, modifiers, timestamp);
1097}
1098
1099WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(NSEvent *event, NSView *)
1100{
1101    WebEvent::Type type             = isKeyUpEvent(event) ? WebEvent::KeyUp : WebEvent::KeyDown;
1102    String text                     = textFromEvent(event);
1103    String unmodifiedText           = unmodifiedTextFromEvent(event);
1104    String keyIdentifier            = keyIdentifierForKeyEvent(event);
1105    int windowsVirtualKeyCode       = windowsKeyCodeForKeyEvent(event);
1106    int nativeVirtualKeyCode        = [event keyCode];
1107    int macCharCode                 = WKGetNSEventKeyChar(event);
1108    bool autoRepeat                 = ([event type] != NSFlagsChanged) && [event isARepeat];
1109    bool isKeypad                   = isKeypadEvent(event);
1110    bool isSystemKey                = false; // SystemKey is always false on the Mac.
1111    WebEvent::Modifiers modifiers   = modifiersForEvent(event);
1112    double timestamp                = [event timestamp];
1113
1114    return WebKeyboardEvent(type, text, unmodifiedText, keyIdentifier, windowsVirtualKeyCode, nativeVirtualKeyCode, macCharCode, autoRepeat, isKeypad, isSystemKey, modifiers, timestamp);
1115}
1116
1117#if ENABLE(GESTURE_EVENTS)
1118WebGestureEvent WebEventFactory::createWebGestureEvent(NSEvent *event, NSView *windowView)
1119{
1120    WebEvent::Type type             = gestureEventTypeForEvent(event);
1121    NSPoint position                = pointForEvent(event, windowView);
1122    NSPoint globalPosition          = globalPointForEvent(event);
1123    WebEvent::Modifiers modifiers   = modifiersForEvent(event);
1124    double timestamp                = [event timestamp];
1125
1126    return WebGestureEvent(type, IntPoint(position), IntPoint(globalPosition), modifiers, timestamp);
1127}
1128#endif
1129
1130} // namespace WebKit
1131