From e1adba4ef7b3a809fa3acd46b9e1e60376644fd2 Mon Sep 17 00:00:00 2001 From: xxzl0130 Date: Sun, 12 Jul 2020 11:08:47 +0800 Subject: [PATCH] fix memory leak and maintain joysticks list --- src/QJoysticks/SDL_Joysticks.cpp | 22 +++++++++++----------- src/QJoysticks/SDL_Joysticks.h | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/QJoysticks/SDL_Joysticks.cpp b/src/QJoysticks/SDL_Joysticks.cpp index 956ade0..3097e5b 100644 --- a/src/QJoysticks/SDL_Joysticks.cpp +++ b/src/QJoysticks/SDL_Joysticks.cpp @@ -89,14 +89,7 @@ SDL_Joysticks::~SDL_Joysticks() */ QList SDL_Joysticks::joysticks() { - QList list; - -#ifdef SDL_SUPPORTED - for (int i = 0; i < SDL_NumJoysticks(); ++i) - list.append (getJoystick (i)); -#endif - - return list; + return m_joysticks; } /** @@ -133,6 +126,8 @@ void SDL_Joysticks::update() case SDL_JOYDEVICEREMOVED: SDL_JoystickClose (SDL_JoystickOpen (event.jdevice.which)); SDL_GameControllerClose (SDL_GameControllerOpen (event.cdevice.which)); + delete m_joysticks[event.cdevice.which]; + m_joysticks.removeAt(event.cdevice.which); emit countChanged(); break; case SDL_JOYAXISMOTION: @@ -187,6 +182,8 @@ void SDL_Joysticks::configureJoystick (const SDL_Event* event) SDL_GameControllerOpen (event->cdevice.which); + m_joysticks.append(getJoystick(event->cdevice.which)); + ++m_tracker; emit countChanged(); #else @@ -270,7 +267,6 @@ QJoystickPOVEvent SDL_Joysticks::getPOVEvent (const SDL_Event* sdl_event) #ifdef SDL_SUPPORTED event.pov = sdl_event->jhat.hat; - event.joystick = getJoystick (sdl_event->jdevice.which); switch (sdl_event->jhat.value) { case SDL_HAT_RIGHTUP: @@ -301,6 +297,8 @@ QJoystickPOVEvent SDL_Joysticks::getPOVEvent (const SDL_Event* sdl_event) event.angle = -1; break; } + event.joystick = m_joysticks[sdl_event->cdevice.which]; + event.joystick->axes[event.pov] = event.angle; #else Q_UNUSED (sdl_event); #endif @@ -319,7 +317,8 @@ QJoystickAxisEvent SDL_Joysticks::getAxisEvent (const SDL_Event* sdl_event) #ifdef SDL_SUPPORTED event.axis = sdl_event->caxis.axis; event.value = static_cast (sdl_event->caxis.value) / 32767; - event.joystick = getJoystick (sdl_event->cdevice.which); + event.joystick = m_joysticks[sdl_event->cdevice.which]; + event.joystick->axes[event.axis] = event.value; #else Q_UNUSED (sdl_event); #endif @@ -339,7 +338,8 @@ QJoystickButtonEvent SDL_Joysticks::getButtonEvent (const SDL_Event* #ifdef SDL_SUPPORTED event.button = sdl_event->jbutton.button; event.pressed = sdl_event->jbutton.state == SDL_PRESSED; - event.joystick = getJoystick (sdl_event->jdevice.which); + event.joystick = m_joysticks[sdl_event->cdevice.which]; + event.joystick->axes[event.button] = event.pressed; #else Q_UNUSED (sdl_event); #endif diff --git a/src/QJoysticks/SDL_Joysticks.h b/src/QJoysticks/SDL_Joysticks.h index d4cf73b..363d4e7 100644 --- a/src/QJoysticks/SDL_Joysticks.h +++ b/src/QJoysticks/SDL_Joysticks.h @@ -71,7 +71,7 @@ private slots: QJoystickButtonEvent getButtonEvent (const SDL_Event* sdl_event); int m_tracker; - QList m_joysticks; + QList m_joysticks; }; #endif