sdlInput.cpp
Engine/source/platformSDL/sdlInput.cpp
Classes:
class
class
Public Functions
Detailed Description
Public Defines
KEY_FIRST()
NUM_KEYS() ( + 1 )
Public Functions
buildScanCodeArray()
fillAsciiTable()
mapScanCode(U32 sdl, U32 torque)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platformInput.h" 25#include "console/engineAPI.h" 26#include "core/util/journal/process.h" 27#include "windowManager/platformWindowMgr.h" 28 29#include "sdlInput.h" 30#include "platform/platformInput.h" 31#include "sdlInputManager.h" 32#include "SDL.h" 33 34// Static class variables: 35InputManager* Input::smManager = NULL; 36bool Input::smActive; 37U8 Input::smModifierKeys; 38bool Input::smLastKeyboardActivated; 39bool Input::smLastMouseActivated; 40bool Input::smLastJoystickActivated; 41InputEvent Input::smInputEvent; 42 43static void fillAsciiTable() {} 44 45//------------------------------------------------------------------------------ 46// 47// This function gets the standard ASCII code corresponding to our key code 48// and the existing modifier key state. 49// 50//------------------------------------------------------------------------------ 51struct AsciiData 52{ 53 struct KeyData 54 { 55 U16 ascii; 56 bool isDeadChar; 57 }; 58 59 KeyData upper; 60 KeyData lower; 61 KeyData goofy; 62}; 63 64 65#define NUM_KEYS ( KEY_OEM_102 + 1 ) 66#define KEY_FIRST KEY_ESCAPE 67 68//------------------------------------------------------------------------------ 69void Input::init() 70{ 71 Con::printf( "Input Init:" ); 72 73 destroy(); 74 75 smActive = false; 76 smLastKeyboardActivated = true; 77 smLastMouseActivated = true; 78 smLastJoystickActivated = true; 79 80 SDL_InitSubSystem( SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS ); 81 82 // Init the current modifier keys 83 setModifierKeys(0); 84 fillAsciiTable(); 85 Con::printf( "" ); 86 87 smManager = new SDLInputManager; 88 if (smManager) 89 { 90 SDLInputManager::init(); 91 } 92 93 // Set ourselves to participate in per-frame processing. 94 Process::notify(Input::process, PROCESS_INPUT_ORDER); 95 96} 97 98//------------------------------------------------------------------------------ 99U16 Input::getKeyCode( U16 asciiCode ) 100{ 101 if( asciiCode > 255 ) 102 return 0; 103 104 char c[2]; 105 c[0]= asciiCode; 106 c[1] = NULL; 107 return KeyMapSDL::getTorqueScanCodeFromSDL( SDL_GetScancodeFromKey( SDL_GetKeyFromName(c) ) ); 108} 109 110//------------------------------------------------------------------------------ 111U16 Input::getAscii( U16 keyCode, KEY_STATE keyState ) 112{ 113 if ( keyCode >= NUM_KEYS ) 114 return 0; 115 116 U32 SDLKey = KeyMapSDL::getSDLScanCodeFromTorque( keyCode ); 117 SDLKey = SDL_GetKeyFromScancode( (SDL_Scancode)SDLKey ); 118 119 const char *text = SDL_GetKeyName( SDLKey ); 120 if(text[1] != 0) 121 return 0; 122 U8 ret = text[0]; 123 124 if( !dIsalpha(ret) ) 125 return ret; 126 127 switch ( keyState ) 128 { 129 case STATE_LOWER: 130 return dTolower( ret ); 131 case STATE_UPPER: 132 return dToupper( ret ); 133 case STATE_GOOFY: 134 return 0; // TODO SDL 135 default: 136 return(0); 137 138 } 139} 140 141//------------------------------------------------------------------------------ 142void Input::destroy() 143{ 144 Process::remove(Input::process); 145 146 SDL_QuitSubSystem( SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER ); 147 148 if (smManager) 149 { 150 if (smManager->isEnabled()) 151 smManager->disable(); 152 delete smManager; 153 smManager = NULL; 154 } 155} 156 157//------------------------------------------------------------------------------ 158bool Input::enable() 159{ 160 if ( smManager && !smManager->isEnabled() ) 161 return( smManager->enable() ); 162 163 return( false ); 164} 165 166//------------------------------------------------------------------------------ 167void Input::disable() 168{ 169 if ( smManager && smManager->isEnabled() ) 170 smManager->disable(); 171} 172 173//------------------------------------------------------------------------------ 174 175void Input::activate() 176{ 177#ifdef UNICODE 178 //winState.imeHandle = ImmGetContext( getWin32WindowHandle() ); 179 //ImmReleaseContext( getWin32WindowHandle(), winState.imeHandle ); 180#endif 181 182 if (smManager && !smManager->isEnabled()) 183 smManager->enable(); 184 185 if ( smManager && smManager->isEnabled() && !smActive ) 186 { 187 Con::printf( "Activating Input..." ); 188 smActive = true; 189 } 190} 191 192//------------------------------------------------------------------------------ 193void Input::deactivate() 194{ 195 if (smManager && smManager->isEnabled()) 196 smManager->disable(); 197 198 if (smActive) 199 { 200 smActive = false; 201 Con::printf( "Input deactivated." ); 202 } 203} 204 205//------------------------------------------------------------------------------ 206bool Input::isEnabled() 207{ 208 if ( smManager ) 209 return smManager->isEnabled(); 210 return false; 211} 212 213//------------------------------------------------------------------------------ 214bool Input::isActive() 215{ 216 return smActive; 217} 218 219//------------------------------------------------------------------------------ 220void Input::process() 221{ 222 if ( smManager && smManager->isEnabled() && smActive ) 223 smManager->process(); 224} 225 226//------------------------------------------------------------------------------ 227InputManager* Input::getManager() 228{ 229 return( smManager ); 230} 231 232//----------------------------------------------------------------------------- 233// Clipboard functions 234const char* Platform::getClipboard() 235{ 236 //note - this function never returns NULL 237 return SDL_HasClipboardText() ? SDL_GetClipboardText() : ""; 238} 239 240//----------------------------------------------------------------------------- 241bool Platform::setClipboard(const char *text) 242{ 243 if (!text) 244 return false; 245 246 SDL_SetClipboardText(text); 247 248 return true; 249} 250 251 252namespace 253{ 254 const int TableSize = 256; 255 U32 SDL_T3D[256]; 256 U32 T3D_SDL[256]; 257 static bool _buildScanCode = true; 258} 259 260void mapScanCode(U32 sdl, U32 torque) 261{ 262 SDL_T3D[sdl] = torque; 263 T3D_SDL[torque] = sdl; 264} 265 266 267 268void buildScanCodeArray() 269{ 270 _buildScanCode = false; 271 272 for(int i = 0; i < TableSize; ++i) 273 { 274 SDL_T3D[i] = 0; 275 T3D_SDL[i] = 0; 276 } 277 278 // SDL, Torque 279 mapScanCode(SDL_SCANCODE_A, KEY_A); 280 mapScanCode(SDL_SCANCODE_B, KEY_B); 281 mapScanCode(SDL_SCANCODE_C, KEY_C); 282 mapScanCode(SDL_SCANCODE_D, KEY_D); 283 mapScanCode(SDL_SCANCODE_E, KEY_E); 284 mapScanCode(SDL_SCANCODE_F, KEY_F); 285 mapScanCode(SDL_SCANCODE_G, KEY_G); 286 mapScanCode(SDL_SCANCODE_H, KEY_H); 287 mapScanCode(SDL_SCANCODE_I, KEY_I); 288 mapScanCode(SDL_SCANCODE_J, KEY_J); 289 mapScanCode(SDL_SCANCODE_K, KEY_K); 290 mapScanCode(SDL_SCANCODE_L, KEY_L); 291 mapScanCode(SDL_SCANCODE_M, KEY_M); 292 mapScanCode(SDL_SCANCODE_N, KEY_N); 293 mapScanCode(SDL_SCANCODE_O, KEY_O); 294 mapScanCode(SDL_SCANCODE_P, KEY_P); 295 mapScanCode(SDL_SCANCODE_Q, KEY_Q); 296 mapScanCode(SDL_SCANCODE_R, KEY_R); 297 mapScanCode(SDL_SCANCODE_S, KEY_S); 298 mapScanCode(SDL_SCANCODE_T, KEY_T); 299 mapScanCode(SDL_SCANCODE_U, KEY_U); 300 mapScanCode(SDL_SCANCODE_V, KEY_V); 301 mapScanCode(SDL_SCANCODE_W, KEY_W); 302 mapScanCode(SDL_SCANCODE_X, KEY_X); 303 mapScanCode(SDL_SCANCODE_Y, KEY_Y); 304 mapScanCode(SDL_SCANCODE_Z, KEY_Z); 305 306 mapScanCode(SDL_SCANCODE_1, KEY_1); 307 mapScanCode(SDL_SCANCODE_2, KEY_2); 308 mapScanCode(SDL_SCANCODE_3, KEY_3); 309 mapScanCode(SDL_SCANCODE_4, KEY_4); 310 mapScanCode(SDL_SCANCODE_5, KEY_5); 311 mapScanCode(SDL_SCANCODE_6, KEY_6); 312 mapScanCode(SDL_SCANCODE_7, KEY_7); 313 mapScanCode(SDL_SCANCODE_8, KEY_8); 314 mapScanCode(SDL_SCANCODE_9, KEY_9); 315 mapScanCode(SDL_SCANCODE_0, KEY_0); 316 317 mapScanCode(SDL_SCANCODE_BACKSPACE, KEY_BACKSPACE); 318 mapScanCode(SDL_SCANCODE_TAB, KEY_TAB); 319 mapScanCode(SDL_SCANCODE_RETURN, KEY_RETURN); 320 mapScanCode(SDL_SCANCODE_LCTRL, KEY_CONTROL); 321 mapScanCode(SDL_SCANCODE_RCTRL, KEY_CONTROL); 322 mapScanCode(SDL_SCANCODE_LALT, KEY_ALT); 323 mapScanCode(SDL_SCANCODE_RALT, KEY_ALT); 324 mapScanCode(SDL_SCANCODE_LSHIFT, KEY_SHIFT); 325 mapScanCode(SDL_SCANCODE_RSHIFT, KEY_SHIFT); 326 mapScanCode(SDL_SCANCODE_PAUSE, KEY_PAUSE); 327 mapScanCode(SDL_SCANCODE_CAPSLOCK, KEY_CAPSLOCK); 328 mapScanCode(SDL_SCANCODE_ESCAPE, KEY_ESCAPE); 329 mapScanCode(SDL_SCANCODE_SPACE, KEY_SPACE); 330 mapScanCode(SDL_SCANCODE_PAGEDOWN, KEY_PAGE_DOWN); 331 mapScanCode(SDL_SCANCODE_PAGEUP, KEY_PAGE_UP); 332 mapScanCode(SDL_SCANCODE_END, KEY_END); 333 mapScanCode(SDL_SCANCODE_HOME, KEY_HOME); 334 mapScanCode(SDL_SCANCODE_LEFT, KEY_LEFT); 335 mapScanCode(SDL_SCANCODE_UP, KEY_UP); 336 mapScanCode(SDL_SCANCODE_RIGHT, KEY_RIGHT); 337 mapScanCode(SDL_SCANCODE_DOWN, KEY_DOWN); 338 mapScanCode(SDL_SCANCODE_PRINTSCREEN, KEY_PRINT); 339 mapScanCode(SDL_SCANCODE_INSERT, KEY_INSERT); 340 mapScanCode(SDL_SCANCODE_DELETE, KEY_DELETE); 341 mapScanCode(SDL_SCANCODE_HELP, KEY_HELP); 342 343 mapScanCode(SDL_SCANCODE_GRAVE, KEY_TILDE); 344 mapScanCode(SDL_SCANCODE_MINUS, KEY_MINUS); 345 mapScanCode(SDL_SCANCODE_EQUALS, KEY_EQUALS); 346 mapScanCode(SDL_SCANCODE_LEFTBRACKET, KEY_LBRACKET); 347 mapScanCode(SDL_SCANCODE_RIGHTBRACKET, KEY_RBRACKET); 348 mapScanCode(SDL_SCANCODE_BACKSLASH, KEY_BACKSLASH); 349 mapScanCode(SDL_SCANCODE_SEMICOLON, KEY_SEMICOLON); 350 mapScanCode(SDL_SCANCODE_APOSTROPHE, KEY_APOSTROPHE); 351 mapScanCode(SDL_SCANCODE_COMMA, KEY_COMMA); 352 mapScanCode(SDL_SCANCODE_PERIOD, KEY_PERIOD); 353 mapScanCode(SDL_SCANCODE_SLASH, KEY_SLASH); 354 mapScanCode(SDL_SCANCODE_KP_0, KEY_NUMPAD0); 355 mapScanCode(SDL_SCANCODE_KP_1, KEY_NUMPAD1); 356 mapScanCode(SDL_SCANCODE_KP_2, KEY_NUMPAD2); 357 mapScanCode(SDL_SCANCODE_KP_3, KEY_NUMPAD3); 358 mapScanCode(SDL_SCANCODE_KP_4, KEY_NUMPAD4); 359 mapScanCode(SDL_SCANCODE_KP_5, KEY_NUMPAD5); 360 mapScanCode(SDL_SCANCODE_KP_6, KEY_NUMPAD6); 361 mapScanCode(SDL_SCANCODE_KP_7, KEY_NUMPAD7); 362 mapScanCode(SDL_SCANCODE_KP_8, KEY_NUMPAD8); 363 mapScanCode(SDL_SCANCODE_KP_9, KEY_NUMPAD9); 364 mapScanCode(SDL_SCANCODE_KP_MULTIPLY, KEY_MULTIPLY); 365 mapScanCode(SDL_SCANCODE_KP_PLUS, KEY_ADD); 366 mapScanCode(SDL_SCANCODE_KP_EQUALS, KEY_SEPARATOR); 367 mapScanCode(SDL_SCANCODE_KP_MINUS, KEY_SUBTRACT); 368 mapScanCode(SDL_SCANCODE_KP_PERIOD, KEY_DECIMAL); 369 mapScanCode(SDL_SCANCODE_KP_DIVIDE, KEY_DIVIDE); 370 mapScanCode(SDL_SCANCODE_KP_ENTER, KEY_NUMPADENTER); 371 372 mapScanCode(SDL_SCANCODE_F1, KEY_F1); 373 mapScanCode(SDL_SCANCODE_F2, KEY_F2); 374 mapScanCode(SDL_SCANCODE_F3, KEY_F3); 375 mapScanCode(SDL_SCANCODE_F4, KEY_F4); 376 mapScanCode(SDL_SCANCODE_F5, KEY_F5); 377 mapScanCode(SDL_SCANCODE_F6, KEY_F6); 378 mapScanCode(SDL_SCANCODE_F7, KEY_F7); 379 mapScanCode(SDL_SCANCODE_F8, KEY_F8); 380 mapScanCode(SDL_SCANCODE_F9, KEY_F9); 381 mapScanCode(SDL_SCANCODE_F10, KEY_F10); 382 mapScanCode(SDL_SCANCODE_F11, KEY_F11); 383 mapScanCode(SDL_SCANCODE_F12, KEY_F12); 384 mapScanCode(SDL_SCANCODE_F13, KEY_F13); 385 mapScanCode(SDL_SCANCODE_F14, KEY_F14); 386 mapScanCode(SDL_SCANCODE_F15, KEY_F15); 387 mapScanCode(SDL_SCANCODE_F16, KEY_F16); 388 mapScanCode(SDL_SCANCODE_F17, KEY_F17); 389 mapScanCode(SDL_SCANCODE_F18, KEY_F18); 390 mapScanCode(SDL_SCANCODE_F19, KEY_F19); 391 mapScanCode(SDL_SCANCODE_F20, KEY_F20); 392 mapScanCode(SDL_SCANCODE_F21, KEY_F21); 393 mapScanCode(SDL_SCANCODE_F22, KEY_F22); 394 mapScanCode(SDL_SCANCODE_F23, KEY_F23); 395 mapScanCode(SDL_SCANCODE_F24, KEY_F24); 396 397 //mapScanCode(SDL_SCANCODE_LOCKINGNUMLOCK, KEY_NUMLOCK); 398 //mapScanCode(SDL_SCANCODE_LOCKINGSCROLLLOCK, KEY_SCROLLLOCK); 399 mapScanCode(SDL_SCANCODE_LCTRL, KEY_LCONTROL); 400 mapScanCode(SDL_SCANCODE_RCTRL, KEY_RCONTROL); 401 mapScanCode(SDL_SCANCODE_LALT, KEY_LALT); 402 mapScanCode(SDL_SCANCODE_RALT, KEY_RALT); 403 mapScanCode(SDL_SCANCODE_LSHIFT, KEY_LSHIFT); 404 mapScanCode(SDL_SCANCODE_RSHIFT, KEY_RSHIFT); 405 //mapScanCode(____, KEY_WIN_LWINDOW); 406 //mapScanCode(____, KEY_WIN_RWINDOW); 407 //mapScanCode(____, KEY_WIN_APPS); 408 //mapScanCode(____, KEY_OEM_102); 409 410 //mapScanCode(____, KEY_MAC_OPT); 411 //mapScanCode(____, KEY_MAC_LOPT); 412 //mapScanCode(____, KEY_MAC_ROPT); 413 414 //for(int i = 0; i < 48; ++i) 415 // mapScanCode(____, KEY_BUTTON0 + i ); 416 417 //mapScanCode(____, KEY_ANYKEY); 418} 419 420U32 KeyMapSDL::getTorqueScanCodeFromSDL(U32 sdl) 421{ 422 if(_buildScanCode) 423 buildScanCodeArray(); 424 425 return SDL_T3D[sdl]; 426} 427 428U32 KeyMapSDL::getSDLScanCodeFromTorque(U32 torque) 429{ 430 if(_buildScanCode) 431 buildScanCodeArray(); 432 433 return T3D_SDL[torque]; 434} 435