diff --git a/src/blackinput/win/joystickwindows.cpp b/src/blackinput/win/joystickwindows.cpp index bb6064f3c..0f5d7a39f 100644 --- a/src/blackinput/win/joystickwindows.cpp +++ b/src/blackinput/win/joystickwindows.cpp @@ -41,6 +41,7 @@ namespace BlackInput // release device before input if (m_directInputDevice) { + m_directInputDevice->Unacquire(); m_directInputDevice->Release(); m_directInputDevice = nullptr; } @@ -75,7 +76,8 @@ namespace BlackInput return E_FAIL; } - if (FAILED(hr = m_directInput->Initialize(instance, DIRECTINPUT_VERSION))) + hr = m_directInput->Initialize(instance, DIRECTINPUT_VERSION); + if (FAILED(hr)) { CLogMessage(this).error("Direct input init failed"); return hr; @@ -92,7 +94,7 @@ namespace BlackInput } HRESULT hr; - if (FAILED(hr = m_directInput->EnumDevices(DI8DEVTYPE_JOYSTICK, enumJoysticksCallback, this, DIEDFL_ATTACHEDONLY))) + if (FAILED(hr = m_directInput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumJoysticksCallback, this, DIEDFL_ATTACHEDONLY))) { CLogMessage(this).error("Error reading joystick devices"); return hr; @@ -158,6 +160,7 @@ namespace BlackInput const CJoystickDeviceData &deviceData = m_availableJoystickDevices.constFirst(); // Create device + Q_ASSERT_X(m_directInput, Q_FUNC_INFO, "We should not get here without direct input"); if (FAILED(hr = m_directInput->CreateDevice(deviceData.guidDevice, &m_directInputDevice, nullptr))) { // FIXME: print error message @@ -167,8 +170,7 @@ namespace BlackInput createHelperWindow(); // Set cooperative level - if (FAILED(hr = m_directInputDevice->SetCooperativeLevel(m_helperWindow, DISCL_NONEXCLUSIVE | - DISCL_BACKGROUND))) + if (FAILED(hr = m_directInputDevice->SetCooperativeLevel(m_helperWindow, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))) { // FIXME: print error message return hr; @@ -281,6 +283,12 @@ namespace BlackInput DIJOYSTATE2 state; HRESULT hr = S_OK; + if (!m_directInputDevice) + { + CLogMessage(this).warning("No input device"); + return S_FALSE; + } + if (FAILED(hr = m_directInputDevice->Poll())) { if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) @@ -355,5 +363,4 @@ namespace BlackInput lhs.m_offset == rhs.m_offset && lhs.m_name == rhs.m_name; } - -} // namespace BlackInput +} // ns diff --git a/src/blackinput/win/joystickwindows.h b/src/blackinput/win/joystickwindows.h index c5bd70c71..6983fd648 100644 --- a/src/blackinput/win/joystickwindows.h +++ b/src/blackinput/win/joystickwindows.h @@ -29,17 +29,17 @@ namespace BlackInput //! Joystick device data struct CJoystickDeviceData { - GUID guidDevice; //!< Device GUID - GUID guidProduct; //!< Product GUID - QString deviceName; //!< Device name + GUID guidDevice; //!< Device GUID + GUID guidProduct; //!< Product GUID + QString deviceName; //!< Device name QString productName; //!< Product name }; //! Joystick device input/button struct CJoystickDeviceInput { - int m_number; //!< Input number - int m_offset; //!< Input offset + int m_number; //!< Input number + int m_offset; //!< Input offset QString m_name; //!< Input name }; @@ -98,18 +98,17 @@ namespace BlackInput void addJoystickDeviceInput(const DIDEVICEOBJECTINSTANCE *dev); //! Joystick enumeration callback - static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE * - pdidInstance, VOID *pContext); + static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, VOID *pContext); //! Joystick button enumeration callback static BOOL CALLBACK enumObjectsCallback(const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef); - // todo RW: Try to use QScopedPointer. So far I could not find out how to use it with - // IDirectInput8::CreateDevice - IDirectInput8 *m_directInput = nullptr; //!< DirectInput object - IDirectInputDevice8 *m_directInputDevice = nullptr; //!< DirectInput device + // todo RW: Try to use QScopedPointer. So far I could not find out how to use it with IDirectInput8::CreateDevice + // remark KB: if created with CoCreateInstance we do not "own" the object and cannot free the memory, and must use release + 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 + QVector m_joystickDeviceInputs; //!< List of available device buttons BlackMisc::Input::CHotkeyCombination m_buttonCombination; @@ -118,6 +117,6 @@ namespace BlackInput static ATOM m_helperWindowClass; static HWND m_helperWindow; //!< Helper window handle }; -} // namespace BlackInput +} // ns #endif // guard