From 9f3cf94dca73fa7fac479990435790a013e6762a Mon Sep 17 00:00:00 2001 From: Elia Cereda Date: Thu, 24 Apr 2025 20:27:02 +0200 Subject: [PATCH] Initialize SDL2_INIT_VIDEO to correctly detect joysticks on macOS --- joy/src/joy.cpp | 4 +- joy/src/joy_enumerate_devices.cpp | 62 +++++++++++++++++-------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/joy/src/joy.cpp b/joy/src/joy.cpp index c3e8c9df..28adb272 100644 --- a/joy/src/joy.cpp +++ b/joy/src/joy.cpp @@ -105,13 +105,13 @@ Joy::Joy(const rclcpp::NodeOptions & options) future_ = exit_signal_.get_future(); - if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) { throw std::runtime_error("SDL could not be initialized: " + std::string(SDL_GetError())); } // In theory we could do this with just a timer, which would simplify the code // a bit. But then we couldn't react to "immediate" events, so we stick with // the thread. - event_thread_ = std::thread(&Joy::eventThread, this); + this->eventThread(); } Joy::~Joy() diff --git a/joy/src/joy_enumerate_devices.cpp b/joy/src/joy_enumerate_devices.cpp index a3c07c93..aa0af0cd 100644 --- a/joy/src/joy_enumerate_devices.cpp +++ b/joy/src/joy_enumerate_devices.cpp @@ -38,43 +38,49 @@ int main() { // SDL_INIT_GAMECONTROLLER implies SDL_INIT_JOYSTICK - if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC ) < 0) { fprintf(stderr, "SDL could not be initialized: %s\n", SDL_GetError()); return 1; } - fprintf( - stdout, - "ID : GUID : GamePad : Mapped : Joystick Device Name\n"); - fprintf( - stdout, - "-------------------------------------------------------------------------------\n"); - for (int i = 0; i < SDL_NumJoysticks(); ++i) { - SDL_JoystickGUID joystick_guid; - const char * joystick_name = "Unknown"; - const char * has_mapping_string = "false"; - const char * is_gamepad = "false"; + while (true) { + SDL_PumpEvents(); - if (SDL_IsGameController(i)) { - SDL_GameController * controller = SDL_GameControllerOpen(i); - joystick_guid = SDL_JoystickGetGUID(SDL_GameControllerGetJoystick(controller)); - joystick_name = SDL_GameControllerName(controller); - if (nullptr != SDL_GameControllerMapping(controller)) { - has_mapping_string = "true"; + fprintf( + stdout, + "ID : GUID : GamePad : Mapped : Joystick Device Name\n"); + fprintf( + stdout, + "-------------------------------------------------------------------------------\n"); + for (int i = 0; i < SDL_NumJoysticks(); ++i) { + SDL_JoystickGUID joystick_guid; + const char * joystick_name = "Unknown"; + const char * has_mapping_string = "false"; + const char * is_gamepad = "false"; + + if (SDL_IsGameController(i)) { + SDL_GameController * controller = SDL_GameControllerOpen(i); + joystick_guid = SDL_JoystickGetGUID(SDL_GameControllerGetJoystick(controller)); + joystick_name = SDL_GameControllerName(controller); + if (nullptr != SDL_GameControllerMapping(controller)) { + has_mapping_string = "true"; + } + is_gamepad = "true"; + + } else { + joystick_name = SDL_JoystickNameForIndex(i); + joystick_guid = SDL_JoystickGetDeviceGUID(i); } - is_gamepad = "true"; - } else { - joystick_name = SDL_JoystickNameForIndex(i); - joystick_guid = SDL_JoystickGetDeviceGUID(i); - } + char guid_string[33]; + SDL_JoystickGetGUIDString(joystick_guid, guid_string, sizeof(guid_string)); - char guid_string[33]; - SDL_JoystickGetGUIDString(joystick_guid, guid_string, sizeof(guid_string)); + fprintf( + stdout, "%2d : %32s : %7s : %6s : %s\n", i, guid_string, is_gamepad, has_mapping_string, + joystick_name); + } - fprintf( - stdout, "%2d : %32s : %7s : %6s : %s\n", i, guid_string, is_gamepad, has_mapping_string, - joystick_name); + SDL_Delay(1000); } SDL_Quit();