Debian Codegraph @

src/api/xinput/XInputDLL.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 "XInputDLL.h"
10 #include "log/Log.h"
11
12 using namespace JOYSTICK;
13
14 CXInputDLL::CXInputDLL(void)
15 : m_dll(NULL),
16 m_getState(NULL),
17 m_getStateEx(NULL),
18 m_setState(NULL),
19 m_getCaps(NULL),
20 m_getBatteryInfo(NULL),
21 m_powerOff(NULL)
22 {
23 }
24
25 CXInputDLL& CXInputDLL::Get(void)
26 {
27 static CXInputDLL _instance;
28 return _instance;
29 }
30
31 bool CXInputDLL::Load(void)
32 {
33 std::lock_guard<std::recursive_mutex> lock(m_mutex);
34
35 m_strVersion = "1.4";
36 #if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
37 dsyslog("Attempting to load XInput1_4.dll...");
38 m_dll = LoadLibrary(TEXT("XInput1_4.dll")); // 1.4 Ships with Windows 8
39 if (!m_dll)
40 {
41 m_strVersion = "1.3";
42 dsyslog("Attempting to load XInput1_3.dll...");
43 m_dll = LoadLibrary(TEXT("XInput1_3.dll")); // 1.3 Ships with Vista and Win7, can be installed as a redistributable component
44 if (!m_dll)
45 {
46 dsyslog("Attempting to load bin\\XInput1_3.dll...");
47 m_dll = LoadLibrary(TEXT("bin\\XInput1_3.dll"));
48 }
49 }
50
51 if (!m_dll)
52 {
53 esyslog("Failed to load XInput DLL");
54 }
55 else
56 #endif // ! WINAPI_FAMILY_APP
57 {
58 dsyslog("Loaded XInput DLL version %s", m_strVersion.c_str());
59
60 // 100 is the ordinal for _XInputGetStateEx, which returns the same struct as
61 // XinputGetState, but with extra data in wButtons for the guide button, we think...
62 #if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
63 if (HasGuideButton())
64 m_getStateEx = (FnXInputGetStateEx)GetProcAddress(m_dll, reinterpret_cast<LPCSTR>(100));
65 else
66 m_getState = (FnXInputGetState)GetProcAddress(m_dll, "XInputGetState");
67 m_setState = (FnXInputSetState)GetProcAddress(m_dll, "XInputSetState");
68 m_getCaps = (FnXInputGetCapabilities)GetProcAddress(m_dll, "XInputGetCapabilities");
69 m_getBatteryInfo = (FnXInputGetBatteryInformation)GetProcAddress(m_dll, "XInputGetBatteryInformation");
70 if (SupportsPowerOff())
71 m_powerOff = (FnXInputPowerOffController)GetProcAddress(m_dll, reinterpret_cast<LPCSTR>(103));
72 #else
73 m_getState = XInputGetState;
74 m_setState = XInputSetState;
75 m_getCaps = XInputGetCapabilities;
76 m_getBatteryInfo = XInputGetBatteryInformation;
77 #endif
78 if ((m_getState || m_getStateEx) && m_setState && m_getCaps && m_getBatteryInfo)
79 {
80 dsyslog("Loaded XInput symbols (GetState=%p, GetStateEx=%p, SetState=%p, GetCaps=%p, GetBatteryInformation=%p, PowerOff=%p)",
81 m_getState, m_getStateEx, m_setState, m_getCaps, m_getBatteryInfo, m_powerOff);
82 return true;
83 }
84 else
85 {
86 esyslog("Failed to load one or more symbols (GetState=%p, GetStateEx=%p, SetState=%p, GetCaps=%p, GetBatteryInformation=%p, PowerOff=%p)",
87 m_getState, m_getStateEx, m_setState, m_getCaps, m_getBatteryInfo, m_powerOff);
88 }
89 }
90
91 Unload();
92 return false;
93 }
94
95 void CXInputDLL::Unload(void)
96 {
97 std::lock_guard<std::recursive_mutex> lock(m_mutex);
98
99 #if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
100 if (m_dll)
101 FreeLibrary(m_dll);
102 #endif
103
104 m_strVersion.clear();
105 m_getState = NULL;
106 m_getStateEx = NULL;
107 m_setState = NULL;
108 m_getCaps = NULL;
109 m_getBatteryInfo = NULL;
110 m_powerOff = NULL;
111 m_dll = NULL;
112 }
113
114 bool CXInputDLL::HasGuideButton(void) const
115 {
116 #if defined(TARGET_WINDOWS_DESKTOP)
117 if (m_strVersion == "1.4")
118 return true;
119 #endif
120
121 return m_strVersion == "1.3";
122 }
123
124 bool CXInputDLL::SupportsPowerOff(void) const
125 {
126 #if defined(TARGET_WINDOWS_DESKTOP)
127 if (m_strVersion == "1.4")
128 return true;
129 #endif
130
131 return m_strVersion == "1.3";
132 }
133
134 bool CXInputDLL::GetState(unsigned int controllerId, XINPUT_STATE& state)
135 {
136 std::lock_guard<std::recursive_mutex> lock(m_mutex);
137
138 if (!m_getState)
139 return false;
140
141 DWORD result = m_getState(controllerId, &state);
142 if (result != ERROR_SUCCESS)
143 {
144 if (result != ERROR_DEVICE_NOT_CONNECTED)
145 esyslog("Failed to get XInput state on port %u (result=%u)", controllerId, result);
146 return false;
147 }
148
149 return true;
150 }
151
152 bool CXInputDLL::GetStateWithGuide(unsigned int controllerId, XINPUT_STATE_EX& state)
153 {
154 std::lock_guard<std::recursive_mutex> lock(m_mutex);
155
156 if (!m_getStateEx)
157 return false;
158
159 DWORD result = m_getStateEx(controllerId, &state);
160 if (result != ERROR_SUCCESS)
161 {
162 if (result != ERROR_DEVICE_NOT_CONNECTED)
163 esyslog("Failed to get XInput state on port %u (result=%u)", controllerId, result);
164 return false;
165 }
166
167 return true;
168 }
169
170 bool CXInputDLL::SetState(unsigned int controllerId, XINPUT_VIBRATION& vibration)
171 {
172 std::lock_guard<std::recursive_mutex> lock(m_mutex);
173
174 if (!m_setState)
175 return false;
176
177 DWORD result = m_setState(controllerId, &vibration);
178 if (result != ERROR_SUCCESS)
179 {
180 if (result == ERROR_DEVICE_NOT_CONNECTED)
181 dsyslog("No XInput devices on port %u", controllerId);
182 else
183 esyslog("Failed to set XInput state on port %u (result=%u)", controllerId, result);
184 return false;
185 }
186
187 return true;
188 }
189
190 bool CXInputDLL::GetCapabilities(unsigned int controllerId, XINPUT_CAPABILITIES& caps)
191 {
192 std::lock_guard<std::recursive_mutex> lock(m_mutex);
193
194 if (!m_getCaps)
195 return false;
196
197 // currently no other value than XINPUT_FLAG_GAMEPAD is supported for the second parameter
198 DWORD result = m_getCaps(controllerId, XINPUT_FLAG_GAMEPAD, &caps);
199 if (result != ERROR_SUCCESS)
200 {
201 if (result == ERROR_DEVICE_NOT_CONNECTED)
202 dsyslog("No XInput devices on port %u", controllerId);
203 else
204 esyslog("Failed to get XInput capabilities on port %u (result=%u)", controllerId, result);
205 return false;
206 }
207
208 return true;
209 }
210
211 bool CXInputDLL::GetBatteryInformation(unsigned int controllerId, BatteryDeviceType deviceType, XINPUT_BATTERY_INFORMATION& battery)
212 {
213 std::lock_guard<std::recursive_mutex> lock(m_mutex);
214
215 if (!m_getBatteryInfo)
216 return false;
217
218 BYTE devType;
219 switch (deviceType)
220 {
221 case BatteryDeviceType::Controller:
222 devType = BATTERY_DEVTYPE_GAMEPAD;
223 break;
224
225 case BatteryDeviceType::Headset:
226 devType = BATTERY_DEVTYPE_HEADSET;
227 break;
228
229 default:
230 esyslog("Invalid battery device type %d", deviceType);
231 return false;
232 }
233
234 DWORD result = m_getBatteryInfo(controllerId, devType, &battery);
235 if (result != ERROR_SUCCESS)
236 {
237 if (result == ERROR_DEVICE_NOT_CONNECTED)
238 dsyslog("No XInput devices on port %u", controllerId);
239 else
240 esyslog("Failed to get XInput battery information on port %u (result=%u)", controllerId, result);
241 return false;
242 }
243
244 return true;
245 }
246
247 bool CXInputDLL::PowerOff(unsigned int controllerId)
248 {
249 std::lock_guard<std::recursive_mutex> lock(m_mutex);
250
251 if (!m_powerOff)
252 return false;
253
254 DWORD result = m_powerOff(controllerId);
255 if (result != ERROR_SUCCESS)
256 {
257 if (result == ERROR_DEVICE_NOT_CONNECTED)
258 dsyslog("No XInput devices on port %u", controllerId);
259 else
260 esyslog("Failed to power off XInput device on port %u (result=%u)", controllerId, result);
261 return false;
262 }
263
264 return true;
265 }