Changed joystick search class

This finds Saitek controls at KB's place
This commit is contained in:
Klaus Basan
2018-02-18 03:47:27 +01:00
parent 68d4243ca7
commit d71e98def3
2 changed files with 25 additions and 19 deletions

View File

@@ -41,6 +41,7 @@ namespace BlackInput
// release device before input // release device before input
if (m_directInputDevice) if (m_directInputDevice)
{ {
m_directInputDevice->Unacquire();
m_directInputDevice->Release(); m_directInputDevice->Release();
m_directInputDevice = nullptr; m_directInputDevice = nullptr;
} }
@@ -75,7 +76,8 @@ namespace BlackInput
return E_FAIL; 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"); CLogMessage(this).error("Direct input init failed");
return hr; return hr;
@@ -92,7 +94,7 @@ namespace BlackInput
} }
HRESULT hr; 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"); CLogMessage(this).error("Error reading joystick devices");
return hr; return hr;
@@ -158,6 +160,7 @@ namespace BlackInput
const CJoystickDeviceData &deviceData = m_availableJoystickDevices.constFirst(); const CJoystickDeviceData &deviceData = m_availableJoystickDevices.constFirst();
// Create device // 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))) if (FAILED(hr = m_directInput->CreateDevice(deviceData.guidDevice, &m_directInputDevice, nullptr)))
{ {
// FIXME: print error message // FIXME: print error message
@@ -167,8 +170,7 @@ namespace BlackInput
createHelperWindow(); createHelperWindow();
// Set cooperative level // Set cooperative level
if (FAILED(hr = m_directInputDevice->SetCooperativeLevel(m_helperWindow, DISCL_NONEXCLUSIVE | if (FAILED(hr = m_directInputDevice->SetCooperativeLevel(m_helperWindow, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
DISCL_BACKGROUND)))
{ {
// FIXME: print error message // FIXME: print error message
return hr; return hr;
@@ -281,6 +283,12 @@ namespace BlackInput
DIJOYSTATE2 state; DIJOYSTATE2 state;
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (!m_directInputDevice)
{
CLogMessage(this).warning("No input device");
return S_FALSE;
}
if (FAILED(hr = m_directInputDevice->Poll())) if (FAILED(hr = m_directInputDevice->Poll()))
{ {
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
@@ -355,5 +363,4 @@ namespace BlackInput
lhs.m_offset == rhs.m_offset && lhs.m_offset == rhs.m_offset &&
lhs.m_name == rhs.m_name; lhs.m_name == rhs.m_name;
} }
} // ns
} // namespace BlackInput

View File

@@ -29,17 +29,17 @@ namespace BlackInput
//! Joystick device data //! Joystick device data
struct CJoystickDeviceData struct CJoystickDeviceData
{ {
GUID guidDevice; //!< Device GUID GUID guidDevice; //!< Device GUID
GUID guidProduct; //!< Product GUID GUID guidProduct; //!< Product GUID
QString deviceName; //!< Device name QString deviceName; //!< Device name
QString productName; //!< Product name QString productName; //!< Product name
}; };
//! Joystick device input/button //! Joystick device input/button
struct CJoystickDeviceInput struct CJoystickDeviceInput
{ {
int m_number; //!< Input number int m_number; //!< Input number
int m_offset; //!< Input offset int m_offset; //!< Input offset
QString m_name; //!< Input name QString m_name; //!< Input name
}; };
@@ -98,18 +98,17 @@ namespace BlackInput
void addJoystickDeviceInput(const DIDEVICEOBJECTINSTANCE *dev); void addJoystickDeviceInput(const DIDEVICEOBJECTINSTANCE *dev);
//! Joystick enumeration callback //! Joystick enumeration callback
static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE * static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, VOID *pContext);
pdidInstance, VOID *pContext);
//! 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);
// todo RW: Try to use QScopedPointer. So far I could not find out how to use it with // todo RW: Try to use QScopedPointer. So far I could not find out how to use it with IDirectInput8::CreateDevice
// 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 IDirectInput8 *m_directInput = nullptr; //!< DirectInput object
IDirectInputDevice8 *m_directInputDevice = nullptr; //!< DirectInput device IDirectInputDevice8 *m_directInputDevice = nullptr; //!< DirectInput device
QVector<CJoystickDeviceData> m_availableJoystickDevices; //!< List of found and available joystick devices QVector<CJoystickDeviceData> m_availableJoystickDevices; //!< List of found and available joystick devices
QVector<CJoystickDeviceInput> m_joystickDeviceInputs; //!< List of available device buttons QVector<CJoystickDeviceInput> m_joystickDeviceInputs; //!< List of available device buttons
BlackMisc::Input::CHotkeyCombination m_buttonCombination; BlackMisc::Input::CHotkeyCombination m_buttonCombination;
@@ -118,6 +117,6 @@ namespace BlackInput
static ATOM m_helperWindowClass; static ATOM m_helperWindowClass;
static HWND m_helperWindow; //!< Helper window handle static HWND m_helperWindow; //!< Helper window handle
}; };
} // namespace BlackInput } // ns
#endif // guard #endif // guard