x86UNIXInputManager.cc 51 KB

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