Open
Description
In macos when I connect a PS5 controller sometimes I get the error HIDAPI device disconnected while opening
I can't reproduce it consistently but it happens sometimes when I connect the controller through bluetooth while the game is running, maybe 1 every 10 times I try.
The controller works fine but I get this message when closing the game
2024-08-15 15:55:57.305 satelital[58554:8629958] Leaked SDL_Gamepad (0x16b11a5a0)
For reference this is how my code looking like to get the controller:
struct Controller_Info {
u32 id;
Controller_Type type;
};
void App::load_game_controllers() {
int joysticks_count;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&joysticks_count);
clear_game_controllers();
if (joysticks) {
for (int index = 0; joysticks[index]; index++) {
if (SDL_IsGamepad(joysticks[index])) {
SDL_Gamepad *controller = SDL_OpenGamepad(joysticks[index]);
if (!controller) {
find_app_error();
} else {
controllers.push_back(controller);
string controller_name = SDL_GetGamepadName(controller);
bool is_playstation_controller =
contains(controller_name, playstation_identifiers);
controller_info.emplace_back(Controller_Info{
.id = SDL_GetJoystickID(SDL_GetGamepadJoystick(controller)),
.type = is_playstation_controller ? Controller_Type::PLAYSTATION
: Controller_Type::XBOX,
});
}
}
}
SDL_free(joysticks);
}
input.kind = controllers.size() > 0 ? Input_Kind::CONTROLLER : Input_Kind::KEYBOARD;
};
void App::clear_game_controllers() {
for (auto &controller : controllers) {
SDL_CloseGamepad(controller);
}
controllers.resize(0);
controller_info.resize(0);
};
void App::find_app_error() {
const char *error = SDL_GetError();
if (strlen(error) > 0) {
string current_error = string(error);
bool ignore_error = contains(current_error, unknown_touch_device_error) ||
contains(current_error, device_not_connected_error) ||
contains(current_error, hidapi_device_disconnected_while_opening);
if (!ignore_error) {
#if __DEVELOPMENT__
quit("SDL error: " + current_error);
#else
app.message_box(current_error, "SDL error");
#endif
}
}
}
Activity