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 "NotImplemented.h"
34#import "WebEvent.h"
35
36using namespace WTF;
37
38namespace WebCore {
39
40static String keyIdentifierForKeyEvent(WebEvent *event)
41{
42    NSString *s = event.charactersIgnoringModifiers;
43    if ([s length] != 1) {
44        LOG(Events, "received an unexpected number of characters in key event: %u", [s length]);
45        return "Unidentified";
46    }
47
48    unichar c = CFStringGetCharacterAtIndex((CFStringRef)s, 0);
49    return keyIdentifierForCharCode(c);
50}
51
52PlatformKeyboardEvent::PlatformKeyboardEvent(WebEvent *event)
53    : PlatformEvent(event.type == WebEventKeyUp ? PlatformEvent::KeyUp : PlatformEvent::KeyDown, event.modifierFlags & WebEventFlagMaskShift, event.modifierFlags & WebEventFlagMaskControl, event.modifierFlags & WebEventFlagMaskAlternate, event.modifierFlags & WebEventFlagMaskCommand)
54    , m_text(event.characters)
55    , m_unmodifiedText(event.charactersIgnoringModifiers)
56    , m_keyIdentifier(keyIdentifierForKeyEvent(event))
57    , m_autoRepeat(event.isKeyRepeating)
58    , m_windowsVirtualKeyCode(event.keyCode)
59    , m_isKeypad(false) // iPhone does not distinguish the numpad <rdar://problem/7190835>
60    , m_Event(event)
61{
62    ASSERT(event.type == WebEventKeyDown || event.type == WebEventKeyUp);
63
64    // Always use 13 for Enter/Return -- we don't want to use AppKit's different character for Enter.
65    if (m_windowsVirtualKeyCode == '\r') {
66        m_text = "\r";
67        m_unmodifiedText = "\r";
68    }
69
70    // The adjustments below are only needed in backward compatibility mode, but we cannot tell what mode we are in from here.
71
72    // Turn 0x7F into 8, because backspace needs to always be 8.
73    if (m_text == "\x7F")
74        m_text = "\x8";
75    if (m_unmodifiedText == "\x7F")
76        m_unmodifiedText = "\x8";
77    // Always use 9 for tab -- we don't want to use AppKit's different character for shift-tab.
78    if (m_windowsVirtualKeyCode == 9) {
79        m_text = "\x9";
80        m_unmodifiedText = "\x9";
81    }
82}
83
84void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
85{
86    // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
87    ASSERT(m_type == KeyDown);
88    ASSERT(type == RawKeyDown || type == Char);
89    m_type = type;
90    if (backwardCompatibilityMode)
91        return;
92
93    if (type == PlatformEvent::RawKeyDown) {
94        m_text = String();
95        m_unmodifiedText = String();
96    } else {
97        m_keyIdentifier = String();
98        m_windowsVirtualKeyCode = 0;
99        if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
100            // According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
101            // happen to be in this range, e.g. the Apple logo (Option+Shift+K).
102            // 0xF7FF is an arbitrary cut-off.
103            m_text = String();
104            m_unmodifiedText = String();
105        }
106    }
107}
108
109bool PlatformKeyboardEvent::currentCapsLockState()
110{
111    notImplemented();
112    return false;
113}
114
115void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
116{
117    notImplemented();
118    shiftKey = false;
119    ctrlKey = false;
120    altKey = false;
121    metaKey = false;
122}
123
124}
125
126#endif // PLATFORM(IOS)
127