summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/android/KeyEventAndroid.cpp
blob: dd1f37dbd52ad804a25c59a4b832a522ad3069ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright 2007, The Android Open Source Project
 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
 * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
 * Copyright (C) 2007 Holger Hans Peter Freyther
 * Copyright (C) 2008 Collabora, Ltd.  All rights reserved.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "PlatformKeyboardEvent.h"

#include "NotImplemented.h"
#include "WindowsKeyboardCodes.h"
#include <androidfw/KeycodeLabels.h>

namespace WebCore {

// compare to same function in gdk/KeyEventGdk.cpp
static int windowsKeyCodeForKeyEvent(unsigned int keyCode)
{
    // Does not provide all key codes, and does not handle all keys.
    switch (keyCode) {
    case AKEYCODE_DEL:
        return VK_BACK;
    case AKEYCODE_TAB:
        return VK_TAB;
    case AKEYCODE_CLEAR:
        return VK_CLEAR;
    case AKEYCODE_DPAD_CENTER:
    case AKEYCODE_ENTER:
        return VK_RETURN;
    case AKEYCODE_SHIFT_LEFT:
    case AKEYCODE_SHIFT_RIGHT:
        return VK_SHIFT;
    // back will serve as escape, although we probably do not have access to it
    case AKEYCODE_BACK:
        return VK_ESCAPE;
    case AKEYCODE_SPACE:
        return VK_SPACE;
    case AKEYCODE_HOME:
        return VK_HOME;
    case AKEYCODE_DPAD_LEFT:
        return VK_LEFT;
    case AKEYCODE_DPAD_UP:
        return VK_UP;
    case AKEYCODE_DPAD_RIGHT:
        return VK_RIGHT;
    case AKEYCODE_DPAD_DOWN:
        return VK_DOWN;
    case AKEYCODE_0:
        return VK_0;
    case AKEYCODE_1:
        return VK_1;
    case AKEYCODE_2:
        return VK_2;
    case AKEYCODE_3:
        return VK_3;
    case AKEYCODE_4:
        return VK_4;
    case AKEYCODE_5:
        return VK_5;
    case AKEYCODE_6:
        return VK_6;
    case AKEYCODE_7:
        return VK_7;
    case AKEYCODE_8:
        return VK_8;
    case AKEYCODE_9:
        return VK_9;
    case AKEYCODE_A:
        return VK_A;
    case AKEYCODE_B:
        return VK_B;
    case AKEYCODE_C:
        return VK_C;
    case AKEYCODE_D:
        return VK_D;
    case AKEYCODE_E:
        return VK_E;
    case AKEYCODE_F:
        return VK_F;
    case AKEYCODE_G:
        return VK_G;
    case AKEYCODE_H:
        return VK_H;
    case AKEYCODE_I:
        return VK_I;
    case AKEYCODE_J:
        return VK_J;
    case AKEYCODE_K:
        return VK_K;
    case AKEYCODE_L:
        return VK_L;
    case AKEYCODE_M:
        return VK_M;
    case AKEYCODE_N:
        return VK_N;
    case AKEYCODE_O:
        return VK_O;
    case AKEYCODE_P:
        return VK_P;
    case AKEYCODE_Q:
        return VK_Q;
    case AKEYCODE_R:
        return VK_R;
    case AKEYCODE_S:
        return VK_S;
    case AKEYCODE_T:
        return VK_T;
    case AKEYCODE_U:
        return VK_U;
    case AKEYCODE_V:
        return VK_V;
    case AKEYCODE_W:
        return VK_W;
    case AKEYCODE_X:
        return VK_X;
    case AKEYCODE_Y:
        return VK_Y;
    case AKEYCODE_Z:
        return VK_Z;
    // colon
    case AKEYCODE_SEMICOLON:
        return VK_OEM_1;
    case AKEYCODE_COMMA:
        return VK_OEM_COMMA;
    case AKEYCODE_MINUS:
        return VK_OEM_MINUS;
    case AKEYCODE_EQUALS:
        return VK_OEM_PLUS;
    case AKEYCODE_PERIOD:
        return VK_OEM_PERIOD;
    case AKEYCODE_SLASH:
        return VK_OEM_2;
    // maybe not the right choice
    case AKEYCODE_LEFT_BRACKET:
        return VK_OEM_4;
    case AKEYCODE_BACKSLASH:
        return VK_OEM_5;
    case AKEYCODE_RIGHT_BRACKET:
        return VK_OEM_6;
    default:
        return 0;
    }
}

static String keyIdentifierForAndroidKeyCode(int keyCode)
{
    // Does not return all of the same key identifiers, and
    // does not handle all the keys.
    switch (keyCode) {
    case AKEYCODE_CLEAR:
        return "Clear";
    case AKEYCODE_ENTER:
    case AKEYCODE_DPAD_CENTER:
        return "Enter";
    case AKEYCODE_HOME:
        return "Home";
    case AKEYCODE_DPAD_DOWN:
        return "Down";
    case AKEYCODE_DPAD_LEFT:
        return "Left";
    case AKEYCODE_DPAD_RIGHT:
        return "Right";
    case AKEYCODE_DPAD_UP:
        return "Up";
    // Standard says that DEL becomes U+00007F.
    case AKEYCODE_DEL:
        return "U+00007F";
    default:
        char upper[16];
        sprintf(upper, "U+%04X", windowsKeyCodeForKeyEvent(keyCode));
        return String(upper);
    }
}

static inline String singleCharacterString(UChar32 c) 
{
    if (!c)
        return String();
    if (c > 0xffff) {
        UChar lead = U16_LEAD(c);
        UChar trail = U16_TRAIL(c);
        UChar utf16[2] = {lead, trail};
        return String(utf16, 2);
    }
    UChar n = (UChar)c;
    return String(&n, 1);
}

PlatformKeyboardEvent::PlatformKeyboardEvent(int keyCode, UChar32 unichar,
        int repeatCount, bool down, bool cap, bool alt, bool sym)
    : m_type(down ? KeyDown : KeyUp)
    , m_text(singleCharacterString(unichar))
    , m_unmodifiedText(singleCharacterString(unichar))
    , m_keyIdentifier(keyIdentifierForAndroidKeyCode(keyCode))
    , m_autoRepeat(repeatCount > 0)
    , m_windowsVirtualKeyCode(windowsKeyCodeForKeyEvent(keyCode))
    , m_nativeVirtualKeyCode(keyCode)
    , m_isKeypad(false)
    , m_shiftKey(cap ? ShiftKey : 0)
    , m_ctrlKey(sym ? CtrlKey : 0)
    , m_altKey(alt ? AltKey : 0)
    , m_metaKey(0)
    // added for android
    , m_repeatCount(repeatCount)
    , m_unichar(unichar)
{
    // Copied from the mac port
    if (m_windowsVirtualKeyCode == '\r') {
        m_text = "\r";
        m_unmodifiedText = "\r";
    }

    if (m_text == "\x7F")
        m_text = "\x8";
    if (m_unmodifiedText == "\x7F")
        m_unmodifiedText = "\x8";

    if (m_windowsVirtualKeyCode == 9) {
        m_text = "\x9";
        m_unmodifiedText = "\x9";
    }
}

bool PlatformKeyboardEvent::currentCapsLockState()
{
    notImplemented();
    return false;
}

void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
{
    notImplemented();
    shiftKey = false;
    ctrlKey = false;
    altKey = false;
    metaKey = false;
}

void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
{
    // Copied with modification from the mac port.
    ASSERT(m_type == KeyDown);
    ASSERT(type == RawKeyDown || type == Char);
    m_type = type;
    if (backwardCompatibilityMode)
        return;

    if (type == RawKeyDown) {
        m_text = String();
        m_unmodifiedText = String();
    } else {
        m_keyIdentifier = String();
        m_windowsVirtualKeyCode = 0;
    }
}

} // namespace WebCore