x86UNIXInputManager.h 5.8 KB

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