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