• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2008 Diego Hidalgo C. Gonzalez
4  * Copyright (C) 2009-2010 ProFUSION embedded systems
5  * Copyright (C) 2009-2010 Samsung Electronics
6  *
7  * All rights reserved.
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 "config.h"
32 
33 #include "PlatformKeyboardEvent.h"
34 
35 #include "NotImplemented.h"
36 #include "TextEncoding.h"
37 #include "WindowsKeyboardCodes.h"
38 
39 #include <Evas.h>
40 #include <stdio.h>
41 #include <wtf/HashMap.h>
42 #include <wtf/text/StringConcatenate.h>
43 #include <wtf/text/StringHash.h>
44 
45 namespace WebCore {
46 
47 typedef HashMap<String, String> KeyMap;
48 typedef HashMap<String, int> WindowsKeyMap;
49 
50 static KeyMap gKeyMap;
51 static WindowsKeyMap gWindowsKeyMap;
52 
createKeyMap()53 static void createKeyMap()
54 {
55     for (unsigned int i = 1; i < 25; i++) {
56         String key = makeString('F', String::number(i));
57         gKeyMap.set(key, key);
58     }
59     gKeyMap.set("Alt_L", "Alt");
60     gKeyMap.set("ISO_Level3_Shift", "Alt");
61     gKeyMap.set("Menu", "Alt");
62     gKeyMap.set("Shift_L", "Shift");
63     gKeyMap.set("Shift_R", "Shift");
64     gKeyMap.set("Down", "Down");
65     gKeyMap.set("End", "End");
66     gKeyMap.set("Return", "Enter");
67     gKeyMap.set("KP_Enter", "Enter");
68     gKeyMap.set("Home", "Home");
69     gKeyMap.set("Insert", "Insert");
70     gKeyMap.set("Left", "Left");
71     gKeyMap.set("Down", "Down");
72     gKeyMap.set("Next", "PageDown");
73     gKeyMap.set("Prior", "PageUp");
74     gKeyMap.set("Right", "Right");
75     gKeyMap.set("Up", "Up");
76     gKeyMap.set("Delete", "U+007F");
77     gKeyMap.set("Tab", "U+0009");
78     gKeyMap.set("ISO_Left_Tab", "U+0009");
79 }
80 
createWindowsKeyMap()81 static void createWindowsKeyMap()
82 {
83     gWindowsKeyMap.set("Return",     VK_RETURN);
84     gWindowsKeyMap.set("KP_Return",  VK_RETURN);
85     gWindowsKeyMap.set("Alt_L",      VK_MENU);
86     gWindowsKeyMap.set("ISO_Level3_Shift", VK_MENU);
87     gWindowsKeyMap.set("Menu",       VK_MENU);
88     gWindowsKeyMap.set("Shift_L",    VK_SHIFT);
89     gWindowsKeyMap.set("Shift_R",    VK_SHIFT);
90     gWindowsKeyMap.set("Control_L",  VK_CONTROL);
91     gWindowsKeyMap.set("Control_R",  VK_CONTROL);
92     gWindowsKeyMap.set("Pause",      VK_PAUSE);
93     gWindowsKeyMap.set("Break",      VK_PAUSE);
94     gWindowsKeyMap.set("Caps_Lock",  VK_CAPITAL);
95     gWindowsKeyMap.set("Scroll_Lock", VK_SCROLL);
96     gWindowsKeyMap.set("Num_Lock",   VK_NUMLOCK);
97     gWindowsKeyMap.set("Escape",     VK_ESCAPE);
98     gWindowsKeyMap.set("Tab",        VK_TAB);
99     gWindowsKeyMap.set("ISO_Left_Tab", VK_TAB);
100     gWindowsKeyMap.set("BackSpace",  VK_BACK);
101     gWindowsKeyMap.set("Space",      VK_SPACE);
102     gWindowsKeyMap.set("Next",       VK_NEXT);
103     gWindowsKeyMap.set("Prior",      VK_PRIOR);
104     gWindowsKeyMap.set("Home",       VK_HOME);
105     gWindowsKeyMap.set("End",        VK_END);
106     gWindowsKeyMap.set("Right",      VK_RIGHT);
107     gWindowsKeyMap.set("Left",       VK_LEFT);
108     gWindowsKeyMap.set("Up",         VK_UP);
109     gWindowsKeyMap.set("Down",       VK_DOWN);
110     gWindowsKeyMap.set("Print",      VK_PRINT);
111     gWindowsKeyMap.set("Insert",     VK_INSERT);
112     gWindowsKeyMap.set("Delete",     VK_DELETE);
113 
114     gWindowsKeyMap.set("comma",        VK_OEM_COMMA);
115     gWindowsKeyMap.set("less",         VK_OEM_COMMA);
116     gWindowsKeyMap.set("period",       VK_OEM_PERIOD);
117     gWindowsKeyMap.set("greater",      VK_OEM_PERIOD);
118     gWindowsKeyMap.set("semicolon",    VK_OEM_1);
119     gWindowsKeyMap.set("colon",        VK_OEM_1);
120     gWindowsKeyMap.set("slash",        VK_OEM_2);
121     gWindowsKeyMap.set("question",     VK_OEM_2);
122     gWindowsKeyMap.set("grave",        VK_OEM_3);
123     gWindowsKeyMap.set("asciitilde",   VK_OEM_3);
124     gWindowsKeyMap.set("bracketleft",  VK_OEM_4);
125     gWindowsKeyMap.set("braceleft",    VK_OEM_4);
126     gWindowsKeyMap.set("backslash",    VK_OEM_5);
127     gWindowsKeyMap.set("bar",          VK_OEM_5);
128     gWindowsKeyMap.set("bracketright", VK_OEM_6);
129     gWindowsKeyMap.set("braceright",   VK_OEM_6);
130     gWindowsKeyMap.set("apostrophe",   VK_OEM_7);
131     gWindowsKeyMap.set("quotedbl",     VK_OEM_7);
132 
133     // Alphabet
134     const char* alphabet = "abcdefghijklmnopqrstuvwxyz";
135     for (unsigned int i = 0; i < 26; i++) {
136         String key(alphabet + i, 1);
137         gWindowsKeyMap.set(key, VK_A + i);
138     }
139 
140     // Digits
141     for (unsigned int i = 0; i < 10; i++) {
142         String key = String::number(i);
143         gWindowsKeyMap.set(key, VK_0 + i);
144     }
145 
146     // Shifted digits
147     gWindowsKeyMap.set("exclam",    VK_1);
148     gWindowsKeyMap.set("at",        VK_2);
149     gWindowsKeyMap.set("numbersign", VK_3);
150     gWindowsKeyMap.set("dollar",    VK_4);
151     gWindowsKeyMap.set("percent",   VK_5);
152     gWindowsKeyMap.set("asciicircum", VK_6);
153     gWindowsKeyMap.set("ampersand", VK_7);
154     gWindowsKeyMap.set("asterisk",  VK_8);
155     gWindowsKeyMap.set("parenleft", VK_9);
156     gWindowsKeyMap.set("parenright", VK_0);
157     gWindowsKeyMap.set("minus",     VK_OEM_MINUS);
158     gWindowsKeyMap.set("underscore", VK_OEM_MINUS);
159     gWindowsKeyMap.set("equal",     VK_OEM_PLUS);
160     gWindowsKeyMap.set("plus",      VK_OEM_PLUS);
161 
162     // F_XX
163     for (unsigned int i = 1; i < 25; i++) {
164         String key = makeString('F', String::number(i));
165         gWindowsKeyMap.set(key, VK_F1 + i);
166     }
167 }
168 
keyIdentifierForEvasKeyName(String & keyName)169 static String keyIdentifierForEvasKeyName(String& keyName)
170 {
171     if (gKeyMap.isEmpty())
172         createKeyMap();
173 
174     if (gKeyMap.contains(keyName))
175         return gKeyMap.get(keyName);
176 
177     return keyName;
178 }
179 
windowsKeyCodeForEvasKeyName(String & keyName)180 static int windowsKeyCodeForEvasKeyName(String& keyName)
181 {
182     if (gWindowsKeyMap.isEmpty())
183         createWindowsKeyMap();
184 
185     if (gWindowsKeyMap.contains(keyName))
186         return gWindowsKeyMap.get(keyName);
187 
188     return 0;
189 }
190 
PlatformKeyboardEvent(const Evas_Event_Key_Down * event)191 PlatformKeyboardEvent::PlatformKeyboardEvent(const Evas_Event_Key_Down* event)
192     : m_type(KeyDown)
193     , m_text(String::fromUTF8(event->string))
194     , m_shiftKey(evas_key_modifier_is_set(event->modifiers, "Shift"))
195     , m_ctrlKey(evas_key_modifier_is_set(event->modifiers, "Control"))
196     , m_altKey(evas_key_modifier_is_set(event->modifiers, "Alt"))
197     , m_metaKey(evas_key_modifier_is_set(event->modifiers, "Meta"))
198 {
199     String keyName = String(event->key);
200     m_keyIdentifier = keyIdentifierForEvasKeyName(keyName);
201     m_windowsVirtualKeyCode = windowsKeyCodeForEvasKeyName(keyName);
202 
203     // FIXME:
204     m_isKeypad = false;
205     m_autoRepeat = false;
206 }
207 
PlatformKeyboardEvent(const Evas_Event_Key_Up * event)208 PlatformKeyboardEvent::PlatformKeyboardEvent(const Evas_Event_Key_Up* event)
209     : m_type(KeyUp)
210     , m_text(String::fromUTF8(event->string))
211     , m_shiftKey(evas_key_modifier_is_set(event->modifiers, "Shift"))
212     , m_ctrlKey(evas_key_modifier_is_set(event->modifiers, "Control"))
213     , m_altKey(evas_key_modifier_is_set(event->modifiers, "Alt"))
214     , m_metaKey(evas_key_modifier_is_set(event->modifiers, "Meta"))
215 {
216     String keyName = String(event->key);
217     m_keyIdentifier = keyIdentifierForEvasKeyName(keyName);
218     m_windowsVirtualKeyCode = windowsKeyCodeForEvasKeyName(keyName);
219 
220     // FIXME:
221     m_isKeypad = false;
222     m_autoRepeat = false;
223 }
224 
disambiguateKeyDownEvent(Type type,bool)225 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool)
226 {
227     ASSERT(m_type == KeyDown);
228     m_type = type;
229 
230     if (type == RawKeyDown) {
231         m_text = String();
232         m_unmodifiedText = String();
233     } else {
234         m_keyIdentifier = String();
235         m_windowsVirtualKeyCode = 0;
236     }
237 }
238 
currentCapsLockState()239 bool PlatformKeyboardEvent::currentCapsLockState()
240 {
241     notImplemented();
242     return false;
243 }
244 
getCurrentModifierState(bool & shiftKey,bool & ctrlKey,bool & altKey,bool & metaKey)245 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
246 {
247     notImplemented();
248     shiftKey = false;
249     ctrlKey = false;
250     altKey = false;
251     metaKey = false;
252 }
253 
254 }
255