x86UNIXInputManager.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifndef _X86UNIXINPUTMANAGER_H_
  23. #define _X86UNIXINPUTMANAGER_H_
  24. #include "core/util/tVector.h"
  25. #include "platform/platformInput.h"
  26. #include "platformX86UNIX/platformX86UNIX.h"
  27. #include <SDL/SDL_events.h>
  28. #define NUM_KEYS ( KEY_OEM_102 + 1 )
  29. #define KEY_FIRST KEY_ESCAPE
  30. struct AsciiData
  31. {
  32. struct KeyData
  33. {
  34. U16 ascii;
  35. bool isDeadChar;
  36. };
  37. KeyData upper;
  38. KeyData lower;
  39. KeyData goofy;
  40. };
  41. struct _SDL_Joystick;
  42. struct JoystickAxisInfo
  43. {
  44. S32 type;
  45. S32 minValue;
  46. S32 maxValue;
  47. };
  48. //------------------------------------------------------------------------------
  49. class JoystickInputDevice : public InputDevice
  50. {
  51. public:
  52. JoystickInputDevice(U8 deviceID);
  53. ~JoystickInputDevice();
  54. bool activate();
  55. bool deactivate();
  56. bool isActive() { return( mActive ); }
  57. U8 getDeviceType() { return( JoystickDeviceType ); }
  58. U8 getDeviceID() { return( mDeviceID ); }
  59. const char* getName();
  60. const char* getJoystickAxesString();
  61. void loadJoystickInfo();
  62. void loadAxisInfo();
  63. JoystickAxisInfo& getAxisInfo(int axisNum) { return mAxisList[axisNum]; }
  64. bool process();
  65. void reset();
  66. private:
  67. bool mActive;
  68. U8 mDeviceID;
  69. SDL_Joystick* mStick;
  70. Vector<JoystickAxisInfo> mAxisList;
  71. Vector<bool> mButtonState;
  72. Vector<U8> mHatState;
  73. S32 mNumAxes;
  74. S32 mNumButtons;
  75. S32 mNumHats;
  76. S32 mNumBalls;
  77. };
  78. //------------------------------------------------------------------------------
  79. class UInputManager : public InputManager
  80. {
  81. friend bool JoystickInputDevice::process(); // for joystick event funcs
  82. friend void JoystickInputDevice::reset();
  83. public:
  84. UInputManager();
  85. void init();
  86. bool enable();
  87. void disable();
  88. void activate();
  89. void deactivate();
  90. void setWindowLocked(bool locked);
  91. bool isActive() { return( mActive ); }
  92. void onDeleteNotify( SimObject* object );
  93. bool onAdd();
  94. void onRemove();
  95. void process();
  96. bool enableKeyboard();
  97. void disableKeyboard();
  98. bool isKeyboardEnabled() { return( mKeyboardEnabled ); }
  99. bool activateKeyboard();
  100. void deactivateKeyboard();
  101. bool isKeyboardActive() { return( mKeyboardActive ); }
  102. bool enableMouse();
  103. void disableMouse();
  104. bool isMouseEnabled() { return( mMouseEnabled ); }
  105. bool activateMouse();
  106. void deactivateMouse();
  107. bool isMouseActive() { return( mMouseActive ); }
  108. bool enableJoystick();
  109. void disableJoystick();
  110. bool isJoystickEnabled() { return( mJoystickEnabled ); }
  111. bool activateJoystick();
  112. void deactivateJoystick();
  113. bool isJoystickActive() { return( mJoystickActive ); }
  114. void setLocking(bool enabled);
  115. bool getLocking() { return mLocking; }
  116. const char* getJoystickAxesString( U32 deviceID );
  117. bool joystickDetected() { return mJoystickList.size() > 0; }
  118. private:
  119. typedef SimGroup Parent;
  120. // the following vector is just for quick access during event processing.
  121. // it does not manage the cleanup of the JoystickInputDevice objects
  122. Vector<JoystickInputDevice*> mJoystickList;
  123. bool mKeyboardEnabled;
  124. bool mMouseEnabled;
  125. bool mJoystickEnabled;
  126. bool mKeyboardActive;
  127. bool mMouseActive;
  128. bool mJoystickActive;
  129. bool mActive;
  130. // Device state variables
  131. S32 mModifierKeys;
  132. bool mKeyboardState[256];
  133. bool mMouseButtonState[3];
  134. // last mousex and y are maintained when window is unlocked
  135. S32 mLastMouseX;
  136. S32 mLastMouseY;
  137. void initJoystick();
  138. void resetKeyboardState();
  139. void resetMouseState();
  140. void resetInputState();
  141. void lockInput();
  142. void unlockInput();
  143. bool mLocking;
  144. void joyHatEvent(U8 deviceID, U8 hatNum,
  145. U8 prevHatState, U8 currHatState);
  146. void joyButtonEvent(U8 deviceID, U8 buttonNum, bool pressed);
  147. void joyButtonEvent(const SDL_Event& event);
  148. void joyAxisEvent(const SDL_Event& event);
  149. void joyAxisEvent(U8 deviceID, U8 axisNum, S16 axisValue);
  150. void mouseButtonEvent(const SDL_Event& event);
  151. void mouseMotionEvent(const SDL_Event& event);
  152. void keyEvent(const SDL_Event& event);
  153. bool processKeyEvent(InputEventInfo &event);
  154. };
  155. #endif // _H_X86UNIXINPUTMANAGER_