From e04cb846562168a4fd403fc1f2eaf488121060a9 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 31 Mar 2019 18:02:17 +0200 Subject: [PATCH] Ref T582, improved joystick error handling * display details * hr to string * reduce number of messages --- src/blackinput/win/joystickwindows.cpp | 19 +++++++++++++++++-- src/blackinput/win/joystickwindows.h | 4 ++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/blackinput/win/joystickwindows.cpp b/src/blackinput/win/joystickwindows.cpp index 29a5b585e..c4e87c4d2 100644 --- a/src/blackinput/win/joystickwindows.cpp +++ b/src/blackinput/win/joystickwindows.cpp @@ -8,6 +8,7 @@ #include "joystickwindows.h" #include "blackmisc/logmessage.h" +#include "comdef.h" // 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 @@ -108,7 +109,9 @@ namespace BlackInput m_directInputDevice->Acquire(); 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; } } @@ -121,12 +124,16 @@ namespace BlackInput m_directInputDevice->Acquire(); 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; } } } + m_lastHRError = hr; + for (const CJoystickDeviceInput &input : as_const(m_joystickDeviceInputs)) { const qint32 buttonIndex = input.m_offset - DIJOFS_BUTTON0; @@ -139,6 +146,14 @@ namespace BlackInput 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) { CJoystickDevice *joystickDevice = static_cast(pvRef); diff --git a/src/blackinput/win/joystickwindows.h b/src/blackinput/win/joystickwindows.h index 819cf5487..f577a2929 100644 --- a/src/blackinput/win/joystickwindows.h +++ b/src/blackinput/win/joystickwindows.h @@ -79,9 +79,13 @@ namespace BlackInput //! Poll the device buttons HRESULT pollDeviceState(); + //! Code as tring + static QString hrString(HRESULT hr); + //! Joystick button enumeration callback static BOOL CALLBACK enumObjectsCallback(const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef); + HRESULT m_lastHRError = S_OK; GUID m_guidDevice; //!< Device GUID GUID m_guidProduct; //!< Product GUID QString m_deviceName; //!< Device name