From 602373c97a0e3b0bfae0be7785ad65315c684f9f Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 18 Feb 2018 00:54:14 +0100 Subject: [PATCH] Ref T254, avoid crashes when there is no input device * init with nullptr * formatting --- src/blackinput/win/joystickwindows.cpp | 9 +++++++-- src/blackinput/win/joystickwindows.h | 11 +++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/blackinput/win/joystickwindows.cpp b/src/blackinput/win/joystickwindows.cpp index 6cd1141c4..bb6064f3c 100644 --- a/src/blackinput/win/joystickwindows.cpp +++ b/src/blackinput/win/joystickwindows.cpp @@ -20,7 +20,6 @@ using namespace BlackMisc::Input; namespace BlackInput { - const TCHAR *CJoystickWindows::m_helperWindowClassName = TEXT("HelperWindow"); const TCHAR *CJoystickWindows::m_helperWindowName = TEXT("JoystickCatcherWindow"); ATOM CJoystickWindows::m_helperWindowClass = 0; @@ -86,6 +85,12 @@ namespace BlackInput HRESULT CJoystickWindows::enumJoystickDevices() { + if (!m_directInput) + { + CLogMessage(this).warning("No direct input"); + return E_FAIL; + } + HRESULT hr; if (FAILED(hr = m_directInput->EnumDevices(DI8DEVTYPE_JOYSTICK, enumJoysticksCallback, this, DIEDFL_ATTACHEDONLY))) { @@ -145,7 +150,7 @@ namespace BlackInput HRESULT hr = S_OK; // Check if device list is empty first - if (m_availableJoystickDevices.isEmpty()) return E_FAIL; + if (m_availableJoystickDevices.isEmpty()) { return E_FAIL; } // FIXME: Take the first device with number of buttons > 0 // For the future, the user should be able to choose which device diff --git a/src/blackinput/win/joystickwindows.h b/src/blackinput/win/joystickwindows.h index 156c7dfff..c5bd70c71 100644 --- a/src/blackinput/win/joystickwindows.h +++ b/src/blackinput/win/joystickwindows.h @@ -52,7 +52,6 @@ namespace BlackInput Q_OBJECT public: - //! Copy Constructor CJoystickWindows(CJoystickWindows const &) = delete; @@ -63,12 +62,10 @@ namespace BlackInput virtual ~CJoystickWindows(); protected: - //! Timer based updates virtual void timerEvent(QTimerEvent *event) override; private: - friend class IJoystick; //! Constructor @@ -109,8 +106,8 @@ namespace BlackInput // todo RW: Try to use QScopedPointer. So far I could not find out how to use it with // IDirectInput8::CreateDevice - IDirectInput8 *m_directInput; //!< DirectInput object - IDirectInputDevice8 *m_directInputDevice; //!< DirectInput device + IDirectInput8 *m_directInput = nullptr; //!< DirectInput object + IDirectInputDevice8 *m_directInputDevice = nullptr; //!< DirectInput device QVector m_availableJoystickDevices; //!< List of found and available joystick devices QVector m_joystickDeviceInputs; //!< List of available device buttons @@ -120,9 +117,7 @@ namespace BlackInput static const WCHAR *m_helperWindowName; //!< Helper window name static ATOM m_helperWindowClass; static HWND m_helperWindow; //!< Helper window handle - }; - } // namespace BlackInput -#endif // BLACKINPUT_JOYSTICK_WINDOWS_H +#endif // guard