Skip to content
Open
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
4 changes: 2 additions & 2 deletions joy/src/joy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
62 changes: 34 additions & 28 deletions joy/src/joy_enumerate_devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down