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 "public/web/WebInputEvent.h"
33
34#include "platform/KeyboardCodes.h"
35#include "wtf/Assertions.h"
36#include "wtf/StringExtras.h"
37#include <ctype.h>
38
39using namespace WebCore;
40
41namespace blink {
42
43struct SameSizeAsWebInputEvent {
44    int inputData[5];
45};
46
47struct SameSizeAsWebKeyboardEvent : public SameSizeAsWebInputEvent {
48    int keyboardData[12];
49};
50
51struct SameSizeAsWebMouseEvent : public SameSizeAsWebInputEvent {
52    int mouseData[10];
53};
54
55struct SameSizeAsWebMouseWheelEvent : public SameSizeAsWebMouseEvent {
56    int mousewheelData[12];
57};
58
59struct SameSizeAsWebGestureEvent : public SameSizeAsWebInputEvent {
60    int gestureData[9];
61};
62
63struct SameSizeAsWebTouchEvent : public SameSizeAsWebInputEvent {
64    WebTouchPoint touchPoints[3 * WebTouchEvent::touchesLengthCap];
65    int touchData[4];
66};
67
68COMPILE_ASSERT(sizeof(WebInputEvent) == sizeof(SameSizeAsWebInputEvent), WebInputEvent_has_gaps);
69COMPILE_ASSERT(sizeof(WebKeyboardEvent) == sizeof(SameSizeAsWebKeyboardEvent), WebKeyboardEvent_has_gaps);
70COMPILE_ASSERT(sizeof(WebMouseEvent) == sizeof(SameSizeAsWebMouseEvent), WebMouseEvent_has_gaps);
71COMPILE_ASSERT(sizeof(WebMouseWheelEvent) == sizeof(SameSizeAsWebMouseWheelEvent), WebMouseWheelEvent_has_gaps);
72COMPILE_ASSERT(sizeof(WebGestureEvent) == sizeof(SameSizeAsWebGestureEvent), WebGestureEvent_has_gaps);
73COMPILE_ASSERT(sizeof(WebTouchEvent) == sizeof(SameSizeAsWebTouchEvent), WebTouchEvent_has_gaps);
74
75static const char* staticKeyIdentifiers(unsigned short keyCode)
76{
77    switch (keyCode) {
78    case VKEY_MENU:
79        return "Alt";
80    case VKEY_CONTROL:
81        return "Control";
82    case VKEY_SHIFT:
83        return "Shift";
84    case VKEY_CAPITAL:
85        return "CapsLock";
86    case VKEY_LWIN:
87    case VKEY_RWIN:
88        return "Win";
89    case VKEY_CLEAR:
90        return "Clear";
91    case VKEY_DOWN:
92        return "Down";
93    case VKEY_END:
94        return "End";
95    case VKEY_RETURN:
96        return "Enter";
97    case VKEY_EXECUTE:
98        return "Execute";
99    case VKEY_F1:
100        return "F1";
101    case VKEY_F2:
102        return "F2";
103    case VKEY_F3:
104        return "F3";
105    case VKEY_F4:
106        return "F4";
107    case VKEY_F5:
108        return "F5";
109    case VKEY_F6:
110        return "F6";
111    case VKEY_F7:
112        return "F7";
113    case VKEY_F8:
114        return "F8";
115    case VKEY_F9:
116        return "F9";
117    case VKEY_F10:
118        return "F10";
119    case VKEY_F11:
120        return "F11";
121    case VKEY_F12:
122        return "F12";
123    case VKEY_F13:
124        return "F13";
125    case VKEY_F14:
126        return "F14";
127    case VKEY_F15:
128        return "F15";
129    case VKEY_F16:
130        return "F16";
131    case VKEY_F17:
132        return "F17";
133    case VKEY_F18:
134        return "F18";
135    case VKEY_F19:
136        return "F19";
137    case VKEY_F20:
138        return "F20";
139    case VKEY_F21:
140        return "F21";
141    case VKEY_F22:
142        return "F22";
143    case VKEY_F23:
144        return "F23";
145    case VKEY_F24:
146        return "F24";
147    case VKEY_HELP:
148        return "Help";
149    case VKEY_HOME:
150        return "Home";
151    case VKEY_INSERT:
152        return "Insert";
153    case VKEY_LEFT:
154        return "Left";
155    case VKEY_NEXT:
156        return "PageDown";
157    case VKEY_PRIOR:
158        return "PageUp";
159    case VKEY_PAUSE:
160        return "Pause";
161    case VKEY_SNAPSHOT:
162        return "PrintScreen";
163    case VKEY_RIGHT:
164        return "Right";
165    case VKEY_SCROLL:
166        return "Scroll";
167    case VKEY_SELECT:
168        return "Select";
169    case VKEY_UP:
170        return "Up";
171    case VKEY_DELETE:
172        return "U+007F"; // Standard says that DEL becomes U+007F.
173    case VKEY_MEDIA_NEXT_TRACK:
174        return "MediaNextTrack";
175    case VKEY_MEDIA_PREV_TRACK:
176        return "MediaPreviousTrack";
177    case VKEY_MEDIA_STOP:
178        return "MediaStop";
179    case VKEY_MEDIA_PLAY_PAUSE:
180        return "MediaPlayPause";
181    case VKEY_VOLUME_MUTE:
182        return "VolumeMute";
183    case VKEY_VOLUME_DOWN:
184        return "VolumeDown";
185    case VKEY_VOLUME_UP:
186        return "VolumeUp";
187    default:
188        return 0;
189    }
190}
191
192void WebKeyboardEvent::setKeyIdentifierFromWindowsKeyCode()
193{
194    const char* id = staticKeyIdentifiers(windowsKeyCode);
195    if (id) {
196        strncpy(keyIdentifier, id, sizeof(keyIdentifier) - 1);
197        keyIdentifier[sizeof(keyIdentifier) - 1] = '\0';
198    } else
199        snprintf(keyIdentifier, sizeof(keyIdentifier), "U+%04X", toupper(windowsKeyCode));
200}
201
202// static
203int WebKeyboardEvent::windowsKeyCodeWithoutLocation(int keycode)
204{
205    switch (keycode) {
206    case VK_LCONTROL:
207    case VK_RCONTROL:
208        return VK_CONTROL;
209    case VK_LSHIFT:
210    case VK_RSHIFT:
211        return VK_SHIFT;
212    case VK_LMENU:
213    case VK_RMENU:
214        return VK_MENU;
215    default:
216        return keycode;
217    }
218}
219
220// static
221int WebKeyboardEvent::locationModifiersFromWindowsKeyCode(int keycode)
222{
223    switch (keycode) {
224    case VK_LCONTROL:
225    case VK_LSHIFT:
226    case VK_LMENU:
227    case VK_LWIN:
228        return IsLeft;
229    case VK_RCONTROL:
230    case VK_RSHIFT:
231    case VK_RMENU:
232    case VK_RWIN:
233        return IsRight;
234    default:
235        return 0;
236    }
237}
238
239} // namespace blink
240