bool Joystick::deviceAdded(const SDL_Event *event) { int which = event->type == SDL_CONTROLLERDEVICEADDED ? event->cdevice.which : event->jdevice.which; if (!m_mapping->matchJoystick(SDL_JoystickGetDeviceGUID(which))) return false; if (event->type == SDL_CONTROLLERDEVICEADDED) { attachGameController(which); } else { if (SDL_IsGameController(which)) { // joystick is a supported game controller // let's wait for the CONTROLLERADDED event to add it return false; } attachJoystick(which); } return true; }
Joystick::Joystick(InputDeviceMapping *mapping) : InputDevice(mapping) { device_attached = false; joystick = nullptr; controller = nullptr; m_mapping = reinterpret_cast<Mapping *>(InputDevice::m_mapping); Q_ASSERT(m_mapping != nullptr); callback = std::bind(&Joystick::handleSDLEvent, this, std::placeholders::_1); sdl_events.registerCallback(&callback); for (int i = 0; i < SDL_NumJoysticks(); i++) { if (!m_mapping->matchJoystick(SDL_JoystickGetDeviceGUID(i))) continue; if (SDL_IsGameController(i)) { attachGameController(i); break; } else { attachJoystick(i); break; } } }
void Joystick::reattachJoystick() { detachJoystick(); attachJoystick(); }