SDL_hidapijoystick_c.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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 "SDL_atomic.h"
  22. #include "SDL_mutex.h"
  23. #include "SDL_joystick.h"
  24. #include "SDL_gamecontroller.h"
  25. #include "../../hidapi/hidapi/hidapi.h"
  26. #include "../usb_ids.h"
  27. /* This is the full set of HIDAPI drivers available */
  28. #define SDL_JOYSTICK_HIDAPI_GAMECUBE
  29. #define SDL_JOYSTICK_HIDAPI_PS4
  30. #define SDL_JOYSTICK_HIDAPI_PS5
  31. #define SDL_JOYSTICK_HIDAPI_STADIA
  32. #define SDL_JOYSTICK_HIDAPI_SWITCH
  33. #define SDL_JOYSTICK_HIDAPI_XBOX360
  34. #define SDL_JOYSTICK_HIDAPI_XBOXONE
  35. #if defined(__IPHONEOS__) || defined(__TVOS__) || defined(__ANDROID__)
  36. /* Very basic Steam Controller support on mobile devices */
  37. #define SDL_JOYSTICK_HIDAPI_STEAM
  38. #endif
  39. /* The maximum size of a USB packet for HID devices */
  40. #define USB_PACKET_LENGTH 64
  41. /* Forward declaration */
  42. struct _SDL_HIDAPI_DeviceDriver;
  43. typedef struct _SDL_HIDAPI_Device
  44. {
  45. char *name;
  46. char *path;
  47. Uint16 vendor_id;
  48. Uint16 product_id;
  49. Uint16 version;
  50. char *serial;
  51. SDL_JoystickGUID guid;
  52. int interface_number; /* Available on Windows and Linux */
  53. int interface_class;
  54. int interface_subclass;
  55. int interface_protocol;
  56. Uint16 usage_page; /* Available on Windows and Mac OS X */
  57. Uint16 usage; /* Available on Windows and Mac OS X */
  58. struct _SDL_HIDAPI_DeviceDriver *driver;
  59. void *context;
  60. SDL_mutex *dev_lock;
  61. hid_device *dev;
  62. SDL_atomic_t rumble_pending;
  63. int num_joysticks;
  64. SDL_JoystickID *joysticks;
  65. /* Used during scanning for device changes */
  66. SDL_bool seen;
  67. /* Used to flag that the device is being updated */
  68. SDL_bool updating;
  69. struct _SDL_HIDAPI_Device *next;
  70. } SDL_HIDAPI_Device;
  71. typedef struct _SDL_HIDAPI_DeviceDriver
  72. {
  73. const char *hint;
  74. SDL_bool enabled;
  75. SDL_bool (*IsSupportedDevice)(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol);
  76. const char *(*GetDeviceName)(Uint16 vendor_id, Uint16 product_id);
  77. SDL_bool (*InitDevice)(SDL_HIDAPI_Device *device);
  78. int (*GetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id);
  79. void (*SetDevicePlayerIndex)(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index);
  80. SDL_bool (*UpdateDevice)(SDL_HIDAPI_Device *device);
  81. SDL_bool (*OpenJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  82. int (*RumbleJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
  83. int (*RumbleJoystickTriggers)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);
  84. SDL_bool (*HasJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  85. int (*SetJoystickLED)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);
  86. int (*SetJoystickSensorsEnabled)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled);
  87. void (*CloseJoystick)(SDL_HIDAPI_Device *device, SDL_Joystick *joystick);
  88. void (*FreeDevice)(SDL_HIDAPI_Device *device);
  89. } SDL_HIDAPI_DeviceDriver;
  90. /* HIDAPI device support */
  91. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube;
  92. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4;
  93. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5;
  94. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia;
  95. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam;
  96. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch;
  97. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
  98. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;
  99. extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
  100. /* Return true if a HID device is present and supported as a joystick */
  101. extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
  102. extern void HIDAPI_UpdateDevices(void);
  103. extern SDL_bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID);
  104. extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID);
  105. extern void HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size);
  106. extern float HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max);
  107. #endif /* SDL_JOYSTICK_HIDAPI_H */
  108. /* vi: set ts=4 sw=4 expandtab: */