18using namespace swift::misc::input;
24 :
QObject(
parent), m_guidDevice(pdidInstance->guidInstance), m_guidProduct(pdidInstance->guidProduct),
25 m_deviceName(
QString::fromWCharArray(pdidInstance->tszInstanceName).simplified()),
26 m_productName(
QString::fromWCharArray(pdidInstance->tszProductName).simplified()),
27 m_directInput(directInputPtr)
37 IDirectInputDevice8 *diDevice =
nullptr;
38 if (FAILED(hr = m_directInput->CreateDevice(m_guidDevice, &diDevice,
nullptr)))
43 m_directInputDevice.reset(diDevice);
51 if (FAILED(hr = m_directInputDevice->SetCooperativeLevel(helperWindow, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
58 if (FAILED(hr = m_directInputDevice->SetDataFormat(&c_dfDIJoystick2)))
65 deviceCaps.dwSize =
sizeof(DIDEVCAPS);
67 if (FAILED(hr = m_directInputDevice->GetCapabilities(&deviceCaps)))
74 if (deviceCaps.dwButtons == 0)
81 if (FAILED(hr = m_directInputDevice->EnumObjects(enumObjectsCallback,
this, DIDFT_PSHBUTTON)))
87 CLogMessage(
this).
info(u
"Created joystick device '%1' with %2 buttons") << m_deviceName << deviceCaps.dwButtons;
108 HRESULT CJoystickDevice::pollDeviceState()
110 m_directInputDevice->Poll();
113 HRESULT hr = m_directInputDevice->GetDeviceState(
sizeof(DIJOYSTATE2), &state);
114 if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
116 m_directInputDevice->Acquire();
117 m_directInputDevice->Poll();
118 hr = m_directInputDevice->GetDeviceState(
sizeof(DIJOYSTATE2), &state);
123 CLogMessage(
this).
warning(u
"Cannot acquire and poll joystick device %1. Removing it.") << m_deviceName;
128 for (
const CJoystickDeviceInput &input : std::as_const(m_joystickDeviceInputs))
130 const qint32 buttonIndex = input.m_offset - DIJOFS_BUTTON0;
131 const bool isPressed = state.rgbButtons[buttonIndex] & 0x80;
137 QString CJoystickDevice::hrString(HRESULT hr)
141 LPCTSTR errMsg = err.ErrorMessage();
145 BOOL CALLBACK CJoystickDevice::enumObjectsCallback(
const DIDEVICEOBJECTINSTANCE *dev, LPVOID pvRef)
150 if (dev->guidType != GUID_Button)
return DIENUM_CONTINUE;
152 CJoystickDeviceInput deviceInput;
153 const int number = joystickDevice->m_joystickDeviceInputs.size();
154 deviceInput.m_offset = DIJOFS_BUTTON(number);
155 deviceInput.m_button = CJoystickButton(joystickDevice->m_deviceName, DIJOFS_BUTTON(number) - DIJOFS_BUTTON0);
157 joystickDevice->m_joystickDeviceInputs.append(deviceInput);
158 CLogMessage(
static_cast<CJoystickWindows *
>(
nullptr)).debug()
161 return DIENUM_CONTINUE;
168 HRESULT hr = CoInitializeEx(
nullptr, COINIT_MULTITHREADED);
171 if (hr == RPC_E_CHANGED_MODE)
173 CLogMessage(
this).debug(u
"CoInitializeEx was already called with a different mode. Trying again.");
174 hr = CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED);
181 if (hr == S_OK || hr == S_FALSE)
183 m_coInitializeSucceeded =
true;
184 this->createHelperWindow();
188 this->initDirectInput();
189 this->enumJoystickDevices();
190 this->requestDeviceNotification();
195 CLogMessage(
this).warning(u
"CoInitializeEx returned error code %1");
202 for (
CJoystickDevice *joystickDevice : std::as_const(m_joystickDevices))
204 delete joystickDevice;
206 m_joystickDevices.clear();
207 m_directInput.reset();
208 if (m_coInitializeSucceeded)
214 UnregisterDeviceNotification(hDevNotify);
216 destroyHelperWindow();
224 availableButtons.
push_back(device->getDeviceButtons());
226 return availableButtons;
229 void ReleaseDirectInput(IDirectInput8 *obj)
237 HRESULT CJoystickWindows::initDirectInput()
239 IDirectInput8 *directInput =
nullptr;
242 HRESULT hr = CoCreateInstance(CLSID_DirectInput8,
nullptr, CLSCTX_INPROC_SERVER, IID_IDirectInput8,
243 reinterpret_cast<LPVOID *
>(&directInput));
250 HINSTANCE instance = GetModuleHandle(
nullptr);
251 hr = m_directInput->Initialize(instance, DIRECTINPUT_VERSION);
255 HRESULT CJoystickWindows::enumJoystickDevices()
259 CLogMessage(
this).warning(u
"No direct input");
264 if (FAILED(hr = m_directInput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumJoysticksCallback,
this,
265 DIEDFL_ATTACHEDONLY)))
267 CLogMessage(
this).error(u
"Error reading joystick devices");
271 if (m_joystickDevices.isEmpty())
273 CLogMessage(
this).info(u
"No joystick device found");
278 int CJoystickWindows::createHelperWindow()
281 if (helperWindow !=
nullptr)
286 HINSTANCE hInstance = GetModuleHandle(
nullptr);
288 ZeroMemory(&wce,
sizeof(wce));
289 wce.cbSize =
sizeof(wce);
290 wce.lpfnWndProc = windowProc;
291 wce.lpszClassName =
static_cast<LPCWSTR
>(helperWindowClassName);
292 wce.hInstance = hInstance;
295 if (!RegisterClassEx(&wce))
302 CreateWindowEx(0, helperWindowClassName, helperWindowName, WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT,
303 CW_USEDEFAULT, CW_USEDEFAULT, HWND_MESSAGE,
nullptr, hInstance,
nullptr);
304 if (helperWindow ==
nullptr)
306 UnregisterClass(helperWindowClassName, hInstance);
310 SetProp(helperWindow, L
"CJoystickWindows",
this);
315 void CJoystickWindows::requestDeviceNotification()
317 DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
318 ZeroMemory(¬ificationFilter,
sizeof(notificationFilter));
319 notificationFilter.dbcc_size =
sizeof(DEV_BROADCAST_DEVICEINTERFACE);
320 notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
321 hDevNotify = RegisterDeviceNotification(helperWindow, ¬ificationFilter,
322 DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
325 void CJoystickWindows::destroyHelperWindow()
327 HINSTANCE hInstance = GetModuleHandle(
nullptr);
329 if (helperWindow ==
nullptr)
334 DestroyWindow(helperWindow);
335 helperWindow =
nullptr;
337 UnregisterClass(helperWindowClassName, hInstance);
340 void CJoystickWindows::addJoystickDevice(
const DIDEVICEINSTANCE *pdidInstance)
342 CJoystickDevice *device =
new CJoystickDevice(m_directInput, pdidInstance,
this);
343 bool success = device->init(helperWindow);
348 m_joystickDevices.push_back(device);
356 bool CJoystickWindows::isJoystickAlreadyAdded(
const DIDEVICEINSTANCE *pdidInstance)
const
358 for (
const CJoystickDevice *device : m_joystickDevices)
360 if (IsEqualGUID(device->getDeviceGuid(), pdidInstance->guidInstance))
369 void CJoystickWindows::joystickButtonChanged(
const CJoystickButton &joystickButton,
bool isPressed)
371 CHotkeyCombination oldCombination(m_buttonCombination);
374 m_buttonCombination.addJoystickButton(joystickButton);
378 m_buttonCombination.removeJoystickButton(joystickButton);
381 if (oldCombination != m_buttonCombination)
387 void CJoystickWindows::removeJoystickDevice(
const GUID &guid)
389 for (
auto it = m_joystickDevices.begin(); it != m_joystickDevices.end(); ++it)
391 CJoystickDevice *device = *it;
392 if (IsEqualGUID(guid, device->getDeviceGuid()))
394 device->deleteLater();
395 m_joystickDevices.erase(it);
404 LRESULT CALLBACK CJoystickWindows::windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
412 case WM_DEVICECHANGE:
414 if (wParam == DBT_DEVICEARRIVAL)
416 const DEV_BROADCAST_HDR *dbh =
reinterpret_cast<DEV_BROADCAST_HDR *
>(lParam);
418 if (dbh && dbh->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
420 joystickWindows->enumJoystickDevices();
427 return DefWindowProc(hWnd, uMsg, wParam, lParam);
430 BOOL CALLBACK CJoystickWindows::enumJoysticksCallback(
const DIDEVICEINSTANCE *pdidInstance, VOID *pContext)
437 if (!obj->isJoystickAlreadyAdded(pdidInstance))
439 obj->addJoystickDevice(pdidInstance);
443 return DIENUM_CONTINUE;
448 return lhs.m_guidDevice == rhs.m_guidDevice && lhs.m_guidProduct == rhs.m_guidProduct &&
449 lhs.m_deviceName == rhs.m_deviceName && lhs.m_productName == rhs.m_productName;
Class for emitting a log message.
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
void push_back(const T &value)
Appends an element at the end of the sequence.
std::shared_ptr< IDirectInput8 > DirectInput8Ptr
Shared IDirectInput8 ptr.
Free functions in swift::misc.
QString classNameShort(const QObject *object)
Class name as from QMetaObject::className without namespace.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
QObject * parent() const const
void setObjectName(QAnyStringView name)
int startTimer(int interval, Qt::TimerType timerType)
QString fromWCharArray(const wchar_t *string, qsizetype size)