Debian Codegraph @

src/api/directinput/JoystickInterfaceDirectInput.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014-2021 Garrett Brown
3 * Copyright (C) 2014-2021 Team Kodi
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSE.md for more information.
7 */
8
9 #include "JoystickInterfaceDirectInput.h"
10 #include "JoystickDirectInput.h"
11 #include "api/JoystickTypes.h"
12 #include "log/Log.h"
13 #include "utils/CommonMacros.h"
14
15 #include <kodi/addon-instance/Peripheral.h>
16
17 #pragma comment(lib, "Dinput8.lib")
18 #pragma comment(lib, "dxguid.lib")
19
20 using namespace JOYSTICK;
21
22 namespace JOYSTICK
23 {
24 struct EnumWindowsCallbackArgs
25 {
26 DWORD pid;
27 HWND* handle;
28 };
29 }
30
31 CJoystickInterfaceDirectInput::CJoystickInterfaceDirectInput(void)
32 : m_hWnd(NULL),
33 m_pDirectInput(NULL)
34 { }
35
36 EJoystickInterface CJoystickInterfaceDirectInput::Type(void) const
37 {
38 return EJoystickInterface::DIRECTINPUT;
39 }
40
41 bool CJoystickInterfaceDirectInput::Initialize(void)
42 {
43 // Defer initialization until we have the main window handle
44 return true;
45 }
46
47 bool CJoystickInterfaceDirectInput::InitializeDirectInput(void)
48 {
49 if (m_pDirectInput == NULL)
50 {
51 if (m_hWnd == NULL)
52 m_hWnd = GetMainWindowHandle();
53
54 if (m_hWnd != NULL)
55 {
56 HRESULT hr;
57
58 hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, reinterpret_cast<VOID**>(&m_pDirectInput), NULL);
59 if (FAILED(hr) || m_pDirectInput == NULL)
60 esyslog("%s: Failed to create DirectInput", __FUNCTION__);
61 }
62 }
63
64 return m_pDirectInput != NULL;
65 }
66
67 void CJoystickInterfaceDirectInput::Deinitialize(void)
68 {
69 SAFE_RELEASE(m_pDirectInput);
70 m_hWnd = NULL;
71 }
72
73 bool CJoystickInterfaceDirectInput::ScanForJoysticks(JoystickVector& joysticks)
74 {
75 if (InitializeDirectInput())
76 {
77 HRESULT hr;
78
79 hr = m_pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, this, DIEDFL_ATTACHEDONLY);
80 if (FAILED(hr))
81 {
82 esyslog("%s: Joystick enumeration failed", __FUNCTION__);
83 return false;
84 }
85
86 GetScanResults(joysticks);
87 }
88
89 return true;
90 }
91
92 BOOL CALLBACK CJoystickInterfaceDirectInput::EnumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, VOID *pContext)
93 {
94 // Skip verified XInput devices
95 if (IsXInputDevice(&pdidInstance->guidProduct))
96 return DIENUM_CONTINUE;
97
98 CJoystickInterfaceDirectInput* context = static_cast<CJoystickInterfaceDirectInput*>(pContext);
99
100 LPDIRECTINPUTDEVICE8 pJoystick = NULL;
101
102 // Obtain an interface to the enumerated joystick.
103 HRESULT hr = context->m_pDirectInput->CreateDevice(pdidInstance->guidInstance, &pJoystick, NULL);
104 if (FAILED(hr) || pJoystick == NULL)
105 {
106 esyslog("%s: Failed to CreateDevice: %s", __FUNCTION__, pdidInstance->tszProductName);
107 return DIENUM_CONTINUE;
108 }
109
110 // Set the data format to "simple joystick" - a predefined data format.
111 // A data format specifies which controls on a device we are interested in,
112 // and how they should be reported. This tells DInput that we will be
113 // passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState().
114 hr = pJoystick->SetDataFormat(&c_dfDIJoystick2);
115 if (FAILED(hr))
116 {
117 esyslog("%s: Failed to SetDataFormat on: %s", __FUNCTION__, pdidInstance->tszProductName);
118 return DIENUM_CONTINUE;
119 }
120
121 if (!context->m_hWnd)
122 return DIENUM_CONTINUE;
123
124 // Set the cooperative level to let DInput know how this device should
125 // interact with the system and with other DInput applications.
126 hr = pJoystick->SetCooperativeLevel(context->m_hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
127 if (FAILED(hr))
128 {
129 esyslog("%s: Failed to SetCooperativeLevel on: %s", __FUNCTION__, pdidInstance->tszProductName);
130 return DIENUM_CONTINUE;
131 }
132
133 const TCHAR *strName = pdidInstance->tszProductName ? pdidInstance->tszProductName : TEXT("");
134
135 context->AddScanResult(JoystickPtr(new CJoystickDirectInput(pdidInstance->guidInstance, pJoystick, strName)));
136
137 return DIENUM_CONTINUE;
138 }
139
140 //-----------------------------------------------------------------------------
141 // This implementation has been taken from
142 // https://github.com/rheit/zdoom/blob/master/src/win32/i_dijoy.cpp
143 // The theory of operation is the same as for the code provided by Microsoft at
144 // http://msdn.microsoft.com/en-us/library/windows/desktop/ee417014(v=vs.85).aspx,
145 // except that we use the Raw Input device list to find the device ID instead of WMI.
146 // This is a huge order of magnitude faster than WMI (around 10000 times faster!)
147 //-----------------------------------------------------------------------------
148 bool CJoystickInterfaceDirectInput::IsXInputDevice(const GUID *pGuidProductFromDirectInput)
149 {
150 UINT nDevices;
151 if (GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST)) != 0U)
152 return false;
153
154 RAWINPUTDEVICELIST *devices;
155 if ((devices = (RAWINPUTDEVICELIST *)malloc(sizeof(RAWINPUTDEVICELIST) * nDevices)) == NULL)
156 return false;
157
158 UINT numDevices;
159 if ((numDevices = GetRawInputDeviceList(devices, &nDevices, sizeof(RAWINPUTDEVICELIST))) == (UINT)-1)
160 {
161 free(devices);
162 return false;
163 }
164
165 bool isXInput = false;
166 for (UINT i = 0; i < numDevices; ++i)
167 {
168 // I am making the assumption here that all possible XInput devices will
169 // report themselves as generic HID devices and not as keyboards or mice
170 if (devices[i].dwType == RIM_TYPEHID)
171 {
172 RID_DEVICE_INFO rdi;
173 UINT cbSize = rdi.cbSize = sizeof(rdi);
174 if ((INT)GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICEINFO, &rdi, &cbSize) >= 0)
175 {
176 if (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)pGuidProductFromDirectInput->Data1)
177 {
178 char name[256];
179 UINT namelen = sizeof(name);
180 UINT reslen = GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICENAME, name, &namelen);
181 if (reslen != (UINT)-1)
182 {
183 isXInput = strstr(name, "IG_") != NULL;
184 break;
185 }
186 }
187 }
188 }
189 }
190 free(devices);
191
192 return isXInput;
193 }
194
195 HWND CJoystickInterfaceDirectInput::GetMainWindowHandle(void)
196 {
197 HWND hWnd = NULL;
198
199 EnumWindowsCallbackArgs args = { ::GetCurrentProcessId(), &hWnd };
200 if (::EnumWindows(&EnumWindowsCallback, (LPARAM)&args) == FALSE)
201 esyslog("Failed to get main window handle");
202
203 return hWnd;
204 }
205
206 BOOL CALLBACK CJoystickInterfaceDirectInput::EnumWindowsCallback(HWND hnd, LPARAM lParam)
207 {
208 EnumWindowsCallbackArgs* args = reinterpret_cast<EnumWindowsCallbackArgs*>(lParam);
209
210 DWORD windowPID;
211 (void)::GetWindowThreadProcessId(hnd, &windowPID);
212 if (windowPID == args->pid && IsWindow(hnd) && IsWindowVisible(hnd))
213 *args->handle = hnd;
214
215 return TRUE;
216 }