x86UNIXInputManager.client.cpp 52 KB

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