SDL_hidapijoystick_c.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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_JOYSTICK_HIDAPI_H
  20. #define SDL_JOYSTICK_HIDAPI_H
  21. #include "../usb_ids.h"
  22. // This is the full set of HIDAPI drivers available
  23. #define SDL_JOYSTICK_HIDAPI_GAMECUBE
  24. #define SDL_JOYSTICK_HIDAPI_LUNA
  25. #define SDL_JOYSTICK_HIDAPI_PS3
  26. #define SDL_JOYSTICK_HIDAPI_PS4
  27. #define SDL_JOYSTICK_HIDAPI_PS5
  28. #define SDL_JOYSTICK_HIDAPI_STADIA
  29. #define SDL_JOYSTICK_HIDAPI_STEAM
  30. #define SDL_JOYSTICK_HIDAPI_STEAMDECK
  31. #define SDL_JOYSTICK_HIDAPI_SWITCH
  32. #define SDL_JOYSTICK_HIDAPI_WII
  33. #define SDL_JOYSTICK_HIDAPI_XBOX360
  34. #define SDL_JOYSTICK_HIDAPI_XBOXONE
  35. #define SDL_JOYSTICK_HIDAPI_SHIELD
  36. #define SDL_JOYSTICK_HIDAPI_STEAM_HORI
  37. // Joystick capability definitions
  38. #define SDL_JOYSTICK_CAP_MONO_LED 0x00000001
  39. #define SDL_JOYSTICK_CAP_RGB_LED 0x00000002
  40. #define SDL_JOYSTICK_CAP_PLAYER_LED 0x00000004
  41. #define SDL_JOYSTICK_CAP_RUMBLE 0x00000010
  42. #define SDL_JOYSTICK_CAP_TRIGGER_RUMBLE 0x00000020
  43. // Whether HIDAPI is enabled by default
  44. #if defined(SDL_PLATFORM_ANDROID) || \
  45. defined(SDL_PLATFORM_IOS) || \
  46. defined(SDL_PLATFORM_TVOS) || \
  47. defined(SDL_PLATFORM_VISIONOS)
  48. // On Android, HIDAPI prompts for permissions and acquires exclusive access to the device, and on Apple mobile platforms it doesn't do anything except for handling Bluetooth Steam Controllers, so we'll leave it off by default.
  49. #define SDL_HIDAPI_DEFAULT false
  50. #else
  51. #define SDL_HIDAPI_DEFAULT true
  52. #endif
  53. // The maximum size of a USB packet for HID devices
  54. #define USB_PACKET_LENGTH 64
  55. // Forward declaration
  56. struct SDL_HIDAPI_DeviceDriver;
  57. typedef struct SDL_HIDAPI_Device
  58. {
  59. char *name;
  60. char *manufacturer_string;
  61. char *product_string;
  62. char *path;
  63. Uint16 vendor_id;
  64. Uint16 product_id;
  65. Uint16 version;
  66. char *serial;
  67. SDL_GUID guid;
  68. int interface_number; // Available on Windows and Linux
  69. int interface_class;
  70. int interface_subclass;
  71. int interface_protocol;
  72. Uint16 usage_page; // Available on Windows and macOS
  73. Uint16 usage; // Available on Windows and macOS
  74. bool is_bluetooth;
  75. SDL_JoystickType joystick_type;
  76. SDL_GamepadType type;
  77. int steam_virtual_gamepad_slot;
  78. struct SDL_HIDAPI_DeviceDriver *driver;
  79. void *context;
  80. SDL_Mutex *dev_lock;
  81. SDL_hid_device *dev;
  82. SDL_AtomicInt rumble_pending;
  83. int num_joysticks;
  84. SDL_JoystickID *joysticks;
  85. // Used during scanning for device changes
  86. bool seen;
  87. // Used to flag that the device is being updated
  88. bool updating;
  89. struct SDL_HIDAPI_Device *parent;
  90. int num_children;
  91. struct SDL_HIDAPI_Device **children;
  92. struct SDL_HIDAPI_Device *next;
  93. } SDL_HIDAPI_Device;
  94. typedef struct SDL_HIDAPI_DeviceDriver
  95. {
  96. const char *name;
  97. bool enabled;
  98. void (*RegisterHints)(SDL_HintCallback callback, void *userdata);
  99. void (*UnregisterHints)(SDL_HintCallback callback, void *userdata);
  100. bool (*IsEnabled)(void);
  101. bool (*IsSupportedDevice)(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol);
  102. bool (*InitDevice)(SDL_HIDAPI_Device *device);
  103. int (*GetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id);
  104. void (*SetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index);
  105. bool (*UpdateDevice)(SDL_HIDAPI_Device *device);
  106. bool (*OpenJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  107. bool (*RumbleJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
  108. bool (*RumbleJoystickTriggers)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
  109. Uint32 (*GetJoystickCapabilities)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  110. bool (*SetJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
  111. bool (*SendJoystickEffect)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size);
  112. bool (*SetJoystickSensorsEnabled)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled);
  113. void (*CloseJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  114. void (*FreeDevice)(SDL_HIDAPI_Device *device);
  115. } SDL_HIDAPI_DeviceDriver;
  116. // HIDAPI device support
  117. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverCombined;
  118. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube;
  119. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverJoyCons;
  120. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLuna;
  121. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverNintendoClassic;
  122. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3;
  123. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3ThirdParty;
  124. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3SonySixaxis;
  125. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4;
  126. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5;
  127. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverShield;
  128. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia;
  129. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam;
  130. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteamDeck;
  131. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch;
  132. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverWii;
  133. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
  134. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;
  135. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
  136. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteamHori;
  137. // Return true if a HID device is present and supported as a joystick of the given type
  138. extern bool HIDAPI_IsDeviceTypePresent(SDL_GamepadType type);
  139. // Return true if a HID device is present and supported as a joystick
  140. extern bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
  141. // Return the name of a connected device, which should be freed with SDL_free(), or NULL if it's not available
  142. extern char *HIDAPI_GetDeviceProductName(Uint16 vendor_id, Uint16 product_id);
  143. // Return the manufacturer of a connected device, which should be freed with SDL_free(), or NULL if it's not available
  144. extern char *HIDAPI_GetDeviceManufacturerName(Uint16 vendor_id, Uint16 product_id);
  145. // Return the type of a joystick if it's present and supported
  146. extern SDL_JoystickType HIDAPI_GetJoystickTypeFromGUID(SDL_GUID guid);
  147. // Return the type of a game controller if it's present and supported
  148. extern SDL_GamepadType HIDAPI_GetGamepadTypeFromGUID(SDL_GUID guid);
  149. extern void HIDAPI_UpdateDevices(void);
  150. extern void HIDAPI_SetDeviceName(SDL_HIDAPI_Device *device, const char *name);
  151. extern void HIDAPI_SetDeviceProduct(SDL_HIDAPI_Device *device, Uint16 vendor_id, Uint16 product_id);
  152. extern void HIDAPI_SetDeviceSerial(SDL_HIDAPI_Device *device, const char *serial);
  153. extern bool HIDAPI_HasConnectedUSBDevice(const char *serial);
  154. extern void HIDAPI_DisconnectBluetoothDevice(const char *serial);
  155. extern bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID);
  156. extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID);
  157. extern void HIDAPI_UpdateDeviceProperties(SDL_HIDAPI_Device *device);
  158. extern void HIDAPI_DumpPacket(const char *prefix, const Uint8 *data, int size);
  159. extern bool HIDAPI_SupportsPlaystationDetection(Uint16 vendor, Uint16 product);
  160. extern float HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max);
  161. #endif // SDL_JOYSTICK_HIDAPI_H