Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add APIs to query joystick/gamecontroller rumble support #4943

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions include/SDL_gamecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,8 @@ extern DECLSPEC int SDLCALL SDL_GameControllerGetSensorData(SDL_GameController *
* \returns 0, or -1 if rumble isn't supported on this controller
*
* \since This function is available since SDL 2.0.9.
*
* \sa SDL_GameControllerHasRumble
*/
extern DECLSPEC int SDLCALL SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);

Expand All @@ -879,6 +881,8 @@ extern DECLSPEC int SDLCALL SDL_GameControllerRumble(SDL_GameController *gamecon
* \returns 0, or -1 if trigger rumble isn't supported on this controller
*
* \since This function is available since SDL 2.0.14.
*
* \sa SDL_GameControllerHasRumbleTriggers
*/
extern DECLSPEC int SDLCALL SDL_GameControllerRumbleTriggers(SDL_GameController *gamecontroller, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms);

Expand All @@ -893,6 +897,32 @@ extern DECLSPEC int SDLCALL SDL_GameControllerRumbleTriggers(SDL_GameController
*/
extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasLED(SDL_GameController *gamecontroller);

/**
* Query whether a game controller has rumble support.
*
* \param gamecontroller The controller to query
* \returns SDL_TRUE, or SDL_FALSE if this controller does not have
* rumble support
*
* \since This function is available since SDL 2.0.18.
*
* \sa SDL_GameControllerRumble
*/
extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasRumble(SDL_GameController *gamecontroller);

/**
* Query whether a game controller has rumble support on triggers.
*
* \param gamecontroller The controller to query
* \returns SDL_TRUE, or SDL_FALSE if this controller does not have
* trigger rumble support
*
* \since This function is available since SDL 2.0.18.
*
* \sa SDL_GameControllerRumbleTriggers
*/
extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerHasRumbleTriggers(SDL_GameController *gamecontroller);

/**
* Update a game controller's LED color.
*
Expand Down
28 changes: 28 additions & 0 deletions include/SDL_joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ extern DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick *joystick,
* \returns 0, or -1 if rumble isn't supported on this joystick
*
* \since This function is available since SDL 2.0.9.
*
* \sa SDL_JoystickHasRumble
*/
extern DECLSPEC int SDLCALL SDL_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);

Expand All @@ -841,6 +843,8 @@ extern DECLSPEC int SDLCALL SDL_JoystickRumble(SDL_Joystick *joystick, Uint16 lo
* \returns 0, or -1 if trigger rumble isn't supported on this joystick
*
* \since This function is available since SDL 2.0.14.
*
* \sa SDL_JoystickHasRumbleTriggers
*/
extern DECLSPEC int SDLCALL SDL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms);

Expand All @@ -857,6 +861,30 @@ extern DECLSPEC int SDLCALL SDL_JoystickRumbleTriggers(SDL_Joystick *joystick, U
*/
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasLED(SDL_Joystick *joystick);

/**
* Query whether a joystick has rumble support.
*
* \param joystick The joystick to query
* \return SDL_TRUE if the joystick has rumble, SDL_FALSE otherwise.
*
* \since This function is available since SDL 2.0.18.
*
* \sa SDL_JoystickRumble
*/
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasRumble(SDL_Joystick *joystick);

/**
* Query whether a joystick has rumble support on triggers.
*
* \param joystick The joystick to query
* \return SDL_TRUE if the joystick has trigger rumble, SDL_FALSE otherwise.
*
* \since This function is available since SDL 2.0.18.
*
* \sa SDL_JoystickRumbleTriggers
*/
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick);

/**
* Update a joystick's LED color.
*
Expand Down
4 changes: 4 additions & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -849,3 +849,7 @@
#define SDL_hid_device_change_count SDL_hid_device_change_count_REAL
#define SDL_hid_open SDL_hid_open_REAL
#define SDL_hid_open_path SDL_hid_open_path_REAL
#define SDL_JoystickHasRumble SDL_JoystickHasRumble_REAL
#define SDL_JoystickHasRumbleTriggers SDL_JoystickHasRumbleTriggers_REAL
#define SDL_GameControllerHasRumble SDL_GameControllerHasRumble_REAL
#define SDL_GameControllerHasRumbleTriggers SDL_GameControllerHasRumbleTriggers_REAL
4 changes: 4 additions & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -918,3 +918,7 @@ SDL_DYNAPI_PROC(void,SDL_RenderLogicalToWindow,(SDL_Renderer *a, float b, float
SDL_DYNAPI_PROC(Uint32,SDL_hid_device_change_count,(void),(),return)
SDL_DYNAPI_PROC(SDL_hid_device*,SDL_hid_open,(unsigned short a, unsigned short b, const wchar_t *c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_hid_device*,SDL_hid_open_path,(const char *a, int b),(a,b),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_JoystickHasRumble,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_JoystickHasRumbleTriggers,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GameControllerHasRumble,(SDL_GameController *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GameControllerHasRumbleTriggers,(SDL_GameController *a),(a),return)
12 changes: 12 additions & 0 deletions src/joystick/SDL_gamecontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,18 @@ SDL_GameControllerHasLED(SDL_GameController *gamecontroller)
return SDL_JoystickHasLED(SDL_GameControllerGetJoystick(gamecontroller));
}

SDL_bool
SDL_GameControllerHasRumble(SDL_GameController *gamecontroller)
{
return SDL_JoystickHasRumble(SDL_GameControllerGetJoystick(gamecontroller));
}

SDL_bool
SDL_GameControllerHasRumbleTriggers(SDL_GameController *gamecontroller)
{
return SDL_JoystickHasRumbleTriggers(SDL_GameControllerGetJoystick(gamecontroller));
}

int
SDL_GameControllerSetLED(SDL_GameController *gamecontroller, Uint8 red, Uint8 green, Uint8 blue)
{
Expand Down
38 changes: 37 additions & 1 deletion src/joystick/SDL_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,43 @@ SDL_JoystickHasLED(SDL_Joystick *joystick)

SDL_LockJoysticks();

result = joystick->driver->HasLED(joystick);
result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_LED) != 0;

SDL_UnlockJoysticks();

return result;
}

SDL_bool
SDL_JoystickHasRumble(SDL_Joystick *joystick)
{
SDL_bool result;

if (!SDL_PrivateJoystickValid(joystick)) {
return SDL_FALSE;
}

SDL_LockJoysticks();

result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE) != 0;

SDL_UnlockJoysticks();

return result;
}

SDL_bool
SDL_JoystickHasRumbleTriggers(SDL_Joystick *joystick)
{
SDL_bool result;

if (!SDL_PrivateJoystickValid(joystick)) {
return SDL_FALSE;
}

SDL_LockJoysticks();

result = (joystick->driver->GetCapabilities(joystick) & SDL_JOYCAP_RUMBLE_TRIGGERS) != 0;

SDL_UnlockJoysticks();

Expand Down
9 changes: 8 additions & 1 deletion src/joystick/SDL_sysjoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ struct _SDL_Joystick
#define SDL_HARDWARE_BUS_USB 0x03
#define SDL_HARDWARE_BUS_BLUETOOTH 0x05

/* Joystick capability flags for GetCapabilities() */
#define SDL_JOYCAP_LED 0x01
#define SDL_JOYCAP_RUMBLE 0x02
#define SDL_JOYCAP_RUMBLE_TRIGGERS 0x04

/* Macro to combine a USB vendor ID and product ID into a single Uint32 value */
#define MAKE_VIDPID(VID, PID) (((Uint32)(VID))<<16|(PID))

Expand Down Expand Up @@ -164,8 +169,10 @@ typedef struct _SDL_JoystickDriver
int (*Rumble)(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble);
int (*RumbleTriggers)(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble);

/* Capability detection */
Uint32 (*GetCapabilities)(SDL_Joystick *joystick);

/* LED functionality */
SDL_bool (*HasLED)(SDL_Joystick *joystick);
int (*SetLED)(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue);

/* General effects */
Expand Down
8 changes: 4 additions & 4 deletions src/joystick/android/SDL_sysjoystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,10 @@ ANDROID_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint1
return SDL_Unsupported();
}

static SDL_bool
ANDROID_JoystickHasLED(SDL_Joystick *joystick)
static Uint32
ANDROID_JoystickGetCapabilities(SDL_Joystick *joystick)
{
return SDL_FALSE;
return 0;
}

static int
Expand Down Expand Up @@ -719,7 +719,7 @@ SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
ANDROID_JoystickOpen,
ANDROID_JoystickRumble,
ANDROID_JoystickRumbleTriggers,
ANDROID_JoystickHasLED,
ANDROID_JoystickGetCapabilities,
ANDROID_JoystickSetLED,
ANDROID_JoystickSendEffect,
ANDROID_JoystickSetSensorsEnabled,
Expand Down
8 changes: 4 additions & 4 deletions src/joystick/bsd/SDL_bsdjoystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,10 @@ BSD_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
return SDL_FALSE;
}

static SDL_bool
BSD_JoystickHasLED(SDL_Joystick *joystick)
static Uint32
BSD_JoystickGetCapabilities(SDL_Joystick *joystick)
{
return SDL_FALSE;
return 0;
}

static int
Expand Down Expand Up @@ -814,7 +814,7 @@ SDL_JoystickDriver SDL_BSD_JoystickDriver =
BSD_JoystickOpen,
BSD_JoystickRumble,
BSD_JoystickRumbleTriggers,
BSD_JoystickHasLED,
BSD_JoystickGetCapabilities,
BSD_JoystickSetLED,
BSD_JoystickSendEffect,
BSD_JoystickSetSensorsEnabled,
Expand Down
19 changes: 15 additions & 4 deletions src/joystick/darwin/SDL_iokitjoystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,10 +939,21 @@ DARWIN_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16
return SDL_Unsupported();
}

static SDL_bool
DARWIN_JoystickHasLED(SDL_Joystick *joystick)
static Uint32
DARWIN_JoystickGetCapabilities(SDL_Joystick *joystick)
{
return SDL_FALSE;
recDevice *device = joystick->hwdata;
Uint32 result = 0;

if (!device) {
return 0;
}

if (device->ffservice) {
result |= SDL_JOYCAP_RUMBLE;
}

return result;
}

static int
Expand Down Expand Up @@ -1111,7 +1122,7 @@ SDL_JoystickDriver SDL_DARWIN_JoystickDriver =
DARWIN_JoystickOpen,
DARWIN_JoystickRumble,
DARWIN_JoystickRumbleTriggers,
DARWIN_JoystickHasLED,
DARWIN_JoystickGetCapabilities,
DARWIN_JoystickSetLED,
DARWIN_JoystickSendEffect,
DARWIN_JoystickSetSensorsEnabled,
Expand Down
8 changes: 4 additions & 4 deletions src/joystick/dummy/SDL_sysjoystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ DUMMY_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16
return SDL_Unsupported();
}

static SDL_bool
DUMMY_JoystickHasLED(SDL_Joystick *joystick)
static Uint32
DUMMY_JoystickGetCapabilities(SDL_Joystick *joystick)
{
return SDL_FALSE;
return 0;
}

static int
Expand Down Expand Up @@ -153,7 +153,7 @@ SDL_JoystickDriver SDL_DUMMY_JoystickDriver =
DUMMY_JoystickOpen,
DUMMY_JoystickRumble,
DUMMY_JoystickRumbleTriggers,
DUMMY_JoystickHasLED,
DUMMY_JoystickGetCapabilities,
DUMMY_JoystickSetLED,
DUMMY_JoystickSendEffect,
DUMMY_JoystickSetSensorsEnabled,
Expand Down
8 changes: 4 additions & 4 deletions src/joystick/emscripten/SDL_sysjoystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ EMSCRIPTEN_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
return SDL_FALSE;
}

static SDL_bool
EMSCRIPTEN_JoystickHasLED(SDL_Joystick *joystick)
static Uint32
EMSCRIPTEN_JoystickGetCapabilities(SDL_Joystick *joystick)
{
return SDL_FALSE;
return 0;
}

static int
Expand Down Expand Up @@ -451,7 +451,7 @@ SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver =
EMSCRIPTEN_JoystickOpen,
EMSCRIPTEN_JoystickRumble,
EMSCRIPTEN_JoystickRumbleTriggers,
EMSCRIPTEN_JoystickHasLED,
EMSCRIPTEN_JoystickGetCapabilities,
EMSCRIPTEN_JoystickSetLED,
EMSCRIPTEN_JoystickSendEffect,
EMSCRIPTEN_JoystickSetSensorsEnabled,
Expand Down
6 changes: 3 additions & 3 deletions src/joystick/haiku/SDL_haikujoystick.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ extern "C"
return SDL_FALSE;
}

static SDL_bool HAIKU_JoystickHasLED(SDL_Joystick *joystick)
static Uint32 HAIKU_JoystickGetCapabilities(SDL_Joystick *joystick)
{
return SDL_FALSE;
return 0;
}

static int HAIKU_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
Expand Down Expand Up @@ -305,7 +305,7 @@ extern "C"
HAIKU_JoystickOpen,
HAIKU_JoystickRumble,
HAIKU_JoystickRumbleTriggers,
HAIKU_JoystickHasLED,
HAIKU_JoystickGetCapabilities,
HAIKU_JoystickSetLED,
HAIKU_JoystickSendEffect,
HAIKU_JoystickSetSensorsEnabled,
Expand Down
24 changes: 20 additions & 4 deletions src/joystick/hidapi/SDL_hidapi_gamecube.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,26 @@ HIDAPI_DriverGameCube_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joys
return SDL_Unsupported();
}

static SDL_bool
HIDAPI_DriverGameCube_HasJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
static Uint32
HIDAPI_DriverGameCube_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
return SDL_FALSE;
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
Uint32 result = 0;

if (!ctx->pc_mode) {
Uint8 i;

for (i = 0; i < MAX_CONTROLLERS; i += 1) {
if (joystick->instance_id == ctx->joysticks[i]) {
if (!ctx->wireless[i] && ctx->rumbleAllowed[i]) {
result |= SDL_JOYCAP_RUMBLE;
break;
}
}
}
}

return result;
}

static int
Expand Down Expand Up @@ -532,7 +548,7 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube =
HIDAPI_DriverGameCube_OpenJoystick,
HIDAPI_DriverGameCube_RumbleJoystick,
HIDAPI_DriverGameCube_RumbleJoystickTriggers,
HIDAPI_DriverGameCube_HasJoystickLED,
HIDAPI_DriverGameCube_GetJoystickCapabilities,
HIDAPI_DriverGameCube_SetJoystickLED,
HIDAPI_DriverGameCube_SendJoystickEffect,
HIDAPI_DriverGameCube_SetJoystickSensorsEnabled,
Expand Down
Loading