EmscriptenInputManager.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Portions Copyright (c) 2014 James S Urquhart
  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. #include "platformEmscripten/platformEmscripten.h"
  24. #include "console/consoleTypes.h"
  25. #include "platform/event.h"
  26. #include "game/gameInterface.h"
  27. #include "platformEmscripten/EmscriptenInputManager.h"
  28. #include "math/mMathFn.h"
  29. #include <SDL/SDL.h>
  30. // ascii table
  31. AsciiData AsciiTable[NUM_KEYS];
  32. // keymap table
  33. static const U32 SDLtoTKeyMapSize = SDLK_LAST;
  34. static U8 SDLtoTKeyMap[SDLtoTKeyMapSize];
  35. static bool keyMapsInitialized = false;
  36. // helper functions
  37. static void MapKey(Uint16 SDLkey, U8 tkey);
  38. static void InitKeyMaps();
  39. static inline U8 TranslateSDLKeytoTKey(SDLKey keysym);
  40. // constants
  41. static const U32 MouseMask = SDL_MOUSEMOTION | SDL_MOUSEBUTTONUP;//SDL_MOUSEEVENTMASK;
  42. static const U32 KeyboardMask = SDL_KEYUP;//SDL_KEYUPMASK | SDL_KEYDOWNMASK;
  43. static const U32 JoystickMask = SDL_JOYAXISMOTION | SDL_JOYBUTTONUP;//SDL_JOYEVENTMASK;
  44. static const U32 AllInputEvents = MouseMask | KeyboardMask | JoystickMask;
  45. // defined in SDL
  46. extern "C" Uint16 X11_KeyToUnicode( SDLKey keysym, SDLMod modifiers );
  47. //==============================================================================
  48. // Static helper functions
  49. //==============================================================================
  50. static void MapKey(Uint16 SDLkey, U8 tkey)
  51. {
  52. SDLtoTKeyMap[SDLkey] = tkey;
  53. AsciiTable[tkey].lower.ascii = SDLkey;
  54. AsciiTable[tkey].upper.ascii = SDLkey;
  55. AsciiTable[tkey].goofy.ascii = SDLkey;
  56. switch (tkey)
  57. {
  58. case KEY_0:
  59. AsciiTable[tkey].upper.ascii = (U16)')';
  60. break;
  61. case KEY_1:
  62. AsciiTable[tkey].upper.ascii = (U16)'!';
  63. break;
  64. case KEY_2:
  65. AsciiTable[tkey].upper.ascii = (U16)'@';
  66. break;
  67. case KEY_3:
  68. AsciiTable[tkey].upper.ascii = 163;
  69. break;
  70. case KEY_4:
  71. AsciiTable[tkey].upper.ascii = (U16)'$';
  72. break;
  73. case KEY_5:
  74. AsciiTable[tkey].upper.ascii = (U16)'%';
  75. break;
  76. case KEY_6:
  77. AsciiTable[tkey].upper.ascii = (U16)'^';
  78. break;
  79. case KEY_7:
  80. AsciiTable[tkey].upper.ascii = (U16)'&';
  81. break;
  82. case KEY_8:
  83. AsciiTable[tkey].upper.ascii = (U16)'*';
  84. break;
  85. case KEY_9:
  86. AsciiTable[tkey].upper.ascii = (U16)'(';
  87. break;
  88. case KEY_MINUS:
  89. AsciiTable[tkey].upper.ascii = (U16)'_';
  90. break;
  91. case KEY_EQUALS:
  92. AsciiTable[tkey].upper.ascii = (U16)'+';
  93. break;
  94. case KEY_TILDE:
  95. AsciiTable[tkey].upper.ascii = (U16)126;
  96. break;
  97. case KEY_SEMICOLON:
  98. AsciiTable[tkey].upper.ascii = ':';
  99. break;
  100. case KEY_APOSTROPHE:
  101. AsciiTable[tkey].upper.ascii = '"';
  102. break;
  103. case KEY_BACKSLASH:
  104. AsciiTable[tkey].upper.ascii = '|';
  105. break;
  106. case KEY_LBRACKET:
  107. AsciiTable[tkey].upper.ascii = '{';
  108. break;
  109. case KEY_RBRACKET:
  110. AsciiTable[tkey].upper.ascii = '}';
  111. break;
  112. case KEY_COMMA:
  113. AsciiTable[tkey].upper.ascii = '<';
  114. break;
  115. case KEY_PERIOD:
  116. AsciiTable[tkey].upper.ascii = '>';
  117. break;
  118. }
  119. }
  120. //------------------------------------------------------------------------------
  121. void InitKeyMaps()
  122. {
  123. Con::printf("InitKeyMaps size == %i, ASCIITABLE SIZE == %i", SDLtoTKeyMapSize, NUM_KEYS);
  124. dMemset( &AsciiTable, 0, sizeof( AsciiTable ) );
  125. dMemset(SDLtoTKeyMap, KEY_NULL, sizeof( SDLtoTKeyMap ));
  126. // set up the X to Torque key map
  127. // stuff
  128. MapKey(SDLK_BACKSPACE, KEY_BACKSPACE);
  129. MapKey(SDLK_TAB, KEY_TAB);
  130. MapKey(SDLK_RETURN, KEY_RETURN);
  131. MapKey(SDLK_PAUSE, KEY_PAUSE);
  132. MapKey(SDLK_CAPSLOCK, KEY_CAPSLOCK);
  133. MapKey(SDLK_ESCAPE, KEY_ESCAPE);
  134. // more stuff
  135. MapKey(SDLK_SPACE, KEY_SPACE);
  136. MapKey(SDLK_PAGEDOWN, KEY_PAGE_DOWN);
  137. MapKey(SDLK_PAGEUP, KEY_PAGE_UP);
  138. MapKey(SDLK_END, KEY_END);
  139. MapKey(SDLK_HOME, KEY_HOME);
  140. MapKey(SDLK_LEFT, KEY_LEFT);
  141. MapKey(SDLK_UP, KEY_UP);
  142. MapKey(SDLK_RIGHT, KEY_RIGHT);
  143. MapKey(SDLK_DOWN, KEY_DOWN);
  144. MapKey(SDLK_PRINT, KEY_PRINT);
  145. MapKey(SDLK_INSERT, KEY_INSERT);
  146. MapKey(SDLK_DELETE, KEY_DELETE);
  147. S32 keysym;
  148. S32 tkeycode;
  149. // main numeric keys
  150. for (keysym = SDLK_0, tkeycode = KEY_0;
  151. keysym <= SDLK_9;
  152. ++keysym, ++tkeycode)
  153. MapKey(static_cast<SDLKey>(keysym), tkeycode);
  154. // lowercase letters
  155. for (keysym = SDLK_a, tkeycode = KEY_A;
  156. keysym <= SDLK_z;
  157. ++keysym, ++tkeycode)
  158. MapKey(static_cast<SDLKey>(keysym), tkeycode);
  159. // various punctuation
  160. MapKey('|', KEY_TILDE);
  161. MapKey(SDLK_BACKQUOTE, KEY_TILDE);
  162. MapKey(SDLK_MINUS, KEY_MINUS);
  163. MapKey(SDLK_EQUALS, KEY_EQUALS);
  164. MapKey(SDLK_LEFTBRACKET, KEY_LBRACKET);
  165. MapKey('{', KEY_LBRACKET);
  166. MapKey(SDLK_RIGHTBRACKET, KEY_RBRACKET);
  167. MapKey('}', KEY_RBRACKET);
  168. MapKey(SDLK_BACKSLASH, KEY_BACKSLASH);
  169. MapKey(SDLK_SEMICOLON, KEY_SEMICOLON);
  170. MapKey(SDLK_QUOTE, KEY_APOSTROPHE);
  171. MapKey(SDLK_COMMA, KEY_COMMA);
  172. MapKey(SDLK_PERIOD, KEY_PERIOD);
  173. MapKey(SDLK_SLASH, KEY_SLASH);
  174. // numpad numbers
  175. for (keysym = SDLK_KP0, tkeycode = KEY_NUMPAD0;
  176. keysym <= SDLK_KP9;
  177. ++keysym, ++tkeycode)
  178. MapKey(static_cast<SDLKey>(keysym), tkeycode);
  179. // other numpad stuff
  180. MapKey(SDLK_KP_MULTIPLY, KEY_MULTIPLY);
  181. MapKey(SDLK_KP_PLUS, KEY_ADD);
  182. MapKey(SDLK_KP_EQUALS, KEY_SEPARATOR);
  183. MapKey(SDLK_KP_MINUS, KEY_SUBTRACT);
  184. MapKey(SDLK_KP_PERIOD, KEY_DECIMAL);
  185. MapKey(SDLK_KP_DIVIDE, KEY_DIVIDE);
  186. MapKey(SDLK_KP_ENTER, KEY_NUMPADENTER);
  187. // F keys
  188. for (keysym = SDLK_F1, tkeycode = KEY_F1;
  189. keysym <= SDLK_F15;
  190. ++keysym, ++tkeycode)
  191. MapKey(static_cast<SDLKey>(keysym), tkeycode);
  192. // various modifiers
  193. MapKey(SDLK_NUMLOCK, KEY_NUMLOCK);
  194. MapKey(SDLK_SCROLLOCK, KEY_SCROLLLOCK);
  195. MapKey(SDLK_LCTRL, KEY_LCONTROL);
  196. MapKey(SDLK_RCTRL, KEY_RCONTROL);
  197. MapKey(SDLK_LALT, KEY_LALT);
  198. MapKey(SDLK_RALT, KEY_RALT);
  199. MapKey(313, KEY_RALT);
  200. MapKey(SDLK_LSHIFT, KEY_LSHIFT);
  201. MapKey(SDLK_RSHIFT, KEY_RSHIFT);
  202. MapKey(SDLK_LSUPER, KEY_WIN_LWINDOW);
  203. MapKey(SDLK_RSUPER, KEY_WIN_RWINDOW);
  204. MapKey(SDLK_MENU, KEY_WIN_APPS);
  205. MapKey(SDLK_MODE, KEY_OEM_102);
  206. keyMapsInitialized = true;
  207. };
  208. //------------------------------------------------------------------------------
  209. U8 TranslateSDLKeytoTKey(SDLKey keysym)
  210. {
  211. if (!keyMapsInitialized)
  212. {
  213. Con::printf("WARNING: SDLkeysymMap is not initialized");
  214. return 0;
  215. }
  216. if (keysym < 0 ||
  217. static_cast<U32>(keysym) >= SDLtoTKeyMapSize)
  218. {
  219. Con::printf("WARNING: invalid keysym: %d", keysym);
  220. return 0;
  221. }
  222. return SDLtoTKeyMap[keysym];
  223. }
  224. //==============================================================================
  225. // UInputManager
  226. //==============================================================================
  227. UInputManager::UInputManager()
  228. {
  229. mActive = false;
  230. mEnabled = false;
  231. mLocking = true; // locking enabled by default
  232. mKeyboardEnabled = mMouseEnabled = mJoystickEnabled = false;
  233. mKeyboardActive = mMouseActive = mJoystickActive = false;
  234. }
  235. //------------------------------------------------------------------------------
  236. void UInputManager::init()
  237. {
  238. Con::addVariable( "pref::Input::KeyboardEnabled",
  239. TypeBool, &mKeyboardEnabled );
  240. Con::addVariable( "pref::Input::MouseEnabled",
  241. TypeBool, &mMouseEnabled );
  242. Con::addVariable( "pref::Input::JoystickEnabled",
  243. TypeBool, &mJoystickEnabled );
  244. }
  245. //------------------------------------------------------------------------------
  246. bool UInputManager::enable()
  247. {
  248. disable();
  249. #ifdef LOG_INPUT
  250. Input::log( "Enabling Input...\n" );
  251. #endif
  252. mModifierKeys = 0;
  253. dMemset( mMouseButtonState, 0, sizeof( mMouseButtonState ) );
  254. dMemset( mKeyboardState, 0, 256 );
  255. InitKeyMaps();
  256. mJoystickEnabled = false;
  257. initJoystick();
  258. mEnabled = true;
  259. mMouseEnabled = true;
  260. mKeyboardEnabled = true;
  261. SDL_EnableKeyRepeat(
  262. SDL_DEFAULT_REPEAT_DELAY,
  263. SDL_DEFAULT_REPEAT_INTERVAL);
  264. return true;
  265. }
  266. //------------------------------------------------------------------------------
  267. void UInputManager::disable()
  268. {
  269. deactivate();
  270. mEnabled = false;
  271. return;
  272. }
  273. //------------------------------------------------------------------------------
  274. void UInputManager::initJoystick()
  275. {
  276. mJoystickList.clear();
  277. // initialize SDL joystick system
  278. if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
  279. {
  280. Con::warnf(" Unable to initialize joystick: %s", SDL_GetError());
  281. return;
  282. }
  283. int numJoysticks = SDL_NumJoysticks();
  284. if (numJoysticks == 0)
  285. Con::printf(" No joysticks found.");
  286. // disable joystick events (use polling instead)
  287. SDL_JoystickEventState(SDL_IGNORE);
  288. // install joysticks
  289. for(int i = 0; i < numJoysticks; i++ )
  290. {
  291. JoystickInputDevice* newDevice = new JoystickInputDevice(i);
  292. addObject(newDevice);
  293. mJoystickList.push_back(newDevice);
  294. Con::printf(" %s: %s",
  295. newDevice->getDeviceName(), newDevice->getName());
  296. #ifdef LOG_INPUT
  297. Input::log(" %s: %s\n",
  298. newDevice->getDeviceName(), newDevice->getName());
  299. #endif
  300. }
  301. mJoystickEnabled = true;
  302. }
  303. //------------------------------------------------------------------------------
  304. void UInputManager::activate()
  305. {
  306. if (mEnabled && !isActive())
  307. {
  308. mActive = true;
  309. SDL_ShowCursor(SDL_DISABLE);
  310. resetInputState();
  311. // hack; if the mouse or keyboard has been disabled, re-enable them.
  312. // prevents scripts like default.cs from breaking our input, although
  313. // there is probably a better solution
  314. mMouseEnabled = mKeyboardEnabled = true;
  315. activateMouse();
  316. activateKeyboard();
  317. activateJoystick();
  318. if (gPlatState.mouseLocked)
  319. lockInput();
  320. }
  321. }
  322. //------------------------------------------------------------------------------
  323. void UInputManager::deactivate()
  324. {
  325. if (mEnabled && isActive())
  326. {
  327. unlockInput();
  328. deactivateKeyboard();
  329. deactivateMouse();
  330. deactivateJoystick();
  331. resetInputState();
  332. SDL_ShowCursor(SDL_ENABLE);
  333. mActive = false;
  334. }
  335. }
  336. //------------------------------------------------------------------------------
  337. void UInputManager::resetKeyboardState()
  338. {
  339. // unpress any pressed keys; in the future we may want
  340. // to actually sync with the keyboard state
  341. for (int i = 0; i < 256; ++i)
  342. {
  343. if (mKeyboardState[i])
  344. {
  345. InputEvent event;
  346. event.deviceInst = 0;
  347. event.deviceType = KeyboardDeviceType;
  348. event.objType = SI_KEY;
  349. event.objInst = i;
  350. event.action = SI_BREAK;
  351. event.fValue = 0.0;
  352. Game->postEvent(event);
  353. }
  354. }
  355. dMemset(mKeyboardState, 0, 256);
  356. // clear modifier keys
  357. mModifierKeys = 0;
  358. }
  359. //------------------------------------------------------------------------------
  360. void UInputManager::resetMouseState()
  361. {
  362. // unpress any buttons; in the future we may want
  363. // to actually sync with the mouse state
  364. for (int i = 0; i < 3; ++i)
  365. {
  366. if (mMouseButtonState[i])
  367. {
  368. // add KEY_BUTTON0 to the index to get the real
  369. // button ID
  370. S32 buttonID = i + KEY_BUTTON0;
  371. InputEvent event;
  372. event.deviceInst = 0;
  373. event.deviceType = MouseDeviceType;
  374. event.objType = SI_BUTTON;
  375. event.objInst = buttonID;
  376. event.action = SI_BREAK;
  377. event.fValue = 0.0;
  378. Game->postEvent(event);
  379. }
  380. }
  381. dMemset(mMouseButtonState, 0, 3);
  382. }
  383. //------------------------------------------------------------------------------
  384. void UInputManager::resetInputState()
  385. {
  386. resetKeyboardState();
  387. resetMouseState();
  388. // reset joysticks
  389. for (Vector<JoystickInputDevice*>::iterator iter = mJoystickList.begin();
  390. iter != mJoystickList.end();
  391. ++iter)
  392. {
  393. (*iter)->reset();
  394. }
  395. gPlatState.eventList.clear();
  396. #ifndef DUMMY_PLATFORM
  397. // Need to do this since there seems to be no flush method!
  398. SDL_Event e;
  399. while (SDL_PollEvent(&e))
  400. {
  401. // ....
  402. }
  403. #endif
  404. }
  405. //------------------------------------------------------------------------------
  406. void UInputManager::setLocking(bool enabled)
  407. {
  408. mLocking = enabled;
  409. if (mLocking)
  410. lockInput();
  411. else
  412. unlockInput();
  413. }
  414. //------------------------------------------------------------------------------
  415. void UInputManager::lockInput()
  416. {
  417. if (!gPlatState.backgrounded && !gPlatState.mouseLocked &&
  418. mLocking &&
  419. SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF)
  420. SDL_WM_GrabInput(SDL_GRAB_ON);
  421. }
  422. //------------------------------------------------------------------------------
  423. void UInputManager::unlockInput()
  424. {
  425. if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
  426. SDL_WM_GrabInput(SDL_GRAB_OFF);
  427. }
  428. //------------------------------------------------------------------------------
  429. void UInputManager::onDeleteNotify( SimObject* object )
  430. {
  431. Parent::onDeleteNotify( object );
  432. }
  433. //------------------------------------------------------------------------------
  434. bool UInputManager::onAdd()
  435. {
  436. if ( !Parent::onAdd() )
  437. return false;
  438. return true;
  439. }
  440. //------------------------------------------------------------------------------
  441. void UInputManager::onRemove()
  442. {
  443. deactivate();
  444. Parent::onRemove();
  445. }
  446. //------------------------------------------------------------------------------
  447. void UInputManager::mouseMotionEvent(const SDL_Event& event)
  448. {
  449. // Con::printf("motion event: %d %d %d %d",
  450. // event.motion.xrel, event.motion.yrel,
  451. // event.motion.x, event.motion.y);
  452. if (gPlatState.mouseLocked)
  453. {
  454. InputEvent ievent;
  455. ievent.deviceInst = 0;
  456. ievent.deviceType = MouseDeviceType;
  457. ievent.objInst = 0;
  458. ievent.modifier = mModifierKeys;
  459. ievent.ascii = 0;
  460. ievent.action = SI_MOVE;
  461. // post events if things have changed
  462. if (event.motion.xrel != 0)
  463. {
  464. ievent.objType = SI_XAXIS;
  465. ievent.fValue = event.motion.xrel;
  466. Game->postEvent(ievent);
  467. }
  468. if (event.motion.yrel != 0)
  469. {
  470. ievent.objType = SI_YAXIS;
  471. ievent.fValue = event.motion.yrel;
  472. Game->postEvent(ievent);
  473. }
  474. #ifdef LOG_INPUT
  475. #ifdef LOG_MOUSEMOVE
  476. Input::log( "EVENT (Input): Mouse relative move (%.1f, %.1f).\n",
  477. event.motion.xrel != 0 ? F32(event.motion.xrel) : 0.0,
  478. event.motion.yrel != 0 ? F32(event.motion.yrel) : 0.0);
  479. #endif
  480. #endif
  481. }
  482. else
  483. {
  484. MouseMoveEvent mmevent;
  485. mmevent.xPos = mLastMouseX = event.motion.x;
  486. mmevent.yPos = mLastMouseY = event.motion.y;
  487. mmevent.modifier = mModifierKeys;
  488. Game->postEvent(mmevent);
  489. #ifdef LOG_INPUT
  490. #ifdef LOG_MOUSEMOVE
  491. Input::log( "EVENT (Input): Mouse absolute move (%.1f, %.1f).\n",
  492. F32(event.motion.x),
  493. F32(event.motion.y));
  494. #endif
  495. #endif
  496. }
  497. }
  498. //------------------------------------------------------------------------------
  499. void UInputManager::joyButtonEvent(const SDL_Event& event)
  500. {
  501. joyButtonEvent(event.jbutton.which, event.jbutton.button,
  502. event.type == SDL_JOYBUTTONDOWN);
  503. }
  504. //------------------------------------------------------------------------------
  505. void UInputManager::joyButtonEvent(U8 deviceID, U8 buttonNum, bool pressed)
  506. {
  507. S32 action = pressed ? SI_MAKE : SI_BREAK;
  508. S32 objInst = buttonNum + KEY_BUTTON0;
  509. InputEvent ievent;
  510. ievent.deviceInst = deviceID;
  511. ievent.deviceType = JoystickDeviceType;
  512. ievent.modifier = mModifierKeys;
  513. ievent.ascii = 0;
  514. ievent.objType = SI_BUTTON;
  515. ievent.objInst = objInst;
  516. ievent.action = action;
  517. ievent.fValue = (action == SI_MAKE) ? 1.0 : 0.0;
  518. Game->postEvent(ievent);
  519. #ifdef LOG_INPUT
  520. Input::log( "EVENT (Input): joystick%d button%d %s. MODS:%c%c%c \n",
  521. deviceID,
  522. buttonNum,
  523. pressed ? "pressed" : "released",
  524. ( mModifierKeys & SI_SHIFT ? 'S' : '.' ),
  525. ( mModifierKeys & SI_CTRL ? 'C' : '.' ),
  526. ( mModifierKeys & SI_ALT ? 'A' : '.' ));
  527. #endif
  528. }
  529. //------------------------------------------------------------------------------
  530. void UInputManager::joyHatEvent(U8 deviceID, U8 hatNum,
  531. U8 prevHatState, U8 currHatState)
  532. {
  533. if (prevHatState == currHatState)
  534. return;
  535. InputEvent ievent;
  536. ievent.deviceInst = deviceID;
  537. ievent.deviceType = JoystickDeviceType;
  538. ievent.modifier = mModifierKeys;
  539. ievent.ascii = 0;
  540. ievent.objType = SI_POV;
  541. // first break any positions that are no longer valid
  542. ievent.action = SI_BREAK;
  543. ievent.fValue = 0.0;
  544. if (prevHatState & SDL_HAT_UP && !(currHatState & SDL_HAT_UP))
  545. {
  546. #ifdef LOG_INPUT
  547. Input::log( "EVENT (Input): Up POV released.\n");
  548. #endif
  549. ievent.objInst = SI_UPOV;
  550. Game->postEvent(ievent);
  551. }
  552. else if (prevHatState & SDL_HAT_DOWN && !(currHatState & SDL_HAT_DOWN))
  553. {
  554. #ifdef LOG_INPUT
  555. Input::log( "EVENT (Input): Down POV released.\n");
  556. #endif
  557. ievent.objInst = SI_DPOV;
  558. Game->postEvent(ievent);
  559. }
  560. if (prevHatState & SDL_HAT_LEFT && !(currHatState & SDL_HAT_LEFT))
  561. {
  562. #ifdef LOG_INPUT
  563. Input::log( "EVENT (Input): Left POV released.\n");
  564. #endif
  565. ievent.objInst = SI_LPOV;
  566. Game->postEvent(ievent);
  567. }
  568. else if (prevHatState & SDL_HAT_RIGHT && !(currHatState & SDL_HAT_RIGHT))
  569. {
  570. #ifdef LOG_INPUT
  571. Input::log( "EVENT (Input): Right POV released.\n");
  572. #endif
  573. ievent.objInst = SI_RPOV;
  574. Game->postEvent(ievent);
  575. }
  576. // now do the make events
  577. ievent.action = SI_MAKE;
  578. ievent.fValue = 1.0;
  579. if (!(prevHatState & SDL_HAT_UP) && currHatState & SDL_HAT_UP)
  580. {
  581. #ifdef LOG_INPUT
  582. Input::log( "EVENT (Input): Up POV pressed.\n");
  583. #endif
  584. ievent.objInst = SI_UPOV;
  585. Game->postEvent(ievent);
  586. }
  587. else if (!(prevHatState & SDL_HAT_DOWN) && currHatState & SDL_HAT_DOWN)
  588. {
  589. #ifdef LOG_INPUT
  590. Input::log( "EVENT (Input): Down POV pressed.\n");
  591. #endif
  592. ievent.objInst = SI_DPOV;
  593. Game->postEvent(ievent);
  594. }
  595. if (!(prevHatState & SDL_HAT_LEFT) && currHatState & SDL_HAT_LEFT)
  596. {
  597. #ifdef LOG_INPUT
  598. Input::log( "EVENT (Input): Left POV pressed.\n");
  599. #endif
  600. ievent.objInst = SI_LPOV;
  601. Game->postEvent(ievent);
  602. }
  603. else if (!(prevHatState & SDL_HAT_RIGHT) && currHatState & SDL_HAT_RIGHT)
  604. {
  605. #ifdef LOG_INPUT
  606. Input::log( "EVENT (Input): Right POV pressed.\n");
  607. #endif
  608. ievent.objInst = SI_RPOV;
  609. Game->postEvent(ievent);
  610. }
  611. }
  612. //------------------------------------------------------------------------------
  613. void UInputManager::joyAxisEvent(const SDL_Event& event)
  614. {
  615. joyAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
  616. }
  617. //------------------------------------------------------------------------------
  618. void UInputManager::joyAxisEvent(U8 deviceID, U8 axisNum, S16 axisValue)
  619. {
  620. JoystickInputDevice* stick;
  621. stick = mJoystickList[deviceID];
  622. AssertFatal(stick, "JoystickInputDevice* is NULL");
  623. JoystickAxisInfo axisInfo = stick->getAxisInfo(axisNum);
  624. if (axisInfo.type == -1)
  625. return;
  626. // scale the value to [-1,1]
  627. F32 scaledValue = 0;
  628. if (axisValue < 0)
  629. scaledValue = -F32(axisValue) / axisInfo.minValue;
  630. else if (axisValue > 0)
  631. scaledValue = F32(axisValue) / axisInfo.maxValue;
  632. // F32 range = F32(axisInfo.maxValue - axisInfo.minValue);
  633. // F32 scaledValue = F32((2 * axisValue) - axisInfo.maxValue -
  634. // axisInfo.minValue) / range;
  635. if (scaledValue > 1.f)
  636. scaledValue = 1.f;
  637. else if (scaledValue < -1.f)
  638. scaledValue = -1.f;
  639. // create and post the event
  640. InputEvent ievent;
  641. ievent.deviceInst = deviceID;
  642. ievent.deviceType = JoystickDeviceType;
  643. ievent.modifier = mModifierKeys;
  644. ievent.ascii = 0;
  645. ievent.objType = axisInfo.type;
  646. ievent.objInst = 0;
  647. ievent.action = SI_MOVE;
  648. ievent.fValue = scaledValue;
  649. Game->postEvent(ievent);
  650. #ifdef LOG_INPUT
  651. Input::log( "EVENT (Input): joystick axis %d moved: %.1f.\n",
  652. axisNum, ievent.fValue);
  653. #endif
  654. }
  655. //------------------------------------------------------------------------------
  656. void UInputManager::mouseButtonEvent(const SDL_Event& event)
  657. {
  658. S32 action = (event.type == SDL_MOUSEBUTTONDOWN) ? SI_MAKE : SI_BREAK;
  659. S32 objInst = -1;
  660. // JMQTODO: support wheel delta like windows version?
  661. // JMQTODO: make this value configurable?
  662. S32 wheelDelta = 10;
  663. bool wheel = false;
  664. switch (event.button.button)
  665. {
  666. case SDL_BUTTON_LEFT:
  667. objInst = KEY_BUTTON0;
  668. break;
  669. case SDL_BUTTON_RIGHT:
  670. objInst = KEY_BUTTON1;
  671. break;
  672. case SDL_BUTTON_MIDDLE:
  673. objInst = KEY_BUTTON2;
  674. break;
  675. /* case Button4:
  676. wheel = true;
  677. break;
  678. case Button5:
  679. wheel = true;
  680. wheelDelta = -wheelDelta;
  681. break;*/
  682. }
  683. if (objInst == -1 && !wheel)
  684. // unsupported button
  685. return;
  686. InputEvent ievent;
  687. ievent.deviceInst = 0;
  688. ievent.deviceType = MouseDeviceType;
  689. ievent.modifier = mModifierKeys;
  690. ievent.ascii = 0;
  691. if (wheel)
  692. {
  693. // SDL generates a button press/release for each wheel move,
  694. // so ignore breaks to translate those into a single event
  695. if (action == SI_BREAK)
  696. return;
  697. ievent.objType = SI_ZAXIS;
  698. ievent.objInst = 0;
  699. ievent.action = SI_MOVE;
  700. ievent.fValue = wheelDelta;
  701. #ifdef LOG_INPUT
  702. Input::log( "EVENT (Input): mouse wheel moved %s: %.1f. MODS:%c%c%c\n",
  703. wheelDelta > 0 ? "up" : "down",
  704. ievent.fValue,
  705. ( mModifierKeys & SI_SHIFT ? 'S' : '.' ),
  706. ( mModifierKeys & SI_CTRL ? 'C' : '.' ),
  707. ( mModifierKeys & SI_ALT ? 'A' : '.' ));
  708. #endif
  709. }
  710. else // regular button
  711. {
  712. S32 buttonID = (objInst - KEY_BUTTON0);
  713. if (buttonID < 3)
  714. mMouseButtonState[buttonID] = ( action == SI_MAKE ) ? true : false;
  715. ievent.objType = SI_BUTTON;
  716. ievent.objInst = objInst;
  717. ievent.action = action;
  718. ievent.fValue = (action == SI_MAKE) ? 1.0 : 0.0;
  719. #ifdef LOG_INPUT
  720. Input::log( "EVENT (Input): mouse button%d %s. MODS:%c%c%c\n",
  721. buttonID,
  722. action == SI_MAKE ? "pressed" : "released",
  723. ( mModifierKeys & SI_SHIFT ? 'S' : '.' ),
  724. ( mModifierKeys & SI_CTRL ? 'C' : '.' ),
  725. ( mModifierKeys & SI_ALT ? 'A' : '.' ));
  726. #endif
  727. }
  728. Game->postEvent(ievent);
  729. }
  730. //------------------------------------------------------------------------------
  731. const char* getKeyName( U16 key )
  732. {
  733. switch ( key )
  734. {
  735. case KEY_BACKSPACE: return "Backspace";
  736. case KEY_TAB: return "Tab";
  737. case KEY_RETURN: return "Return";
  738. case KEY_PAUSE: return "Pause";
  739. case KEY_CAPSLOCK: return "CapsLock";
  740. case KEY_ESCAPE: return "Esc";
  741. case KEY_SPACE: return "SpaceBar";
  742. case KEY_PAGE_DOWN: return "PageDown";
  743. case KEY_PAGE_UP: return "PageUp";
  744. case KEY_END: return "End";
  745. case KEY_HOME: return "Home";
  746. case KEY_LEFT: return "Left";
  747. case KEY_UP: return "Up";
  748. case KEY_RIGHT: return "Right";
  749. case KEY_DOWN: return "Down";
  750. case KEY_PRINT: return "PrintScreen";
  751. case KEY_INSERT: return "Insert";
  752. case KEY_DELETE: return "Delete";
  753. case KEY_HELP: return "Help";
  754. case KEY_NUMPAD0: return "Numpad 0";
  755. case KEY_NUMPAD1: return "Numpad 1";
  756. case KEY_NUMPAD2: return "Numpad 2";
  757. case KEY_NUMPAD3: return "Numpad 3";
  758. case KEY_NUMPAD4: return "Numpad 4";
  759. case KEY_NUMPAD5: return "Numpad 5";
  760. case KEY_NUMPAD6: return "Numpad 6";
  761. case KEY_NUMPAD7: return "Numpad 7";
  762. case KEY_NUMPAD8: return "Numpad 8";
  763. case KEY_NUMPAD9: return "Numpad 9";
  764. case KEY_MULTIPLY: return "Multiply";
  765. case KEY_ADD: return "Add";
  766. case KEY_SEPARATOR: return "Separator";
  767. case KEY_SUBTRACT: return "Subtract";
  768. case KEY_DECIMAL: return "Decimal";
  769. case KEY_DIVIDE: return "Divide";
  770. case KEY_NUMPADENTER: return "Numpad Enter";
  771. case KEY_F1: return "F1";
  772. case KEY_F2: return "F2";
  773. case KEY_F3: return "F3";
  774. case KEY_F4: return "F4";
  775. case KEY_F5: return "F5";
  776. case KEY_F6: return "F6";
  777. case KEY_F7: return "F7";
  778. case KEY_F8: return "F8";
  779. case KEY_F9: return "F9";
  780. case KEY_F10: return "F10";
  781. case KEY_F11: return "F11";
  782. case KEY_F12: return "F12";
  783. case KEY_F13: return "F13";
  784. case KEY_F14: return "F14";
  785. case KEY_F15: return "F15";
  786. case KEY_F16: return "F16";
  787. case KEY_F17: return "F17";
  788. case KEY_F18: return "F18";
  789. case KEY_F19: return "F19";
  790. case KEY_F20: return "F20";
  791. case KEY_F21: return "F21";
  792. case KEY_F22: return "F22";
  793. case KEY_F23: return "F23";
  794. case KEY_F24: return "F24";
  795. case KEY_NUMLOCK: return "NumLock";
  796. case KEY_SCROLLLOCK: return "ScrollLock";
  797. case KEY_LCONTROL: return "LCtrl";
  798. case KEY_RCONTROL: return "RCtrl";
  799. case KEY_LALT: return "LAlt";
  800. case KEY_RALT: return "RAlt";
  801. case KEY_LSHIFT: return "LShift";
  802. case KEY_RSHIFT: return "RShift";
  803. case KEY_WIN_LWINDOW: return "LWin";
  804. case KEY_WIN_RWINDOW: return "RWin";
  805. case KEY_WIN_APPS: return "Apps";
  806. }
  807. static char returnString[5];
  808. dSprintf( returnString, sizeof( returnString ), "%c", Input::getAscii( key, STATE_UPPER ) );
  809. return returnString;
  810. }
  811. //------------------------------------------------------------------------------
  812. void UInputManager::keyEvent(const SDL_Event& event)
  813. {
  814. S32 action = (event.type == SDL_KEYDOWN) ? SI_MAKE : SI_BREAK;
  815. InputEvent ievent;
  816. // Con::printf("keyEvent: %u", event.key.keysym.sym);
  817. ievent.deviceInst = 0;
  818. ievent.deviceType = KeyboardDeviceType;
  819. ievent.objType = SI_KEY;
  820. ievent.objInst = TranslateSDLKeytoTKey(event.key.keysym.sym);
  821. // Con::printf("keyEvent: objInst %u", ievent.objInst);
  822. // if the action is a make but this key is already pressed,
  823. // count it as a repeat
  824. if (action == SI_MAKE && mKeyboardState[ievent.objInst])
  825. action = SI_REPEAT;
  826. ievent.action = action;
  827. ievent.fValue = (action == SI_MAKE || action == SI_REPEAT) ? 1.0 : 0.0;
  828. processKeyEvent(ievent);
  829. Game->postEvent(ievent);
  830. #if 0
  831. if (ievent.action == SI_MAKE)
  832. dPrintf("key event: : %s key pressed. MODS:%c%c%c\n",
  833. getKeyName(ievent.objInst),
  834. ( mModifierKeys & SI_SHIFT ? 'S' : '.' ),
  835. ( mModifierKeys & SI_CTRL ? 'C' : '.' ),
  836. ( mModifierKeys & SI_ALT ? 'A' : '.' ));
  837. else if (ievent.action == SI_REPEAT)
  838. dPrintf("key event: : %s key repeated. MODS:%c%c%c\n",
  839. getKeyName(ievent.objInst),
  840. ( mModifierKeys & SI_SHIFT ? 'S' : '.' ),
  841. ( mModifierKeys & SI_CTRL ? 'C' : '.' ),
  842. ( mModifierKeys & SI_ALT ? 'A' : '.' ));
  843. else if (ievent.action == SI_BREAK)
  844. dPrintf("key event: : %s key released. MODS:%c%c%c\n",
  845. getKeyName(ievent.objInst),
  846. ( mModifierKeys & SI_SHIFT ? 'S' : '.' ),
  847. ( mModifierKeys & SI_CTRL ? 'C' : '.' ),
  848. ( mModifierKeys & SI_ALT ? 'A' : '.' ));
  849. else
  850. dPrintf("unknown key event!\n");
  851. #endif
  852. #ifdef LOG_INPUT
  853. Input::log( "EVENT (Input): %s key %s. MODS:%c%c%c\n",
  854. getKeyName(ievent.objInst),
  855. action == SI_MAKE ? "pressed" : "released",
  856. ( mModifierKeys & SI_SHIFT ? 'S' : '.' ),
  857. ( mModifierKeys & SI_CTRL ? 'C' : '.' ),
  858. ( mModifierKeys & SI_ALT ? 'A' : '.' ));
  859. #endif
  860. }
  861. //------------------------------------------------------------------------------
  862. // This function was ripped from DInputDevice almost entirely intact.
  863. bool UInputManager::processKeyEvent( InputEvent &event )
  864. {
  865. if ( event.deviceType != KeyboardDeviceType || event.objType != SI_KEY )
  866. return false;
  867. bool modKey = false;
  868. U8 keyCode = event.objInst;
  869. if ( event.action == SI_MAKE || event.action == SI_REPEAT)
  870. {
  871. // Maintain the key structure:
  872. mKeyboardState[keyCode] = true;
  873. switch ( event.objInst )
  874. {
  875. case KEY_LSHIFT:
  876. // Con::printf("keyEvent: LSHIFT");
  877. mModifierKeys |= SI_LSHIFT;
  878. modKey = true;
  879. break;
  880. case KEY_RSHIFT:
  881. // Con::printf("keyEvent: RSHIFT");
  882. mModifierKeys |= SI_RSHIFT;
  883. modKey = true;
  884. break;
  885. case KEY_LCONTROL:
  886. mModifierKeys |= SI_LCTRL;
  887. modKey = true;
  888. break;
  889. case KEY_RCONTROL:
  890. mModifierKeys |= SI_RCTRL;
  891. modKey = true;
  892. break;
  893. case KEY_LALT:
  894. mModifierKeys |= SI_LALT;
  895. modKey = true;
  896. break;
  897. case KEY_RALT:
  898. mModifierKeys |= SI_RALT;
  899. modKey = true;
  900. break;
  901. }
  902. }
  903. else
  904. {
  905. // Maintain the keys structure:
  906. mKeyboardState[keyCode] = false;
  907. switch ( event.objInst )
  908. {
  909. case KEY_LSHIFT:
  910. // Con::printf("keyEvent UNSET: LSHIFT");
  911. mModifierKeys &= ~SI_LSHIFT;
  912. modKey = true;
  913. break;
  914. case KEY_RSHIFT:
  915. // Con::printf("keyEvent UNSET: RSHIFT");
  916. mModifierKeys &= ~SI_RSHIFT;
  917. modKey = true;
  918. break;
  919. case KEY_LCONTROL:
  920. mModifierKeys &= ~SI_LCTRL;
  921. modKey = true;
  922. break;
  923. case KEY_RCONTROL:
  924. mModifierKeys &= ~SI_RCTRL;
  925. modKey = true;
  926. break;
  927. case KEY_LALT:
  928. mModifierKeys &= ~SI_LALT;
  929. modKey = true;
  930. break;
  931. case KEY_RALT:
  932. mModifierKeys &= ~SI_RALT;
  933. modKey = true;
  934. break;
  935. }
  936. }
  937. if ( modKey )
  938. event.modifier = 0;
  939. else
  940. event.modifier = mModifierKeys;
  941. // TODO: alter this getAscii call
  942. KEY_STATE state = STATE_LOWER;
  943. if (event.modifier & (SI_CTRL|SI_ALT) )
  944. {
  945. state = STATE_GOOFY;
  946. }
  947. if ( event.modifier & SI_SHIFT )
  948. {
  949. // Con::printf("keyEvent: UPCASE GET KEY");
  950. state = STATE_UPPER;
  951. }
  952. event.ascii = Input::getAscii( event.objInst, state );
  953. return modKey;
  954. }
  955. //------------------------------------------------------------------------------
  956. void UInputManager::setWindowLocked(bool locked)
  957. {
  958. if (locked)
  959. lockInput();
  960. else
  961. {
  962. unlockInput();
  963. // SDL keeps track of abs mouse position in fullscreen mode, which means
  964. // that if you switch to unlocked mode while fullscreen, the mouse will
  965. // suddenly warp to someplace unexpected on screen. To fix this, we
  966. // warp the mouse to the last known Torque abs mouse position.
  967. if (mLastMouseX != -1 && mLastMouseY != -1)
  968. SDL_WarpMouse(mLastMouseX, mLastMouseY);
  969. }
  970. }
  971. //------------------------------------------------------------------------------
  972. void UInputManager::process()
  973. {
  974. if (!mEnabled || !isActive())
  975. return;
  976. #ifndef DUMMY_PLATFORM
  977. if (mMouseActive || mKeyboardActive)
  978. {
  979. S32 numEvents = gPlatState.eventList.size();
  980. for (int i = 0; i < numEvents; ++i)
  981. {
  982. switch (gPlatState.eventList[i].type)
  983. {
  984. case SDL_MOUSEMOTION:
  985. if (mMouseActive)
  986. mouseMotionEvent(gPlatState.eventList[i]);
  987. break;
  988. case SDL_MOUSEBUTTONUP:
  989. case SDL_MOUSEBUTTONDOWN:
  990. if (mMouseActive)
  991. mouseButtonEvent(gPlatState.eventList[i]);
  992. break;
  993. case SDL_KEYDOWN:
  994. case SDL_KEYUP:
  995. if (mKeyboardActive)
  996. keyEvent(gPlatState.eventList[i]);
  997. break;
  998. }
  999. }
  1000. }
  1001. #endif
  1002. // poll joysticks
  1003. if (!mJoystickActive)
  1004. return;
  1005. SDL_JoystickUpdate();
  1006. for (Vector<JoystickInputDevice*>::iterator iter = mJoystickList.begin();
  1007. iter != mJoystickList.end();
  1008. ++iter)
  1009. {
  1010. (*iter)->process();
  1011. }
  1012. }
  1013. //------------------------------------------------------------------------------
  1014. bool UInputManager::enableKeyboard()
  1015. {
  1016. if ( !isEnabled() )
  1017. return( false );
  1018. if ( isKeyboardEnabled() && isKeyboardActive() )
  1019. return( true );
  1020. mKeyboardEnabled = true;
  1021. if ( isActive() )
  1022. mKeyboardEnabled = activateKeyboard();
  1023. if ( mKeyboardEnabled )
  1024. {
  1025. Con::printf( "Keyboard enabled." );
  1026. #ifdef LOG_INPUT
  1027. Input::log( "Keyboard enabled.\n" );
  1028. #endif
  1029. }
  1030. else
  1031. {
  1032. Con::warnf( "Keyboard failed to enable!" );
  1033. #ifdef LOG_INPUT
  1034. Input::log( "Keyboard failed to enable!\n" );
  1035. #endif
  1036. }
  1037. return( mKeyboardEnabled );
  1038. }
  1039. //------------------------------------------------------------------------------
  1040. void UInputManager::disableKeyboard()
  1041. {
  1042. if ( !isEnabled() || !isKeyboardEnabled())
  1043. return;
  1044. deactivateKeyboard();
  1045. mKeyboardEnabled = false;
  1046. Con::printf( "Keyboard disabled." );
  1047. #ifdef LOG_INPUT
  1048. Input::log( "Keyboard disabled.\n" );
  1049. #endif
  1050. }
  1051. //------------------------------------------------------------------------------
  1052. bool UInputManager::activateKeyboard()
  1053. {
  1054. if ( !isEnabled() || !isActive() || !isKeyboardEnabled() )
  1055. return( false );
  1056. mKeyboardActive = true;
  1057. #ifdef LOG_INPUT
  1058. Input::log( mKeyboardActive ? "Keyboard activated.\n" : "Keyboard failed to activate!\n" );
  1059. #endif
  1060. return( mKeyboardActive );
  1061. }
  1062. //------------------------------------------------------------------------------
  1063. void UInputManager::deactivateKeyboard()
  1064. {
  1065. if ( isEnabled() && isKeyboardActive() )
  1066. {
  1067. mKeyboardActive = false;
  1068. #ifdef LOG_INPUT
  1069. Input::log( "Keyboard deactivated.\n" );
  1070. #endif
  1071. }
  1072. }
  1073. //------------------------------------------------------------------------------
  1074. bool UInputManager::enableMouse()
  1075. {
  1076. if ( !isEnabled() )
  1077. return( false );
  1078. if ( isMouseEnabled() && isMouseActive() )
  1079. return( true );
  1080. mMouseEnabled = true;
  1081. if ( isActive() )
  1082. mMouseEnabled = activateMouse();
  1083. if ( mMouseEnabled )
  1084. {
  1085. Con::printf( "Mouse enabled." );
  1086. #ifdef LOG_INPUT
  1087. Input::log( "Mouse enabled.\n" );
  1088. #endif
  1089. }
  1090. else
  1091. {
  1092. Con::warnf( "Mouse failed to enable!" );
  1093. #ifdef LOG_INPUT
  1094. Input::log( "Mouse failed to enable!\n" );
  1095. #endif
  1096. }
  1097. return( mMouseEnabled );
  1098. }
  1099. //------------------------------------------------------------------------------
  1100. void UInputManager::disableMouse()
  1101. {
  1102. if ( !isEnabled() || !isMouseEnabled())
  1103. return;
  1104. deactivateMouse();
  1105. mMouseEnabled = false;
  1106. Con::printf( "Mouse disabled." );
  1107. #ifdef LOG_INPUT
  1108. Input::log( "Mouse disabled.\n" );
  1109. #endif
  1110. }
  1111. //------------------------------------------------------------------------------
  1112. bool UInputManager::activateMouse()
  1113. {
  1114. if ( !isEnabled() || !isActive() || !isMouseEnabled() )
  1115. return( false );
  1116. Con::printf("Mouse Activated");
  1117. mMouseActive = true;
  1118. #ifdef LOG_INPUT
  1119. Input::log( mMouseActive ?
  1120. "Mouse activated.\n" : "Mouse failed to activate!\n" );
  1121. #endif
  1122. return( mMouseActive );
  1123. }
  1124. //------------------------------------------------------------------------------
  1125. void UInputManager::deactivateMouse()
  1126. {
  1127. Con::printf("Mouse Deactivated");
  1128. if ( isEnabled() && isMouseActive() )
  1129. {
  1130. mMouseActive = false;
  1131. #ifdef LOG_INPUT
  1132. Input::log( "Mouse deactivated.\n" );
  1133. #endif
  1134. }
  1135. }
  1136. //------------------------------------------------------------------------------
  1137. bool UInputManager::enableJoystick()
  1138. {
  1139. if ( !isEnabled() )
  1140. return( false );
  1141. if ( isJoystickEnabled() && isJoystickActive() )
  1142. return( true );
  1143. mJoystickEnabled = true;
  1144. if ( isActive() )
  1145. mJoystickEnabled = activateJoystick();
  1146. if ( mJoystickEnabled )
  1147. {
  1148. Con::printf( "Joystick enabled." );
  1149. #ifdef LOG_INPUT
  1150. Input::log( "Joystick enabled.\n" );
  1151. #endif
  1152. }
  1153. else
  1154. {
  1155. Con::warnf( "Joystick failed to enable!" );
  1156. #ifdef LOG_INPUT
  1157. Input::log( "Joystick failed to enable!\n" );
  1158. #endif
  1159. }
  1160. return( mJoystickEnabled );
  1161. }
  1162. //------------------------------------------------------------------------------
  1163. void UInputManager::disableJoystick()
  1164. {
  1165. if ( !isEnabled() || !isJoystickEnabled())
  1166. return;
  1167. deactivateJoystick();
  1168. mJoystickEnabled = false;
  1169. Con::printf( "Joystick disabled." );
  1170. #ifdef LOG_INPUT
  1171. Input::log( "Joystick disabled.\n" );
  1172. #endif
  1173. }
  1174. //------------------------------------------------------------------------------
  1175. bool UInputManager::activateJoystick()
  1176. {
  1177. if ( !isEnabled() || !isActive() || !isJoystickEnabled() )
  1178. return( false );
  1179. mJoystickActive = false;
  1180. JoystickInputDevice* dptr;
  1181. for ( iterator ptr = begin(); ptr != end(); ptr++ )
  1182. {
  1183. dptr = dynamic_cast<JoystickInputDevice*>( *ptr );
  1184. if ( dptr && dptr->getDeviceType() == JoystickDeviceType)
  1185. if ( dptr->activate() )
  1186. mJoystickActive = true;
  1187. }
  1188. #ifdef LOG_INPUT
  1189. Input::log( mJoystickActive ?
  1190. "Joystick activated.\n" : "Joystick failed to activate!\n" );
  1191. #endif
  1192. return( mJoystickActive );
  1193. }
  1194. //------------------------------------------------------------------------------
  1195. void UInputManager::deactivateJoystick()
  1196. {
  1197. if ( isEnabled() && isJoystickActive() )
  1198. {
  1199. mJoystickActive = false;
  1200. JoystickInputDevice* dptr;
  1201. for ( iterator ptr = begin(); ptr != end(); ptr++ )
  1202. {
  1203. dptr = dynamic_cast<JoystickInputDevice*>( *ptr );
  1204. if ( dptr && dptr->getDeviceType() == JoystickDeviceType)
  1205. dptr->deactivate();
  1206. }
  1207. #ifdef LOG_INPUT
  1208. Input::log( "Joystick deactivated.\n" );
  1209. #endif
  1210. }
  1211. }
  1212. //------------------------------------------------------------------------------
  1213. const char* UInputManager::getJoystickAxesString( U32 deviceID )
  1214. {
  1215. for (Vector<JoystickInputDevice*>::iterator iter = mJoystickList.begin();
  1216. iter != mJoystickList.end();
  1217. ++iter)
  1218. {
  1219. if ((*iter)->getDeviceID() == deviceID)
  1220. return (*iter)->getJoystickAxesString();
  1221. }
  1222. return( "" );
  1223. }
  1224. //==============================================================================
  1225. // JoystickInputDevice
  1226. //==============================================================================
  1227. JoystickInputDevice::JoystickInputDevice(U8 deviceID)
  1228. {
  1229. mActive = false;
  1230. mStick = NULL;
  1231. mAxisList.clear();
  1232. mDeviceID = deviceID;
  1233. dSprintf(mName, 29, "joystick%d", mDeviceID);
  1234. mButtonState.clear();
  1235. mHatState.clear();
  1236. mNumAxes = mNumButtons = mNumHats = mNumBalls = 0;
  1237. loadJoystickInfo();
  1238. // initialize state variables
  1239. for (int i = 0; i < mNumButtons; ++i)
  1240. mButtonState.push_back(false); // all buttons unpressed initially
  1241. for (int i = 0; i < mNumHats; ++i)
  1242. mHatState.push_back(SDL_HAT_CENTERED); // hats centered initially
  1243. }
  1244. //------------------------------------------------------------------------------
  1245. JoystickInputDevice::~JoystickInputDevice()
  1246. {
  1247. if (isActive())
  1248. deactivate();
  1249. }
  1250. //------------------------------------------------------------------------------
  1251. bool JoystickInputDevice::activate()
  1252. {
  1253. if (isActive())
  1254. return true;
  1255. // open the stick
  1256. mStick = SDL_JoystickOpen(mDeviceID);
  1257. if (mStick == NULL)
  1258. {
  1259. Con::printf("Unable to activate %s: %s", getDeviceName(), SDL_GetError());
  1260. return false;
  1261. }
  1262. // reload axis mapping info
  1263. loadAxisInfo();
  1264. mActive = true;
  1265. return true;
  1266. }
  1267. //------------------------------------------------------------------------------
  1268. bool JoystickInputDevice::deactivate()
  1269. {
  1270. if (!isActive())
  1271. return true;
  1272. if (mStick != NULL)
  1273. {
  1274. SDL_JoystickClose(mStick);
  1275. mStick = NULL;
  1276. }
  1277. mActive = false;
  1278. return true;
  1279. }
  1280. //------------------------------------------------------------------------------
  1281. const char* JoystickInputDevice::getName()
  1282. {
  1283. return SDL_JoystickName(mDeviceID);
  1284. }
  1285. //------------------------------------------------------------------------------
  1286. void JoystickInputDevice::reset()
  1287. {
  1288. UInputManager* manager = dynamic_cast<UInputManager*>(Input::getManager());
  1289. if (!manager)
  1290. return;
  1291. // clear joystick state variables
  1292. // buttons
  1293. for (int i = 0; i < mButtonState.size(); ++i)
  1294. if (mButtonState[i])
  1295. {
  1296. manager->joyButtonEvent(mDeviceID, i, false);
  1297. mButtonState[i] = false;
  1298. }
  1299. // hats
  1300. for (int i = 0; i < mHatState.size(); ++i)
  1301. if (mHatState[i] != SDL_HAT_CENTERED)
  1302. {
  1303. manager->joyHatEvent(mDeviceID, i, mHatState[i], SDL_HAT_CENTERED);
  1304. mHatState[i] = SDL_HAT_CENTERED;
  1305. }
  1306. // axis and ball state is not maintained
  1307. }
  1308. //------------------------------------------------------------------------------
  1309. bool JoystickInputDevice::process()
  1310. {
  1311. if (!isActive())
  1312. return false;
  1313. UInputManager* manager = dynamic_cast<UInputManager*>(Input::getManager());
  1314. if (!manager)
  1315. return false;
  1316. // axes
  1317. for (int i = 0; i < mNumAxes; ++i)
  1318. {
  1319. // skip the axis if we don't have a mapping for it
  1320. if (mAxisList[i].type == -1)
  1321. continue;
  1322. manager->joyAxisEvent(mDeviceID, i, SDL_JoystickGetAxis(mStick, i));
  1323. }
  1324. // buttons
  1325. for (int i = 0; i < mNumButtons; ++i)
  1326. {
  1327. if (bool(SDL_JoystickGetButton(mStick, i)) ==
  1328. mButtonState[i])
  1329. continue;
  1330. mButtonState[i] = !mButtonState[i];
  1331. manager->joyButtonEvent(mDeviceID, i, mButtonState[i]);
  1332. }
  1333. // hats
  1334. for (int i = 0; i < mNumHats; ++i)
  1335. {
  1336. U8 currHatState = SDL_JoystickGetHat(mStick, i);
  1337. if (mHatState[i] == currHatState)
  1338. continue;
  1339. manager->joyHatEvent(mDeviceID, i, mHatState[i], currHatState);
  1340. mHatState[i] = currHatState;
  1341. }
  1342. // ballz
  1343. // JMQTODO: how to map ball events (xaxis,yaxis?)
  1344. return true;
  1345. }
  1346. //------------------------------------------------------------------------------
  1347. static S32 GetAxisType(S32 axisNum, const char* namedType)
  1348. {
  1349. S32 axisType = -1;
  1350. if (namedType != NULL)
  1351. {
  1352. if (dStricmp(namedType, "xaxis")==0)
  1353. axisType = SI_XAXIS;
  1354. else if (dStricmp(namedType, "yaxis")==0)
  1355. axisType = SI_YAXIS;
  1356. else if (dStricmp(namedType, "zaxis")==0)
  1357. axisType = SI_ZAXIS;
  1358. else if (dStricmp(namedType, "rxaxis")==0)
  1359. axisType = SI_RXAXIS;
  1360. else if (dStricmp(namedType, "ryaxis")==0)
  1361. axisType = SI_RYAXIS;
  1362. else if (dStricmp(namedType, "rzaxis")==0)
  1363. axisType = SI_RZAXIS;
  1364. else if (dStricmp(namedType, "slider")==0)
  1365. axisType = SI_SLIDER;
  1366. }
  1367. if (axisType == -1)
  1368. {
  1369. // use a hardcoded default mapping if possible
  1370. switch (axisNum)
  1371. {
  1372. case 0:
  1373. axisType = SI_XAXIS;
  1374. break;
  1375. case 1:
  1376. axisType = SI_YAXIS;
  1377. break;
  1378. case 2:
  1379. axisType = SI_RZAXIS;
  1380. break;
  1381. case 3:
  1382. axisType = SI_SLIDER;
  1383. break;
  1384. }
  1385. }
  1386. return axisType;
  1387. }
  1388. //------------------------------------------------------------------------------
  1389. void JoystickInputDevice::loadJoystickInfo()
  1390. {
  1391. bool opened = false;
  1392. if (mStick == NULL)
  1393. {
  1394. mStick = SDL_JoystickOpen(mDeviceID);
  1395. if (mStick == NULL)
  1396. {
  1397. Con::printf("Unable to open %s: %s", getDeviceName(), SDL_GetError());
  1398. return;
  1399. }
  1400. opened = true;
  1401. }
  1402. // get the number of thingies on this joystick
  1403. mNumAxes = SDL_JoystickNumAxes(mStick);
  1404. mNumButtons = SDL_JoystickNumButtons(mStick);
  1405. mNumHats = SDL_JoystickNumHats(mStick);
  1406. mNumBalls = SDL_JoystickNumBalls(mStick);
  1407. // load axis mapping info
  1408. loadAxisInfo();
  1409. if (opened)
  1410. SDL_JoystickClose(mStick);
  1411. }
  1412. //------------------------------------------------------------------------------
  1413. // for each axis on a joystick, torque needs to know the type of the axis
  1414. // (SI_XAXIS, etc), the minimum value, and the maximum value. However none of
  1415. // this information is generally available with the unix/linux api. All you
  1416. // get is a device and axis number and a value. Therefore,
  1417. // we allow the user to specify these values in preferences. hopefully
  1418. // someday we can implement a gui joystick calibrator that takes care of this
  1419. // cruft for the user.
  1420. void JoystickInputDevice::loadAxisInfo()
  1421. {
  1422. mAxisList.clear();
  1423. AssertFatal(mStick, "mStick is NULL");
  1424. static int AxisDefaults[] = { SI_XAXIS, SI_YAXIS, SI_ZAXIS,
  1425. SI_RXAXIS, SI_RYAXIS, SI_RZAXIS,
  1426. SI_SLIDER };
  1427. int numAxis = SDL_JoystickNumAxes(mStick);
  1428. for (int i = 0; i < numAxis; ++i)
  1429. {
  1430. JoystickAxisInfo axisInfo;
  1431. // defaults
  1432. axisInfo.type = -1;
  1433. axisInfo.minValue = -32768;
  1434. axisInfo.maxValue = 32767;
  1435. // look in console to see if there is mapping information for this axis
  1436. const int TempBufSize = 1024;
  1437. char tempBuf[TempBufSize];
  1438. dSprintf(tempBuf, TempBufSize, "$Pref::Input::Joystick%d::Axis%d",
  1439. mDeviceID, i);
  1440. const char* axisStr = Con::getVariable(tempBuf);
  1441. if (axisStr == NULL || dStrlen(axisStr) == 0)
  1442. {
  1443. if (i < sizeof(AxisDefaults))
  1444. axisInfo.type = AxisDefaults[i];
  1445. }
  1446. else
  1447. {
  1448. // format is "TorqueAxisName MinValue MaxValue";
  1449. dStrncpy(tempBuf, axisStr, TempBufSize);
  1450. char* temp = dStrtok( tempBuf, " \0" );
  1451. if (temp)
  1452. {
  1453. axisInfo.type = GetAxisType(i, temp);
  1454. temp = dStrtok( NULL, " \0" );
  1455. if (temp)
  1456. {
  1457. axisInfo.minValue = dAtoi(temp);
  1458. temp = dStrtok( NULL, "\0" );
  1459. if (temp)
  1460. {
  1461. axisInfo.maxValue = dAtoi(temp);
  1462. }
  1463. }
  1464. }
  1465. }
  1466. mAxisList.push_back(axisInfo);
  1467. }
  1468. }
  1469. //------------------------------------------------------------------------------
  1470. const char* JoystickInputDevice::getJoystickAxesString()
  1471. {
  1472. char buf[64];
  1473. dSprintf( buf, sizeof( buf ), "%d", mAxisList.size());
  1474. for (Vector<JoystickAxisInfo>::iterator iter = mAxisList.begin();
  1475. iter != mAxisList.end();
  1476. ++iter)
  1477. {
  1478. switch ((*iter).type)
  1479. {
  1480. case SI_XAXIS:
  1481. dStrcat( buf, "\tX" );
  1482. break;
  1483. case SI_YAXIS:
  1484. dStrcat( buf, "\tY" );
  1485. break;
  1486. case SI_ZAXIS:
  1487. dStrcat( buf, "\tZ" );
  1488. break;
  1489. case SI_RXAXIS:
  1490. dStrcat( buf, "\tR" );
  1491. break;
  1492. case SI_RYAXIS:
  1493. dStrcat( buf, "\tU" );
  1494. break;
  1495. case SI_RZAXIS:
  1496. dStrcat( buf, "\tV" );
  1497. break;
  1498. case SI_SLIDER:
  1499. dStrcat( buf, "\tS" );
  1500. break;
  1501. }
  1502. }
  1503. char* returnString = Con::getReturnBuffer( dStrlen( buf ) + 1 );
  1504. dStrcpy( returnString, buf );
  1505. return( returnString );
  1506. }
  1507. //==============================================================================
  1508. // Console Functions
  1509. //==============================================================================
  1510. ConsoleFunction( activateKeyboard, bool, 1, 1, "activateKeyboard()" )
  1511. {
  1512. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1513. if ( mgr )
  1514. return( mgr->activateKeyboard() );
  1515. return( false );
  1516. }
  1517. // JMQ: disabled deactivateKeyboard since the script calls it but there is
  1518. // no fallback keyboard input in unix, resulting in a permanently disabled
  1519. // keyboard
  1520. //------------------------------------------------------------------------------
  1521. ConsoleFunction( deactivateKeyboard, void, 1, 1, "deactivateKeyboard()" )
  1522. {
  1523. #if 0
  1524. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1525. if ( mgr )
  1526. mgr->deactivateKeyboard();
  1527. #endif
  1528. }
  1529. //------------------------------------------------------------------------------
  1530. ConsoleFunction( enableMouse, bool, 1, 1, "enableMouse()" )
  1531. {
  1532. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1533. if ( mgr )
  1534. return( mgr->enableMouse() );
  1535. return ( false );
  1536. }
  1537. //------------------------------------------------------------------------------
  1538. ConsoleFunction( disableMouse, void, 1, 1, "disableMouse()" )
  1539. {
  1540. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1541. if ( mgr )
  1542. mgr->disableMouse();
  1543. }
  1544. //------------------------------------------------------------------------------
  1545. ConsoleFunction( enableJoystick, bool, 1, 1, "enableJoystick()" )
  1546. {
  1547. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1548. if ( mgr )
  1549. return( mgr->enableJoystick() );
  1550. return ( false );
  1551. }
  1552. //------------------------------------------------------------------------------
  1553. ConsoleFunction( disableJoystick, void, 1, 1, "disableJoystick()" )
  1554. {
  1555. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1556. if ( mgr )
  1557. mgr->disableJoystick();
  1558. }
  1559. //------------------------------------------------------------------------------
  1560. ConsoleFunction( enableLocking, void, 1, 1, "enableLocking()" )
  1561. {
  1562. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1563. if ( mgr )
  1564. mgr->setLocking(true);
  1565. }
  1566. //------------------------------------------------------------------------------
  1567. ConsoleFunction( disableLocking, void, 1, 1, "disableLocking()" )
  1568. {
  1569. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1570. if ( mgr )
  1571. mgr->setLocking(false);
  1572. }
  1573. //------------------------------------------------------------------------------
  1574. ConsoleFunction( toggleLocking, void, 1, 1, "toggleLocking()" )
  1575. {
  1576. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1577. if ( mgr )
  1578. mgr->setLocking(!mgr->getLocking());
  1579. }
  1580. //------------------------------------------------------------------------------
  1581. ConsoleFunction( echoInputState, void, 1, 1, "echoInputState()" )
  1582. {
  1583. UInputManager* mgr = dynamic_cast<UInputManager*>( Input::getManager() );
  1584. if ( mgr && mgr->isEnabled() )
  1585. {
  1586. Con::printf( "Input is enabled %s.",
  1587. mgr->isActive() ? "and active" : "but inactive" );
  1588. Con::printf( "- Keyboard is %sabled and %sactive.",
  1589. mgr->isKeyboardEnabled() ? "en" : "dis",
  1590. mgr->isKeyboardActive() ? "" : "in" );
  1591. Con::printf( "- Mouse is %sabled and %sactive.",
  1592. mgr->isMouseEnabled() ? "en" : "dis",
  1593. mgr->isMouseActive() ? "" : "in" );
  1594. Con::printf( "- Joystick is %sabled and %sactive.",
  1595. mgr->isJoystickEnabled() ? "en" : "dis",
  1596. mgr->isJoystickActive() ? "" : "in" );
  1597. }
  1598. else
  1599. Con::printf( "Input is not enabled." );
  1600. }