SDL: Use SDL_JoystickRumble where available

This commit is contained in:
Vicki Pfau 2021-11-16 18:53:11 -08:00
parent e8f3db4b3e
commit 4b4eef373f
2 changed files with 17 additions and 2 deletions

View File

@ -39,6 +39,7 @@ Misc:
- Qt: Rearrange menus some - Qt: Rearrange menus some
- Qt: Clean up cheats dialog - Qt: Clean up cheats dialog
- Qt: Only set default controller bindings if loading fails (fixes mgba.io/i/799) - Qt: Only set default controller bindings if loading fails (fixes mgba.io/i/799)
- SDL: Use SDL_JoystickRumble where available
- Wii: Add adjustable gyroscope settings (closes mgba.io/i/2245) - Wii: Add adjustable gyroscope settings (closes mgba.io/i/2245)
0.9.2: (2021-07-10) 0.9.2: (2021-07-10)

View File

@ -47,7 +47,9 @@ static struct SDL_JoystickCombo* _mSDLOpenJoystick(struct mSDLEvents* events, in
joystick->joystick = sdlJoystick; joystick->joystick = sdlJoystick;
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
joystick->id = SDL_JoystickInstanceID(joystick->joystick); joystick->id = SDL_JoystickInstanceID(joystick->joystick);
#if !SDL_VERSION_ATLEAST(2, 0, 9)
joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick); joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
#endif
joystick->controller = SDL_GameControllerOpen(i); joystick->controller = SDL_GameControllerOpen(i);
#if SDL_VERSION_ATLEAST(2, 0, 14) #if SDL_VERSION_ATLEAST(2, 0, 14)
if (joystick->controller) { if (joystick->controller) {
@ -75,9 +77,11 @@ bool mSDLInitEvents(struct mSDLEvents* context) {
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
#if !SDL_VERSION_ATLEAST(2, 0, 9)
if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) { if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
mLOG(SDL_EVENTS, ERROR, "SDL haptic initialization failed: %s", SDL_GetError()); mLOG(SDL_EVENTS, ERROR, "SDL haptic initialization failed: %s", SDL_GetError());
} }
#endif
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) < 0) { if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) < 0) {
mLOG(SDL_EVENTS, ERROR, "SDL game controller initialization failed: %s", SDL_GetError()); mLOG(SDL_EVENTS, ERROR, "SDL game controller initialization failed: %s", SDL_GetError());
} }
@ -121,7 +125,9 @@ void mSDLDeinitEvents(struct mSDLEvents* context) {
struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i); struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i);
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_GameControllerClose(joystick->controller); SDL_GameControllerClose(joystick->controller);
#if !SDL_VERSION_ATLEAST(2, 0, 9)
SDL_HapticClose(joystick->haptic); SDL_HapticClose(joystick->haptic);
#endif
#endif #endif
SDL_JoystickClose(joystick->joystick); SDL_JoystickClose(joystick->joystick);
} }
@ -243,7 +249,7 @@ bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
if (index != SIZE_MAX) { if (index != SIZE_MAX) {
player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index); player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0) && !SDL_VERSION_ATLEAST(2, 0, 9)
if (player->joystick->haptic) { if (player->joystick->haptic) {
SDL_HapticRumbleInit(player->joystick->haptic); SDL_HapticRumbleInit(player->joystick->haptic);
} }
@ -649,7 +655,11 @@ void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext,
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
static void _mSDLSetRumble(struct mRumble* rumble, int enable) { static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble; struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
if (!sdlRumble->p->joystick || !sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) { if (!sdlRumble->p->joystick
#if !SDL_VERSION_ATLEAST(2, 0, 9)
|| !sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)
#endif
) {
return; return;
} }
int8_t originalLevel = sdlRumble->level; int8_t originalLevel = sdlRumble->level;
@ -668,12 +678,16 @@ static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
return; return;
} }
sdlRumble->activeLevel = activeLevel; sdlRumble->activeLevel = activeLevel;
#if SDL_VERSION_ATLEAST(2, 0, 9)
SDL_JoystickRumble(sdlRumble->p->joystick->joystick, activeLevel * 0xFFFF, activeLevel * 0xFFFF, 500);
#else
if (sdlRumble->activeLevel > 0.5 / RUMBLE_STEPS) { if (sdlRumble->activeLevel > 0.5 / RUMBLE_STEPS) {
SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic); SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, activeLevel, 500); SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, activeLevel, 500);
} else { } else {
SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic); SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
} }
#endif
} }
#endif #endif