SDL_sysjoystick.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. bool swap_face_buttons _guarded; // Whether we should swap face buttons
  73. bool is_virtual _guarded; // Whether this is a virtual joystick
  74. int naxes _guarded; // Number of axis controls on the joystick
  75. SDL_JoystickAxisInfo *axes _guarded;
  76. int nballs _guarded; // Number of trackballs on the joystick
  77. SDL_JoystickBallData *balls _guarded; // Current ball motion deltas
  78. int nhats _guarded; // Number of hats on the joystick
  79. Uint8 *hats _guarded; // Current hat states
  80. int nbuttons _guarded; // Number of buttons on the joystick
  81. bool *buttons _guarded; // Current button states
  82. int ntouchpads _guarded; // Number of touchpads on the joystick
  83. SDL_JoystickTouchpadInfo *touchpads _guarded; // Current touchpad states
  84. int nsensors _guarded; // Number of sensors on the joystick
  85. int nsensors_enabled _guarded;
  86. SDL_JoystickSensorInfo *sensors _guarded;
  87. Uint16 low_frequency_rumble _guarded;
  88. Uint16 high_frequency_rumble _guarded;
  89. Uint64 rumble_expiration _guarded;
  90. Uint64 rumble_resend _guarded;
  91. Uint16 left_trigger_rumble _guarded;
  92. Uint16 right_trigger_rumble _guarded;
  93. Uint64 trigger_rumble_expiration _guarded;
  94. Uint64 trigger_rumble_resend _guarded;
  95. Uint8 led_red _guarded;
  96. Uint8 led_green _guarded;
  97. Uint8 led_blue _guarded;
  98. Uint64 led_expiration _guarded;
  99. bool attached _guarded;
  100. SDL_JoystickConnectionState connection_state _guarded;
  101. SDL_PowerState battery_state _guarded;
  102. int battery_percent _guarded;
  103. bool delayed_guide_button _guarded; // true if this device has the guide button event delayed
  104. SDL_SensorID accel_sensor _guarded;
  105. SDL_Sensor *accel _guarded;
  106. SDL_SensorID gyro_sensor _guarded;
  107. SDL_Sensor *gyro _guarded;
  108. float sensor_transform[3][3] _guarded;
  109. Uint64 update_complete _guarded;
  110. struct SDL_JoystickDriver *driver _guarded;
  111. struct joystick_hwdata *hwdata _guarded; // Driver dependent information
  112. SDL_PropertiesID props _guarded;
  113. int ref_count _guarded; // Reference count for multiple opens
  114. struct SDL_Joystick *next _guarded; // pointer to next joystick we have allocated
  115. };
  116. #undef _guarded
  117. // Device bus definitions
  118. #define SDL_HARDWARE_BUS_UNKNOWN 0x00
  119. #define SDL_HARDWARE_BUS_USB 0x03
  120. #define SDL_HARDWARE_BUS_BLUETOOTH 0x05
  121. #define SDL_HARDWARE_BUS_VIRTUAL 0xFF
  122. // Macro to combine a USB vendor ID and product ID into a single Uint32 value
  123. #define MAKE_VIDPID(VID, PID) (((Uint32)(VID)) << 16 | (PID))
  124. typedef struct SDL_JoystickDriver
  125. {
  126. /* Function to scan the system for joysticks.
  127. * Joystick 0 should be the system default joystick.
  128. * This function should return 0, or -1 on an unrecoverable error.
  129. */
  130. bool (*Init)(void);
  131. // Function to return the number of joystick devices plugged in right now
  132. int (*GetCount)(void);
  133. // Function to cause any queued joystick insertions to be processed
  134. void (*Detect)(void);
  135. // Function to determine whether a device is currently detected by this driver
  136. bool (*IsDevicePresent)(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
  137. // Function to get the device-dependent name of a joystick
  138. const char *(*GetDeviceName)(int device_index);
  139. // Function to get the device-dependent path of a joystick
  140. const char *(*GetDevicePath)(int device_index);
  141. // Function to get the Steam virtual gamepad slot of a joystick
  142. int (*GetDeviceSteamVirtualGamepadSlot)(int device_index);
  143. // Function to get the player index of a joystick
  144. int (*GetDevicePlayerIndex)(int device_index);
  145. // Function to set the player index of a joystick
  146. void (*SetDevicePlayerIndex)(int device_index, int player_index);
  147. // Function to return the stable GUID for a plugged in device
  148. SDL_GUID (*GetDeviceGUID)(int device_index);
  149. // Function to get the current instance id of the joystick located at device_index
  150. SDL_JoystickID (*GetDeviceInstanceID)(int device_index);
  151. /* Function to open a joystick for use.
  152. The joystick to open is specified by the device index.
  153. This should fill the nbuttons and naxes fields of the joystick structure.
  154. It returns 0, or -1 if there is an error.
  155. */
  156. bool (*Open)(SDL_Joystick *joystick, int device_index);
  157. // Rumble functionality
  158. bool (*Rumble)(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
  159. bool (*RumbleTriggers)(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
  160. // LED functionality
  161. bool (*SetLED)(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
  162. // General effects
  163. bool (*SendEffect)(SDL_Joystick *joystick, const void *data, int size);
  164. // Sensor functionality
  165. bool (*SetSensorsEnabled)(SDL_Joystick *joystick, bool enabled);
  166. /* Function to update the state of a joystick - called as a device poll.
  167. * This function shouldn't update the joystick structure directly,
  168. * but instead should call SDL_PrivateJoystick*() to deliver events
  169. * and update joystick device state.
  170. */
  171. void (*Update)(SDL_Joystick *joystick);
  172. // Function to close a joystick after use
  173. void (*Close)(SDL_Joystick *joystick);
  174. // Function to perform any system-specific joystick related cleanup
  175. void (*Quit)(void);
  176. // Function to get the autodetected controller mapping; returns false if there isn't any.
  177. bool (*GetGamepadMapping)(int device_index, SDL_GamepadMapping *out);
  178. } SDL_JoystickDriver;
  179. // Windows and Mac OSX has a limit of MAX_DWORD / 1000, Linux kernel has a limit of 0xFFFF
  180. #define SDL_MAX_RUMBLE_DURATION_MS 0xFFFF
  181. /* Dualshock4 only rumbles for about 5 seconds max, resend rumble command every 2 seconds
  182. * to make long rumble work. */
  183. #define SDL_RUMBLE_RESEND_MS 2000
  184. #define SDL_LED_MIN_REPEAT_MS 5000
  185. // The available joystick drivers
  186. extern SDL_JoystickDriver SDL_PRIVATE_JoystickDriver;
  187. extern SDL_JoystickDriver SDL_ANDROID_JoystickDriver;
  188. extern SDL_JoystickDriver SDL_BSD_JoystickDriver;
  189. extern SDL_JoystickDriver SDL_DARWIN_JoystickDriver;
  190. extern SDL_JoystickDriver SDL_DUMMY_JoystickDriver;
  191. extern SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver;
  192. extern SDL_JoystickDriver SDL_HAIKU_JoystickDriver;
  193. extern SDL_JoystickDriver SDL_HIDAPI_JoystickDriver;
  194. extern SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver;
  195. extern SDL_JoystickDriver SDL_IOS_JoystickDriver;
  196. extern SDL_JoystickDriver SDL_LINUX_JoystickDriver;
  197. extern SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver;
  198. extern SDL_JoystickDriver SDL_WGI_JoystickDriver;
  199. extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver;
  200. extern SDL_JoystickDriver SDL_WINMM_JoystickDriver;
  201. extern SDL_JoystickDriver SDL_PS2_JoystickDriver;
  202. extern SDL_JoystickDriver SDL_PSP_JoystickDriver;
  203. extern SDL_JoystickDriver SDL_VITA_JoystickDriver;
  204. extern SDL_JoystickDriver SDL_N3DS_JoystickDriver;
  205. extern SDL_JoystickDriver SDL_GAMEINPUT_JoystickDriver;
  206. // Ends C function definitions when using C++
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif // SDL_sysjoystick_h_