sdlInputManager.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 _SDLINPUTMANAGER_H_
  23. #define _SDLINPUTMANAGER_H_
  24. #ifndef _PLATFORMINPUT_H_
  25. #include "platform/platformInput.h"
  26. #endif
  27. #include "SDL.h"
  28. //------------------------------------------------------------------------------
  29. class SDLInputManager : public InputManager
  30. {
  31. enum Constants {
  32. MaxJoysticks = 4, // Up to 4 simultaneous joysticks
  33. MaxControllers = 4, // Up to 4 simultaneous controllers
  34. MaxHats = 2, // Maximum 2 hats per device
  35. MaxBalls = 2, // Maximum 2 trackballs per device
  36. MaxControllerAxes = 7, // From map_StringForControllerAxis[] in SDL_gamecontroller.c
  37. MaxControllerButtons = 16 // From map_StringForControllerButton[] in SDL_gamecontroller.c
  38. };
  39. struct controllerState
  40. {
  41. S32 sdlInstID; // SDL device instance id
  42. U32 torqueInstID; // Torque device instance id
  43. SDL_GameController *inputDevice;
  44. };
  45. struct joystickState
  46. {
  47. S32 sdlInstID; // SDL device instance id
  48. U32 torqueInstID; // Torque device instance id
  49. SDL_Joystick *inputDevice;
  50. U32 numAxes;
  51. U8 lastHatState[MaxHats];
  52. void reset();
  53. };
  54. private:
  55. typedef InputManager Parent;
  56. static S32 map_EventForControllerAxis[MaxControllerAxes];
  57. static S32 map_EventForControllerButton[MaxControllerButtons];
  58. static bool smJoystickEnabled;
  59. static bool smJoystickSplitAxesLR;
  60. static bool smControllerEnabled;
  61. static bool smPOVButtonEvents;
  62. static bool smPOVMaskEvents;
  63. joystickState mJoysticks[MaxJoysticks];
  64. controllerState mControllers[MaxControllers];
  65. // Used to look up a torque instance based on a device inst
  66. HashTable<S32, joystickState*> mJoystickMap;
  67. HashTable<S32, controllerState*> mControllerMap;
  68. bool mJoystickActive;
  69. void deviceConnectedCallback(S32 index);
  70. bool closeControllerByIndex(S32 index);
  71. void closeController(SDL_JoystickID sdlId);
  72. bool closeJoystickByIndex(S32 index);
  73. void closeJoystick(SDL_JoystickID sdlId);
  74. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, S32 iValue);
  75. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, F32 fValue);
  76. void buildHatEvents(U32 deviceType, U32 deviceInst, U8 lastState, U8 currentState, S32 hatIndex);
  77. public:
  78. DECLARE_STATIC_CLASS(SDLInputManager);
  79. public:
  80. SDLInputManager();
  81. bool enable();
  82. void disable();
  83. void process();
  84. void processEvent(SDL_Event &evt);
  85. static void init();
  86. // Console interface:
  87. S32 openJoystick(S32 sdlIndex, S32 requestedTID);
  88. S32 openController(S32 sdlIndex, S32 requestedTID);
  89. void closeDevice(S32 sdlIndex);
  90. S32 getJoystickOpenState(S32 sdlIndex);
  91. void getJoystickTorqueInst(S32 sdlIndex, char* instBuffer);
  92. };
  93. #endif // _SDLINPUTMANAGER_H_