SDL_sysjoystick.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifndef SDL_sysjoystick_h_
  20. #define SDL_sysjoystick_h_
  21. // This is the system specific header for the SDL joystick API
  22. #include "SDL_joystick_c.h"
  23. // Set up for C function definitions, even when using C++
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. // The SDL joystick structure
  28. typedef struct SDL_JoystickAxisInfo
  29. {
  30. Sint16 initial_value; // Initial axis state
  31. Sint16 value; // Current axis state
  32. Sint16 zero; // Zero point on the axis (-32768 for triggers)
  33. bool has_initial_value; // Whether we've seen a value on the axis yet
  34. bool has_second_value; // Whether we've seen a second value on the axis yet
  35. bool sent_initial_value; // Whether we've sent the initial axis value
  36. bool sending_initial_value; // Whether we are sending the initial axis value
  37. } SDL_JoystickAxisInfo;
  38. typedef struct SDL_JoystickBallData
  39. {
  40. int dx;
  41. int dy;
  42. } SDL_JoystickBallData;
  43. typedef struct SDL_JoystickTouchpadFingerInfo
  44. {
  45. bool down;
  46. float x;
  47. float y;
  48. float pressure;
  49. } SDL_JoystickTouchpadFingerInfo;
  50. typedef struct SDL_JoystickTouchpadInfo
  51. {
  52. int nfingers;
  53. SDL_JoystickTouchpadFingerInfo *fingers;
  54. } SDL_JoystickTouchpadInfo;
  55. typedef struct SDL_JoystickSensorInfo
  56. {
  57. SDL_SensorType type;
  58. bool enabled;
  59. float rate;
  60. float data[3]; // If this needs to expand, update SDL_GamepadSensorEvent
  61. } SDL_JoystickSensorInfo;
  62. #define _guarded SDL_GUARDED_BY(SDL_joystick_lock)
  63. struct SDL_Joystick
  64. {
  65. SDL_JoystickID instance_id _guarded; // Device instance, monotonically increasing from 0
  66. char *name _guarded; // Joystick name - system dependent
  67. char *path _guarded; // Joystick path - system dependent
  68. char *serial _guarded; // Joystick serial
  69. SDL_GUID guid _guarded; // Joystick guid
  70. Uint16 firmware_version _guarded; // Firmware version, if available
  71. Uint64 steam_handle _guarded; // Steam controller API handle
  72. int naxes _guarded; // Number of axis controls on the joystick
  73. SDL_JoystickAxisInfo *axes _guarded;
  74. int nballs _guarded; // Number of trackballs on the joystick
  75. SDL_JoystickBallData *balls _guarded; // Current ball motion deltas
  76. int nhats _guarded; // Number of hats on the joystick
  77. Uint8 *hats _guarded; // Current hat states
  78. int nbuttons _guarded; // Number of buttons on the joystick
  79. bool *buttons _guarded; // Current button states
  80. int ntouchpads _guarded; // Number of touchpads on the joystick
  81. SDL_JoystickTouchpadInfo *touchpads _guarded; // Current touchpad states
  82. int nsensors _guarded; // Number of sensors on the joystick
  83. int nsensors_enabled _guarded;
  84. SDL_JoystickSensorInfo *sensors _guarded;
  85. Uint16 low_frequency_rumble _guarded;
  86. Uint16 high_frequency_rumble _guarded;
  87. Uint64 rumble_expiration _guarded;
  88. Uint64 rumble_resend _guarded;
  89. Uint16 left_trigger_rumble _guarded;
  90. Uint16 right_trigger_rumble _guarded;
  91. Uint64 trigger_rumble_expiration _guarded;
  92. Uint8 led_red _guarded;
  93. Uint8 led_green _guarded;
  94. Uint8 led_blue _guarded;
  95. Uint64 led_expiration _guarded;
  96. bool attached _guarded;
  97. SDL_JoystickConnectionState connection_state _guarded;
  98. SDL_PowerState battery_state _guarded;
  99. int battery_percent _guarded;
  100. bool delayed_guide_button _guarded; // true if this device has the guide button event delayed
  101. SDL_SensorID accel_sensor _guarded;
  102. SDL_Sensor *accel _guarded;
  103. SDL_SensorID gyro_sensor _guarded;
  104. SDL_Sensor *gyro _guarded;
  105. float sensor_transform[3][3] _guarded;
  106. Uint64 update_complete _guarded;
  107. struct SDL_JoystickDriver *driver _guarded;
  108. struct joystick_hwdata *hwdata _guarded; // Driver dependent information
  109. SDL_PropertiesID props _guarded;
  110. int ref_count _guarded; // Reference count for multiple opens
  111. struct SDL_Joystick *next _guarded; // pointer to next joystick we have allocated
  112. };
  113. #undef _guarded
  114. // Device bus definitions
  115. #define SDL_HARDWARE_BUS_UNKNOWN 0x00
  116. #define SDL_HARDWARE_BUS_USB 0x03
  117. #define SDL_HARDWARE_BUS_BLUETOOTH 0x05
  118. #define SDL_HARDWARE_BUS_VIRTUAL 0xFF
  119. // Macro to combine a USB vendor ID and product ID into a single Uint32 value
  120. #define MAKE_VIDPID(VID, PID) (((Uint32)(VID)) << 16 | (PID))
  121. typedef struct SDL_JoystickDriver
  122. {
  123. /* Function to scan the system for joysticks.
  124. * Joystick 0 should be the system default joystick.
  125. * This function should return 0, or -1 on an unrecoverable error.
  126. */
  127. bool (*Init)(void);
  128. // Function to return the number of joystick devices plugged in right now
  129. int (*GetCount)(void);
  130. // Function to cause any queued joystick insertions to be processed
  131. void (*Detect)(void);
  132. // Function to determine whether a device is currently detected by this driver
  133. bool (*IsDevicePresent)(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
  134. // Function to get the device-dependent name of a joystick
  135. const char *(*GetDeviceName)(int device_index);
  136. // Function to get the device-dependent path of a joystick
  137. const char *(*GetDevicePath)(int device_index);
  138. // Function to get the Steam virtual gamepad slot of a joystick
  139. int (*GetDeviceSteamVirtualGamepadSlot)(int device_index);
  140. // Function to get the player index of a joystick
  141. int (*GetDevicePlayerIndex)(int device_index);
  142. // Function to set the player index of a joystick
  143. void (*SetDevicePlayerIndex)(int device_index, int player_index);
  144. // Function to return the stable GUID for a plugged in device
  145. SDL_GUID (*GetDeviceGUID)(int device_index);
  146. // Function to get the current instance id of the joystick located at device_index
  147. SDL_JoystickID (*GetDeviceInstanceID)(int device_index);
  148. /* Function to open a joystick for use.
  149. The joystick to open is specified by the device index.
  150. This should fill the nbuttons and naxes fields of the joystick structure.
  151. It returns 0, or -1 if there is an error.
  152. */
  153. bool (*Open)(SDL_Joystick *joystick, int device_index);
  154. // Rumble functionality
  155. bool (*Rumble)(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
  156. bool (*RumbleTriggers)(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
  157. // LED functionality
  158. bool (*SetLED)(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
  159. // General effects
  160. bool (*SendEffect)(SDL_Joystick *joystick, const void *data, int size);
  161. // Sensor functionality
  162. bool (*SetSensorsEnabled)(SDL_Joystick *joystick, bool enabled);
  163. /* Function to update the state of a joystick - called as a device poll.
  164. * This function shouldn't update the joystick structure directly,
  165. * but instead should call SDL_PrivateJoystick*() to deliver events
  166. * and update joystick device state.
  167. */
  168. void (*Update)(SDL_Joystick *joystick);
  169. // Function to close a joystick after use
  170. void (*Close)(SDL_Joystick *joystick);
  171. // Function to perform any system-specific joystick related cleanup
  172. void (*Quit)(void);
  173. // Function to get the autodetected controller mapping; returns false if there isn't any.
  174. bool (*GetGamepadMapping)(int device_index, SDL_GamepadMapping *out);
  175. } SDL_JoystickDriver;
  176. // Windows and Mac OSX has a limit of MAX_DWORD / 1000, Linux kernel has a limit of 0xFFFF
  177. #define SDL_MAX_RUMBLE_DURATION_MS 0xFFFF
  178. /* Dualshock4 only rumbles for about 5 seconds max, resend rumble command every 2 seconds
  179. * to make long rumble work. */
  180. #define SDL_RUMBLE_RESEND_MS 2000
  181. #define SDL_LED_MIN_REPEAT_MS 5000
  182. // The available joystick drivers
  183. extern SDL_JoystickDriver SDL_PRIVATE_JoystickDriver;
  184. extern SDL_JoystickDriver SDL_ANDROID_JoystickDriver;
  185. extern SDL_JoystickDriver SDL_BSD_JoystickDriver;
  186. extern SDL_JoystickDriver SDL_DARWIN_JoystickDriver;
  187. extern SDL_JoystickDriver SDL_DUMMY_JoystickDriver;
  188. extern SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver;
  189. extern SDL_JoystickDriver SDL_HAIKU_JoystickDriver;
  190. extern SDL_JoystickDriver SDL_HIDAPI_JoystickDriver;
  191. extern SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver;
  192. extern SDL_JoystickDriver SDL_IOS_JoystickDriver;
  193. extern SDL_JoystickDriver SDL_LINUX_JoystickDriver;
  194. extern SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver;
  195. extern SDL_JoystickDriver SDL_WGI_JoystickDriver;
  196. extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver;
  197. extern SDL_JoystickDriver SDL_WINMM_JoystickDriver;
  198. extern SDL_JoystickDriver SDL_PS2_JoystickDriver;
  199. extern SDL_JoystickDriver SDL_PSP_JoystickDriver;
  200. extern SDL_JoystickDriver SDL_VITA_JoystickDriver;
  201. extern SDL_JoystickDriver SDL_N3DS_JoystickDriver;
  202. extern SDL_JoystickDriver SDL_GAMEINPUT_JoystickDriver;
  203. // Ends C function definitions when using C++
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif // SDL_sysjoystick_h_