Skip to content

Commit

Permalink
Merge pull request #8 from xxzl0130/master
Browse files Browse the repository at this point in the history
fix memory leak and maintain joysticks list
  • Loading branch information
alex-spataru authored Jul 12, 2020
2 parents 444375b + e1adba4 commit 4eb58ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/QJoysticks/SDL_Joysticks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,7 @@ SDL_Joysticks::~SDL_Joysticks()
*/
QList<QJoystickDevice*> SDL_Joysticks::joysticks()
{
QList<QJoystickDevice*> list;

#ifdef SDL_SUPPORTED
for (int i = 0; i < SDL_NumJoysticks(); ++i)
list.append (getJoystick (i));
#endif

return list;
return m_joysticks;
}

/**
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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<qreal> (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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/QJoysticks/SDL_Joysticks.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private slots:
QJoystickButtonEvent getButtonEvent (const SDL_Event* sdl_event);

int m_tracker;
QList<QJoystickDevice> m_joysticks;
QList<QJoystickDevice*> m_joysticks;
};

#endif

0 comments on commit 4eb58ec

Please sign in to comment.