initial import
[vuplus_webkit] / Source / WebCore / platform / iphone / KeyEventIPhone.mm
1 /*
2  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 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 COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "PlatformKeyboardEvent.h"
28
29 #if PLATFORM(IOS)
30
31 #import "KeyEventCocoa.h"
32 #import "Logging.h"
33 #import "WebEvent.h"
34
35 using namespace WTF;
36
37 namespace WebCore {
38
39 static String keyIdentifierForKeyEvent(WebEvent *event)
40 {
41     NSString *s = event.charactersIgnoringModifiers;
42     if ([s length] != 1) {
43         LOG(Events, "received an unexpected number of characters in key event: %u", [s length]);
44         return "Unidentified";
45     }
46
47     unichar c = CFStringGetCharacterAtIndex((CFStringRef)s, 0);
48     return keyIdentifierForCharCode(c);
49 }
50
51 PlatformKeyboardEvent::PlatformKeyboardEvent(WebEvent *event)
52     : m_type(event.type == WebEventKeyUp ? PlatformKeyboardEvent::KeyUp : PlatformKeyboardEvent::KeyDown)
53     , m_text(event.characters)
54     , m_unmodifiedText(event.charactersIgnoringModifiers)
55     , m_keyIdentifier(keyIdentifierForKeyEvent(event))
56     , m_autoRepeat(event.isKeyRepeating)
57     , m_windowsVirtualKeyCode(event.keyCode)
58     , m_isKeypad(false) // iPhone does not distinguish the numpad <rdar://problem/7190835>
59     , m_shiftKey(event.modifierFlags & WebEventFlagMaskShift)
60     , m_ctrlKey(event.modifierFlags & WebEventFlagMaskControl)
61     , m_altKey(event.modifierFlags & WebEventFlagMaskAlternate)
62     , m_metaKey(event.modifierFlags & WebEventFlagMaskCommand)
63     , m_Event(event)
64 {
65     ASSERT(event.type == WebEventKeyDown || event.type == WebEventKeyUp);
66
67     // Always use 13 for Enter/Return -- we don't want to use AppKit's different character for Enter.
68     if (m_windowsVirtualKeyCode == '\r') {
69         m_text = "\r";
70         m_unmodifiedText = "\r";
71     }
72
73     // The adjustments below are only needed in backward compatibility mode, but we cannot tell what mode we are in from here.
74
75     // Turn 0x7F into 8, because backspace needs to always be 8.
76     if (m_text == "\x7F")
77         m_text = "\x8";
78     if (m_unmodifiedText == "\x7F")
79         m_unmodifiedText = "\x8";
80     // Always use 9 for tab -- we don't want to use AppKit's different character for shift-tab.
81     if (m_windowsVirtualKeyCode == 9) {
82         m_text = "\x9";
83         m_unmodifiedText = "\x9";
84     }
85 }
86
87 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
88 {
89     // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
90     ASSERT(m_type == KeyDown);
91     ASSERT(type == RawKeyDown || type == Char);
92     m_type = type;
93     if (backwardCompatibilityMode)
94         return;
95
96     if (type == RawKeyDown) {
97         m_text = String();
98         m_unmodifiedText = String();
99     } else {
100         m_keyIdentifier = String();
101         m_windowsVirtualKeyCode = 0;
102         if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
103             // According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
104             // happen to be in this range, e.g. the Apple logo (Option+Shift+K).
105             // 0xF7FF is an arbitrary cut-off.
106             m_text = String();
107             m_unmodifiedText = String();
108         }
109     }
110 }
111
112 bool PlatformKeyboardEvent::currentCapsLockState()
113 {
114     return 0;
115 }
116
117 }
118
119 #endif // PLATFORM(IOS)