Skip to content

Commit

Permalink
[Issue No.65] AddJoystick & RemoveJoystick crash or Joysticklist disp…
Browse files Browse the repository at this point in the history
…lay error issue fix.

Signed-off-by: Melvin Li <[email protected]>
  • Loading branch information
Zalafina committed Dec 7, 2024
1 parent be427a2 commit a31f16f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
18 changes: 13 additions & 5 deletions QKeyMapper/QJoysticks/src/QJoysticks/SDL_Joysticks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ void SDL_Joysticks::update()
configureJoystick(&event);
break;
case SDL_JOYDEVICEREMOVED: {
const QJoystickDevice joystick_removed = *m_joysticks[event.jdevice.which];
emit joystickRemoved(joystick_removed);
if (m_joysticks.contains(event.jdevice.which)
&& m_joysticks[event.jdevice.which] != nullptr) {
const QJoystickDevice joystick_removed = *m_joysticks[event.jdevice.which];
emit joystickRemoved(joystick_removed);
}

SDL_Joystick *js = SDL_JoystickFromInstanceID(event.jdevice.which);
if (js)
Expand All @@ -162,11 +165,16 @@ void SDL_Joysticks::update()
SDL_GameControllerClose(gc);
}

delete m_joysticks[event.jdevice.which];
m_joysticks.remove(event.jdevice.which);
if (m_joysticks.contains(event.jdevice.which))
{
if (m_joysticks[event.jdevice.which] != nullptr) {
delete m_joysticks[event.jdevice.which];
}
m_joysticks.remove(event.jdevice.which);
}

emit countChanged();
}
}
break;
case SDL_JOYAXISMOTION:
if (!SDL_IsGameController(event.cdevice.which))
Expand Down
4 changes: 2 additions & 2 deletions QKeyMapper/QKeyMapper.rc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ IDI_ICON1 ICON "image/QKeyMapper.ico"

// Version
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,3,6,0
PRODUCTVERSION 1,3,6,0
FILEVERSION 1,3,7,0
PRODUCTVERSION 1,3,7,0
FILEFLAGSMASK 0x17L
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand Down
9 changes: 6 additions & 3 deletions QKeyMapper/qkeymapper_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5262,7 +5262,8 @@ void QKeyMapper_Worker::onJoystickAdded(QJoystickDevice *joystick_added)
#ifdef DEBUG_LOGOUT_ON
QString vendorIdStr = QString("0x%1").arg(QString::number(joystick_added->vendorid, 16).toUpper(), 4, '0');
QString productIdStr = QString("0x%1").arg(QString::number(joystick_added->productid, 16).toUpper(), 4, '0');
qDebug().nospace() << "[onJoystickAdded] Added a New Gamepad -> " << "Name = " << joystick_added->name << ", PlayerIndex = " << joystick_added->playerindex << ", ID = " << joystick_added->id << ", VendorID = " << vendorIdStr << ", ProductID = " << productIdStr << ", ButtonNumbers = " << joystick_added->numbuttons << ", Serial = " << joystick_added->serial;
QString debugmessage = QString("[onJoystickAdded] Added a New Gamepad -> Name=\"%1\", PlayerIndex=%2, ID=%3, VendorID=%4, ProductID=%5, ButtonNumbers=%6, Serial=%7").arg(joystick_added->name).arg(joystick_added->playerindex).arg(joystick_added->id).arg(vendorIdStr, productIdStr).arg(joystick_added->numbuttons).arg(joystick_added->serial);
qDebug().nospace().noquote() << debugmessage;
#endif

if (joystick_added == Q_NULLPTR) {
Expand All @@ -5286,7 +5287,8 @@ void QKeyMapper_Worker::onJoystickAdded(QJoystickDevice *joystick_added)
if (virtualgamepad) {
joystick_added->blacklisted = true;
#ifdef DEBUG_LOGOUT_ON
qDebug().noquote().nospace() << "[onJoystickAdded] VirtualGamdpad[" << joystick_added->name << "] PlayerIndex = " << joystick_added->playerindex << ", ID = " << joystick_added->id << ", VendorID = " << vendorIdStr << ", ProductID = " << productIdStr << ", is Blacklisted!";
QString debugmessage = QString("[onJoystickAdded] VirtualGamdpad[%1] PlayerIndex=%2, ID=%3, VendorID=%4, ProductID=%5, is Blacklisted!").arg(joystick_added->name).arg(joystick_added->playerindex).arg(joystick_added->id).arg(vendorIdStr, productIdStr);
qDebug().nospace().noquote() << debugmessage;
#endif
}

Expand All @@ -5299,7 +5301,8 @@ void QKeyMapper_Worker::onJoystickRemoved(const QJoystickDevice joystick_removed
#ifdef DEBUG_LOGOUT_ON
QString vendorIdStr = QString("0x%1").arg(QString::number(joystick_removed.vendorid, 16).toUpper(), 4, '0');
QString productIdStr = QString("0x%1").arg(QString::number(joystick_removed.productid, 16).toUpper(), 4, '0');
qDebug().nospace() << "[onJoystickRemoved] Removed a Gamepad -> " << "Name = " << joystick_removed.name << ", PlayerIndex = " << joystick_removed.playerindex << ", ID = " << joystick_removed.id << ", VendorID = " << vendorIdStr << ", ProductID = " << productIdStr << ", ButtonNumbers = " << joystick_removed.numbuttons << ", Serial = " << joystick_removed.serial;
QString debugmessage = QString("[onJoystickRemoved] Removed a Gamepad -> Name=\"%1\", PlayerIndex=%2, ID=%3, VendorID=%4, ProductID=%5, ButtonNumbers=%6, Serial=%7").arg(joystick_removed.name).arg(joystick_removed.playerindex).arg(joystick_removed.id).arg(vendorIdStr, productIdStr).arg(joystick_removed.numbuttons).arg(joystick_removed.serial);
qDebug().nospace().noquote() << debugmessage;
#endif

emit QKeyMapper::getInstance()->updateGamepadSelectComboBox_Signal();
Expand Down

0 comments on commit a31f16f

Please sign in to comment.