Ref T582, improved joystick error handling

* display details
* hr to string
* reduce number of messages
This commit is contained in:
Klaus Basan
2019-03-31 18:02:17 +02:00
committed by Mat Sutcliffe
parent 4bc5b7720e
commit e04cb84656
2 changed files with 21 additions and 2 deletions

View File

@@ -8,6 +8,7 @@
#include "joystickwindows.h" #include "joystickwindows.h"
#include "blackmisc/logmessage.h" #include "blackmisc/logmessage.h"
#include "comdef.h"
// Qt5 defines UNICODE, hence we can expect an wchar_t strings. // Qt5 defines UNICODE, hence we can expect an wchar_t strings.
// If it fails to compile, because of char/wchar_t errors, you are most likely // If it fails to compile, because of char/wchar_t errors, you are most likely
@@ -108,7 +109,9 @@ namespace BlackInput
m_directInputDevice->Acquire(); m_directInputDevice->Acquire();
if (FAILED(hr = m_directInputDevice->Poll())) if (FAILED(hr = m_directInputDevice->Poll()))
{ {
CLogMessage(this).warning(u"DirectInput error code: ") << hr; if (m_lastHRError == hr) { return hr; } // avoid flooding with messages
m_lastHRError = hr;
CLogMessage(this).warning(u"DirectInput error code (POLL input lost/notacquired): %1 %2") << hr << hrString(hr);
return hr; return hr;
} }
} }
@@ -121,12 +124,16 @@ namespace BlackInput
m_directInputDevice->Acquire(); m_directInputDevice->Acquire();
if (FAILED(hr = m_directInputDevice->GetDeviceState(sizeof(DIJOYSTATE2), &state))) if (FAILED(hr = m_directInputDevice->GetDeviceState(sizeof(DIJOYSTATE2), &state)))
{ {
CLogMessage(this).warning(u"DirectInput error code: ") << hr; if (m_lastHRError == hr) { return hr; } // avoid flooding with messages
m_lastHRError = hr;
CLogMessage(this).warning(u"DirectInput error code (state input lost/notacquired): %1 %2") << hr << hrString(hr);
return hr; return hr;
} }
} }
} }
m_lastHRError = hr;
for (const CJoystickDeviceInput &input : as_const(m_joystickDeviceInputs)) for (const CJoystickDeviceInput &input : as_const(m_joystickDeviceInputs))
{ {
const qint32 buttonIndex = input.m_offset - DIJOFS_BUTTON0; const qint32 buttonIndex = input.m_offset - DIJOFS_BUTTON0;
@@ -139,6 +146,14 @@ namespace BlackInput
return hr; return hr;
} }
QString CJoystickDevice::hrString(HRESULT hr)
{
// https://stackoverflow.com/questions/7008047/is-there-a-way-to-get-the-string-representation-of-hresult-value-using-win-api
_com_error err(hr);
LPCTSTR errMsg = err.ErrorMessage();
return QString::fromWCharArray(errMsg);
}
BOOL CALLBACK CJoystickDevice::enumObjectsCallback(const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef) BOOL CALLBACK CJoystickDevice::enumObjectsCallback(const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef)
{ {
CJoystickDevice *joystickDevice = static_cast<CJoystickDevice *>(pvRef); CJoystickDevice *joystickDevice = static_cast<CJoystickDevice *>(pvRef);

View File

@@ -79,9 +79,13 @@ namespace BlackInput
//! Poll the device buttons //! Poll the device buttons
HRESULT pollDeviceState(); HRESULT pollDeviceState();
//! Code as tring
static QString hrString(HRESULT hr);
//! Joystick button enumeration callback //! Joystick button enumeration callback
static BOOL CALLBACK enumObjectsCallback(const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef); static BOOL CALLBACK enumObjectsCallback(const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef);
HRESULT m_lastHRError = S_OK;
GUID m_guidDevice; //!< Device GUID GUID m_guidDevice; //!< Device GUID
GUID m_guidProduct; //!< Product GUID GUID m_guidProduct; //!< Product GUID
QString m_deviceName; //!< Device name QString m_deviceName; //!< Device name