1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY 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#include "WebInputEvent.h"
33#include "platform/KeyboardCodes.h"
34#include "wtf/Assertions.h"
35#include "wtf/StringExtras.h"
36#include <ctype.h>
37
38using namespace WebCore;
39
40namespace blink {
41
42struct SameSizeAsWebInputEvent {
43    int inputData[5];
44};
45
46struct SameSizeAsWebKeyboardEvent : public SameSizeAsWebInputEvent {
47    int keyboardData[12];
48};
49
50struct SameSizeAsWebMouseEvent : public SameSizeAsWebInputEvent {
51    int mouseData[10];
52};
53
54struct SameSizeAsWebMouseWheelEvent : public SameSizeAsWebMouseEvent {
55    int mousewheelData[10];
56};
57
58struct SameSizeAsWebGestureEvent : public SameSizeAsWebInputEvent {
59    int gestureData[9];
60};
61
62struct SameSizeAsWebTouchEvent : public SameSizeAsWebInputEvent {
63    WebTouchPoint touchPoints[3 * WebTouchEvent::touchesLengthCap];
64    int touchData[3];
65};
66
67COMPILE_ASSERT(sizeof(WebInputEvent) == sizeof(SameSizeAsWebInputEvent), WebInputEvent_has_gaps);
68COMPILE_ASSERT(sizeof(WebKeyboardEvent) == sizeof(SameSizeAsWebKeyboardEvent), WebKeyboardEvent_has_gaps);
69COMPILE_ASSERT(sizeof(WebMouseEvent) == sizeof(SameSizeAsWebMouseEvent), WebMouseEvent_has_gaps);
70COMPILE_ASSERT(sizeof(WebMouseWheelEvent) == sizeof(SameSizeAsWebMouseWheelEvent), WebMouseWheelEvent_has_gaps);
71COMPILE_ASSERT(sizeof(WebGestureEvent) == sizeof(SameSizeAsWebGestureEvent), WebGestureEvent_has_gaps);
72COMPILE_ASSERT(sizeof(WebTouchEvent) == sizeof(SameSizeAsWebTouchEvent), WebTouchEvent_has_gaps);
73
74static const char* staticKeyIdentifiers(unsigned short keyCode)
75{
76    switch (keyCode) {
77    case VKEY_MENU:
78        return "Alt";
79    case VKEY_CONTROL:
80        return "Control";
81    case VKEY_SHIFT:
82        return "Shift";
83    case VKEY_CAPITAL:
84        return "CapsLock";
85    case VKEY_LWIN:
86    case VKEY_RWIN:
87        return "Win";
88    case VKEY_CLEAR:
89        return "Clear";
90    case VKEY_DOWN:
91        return "Down";
92    case VKEY_END:
93        return "End";
94    case VKEY_RETURN:
95        return "Enter";
96    case VKEY_EXECUTE:
97        return "Execute";
98    case VKEY_F1:
99        return "F1";
100    case VKEY_F2:
101        return "F2";
102    case VKEY_F3:
103        return "F3";
104    case VKEY_F4:
105        return "F4";
106    case VKEY_F5:
107        return "F5";
108    case VKEY_F6:
109        return "F6";
110    case VKEY_F7:
111        return "F7";
112    case VKEY_F8:
113        return "F8";
114    case VKEY_F9:
115        return "F9";
116    case VKEY_F10:
117        return "F10";
118    case VKEY_F11:
119        return "F11";
120    case VKEY_F12:
121        return "F12";
122    case VKEY_F13:
123        return "F13";
124    case VKEY_F14:
125        return "F14";
126    case VKEY_F15:
127        return "F15";
128    case VKEY_F16:
129        return "F16";
130    case VKEY_F17:
131        return "F17";
132    case VKEY_F18:
133        return "F18";
134    case VKEY_F19:
135        return "F19";
136    case VKEY_F20:
137        return "F20";
138    case VKEY_F21:
139        return "F21";
140    case VKEY_F22:
141        return "F22";
142    case VKEY_F23:
143        return "F23";
144    case VKEY_F24:
145        return "F24";
146    case VKEY_HELP:
147        return "Help";
148    case VKEY_HOME:
149        return "Home";
150    case VKEY_INSERT:
151        return "Insert";
152    case VKEY_LEFT:
153        return "Left";
154    case VKEY_NEXT:
155        return "PageDown";
156    case VKEY_PRIOR:
157        return "PageUp";
158    case VKEY_PAUSE:
159        return "Pause";
160    case VKEY_SNAPSHOT:
161        return "PrintScreen";
162    case VKEY_RIGHT:
163        return "Right";
164    case VKEY_SCROLL:
165        return "Scroll";
166    case VKEY_SELECT:
167        return "Select";
168    case VKEY_UP:
169        return "Up";
170    case VKEY_DELETE:
171        return "U+007F"; // Standard says that DEL becomes U+007F.
172    case VKEY_MEDIA_NEXT_TRACK:
173        return "MediaNextTrack";
174    case VKEY_MEDIA_PREV_TRACK:
175        return "MediaPreviousTrack";
176    case VKEY_MEDIA_STOP:
177        return "MediaStop";
178    case VKEY_MEDIA_PLAY_PAUSE:
179        return "MediaPlayPause";
180    case VKEY_VOLUME_MUTE:
181        return "VolumeMute";
182    case VKEY_VOLUME_DOWN:
183        return "VolumeDown";
184    case VKEY_VOLUME_UP:
185        return "VolumeUp";
186    default:
187        return 0;
188    }
189}
190
191void WebKeyboardEvent::setKeyIdentifierFromWindowsKeyCode()
192{
193    const char* id = staticKeyIdentifiers(windowsKeyCode);
194    if (id) {
195        strncpy(keyIdentifier, id, sizeof(keyIdentifier) - 1);
196        keyIdentifier[sizeof(keyIdentifier) - 1] = '\0';
197    } else
198        snprintf(keyIdentifier, sizeof(keyIdentifier), "U+%04X", toupper(windowsKeyCode));
199}
200
201// static
202int WebKeyboardEvent::windowsKeyCodeWithoutLocation(int keycode)
203{
204    switch (keycode) {
205    case VK_LCONTROL:
206    case VK_RCONTROL:
207        return VK_CONTROL;
208    case VK_LSHIFT:
209    case VK_RSHIFT:
210        return VK_SHIFT;
211    case VK_LMENU:
212    case VK_RMENU:
213        return VK_MENU;
214    default:
215        return keycode;
216    }
217}
218
219// static
220int WebKeyboardEvent::locationModifiersFromWindowsKeyCode(int keycode)
221{
222    switch (keycode) {
223    case VK_LCONTROL:
224    case VK_LSHIFT:
225    case VK_LMENU:
226    case VK_LWIN:
227        return IsLeft;
228    case VK_RCONTROL:
229    case VK_RSHIFT:
230    case VK_RMENU:
231    case VK_RWIN:
232        return IsRight;
233    default:
234        return 0;
235    }
236}
237
238} // namespace blink
239