SDL_hidapijoystick_c.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 /* Simple support for BLE Steam Controller, hint is disabled by default */
  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. /* Joystick capability definitions */
  37. #define SDL_JOYSTICK_CAP_MONO_LED 0x00000001
  38. #define SDL_JOYSTICK_CAP_RGB_LED 0x00000002
  39. #define SDL_JOYSTICK_CAP_PLAYER_LED 0x00000004
  40. #define SDL_JOYSTICK_CAP_RUMBLE 0x00000010
  41. #define SDL_JOYSTICK_CAP_TRIGGER_RUMBLE 0x00000020
  42. /* Whether HIDAPI is enabled by default */
  43. #if defined(SDL_PLATFORM_ANDROID) || \
  44. defined(SDL_PLATFORM_IOS) || \
  45. defined(SDL_PLATFORM_TVOS) || \
  46. defined(SDL_PLATFORM_VISIONOS)
  47. /* 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. */
  48. #define SDL_HIDAPI_DEFAULT SDL_FALSE
  49. #else
  50. #define SDL_HIDAPI_DEFAULT SDL_TRUE
  51. #endif
  52. /* The maximum size of a USB packet for HID devices */
  53. #define USB_PACKET_LENGTH 64
  54. /* Forward declaration */
  55. struct SDL_HIDAPI_DeviceDriver;
  56. typedef struct SDL_HIDAPI_Device
  57. {
  58. const void *magic;
  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_JoystickGUID 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. SDL_bool is_bluetooth;
  75. SDL_JoystickType joystick_type;
  76. SDL_GamepadType type;
  77. struct SDL_HIDAPI_DeviceDriver *driver;
  78. void *context;
  79. SDL_Mutex *dev_lock;
  80. SDL_hid_device *dev;
  81. SDL_AtomicInt rumble_pending;
  82. int num_joysticks;
  83. SDL_JoystickID *joysticks;
  84. /* Used during scanning for device changes */
  85. SDL_bool seen;
  86. /* Used to flag that the device is being updated */
  87. SDL_bool updating;
  88. struct SDL_HIDAPI_Device *parent;
  89. int num_children;
  90. struct SDL_HIDAPI_Device **children;
  91. struct SDL_HIDAPI_Device *next;
  92. } SDL_HIDAPI_Device;
  93. typedef struct SDL_HIDAPI_DeviceDriver
  94. {
  95. const char *name;
  96. SDL_bool enabled;
  97. void (*RegisterHints)(SDL_HintCallback callback, void *userdata);
  98. void (*UnregisterHints)(SDL_HintCallback callback, void *userdata);
  99. SDL_bool (*IsEnabled)(void);
  100. SDL_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);
  101. SDL_bool (*InitDevice)(SDL_HIDAPI_Device *device);
  102. int (*GetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id);
  103. void (*SetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index);
  104. SDL_bool (*UpdateDevice)(SDL_HIDAPI_Device *device);
  105. SDL_bool (*OpenJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  106. int (*RumbleJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
  107. int (*RumbleJoystickTriggers)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
  108. Uint32 (*GetJoystickCapabilities)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  109. int (*SetJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
  110. int (*SendJoystickEffect)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size);
  111. int (*SetJoystickSensorsEnabled)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled);
  112. void (*CloseJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  113. void (*FreeDevice)(SDL_HIDAPI_Device *device);
  114. } SDL_HIDAPI_DeviceDriver;
  115. /* HIDAPI device support */
  116. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverCombined;
  117. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube;
  118. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverJoyCons;
  119. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLuna;
  120. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverNintendoClassic;
  121. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3;
  122. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3ThirdParty;
  123. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3SonySixaxis;
  124. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4;
  125. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5;
  126. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverShield;
  127. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia;
  128. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam;
  129. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteamDeck;
  130. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch;
  131. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverWii;
  132. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
  133. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;
  134. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
  135. /* Return true if a HID device is present and supported as a joystick of the given type */
  136. extern SDL_bool HIDAPI_IsDeviceTypePresent(SDL_GamepadType type);
  137. /* Return true if a HID device is present and supported as a joystick */
  138. extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
  139. /* Return the type of a joystick if it's present and supported */
  140. extern SDL_JoystickType HIDAPI_GetJoystickTypeFromGUID(SDL_JoystickGUID guid);
  141. /* Return the type of a game controller if it's present and supported */
  142. extern SDL_GamepadType HIDAPI_GetGamepadTypeFromGUID(SDL_JoystickGUID guid);
  143. extern void HIDAPI_UpdateDevices(void);
  144. extern void HIDAPI_SetDeviceName(SDL_HIDAPI_Device *device, const char *name);
  145. extern void HIDAPI_SetDeviceProduct(SDL_HIDAPI_Device *device, Uint16 vendor_id, Uint16 product_id);
  146. extern void HIDAPI_SetDeviceSerial(SDL_HIDAPI_Device *device, const char *serial);
  147. extern SDL_bool HIDAPI_HasConnectedUSBDevice(const char *serial);
  148. extern void HIDAPI_DisconnectBluetoothDevice(const char *serial);
  149. extern SDL_bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID);
  150. extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID);
  151. extern void HIDAPI_UpdateDeviceProperties(SDL_HIDAPI_Device *device);
  152. extern void HIDAPI_DumpPacket(const char *prefix, const Uint8 *data, int size);
  153. extern SDL_bool HIDAPI_SupportsPlaystationDetection(Uint16 vendor, Uint16 product);
  154. extern float HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max);
  155. #endif /* SDL_JOYSTICK_HIDAPI_H */