From 2bc577bc3cf7e8e7877eab1220057cd11286f547 Mon Sep 17 00:00:00 2001 From: Zerocker Date: Sat, 30 May 2020 20:37:37 +0900 Subject: [PATCH] Added haptic implementation --- src/pc/controller/controller_entry_point.c | 3 +- src/pc/controller/controller_sdl.c | 33 ++++++++++++++++++---- src/pc/controller/controller_sdl.h | 2 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/pc/controller/controller_entry_point.c b/src/pc/controller/controller_entry_point.c index beb0a48a..b8b54ad3 100644 --- a/src/pc/controller/controller_entry_point.c +++ b/src/pc/controller/controller_entry_point.c @@ -30,7 +30,8 @@ s32 osContInit(OSMesgQueue *mq, u8 *controllerBits, OSContStatus *status) { } s32 osMotorStart(void *pfs) { - return controller_rumble_play(0.5, 1000); + // Since rumble stops by osMotorStop, its duration is not nessecary. + return controller_rumble_play(configRumbleStrength / 100.0, 50); } s32 osMotorStop(void *pfs) { diff --git a/src/pc/controller/controller_sdl.c b/src/pc/controller/controller_sdl.c index 1867169c..15384a37 100644 --- a/src/pc/controller/controller_sdl.c +++ b/src/pc/controller/controller_sdl.c @@ -34,7 +34,7 @@ extern u8 newcam_mouse; static bool init_ok; static SDL_GameController *sdl_cntrl; - +static SDL_Haptic *sdl_haptic; static u32 num_joy_binds = 0; static u32 num_mouse_binds = 0; @@ -81,7 +81,7 @@ static void controller_sdl_bind(void) { } static void controller_sdl_init(void) { - if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS) != 0) { + if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS | SDL_INIT_HAPTIC) != 0) { fprintf(stderr, "SDL init error: %s\n", SDL_GetError()); return; } @@ -130,6 +130,7 @@ static void controller_sdl_read(OSContPad *pad) { for (int i = 0; i < SDL_NumJoysticks(); i++) { if (SDL_IsGameController(i)) { sdl_cntrl = SDL_GameControllerOpen(i); + sdl_haptic = SDL_HapticOpen(i); if (sdl_cntrl != NULL) { break; } @@ -213,21 +214,43 @@ static void controller_sdl_shutdown(void) { SDL_GameControllerClose(sdl_cntrl); sdl_cntrl = NULL; } + if (sdl_haptic) { + SDL_HapticClose(sdl_haptic); + sdl_haptic = NULL; + } SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER); } init_ok = false; } -s32 controller_rumble_init(void) { +u32 controller_rumble_init(void) { + if (SDL_HapticRumbleSupported(sdl_haptic) != SDL_TRUE) { + printf("Controller does not support haptics! %s\n", SDL_GetError()); + return 1; + } + if (SDL_HapticRumbleInit(sdl_haptic) != 0) { + printf("Unable to initialize rumble! %s\n", SDL_GetError()); + return 1; + } return 0; } s32 controller_rumble_play(f32 strength, u32 length) { - return 0; + if (SDL_HapticRumblePlay(sdl_haptic, strength, length) != 0) { + printf("Unable to start rumble! %s\n", SDL_GetError()); + return -1; + } else { + return 0; + } } s32 controller_rumble_stop(void) { - return 0; + if (SDL_HapticRumbleStop(sdl_haptic) != 0) { + printf("Unable to stop rumble! %s\n", SDL_GetError()); + return -1; + } else { + return 0; + } } struct ControllerAPI controller_sdl = { diff --git a/src/pc/controller/controller_sdl.h b/src/pc/controller/controller_sdl.h index 8a3a587c..c1a43cec 100644 --- a/src/pc/controller/controller_sdl.h +++ b/src/pc/controller/controller_sdl.h @@ -7,7 +7,7 @@ extern struct ControllerAPI controller_sdl; -s32 controller_rumble_init(void); +u32 controller_rumble_init(void); s32 controller_rumble_play(f32 strength, u32 length); s32 controller_rumble_stop(void);