event.cpp
Engine/source/platform/input/event.cpp
Classes:
class
Public Variables
Detailed Description
Public Variables
CodeMapping gVirtualMap []
MODULE_END
MODULE_INIT
MODULE_SHUTDOWN
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/input/event.h" 25#include "core/module.h" 26#include "core/util/journal/process.h" 27#include "core/strings/stringFunctions.h" 28#include "core/stringTable.h" 29#include "platform/platformInput.h" 30#include "math/mQuat.h" 31#include "math/mAngAxis.h" 32 33MODULE_BEGIN( InputEventManager ) 34 35 MODULE_INIT_BEFORE( SIM ) 36 MODULE_SHUTDOWN_AFTER( SIM ) 37 38 MODULE_INIT 39 { 40 ManagedSingleton< InputEventManager >::createSingleton(); 41 } 42 43 MODULE_SHUTDOWN 44 { 45 ManagedSingleton< InputEventManager >::deleteSingleton(); 46 } 47 48MODULE_END; 49 50InputEventManager::InputEventManager() 51{ 52 mNextDeviceTypeCode = INPUT_DEVICE_PLUGIN_DEVICES_START; 53 mNextDeviceCode = INPUT_DEVICE_PLUGIN_CODES_START; 54 55 buildVirtualMap(); 56} 57 58InputEventManager::~InputEventManager() 59{ 60} 61 62U32 InputEventManager::getNextDeviceType() 63{ 64 U32 code = mNextDeviceTypeCode; 65 ++mNextDeviceTypeCode; 66 return code; 67} 68 69U32 InputEventManager::getNextDeviceCode() 70{ 71 U32 code = mNextDeviceCode; 72 ++mNextDeviceCode; 73 return code; 74} 75 76void InputEventManager::registerDevice(IInputDevice* device) 77{ 78 // Make sure the device is not already registered 79 for(U32 i=0; i<mDeviceList.size(); ++i) 80 { 81 if(mDeviceList[i] == device) 82 return; 83 } 84 85 // Add the new device to the list 86 mDeviceList.push_back(device); 87} 88 89void InputEventManager::unregisterDevice(IInputDevice* device) 90{ 91 // Remove the device from the list 92 for(U32 i=0; i<mDeviceList.size(); ++i) 93 { 94 if(mDeviceList[i] == device) 95 { 96 mDeviceList.erase(i); 97 return; 98 } 99 } 100} 101 102bool InputEventManager::isRegisteredDevice(const char* name) 103{ 104 for(Vector<IInputDevice*>::iterator itr = mDeviceList.begin(); itr != mDeviceList.end(); ++itr) 105 { 106 if((*itr)->isEnabled()) 107 { 108 const char* deviceName = (*itr)->getDeviceName(); 109 if(dStrnicmp(name, deviceName, dStrlen(deviceName)) == 0) 110 { 111 return true; 112 } 113 } 114 } 115 116 return false; 117} 118 119bool InputEventManager::isRegisteredDevice(U32 type) 120{ 121 for(Vector<IInputDevice*>::iterator itr = mDeviceList.begin(); itr != mDeviceList.end(); ++itr) 122 { 123 if((*itr)->isEnabled()) 124 { 125 U32 deviceType = (*itr)->getDeviceType(); 126 if(deviceType == type) 127 { 128 return true; 129 } 130 } 131 } 132 133 return false; 134} 135 136bool InputEventManager::isRegisteredDeviceWithAttributes(const char* name, U32& deviceType, U32&nameLen) 137{ 138 for(Vector<IInputDevice*>::iterator itr = mDeviceList.begin(); itr != mDeviceList.end(); ++itr) 139 { 140 if((*itr)->isEnabled()) 141 { 142 const char* deviceName = (*itr)->getDeviceName(); 143 S32 length = dStrlen(deviceName); 144 if(dStrnicmp(name, deviceName, length) == 0) 145 { 146 deviceType = (*itr)->getDeviceType(); 147 nameLen = length; 148 return true; 149 } 150 } 151 } 152 153 return false; 154} 155 156const char* InputEventManager::getRegisteredDeviceName(U32 type) 157{ 158 for(Vector<IInputDevice*>::iterator itr = mDeviceList.begin(); itr != mDeviceList.end(); ++itr) 159 { 160 if((*itr)->isEnabled()) 161 { 162 U32 deviceType = (*itr)->getDeviceType(); 163 if(deviceType == type) 164 { 165 return (*itr)->getDeviceName(); 166 } 167 } 168 } 169 170 return NULL; 171} 172 173void InputEventManager::start() 174{ 175 Process::notify(this, &InputEventManager::process, PROCESS_INPUT_ORDER); 176} 177 178void InputEventManager::stop() 179{ 180 Process::remove(this, &InputEventManager::process); 181} 182 183void InputEventManager::process() 184{ 185 // Process each device 186 for(Vector<IInputDevice*>::iterator itr = mDeviceList.begin(); itr != mDeviceList.end(); ++itr) 187 { 188 if((*itr)->isEnabled()) 189 { 190 (*itr)->process(); 191 } 192 } 193} 194 195// Used for the old virtual map table that was originally in actionMap.cpp 196struct CodeMapping 197{ 198 const char* pDescription; 199 InputEventType type; 200 InputObjectInstances code; 201}; 202 203CodeMapping gVirtualMap[] = 204{ 205 //-------------------------------------- KEYBOARD EVENTS 206 // 207 { "backspace", SI_KEY, KEY_BACKSPACE }, 208 { "tab", SI_KEY, KEY_TAB }, 209 210 { "return", SI_KEY, KEY_RETURN }, 211 { "enter", SI_KEY, KEY_RETURN }, 212 213 { "shift", SI_KEY, KEY_SHIFT }, 214 { "ctrl", SI_KEY, KEY_CONTROL }, 215 { "alt", SI_KEY, KEY_ALT }, 216 { "pause", SI_KEY, KEY_PAUSE }, 217 { "capslock", SI_KEY, KEY_CAPSLOCK }, 218 219 { "escape", SI_KEY, KEY_ESCAPE }, 220 221 { "space", SI_KEY, KEY_SPACE }, 222 { "pagedown", SI_KEY, KEY_PAGE_DOWN }, 223 { "pageup", SI_KEY, KEY_PAGE_UP }, 224 { "end", SI_KEY, KEY_END }, 225 { "home", SI_KEY, KEY_HOME }, 226 { "left", SI_KEY, KEY_LEFT }, 227 { "up", SI_KEY, KEY_UP }, 228 { "right", SI_KEY, KEY_RIGHT }, 229 { "down", SI_KEY, KEY_DOWN }, 230 { "print", SI_KEY, KEY_PRINT }, 231 { "insert", SI_KEY, KEY_INSERT }, 232 { "delete", SI_KEY, KEY_DELETE }, 233 { "help", SI_KEY, KEY_HELP }, 234 235 { "win_lwindow", SI_KEY, KEY_WIN_LWINDOW }, 236 { "win_rwindow", SI_KEY, KEY_WIN_RWINDOW }, 237 { "win_apps", SI_KEY, KEY_WIN_APPS }, 238 239 { "cmd", SI_KEY, KEY_ALT }, 240 { "opt", SI_KEY, KEY_MAC_OPT }, 241 { "lopt", SI_KEY, KEY_MAC_LOPT }, 242 { "ropt", SI_KEY, KEY_MAC_ROPT }, 243 244 { "numpad0", SI_KEY, KEY_NUMPAD0 }, 245 { "numpad1", SI_KEY, KEY_NUMPAD1 }, 246 { "numpad2", SI_KEY, KEY_NUMPAD2 }, 247 { "numpad3", SI_KEY, KEY_NUMPAD3 }, 248 { "numpad4", SI_KEY, KEY_NUMPAD4 }, 249 { "numpad5", SI_KEY, KEY_NUMPAD5 }, 250 { "numpad6", SI_KEY, KEY_NUMPAD6 }, 251 { "numpad7", SI_KEY, KEY_NUMPAD7 }, 252 { "numpad8", SI_KEY, KEY_NUMPAD8 }, 253 { "numpad9", SI_KEY, KEY_NUMPAD9 }, 254 { "numpadmult", SI_KEY, KEY_MULTIPLY }, 255 { "numpadadd", SI_KEY, KEY_ADD }, 256 { "numpadsep", SI_KEY, KEY_SEPARATOR }, 257 { "numpadminus", SI_KEY, KEY_SUBTRACT }, 258 { "numpaddecimal", SI_KEY, KEY_DECIMAL }, 259 { "numpaddivide", SI_KEY, KEY_DIVIDE }, 260 { "numpadenter", SI_KEY, KEY_NUMPADENTER }, 261 262 { "f1", SI_KEY, KEY_F1 }, 263 { "f2", SI_KEY, KEY_F2 }, 264 { "f3", SI_KEY, KEY_F3 }, 265 { "f4", SI_KEY, KEY_F4 }, 266 { "f5", SI_KEY, KEY_F5 }, 267 { "f6", SI_KEY, KEY_F6 }, 268 { "f7", SI_KEY, KEY_F7 }, 269 { "f8", SI_KEY, KEY_F8 }, 270 { "f9", SI_KEY, KEY_F9 }, 271 { "f10", SI_KEY, KEY_F10 }, 272 { "f11", SI_KEY, KEY_F11 }, 273 { "f12", SI_KEY, KEY_F12 }, 274 { "f13", SI_KEY, KEY_F13 }, 275 { "f14", SI_KEY, KEY_F14 }, 276 { "f15", SI_KEY, KEY_F15 }, 277 { "f16", SI_KEY, KEY_F16 }, 278 { "f17", SI_KEY, KEY_F17 }, 279 { "f18", SI_KEY, KEY_F18 }, 280 { "f19", SI_KEY, KEY_F19 }, 281 { "f20", SI_KEY, KEY_F20 }, 282 { "f21", SI_KEY, KEY_F21 }, 283 { "f22", SI_KEY, KEY_F22 }, 284 { "f23", SI_KEY, KEY_F23 }, 285 { "f24", SI_KEY, KEY_F24 }, 286 287 { "numlock", SI_KEY, KEY_NUMLOCK }, 288 { "scrolllock", SI_KEY, KEY_SCROLLLOCK }, 289 290 { "lshift", SI_KEY, KEY_LSHIFT }, 291 { "rshift", SI_KEY, KEY_RSHIFT }, 292 { "lcontrol", SI_KEY, KEY_LCONTROL }, 293 { "rcontrol", SI_KEY, KEY_RCONTROL }, 294 { "lalt", SI_KEY, KEY_LALT }, 295 { "ralt", SI_KEY, KEY_RALT }, 296 { "tilde", SI_KEY, KEY_TILDE }, 297 298 { "minus", SI_KEY, KEY_MINUS }, 299 { "equals", SI_KEY, KEY_EQUALS }, 300 { "lbracket", SI_KEY, KEY_LBRACKET }, 301 { "rbracket", SI_KEY, KEY_RBRACKET }, 302 { "backslash", SI_KEY, KEY_BACKSLASH }, 303 { "semicolon", SI_KEY, KEY_SEMICOLON }, 304 { "apostrophe", SI_KEY, KEY_APOSTROPHE }, 305 { "comma", SI_KEY, KEY_COMMA }, 306 { "period", SI_KEY, KEY_PERIOD }, 307 { "slash", SI_KEY, KEY_SLASH }, 308 { "lessthan", SI_KEY, KEY_OEM_102 }, 309 310 //-------------------------------------- BUTTON EVENTS 311 // Joystick/Mouse buttons 312 { "button0", SI_BUTTON, KEY_BUTTON0 }, 313 { "button1", SI_BUTTON, KEY_BUTTON1 }, 314 { "button2", SI_BUTTON, KEY_BUTTON2 }, 315 { "button3", SI_BUTTON, KEY_BUTTON3 }, 316 { "button4", SI_BUTTON, KEY_BUTTON4 }, 317 { "button5", SI_BUTTON, KEY_BUTTON5 }, 318 { "button6", SI_BUTTON, KEY_BUTTON6 }, 319 { "button7", SI_BUTTON, KEY_BUTTON7 }, 320 { "button8", SI_BUTTON, KEY_BUTTON8 }, 321 { "button9", SI_BUTTON, KEY_BUTTON9 }, 322 { "button10", SI_BUTTON, KEY_BUTTON10 }, 323 { "button11", SI_BUTTON, KEY_BUTTON11 }, 324 { "button12", SI_BUTTON, KEY_BUTTON12 }, 325 { "button13", SI_BUTTON, KEY_BUTTON13 }, 326 { "button14", SI_BUTTON, KEY_BUTTON14 }, 327 { "button15", SI_BUTTON, KEY_BUTTON15 }, 328 { "button16", SI_BUTTON, KEY_BUTTON16 }, 329 { "button17", SI_BUTTON, KEY_BUTTON17 }, 330 { "button18", SI_BUTTON, KEY_BUTTON18 }, 331 { "button19", SI_BUTTON, KEY_BUTTON19 }, 332 { "button20", SI_BUTTON, KEY_BUTTON20 }, 333 { "button21", SI_BUTTON, KEY_BUTTON21 }, 334 { "button22", SI_BUTTON, KEY_BUTTON22 }, 335 { "button23", SI_BUTTON, KEY_BUTTON23 }, 336 { "button24", SI_BUTTON, KEY_BUTTON24 }, 337 { "button25", SI_BUTTON, KEY_BUTTON25 }, 338 { "button26", SI_BUTTON, KEY_BUTTON26 }, 339 { "button27", SI_BUTTON, KEY_BUTTON27 }, 340 { "button28", SI_BUTTON, KEY_BUTTON28 }, 341 { "button29", SI_BUTTON, KEY_BUTTON29 }, 342 { "button30", SI_BUTTON, KEY_BUTTON30 }, 343 { "button31", SI_BUTTON, KEY_BUTTON31 }, 344 { "button32", SI_BUTTON, KEY_BUTTON32 }, 345 { "button33", SI_BUTTON, KEY_BUTTON33 }, 346 { "button34", SI_BUTTON, KEY_BUTTON34 }, 347 { "button35", SI_BUTTON, KEY_BUTTON35 }, 348 { "button36", SI_BUTTON, KEY_BUTTON36 }, 349 { "button37", SI_BUTTON, KEY_BUTTON37 }, 350 { "button38", SI_BUTTON, KEY_BUTTON38 }, 351 { "button39", SI_BUTTON, KEY_BUTTON39 }, 352 { "button40", SI_BUTTON, KEY_BUTTON40 }, 353 { "button41", SI_BUTTON, KEY_BUTTON41 }, 354 { "button42", SI_BUTTON, KEY_BUTTON42 }, 355 { "button43", SI_BUTTON, KEY_BUTTON43 }, 356 { "button44", SI_BUTTON, KEY_BUTTON44 }, 357 { "button45", SI_BUTTON, KEY_BUTTON45 }, 358 { "button46", SI_BUTTON, KEY_BUTTON46 }, 359 { "button47", SI_BUTTON, KEY_BUTTON47 }, 360 361 //-------------------------------------- MOVE EVENTS 362 // Mouse/Joystick axes: 363 { "xaxis", SI_AXIS, SI_XAXIS }, 364 { "yaxis", SI_AXIS, SI_YAXIS }, 365 { "zaxis", SI_AXIS, SI_ZAXIS }, 366 { "rxaxis", SI_AXIS, SI_RXAXIS }, 367 { "ryaxis", SI_AXIS, SI_RYAXIS }, 368 { "rzaxis", SI_AXIS, SI_RZAXIS }, 369 { "slider", SI_AXIS, SI_SLIDER }, 370 371 //-------------------------------------- POV EVENTS 372 // Joystick POV: 373 { "xpov", SI_POV, SI_XPOV }, 374 { "ypov", SI_POV, SI_YPOV }, 375 { "upov", SI_POV, SI_UPOV }, 376 { "dpov", SI_POV, SI_DPOV }, 377 { "lpov", SI_POV, SI_LPOV }, 378 { "rpov", SI_POV, SI_RPOV }, 379 { "xpov2", SI_POV, SI_XPOV2 }, 380 { "ypov2", SI_POV, SI_YPOV2 }, 381 { "upov2", SI_POV, SI_UPOV2 }, 382 { "dpov2", SI_POV, SI_DPOV2 }, 383 { "lpov2", SI_POV, SI_LPOV2 }, 384 { "rpov2", SI_POV, SI_RPOV2 }, 385 386 //-------------------------------------- GAMEPAD EVENTS 387 // Controller connect / disconnect: 388 { "connect", SI_BUTTON, XI_CONNECT }, 389 390 // L & R Thumbsticks: 391 { "thumblx", SI_AXIS, XI_THUMBLX }, 392 { "thumbly", SI_AXIS, XI_THUMBLY }, 393 { "thumbrx", SI_AXIS, XI_THUMBRX }, 394 { "thumbry", SI_AXIS, XI_THUMBRY }, 395 396 // L & R Triggers: 397 { "triggerl", SI_AXIS, XI_LEFT_TRIGGER }, 398 { "triggerr", SI_AXIS, XI_RIGHT_TRIGGER }, 399 400 // DPAD Buttons: 401 { "dpadu", SI_BUTTON, SI_UPOV }, 402 { "dpadd", SI_BUTTON, SI_DPOV }, 403 { "dpadl", SI_BUTTON, SI_LPOV }, 404 { "dpadr", SI_BUTTON, SI_RPOV }, 405 406 // START & BACK Buttons: 407 { "btn_start", SI_BUTTON, XI_START }, 408 { "btn_back", SI_BUTTON, XI_BACK }, 409 410 // L & R Thumbstick Buttons: 411 { "btn_lt", SI_BUTTON, XI_LEFT_THUMB }, 412 { "btn_rt", SI_BUTTON, XI_RIGHT_THUMB }, 413 414 // L & R Shoulder Buttons: 415 { "btn_l", SI_BUTTON, XI_LEFT_SHOULDER }, 416 { "btn_r", SI_BUTTON, XI_RIGHT_SHOULDER }, 417 418 // Primary buttons: 419 { "btn_a", SI_BUTTON, XI_A }, 420 { "btn_b", SI_BUTTON, XI_B }, 421 { "btn_x", SI_BUTTON, XI_X }, 422 { "btn_y", SI_BUTTON, XI_Y }, 423 424 //-------------------------------------- MISCELLANEOUS EVENTS 425 // 426 427 { "anykey", SI_KEY, KEY_ANYKEY }, 428 { "nomatch", SI_UNKNOWN, (InputObjectInstances)0xFFFFFFFF } 429}; 430 431void InputEventManager::buildVirtualMap() 432{ 433 char desc[256]; 434 VirtualMapData* data; 435 436 for (U32 j = 0; gVirtualMap[j].code != 0xFFFFFFFF; j++) 437 { 438 // Make sure the description is lower case 439 desc[0] = 0; 440 dStrncpy(desc, gVirtualMap[j].pDescription, 255); 441 dStrlwr(desc); 442 443 data = new VirtualMapData(); 444 data->type = gVirtualMap[j].type; 445 data->code = gVirtualMap[j].code; 446 data->desc = StringTable->insert(desc); 447 448 mVirtualMap.insert(data, desc); 449 mActionCodeMap.insertUnique(data->code, *data); 450 } 451} 452 453void InputEventManager::addVirtualMap(const char* description, InputEventType type, InputObjectInstances code) 454{ 455 // Make sure the description is lower case 456 char desc[256]; 457 desc[0] = 0; 458 dStrncpy(desc, description, 255); 459 dStrlwr(desc); 460 461 VirtualMapData* data = new VirtualMapData(); 462 data->type = type; 463 data->code = code; 464 data->desc = StringTable->insert(desc); 465 466 mVirtualMap.insert(data, desc); 467 mActionCodeMap.insertUnique(data->code, *data); 468} 469 470InputEventManager::VirtualMapData* InputEventManager::findVirtualMap(const char* description) 471{ 472 char desc[256]; 473 desc[0] = 0; 474 dStrncpy(desc, description, 255); 475 dStrlwr(desc); 476 477 return mVirtualMap.retreive(desc); 478} 479 480const char* InputEventManager::findVirtualMapDescFromCode(U32 code) 481{ 482 HashTable<U32, VirtualMapData>::Iterator itr = mActionCodeMap.find(code); 483 if(itr != mActionCodeMap.end()) 484 return itr->value.desc; 485 486 return NULL; 487} 488 489void InputEventManager::buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, S32 iValue) 490{ 491 InputEventInfo newEvent; 492 493 newEvent.deviceType = deviceType; 494 newEvent.deviceInst = deviceInst; 495 newEvent.objType = objType; 496 newEvent.objInst = objInst; 497 newEvent.action = action; 498 newEvent.iValue = iValue; 499 500 newEvent.postToSignal(Input::smInputEvent); 501} 502 503void InputEventManager::buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, F32 fValue) 504{ 505 InputEventInfo newEvent; 506 507 newEvent.deviceType = deviceType; 508 newEvent.deviceInst = deviceInst; 509 newEvent.objType = objType; 510 newEvent.objInst = objInst; 511 newEvent.action = action; 512 newEvent.fValue = fValue; 513 514 newEvent.postToSignal(Input::smInputEvent); 515} 516 517void InputEventManager::buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, Point3F& pValue) 518{ 519 InputEventInfo newEvent; 520 521 newEvent.deviceType = deviceType; 522 newEvent.deviceInst = deviceInst; 523 newEvent.objType = objType; 524 newEvent.objInst = objInst; 525 newEvent.action = action; 526 newEvent.fValue = pValue.x; 527 newEvent.fValue2 = pValue.y; 528 newEvent.fValue3 = pValue.z; 529 530 newEvent.postToSignal(Input::smInputEvent); 531} 532 533void InputEventManager::buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, QuatF& qValue) 534{ 535 InputEventInfo newEvent; 536 537 newEvent.deviceType = deviceType; 538 newEvent.deviceInst = deviceInst; 539 newEvent.objType = objType; 540 newEvent.objInst = objInst; 541 newEvent.action = action; 542 newEvent.fValue = qValue.x; 543 newEvent.fValue2 = qValue.y; 544 newEvent.fValue3 = qValue.z; 545 newEvent.fValue4 = qValue.w; 546 547 newEvent.postToSignal(Input::smInputEvent); 548} 549 550void InputEventManager::buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, AngAxisF& aValue) 551{ 552 InputEventInfo newEvent; 553 554 newEvent.deviceType = deviceType; 555 newEvent.deviceInst = deviceInst; 556 newEvent.objType = objType; 557 newEvent.objInst = objInst; 558 newEvent.action = action; 559 newEvent.fValue = aValue.axis.x; 560 newEvent.fValue2 = aValue.axis.y; 561 newEvent.fValue3 = aValue.axis.z; 562 newEvent.fValue4 = aValue.angle; 563 564 newEvent.postToSignal(Input::smInputEvent); 565} 566 567