Last active
December 30, 2020 11:58
-
-
Save egore/4721bc21047d165c8bc47dfa7b848775 to your computer and use it in GitHub Desktop.
SDL 2 based rumble tester I use for PS3 controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL2/SDL.h> | |
#include <cstdio> | |
#include <list> | |
#include <signal.h> | |
std::list<std::pair<SDL_Joystick*, SDL_Haptic*>> joysticks; | |
void shutdown_joysticks() { | |
SDL_Log("Closing joysticks"); | |
for (std::pair<SDL_Joystick*, SDL_Haptic*> joystick : joysticks) { | |
SDL_HapticClose(joystick.second); | |
SDL_JoystickClose(joystick.first); | |
} | |
SDL_Quit(); | |
} | |
void ctrlc_handler(int s) { | |
shutdown_joysticks(); | |
exit(1); | |
} | |
int init_joysticks() { | |
int count = SDL_NumJoysticks(); | |
if (count == 0) { | |
SDL_Log("Failed to find any joystick"); | |
return 1; | |
} | |
SDL_Log("Found %d joysticks", count); | |
for (int i = 0; i < count; i++) { | |
SDL_Joystick* joystick = SDL_JoystickOpen(i); | |
if (joystick == NULL) { | |
SDL_Log("Failed to open joystick %d: %s", i, SDL_GetError()); | |
continue; | |
} | |
SDL_Haptic* haptic = SDL_HapticOpen(i); | |
if (haptic == NULL) { | |
SDL_Log("Failed to open haptic %d: %s", i, SDL_GetError()); | |
SDL_JoystickClose(joystick); | |
continue; | |
} | |
if (SDL_HapticRumbleInit(haptic) != 0) { | |
SDL_Log("Failed to init rumble %d: %s", i, SDL_GetError()); | |
SDL_HapticClose(haptic); | |
SDL_JoystickClose(joystick); | |
continue; | |
} | |
joysticks.push_back(std::make_pair(joystick, haptic)); | |
SDL_Log("Opened joystick and haptic %d", i); | |
} | |
return 0; | |
} | |
void rumble_if_button(int button, const std::pair<SDL_Joystick*, SDL_Haptic*>& joystick, float power) { | |
Uint8 state = SDL_JoystickGetButton(joystick.first, button); | |
if (state == (Uint8) 1) { | |
if (SDL_HapticRumblePlay(joystick.second, power, 250) != 0) { | |
SDL_Log("Failed to rumble %s", SDL_GetError()); | |
} else { | |
SDL_Log("Rumbled %.2f", power); | |
} | |
} | |
} | |
int main(int argc, const char* argv[]) { | |
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) != 0) { | |
SDL_Log("Failed to initialize: %s", SDL_GetError()); | |
return 1; | |
} | |
struct sigaction handler; | |
handler.sa_handler = ctrlc_handler; | |
sigemptyset(&handler.sa_mask); | |
handler.sa_flags = 0; | |
sigaction(SIGINT, &handler, NULL); | |
if (init_joysticks() != 0) { | |
return 1; | |
} | |
bool run = joysticks.size() > 0; | |
while (run) { | |
SDL_JoystickUpdate(); | |
for (std::pair<SDL_Joystick*, SDL_Haptic*> joystick : joysticks) { | |
Uint8 state = SDL_JoystickGetButton(joystick.first, 0); | |
if (state == (Uint8) 1) { | |
SDL_Log("Quitting"); | |
run = false; | |
} | |
rumble_if_button(1, joystick, 1.0F); | |
rumble_if_button(2, joystick, 0.25F); | |
rumble_if_button(3, joystick, 0.5F); | |
SDL_Delay(50); | |
} | |
} | |
shutdown_joysticks(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use the following for meson:
project('rumble', 'cpp')
sdldep = dependency('sdl2')
executable('rumble', 'rumble.cpp', dependencies : sdldep)