1 /*************************************************************************/
2 /*  joypad_windows.cpp                                                   */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "joypad_windows.h"
32 
33 #include <oleauto.h>
34 #include <wbemidl.h>
35 
36 #if defined(__GNUC__)
37 // Workaround GCC warning from -Wcast-function-type.
38 #define GetProcAddress (void *)GetProcAddress
39 #endif
40 
_xinput_get_state(DWORD dwUserIndex,XINPUT_STATE * pState)41 DWORD WINAPI _xinput_get_state(DWORD dwUserIndex, XINPUT_STATE *pState) {
42 	return ERROR_DEVICE_NOT_CONNECTED;
43 }
_xinput_set_state(DWORD dwUserIndex,XINPUT_VIBRATION * pVibration)44 DWORD WINAPI _xinput_set_state(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration) {
45 	return ERROR_DEVICE_NOT_CONNECTED;
46 }
47 
JoypadWindows()48 JoypadWindows::JoypadWindows() {
49 }
50 
JoypadWindows(InputDefault * _input,HWND * hwnd)51 JoypadWindows::JoypadWindows(InputDefault *_input, HWND *hwnd) {
52 
53 	input = _input;
54 	hWnd = hwnd;
55 	joypad_count = 0;
56 	dinput = NULL;
57 	xinput_dll = NULL;
58 	xinput_get_state = NULL;
59 	xinput_set_state = NULL;
60 
61 	load_xinput();
62 
63 	for (int i = 0; i < JOYPADS_MAX; i++)
64 		attached_joypads[i] = false;
65 
66 	HRESULT result = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&dinput, nullptr);
67 	if (result == DI_OK) {
68 		probe_joypads();
69 	} else {
70 		ERR_PRINT("Couldn't initialize DirectInput. Error: " + itos(result));
71 		if (result == DIERR_OUTOFMEMORY) {
72 			ERR_PRINT("The Windows DirectInput subsystem could not allocate sufficient memory.");
73 			ERR_PRINT("Rebooting your PC may solve this issue.");
74 		}
75 		// Ensure dinput is still a nullptr.
76 		dinput = nullptr;
77 	}
78 }
79 
~JoypadWindows()80 JoypadWindows::~JoypadWindows() {
81 
82 	close_joypad();
83 	if (dinput) {
84 		dinput->Release();
85 	}
86 	unload_xinput();
87 }
88 
have_device(const GUID & p_guid)89 bool JoypadWindows::have_device(const GUID &p_guid) {
90 
91 	for (int i = 0; i < JOYPADS_MAX; i++) {
92 
93 		if (d_joypads[i].guid == p_guid) {
94 
95 			d_joypads[i].confirmed = true;
96 			return true;
97 		}
98 	}
99 	return false;
100 }
101 
102 // adapted from SDL2, works a lot better than the MSDN version
is_xinput_device(const GUID * p_guid)103 bool JoypadWindows::is_xinput_device(const GUID *p_guid) {
104 
105 	static GUID IID_ValveStreamingGamepad = { MAKELONG(0x28DE, 0x11FF), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
106 	static GUID IID_X360WiredGamepad = { MAKELONG(0x045E, 0x02A1), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
107 	static GUID IID_X360WirelessGamepad = { MAKELONG(0x045E, 0x028E), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
108 
109 	if (p_guid == &IID_ValveStreamingGamepad || p_guid == &IID_X360WiredGamepad || p_guid == &IID_X360WirelessGamepad)
110 		return true;
111 
112 	PRAWINPUTDEVICELIST dev_list = NULL;
113 	unsigned int dev_list_count = 0;
114 
115 	if (GetRawInputDeviceList(NULL, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) {
116 		return false;
117 	}
118 	dev_list = (PRAWINPUTDEVICELIST)malloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count);
119 	if (!dev_list) return false;
120 
121 	if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) {
122 		free(dev_list);
123 		return false;
124 	}
125 	for (unsigned int i = 0; i < dev_list_count; i++) {
126 
127 		RID_DEVICE_INFO rdi;
128 		char dev_name[128];
129 		UINT rdiSize = sizeof(rdi);
130 		UINT nameSize = sizeof(dev_name);
131 
132 		rdi.cbSize = rdiSize;
133 		if ((dev_list[i].dwType == RIM_TYPEHID) &&
134 				(GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1) &&
135 				(MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)p_guid->Data1) &&
136 				(GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) &&
137 				(strstr(dev_name, "IG_") != NULL)) {
138 
139 			free(dev_list);
140 			return true;
141 		}
142 	}
143 	free(dev_list);
144 	return false;
145 }
146 
setup_dinput_joypad(const DIDEVICEINSTANCE * instance)147 bool JoypadWindows::setup_dinput_joypad(const DIDEVICEINSTANCE *instance) {
148 
149 	ERR_FAIL_NULL_V_MSG(dinput, false, "DirectInput not initialized. Rebooting your PC may solve this issue.");
150 	HRESULT hr;
151 	int num = input->get_unused_joy_id();
152 
153 	if (have_device(instance->guidInstance) || num == -1)
154 		return false;
155 
156 	d_joypads[num] = dinput_gamepad();
157 	dinput_gamepad *joy = &d_joypads[num];
158 
159 	const DWORD devtype = (instance->dwDevType & 0xFF);
160 
161 	if ((devtype != DI8DEVTYPE_JOYSTICK) && (devtype != DI8DEVTYPE_GAMEPAD) && (devtype != DI8DEVTYPE_1STPERSON)) {
162 		return false;
163 	}
164 
165 	hr = dinput->CreateDevice(instance->guidInstance, &joy->di_joy, NULL);
166 
167 	if (FAILED(hr)) {
168 		return false;
169 	}
170 
171 	const GUID &guid = instance->guidProduct;
172 	char uid[128];
173 
174 	ERR_FAIL_COND_V_MSG(memcmp(&guid.Data4[2], "PIDVID", 6), false, "DirectInput device not recognised.");
175 	WORD type = BSWAP16(0x03);
176 	WORD vendor = BSWAP16(LOWORD(guid.Data1));
177 	WORD product = BSWAP16(HIWORD(guid.Data1));
178 	WORD version = 0;
179 	sprintf_s(uid, "%04x%04x%04x%04x%04x%04x%04x%04x", type, 0, vendor, 0, product, 0, version, 0);
180 
181 	id_to_change = num;
182 	slider_count = 0;
183 
184 	joy->di_joy->SetDataFormat(&c_dfDIJoystick2);
185 	joy->di_joy->SetCooperativeLevel(*hWnd, DISCL_FOREGROUND);
186 	joy->di_joy->EnumObjects(objectsCallback, this, 0);
187 	joy->joy_axis.sort();
188 
189 	joy->guid = instance->guidInstance;
190 	input->joy_connection_changed(num, true, instance->tszProductName, uid);
191 	joy->attached = true;
192 	joy->id = num;
193 	attached_joypads[num] = true;
194 	joy->confirmed = true;
195 	joypad_count++;
196 	return true;
197 }
198 
setup_joypad_object(const DIDEVICEOBJECTINSTANCE * ob,int p_joy_id)199 void JoypadWindows::setup_joypad_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id) {
200 
201 	if (ob->dwType & DIDFT_AXIS) {
202 
203 		HRESULT res;
204 		DIPROPRANGE prop_range;
205 		DIPROPDWORD dilong;
206 		DWORD ofs;
207 		if (ob->guidType == GUID_XAxis)
208 			ofs = DIJOFS_X;
209 		else if (ob->guidType == GUID_YAxis)
210 			ofs = DIJOFS_Y;
211 		else if (ob->guidType == GUID_ZAxis)
212 			ofs = DIJOFS_Z;
213 		else if (ob->guidType == GUID_RxAxis)
214 			ofs = DIJOFS_RX;
215 		else if (ob->guidType == GUID_RyAxis)
216 			ofs = DIJOFS_RY;
217 		else if (ob->guidType == GUID_RzAxis)
218 			ofs = DIJOFS_RZ;
219 		else if (ob->guidType == GUID_Slider) {
220 			if (slider_count < 2) {
221 				ofs = DIJOFS_SLIDER(slider_count);
222 				slider_count++;
223 			} else {
224 				return;
225 			}
226 		} else
227 			return;
228 		prop_range.diph.dwSize = sizeof(DIPROPRANGE);
229 		prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
230 		prop_range.diph.dwObj = ob->dwType;
231 		prop_range.diph.dwHow = DIPH_BYID;
232 		prop_range.lMin = -MAX_JOY_AXIS;
233 		prop_range.lMax = +MAX_JOY_AXIS;
234 
235 		dinput_gamepad &joy = d_joypads[p_joy_id];
236 
237 		res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_RANGE, &prop_range.diph);
238 		if (FAILED(res))
239 			return;
240 
241 		dilong.diph.dwSize = sizeof(dilong);
242 		dilong.diph.dwHeaderSize = sizeof(dilong.diph);
243 		dilong.diph.dwObj = ob->dwType;
244 		dilong.diph.dwHow = DIPH_BYID;
245 		dilong.dwData = 0;
246 
247 		res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_DEADZONE, &dilong.diph);
248 		if (FAILED(res))
249 			return;
250 
251 		joy.joy_axis.push_back(ofs);
252 	}
253 }
254 
enumCallback(const DIDEVICEINSTANCE * p_instance,void * p_context)255 BOOL CALLBACK JoypadWindows::enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context) {
256 
257 	JoypadWindows *self = (JoypadWindows *)p_context;
258 	if (self->is_xinput_device(&p_instance->guidProduct)) {
259 		return DIENUM_CONTINUE;
260 	}
261 	self->setup_dinput_joypad(p_instance);
262 	return DIENUM_CONTINUE;
263 }
264 
objectsCallback(const DIDEVICEOBJECTINSTANCE * instance,void * context)265 BOOL CALLBACK JoypadWindows::objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context) {
266 
267 	JoypadWindows *self = (JoypadWindows *)context;
268 	self->setup_joypad_object(instance, self->id_to_change);
269 
270 	return DIENUM_CONTINUE;
271 }
272 
close_joypad(int id)273 void JoypadWindows::close_joypad(int id) {
274 
275 	if (id == -1) {
276 
277 		for (int i = 0; i < JOYPADS_MAX; i++) {
278 
279 			close_joypad(i);
280 		}
281 		return;
282 	}
283 
284 	if (!d_joypads[id].attached) return;
285 
286 	d_joypads[id].di_joy->Unacquire();
287 	d_joypads[id].di_joy->Release();
288 	d_joypads[id].attached = false;
289 	attached_joypads[d_joypads[id].id] = false;
290 	d_joypads[id].guid.Data1 = d_joypads[id].guid.Data2 = d_joypads[id].guid.Data3 = 0;
291 	input->joy_connection_changed(d_joypads[id].id, false, "");
292 	joypad_count--;
293 }
294 
probe_joypads()295 void JoypadWindows::probe_joypads() {
296 
297 	ERR_FAIL_NULL_MSG(dinput, "DirectInput not initialized. Rebooting your PC may solve this issue.");
298 	DWORD dwResult;
299 	for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
300 
301 		ZeroMemory(&x_joypads[i].state, sizeof(XINPUT_STATE));
302 
303 		dwResult = xinput_get_state(i, &x_joypads[i].state);
304 		if (dwResult == ERROR_SUCCESS) {
305 
306 			int id = input->get_unused_joy_id();
307 			if (id != -1 && !x_joypads[i].attached) {
308 
309 				x_joypads[i].attached = true;
310 				x_joypads[i].id = id;
311 				x_joypads[i].ff_timestamp = 0;
312 				x_joypads[i].ff_end_timestamp = 0;
313 				x_joypads[i].vibrating = false;
314 				attached_joypads[id] = true;
315 				input->joy_connection_changed(id, true, "XInput Gamepad", "__XINPUT_DEVICE__");
316 			}
317 		} else if (x_joypads[i].attached) {
318 
319 			x_joypads[i].attached = false;
320 			attached_joypads[x_joypads[i].id] = false;
321 			input->joy_connection_changed(x_joypads[i].id, false, "");
322 		}
323 	}
324 
325 	for (int i = 0; i < joypad_count; i++) {
326 
327 		d_joypads[i].confirmed = false;
328 	}
329 
330 	dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY);
331 
332 	for (int i = 0; i < joypad_count; i++) {
333 
334 		if (!d_joypads[i].confirmed) {
335 
336 			close_joypad(i);
337 		}
338 	}
339 }
340 
process_joypads()341 void JoypadWindows::process_joypads() {
342 
343 	HRESULT hr;
344 
345 	for (int i = 0; i < XUSER_MAX_COUNT; i++) {
346 
347 		xinput_gamepad &joy = x_joypads[i];
348 		if (!joy.attached) {
349 			continue;
350 		}
351 		ZeroMemory(&joy.state, sizeof(XINPUT_STATE));
352 
353 		xinput_get_state(i, &joy.state);
354 		if (joy.state.dwPacketNumber != joy.last_packet) {
355 
356 			int button_mask = XINPUT_GAMEPAD_DPAD_UP;
357 			for (int j = 0; j <= 16; j++) {
358 
359 				input->joy_button(joy.id, j, joy.state.Gamepad.wButtons & button_mask);
360 				button_mask = button_mask * 2;
361 			}
362 
363 			input->joy_axis(joy.id, JOY_AXIS_0, axis_correct(joy.state.Gamepad.sThumbLX, true));
364 			input->joy_axis(joy.id, JOY_AXIS_1, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true));
365 			input->joy_axis(joy.id, JOY_AXIS_2, axis_correct(joy.state.Gamepad.sThumbRX, true));
366 			input->joy_axis(joy.id, JOY_AXIS_3, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true));
367 			input->joy_axis(joy.id, JOY_AXIS_4, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true));
368 			input->joy_axis(joy.id, JOY_AXIS_5, axis_correct(joy.state.Gamepad.bRightTrigger, true, true));
369 			joy.last_packet = joy.state.dwPacketNumber;
370 		}
371 		uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
372 		if (timestamp > joy.ff_timestamp) {
373 			Vector2 strength = input->get_joy_vibration_strength(joy.id);
374 			float duration = input->get_joy_vibration_duration(joy.id);
375 			if (strength.x == 0 && strength.y == 0) {
376 				joypad_vibration_stop_xinput(i, timestamp);
377 			} else {
378 				joypad_vibration_start_xinput(i, strength.x, strength.y, duration, timestamp);
379 			}
380 		} else if (joy.vibrating && joy.ff_end_timestamp != 0) {
381 			uint64_t current_time = OS::get_singleton()->get_ticks_usec();
382 			if (current_time >= joy.ff_end_timestamp)
383 				joypad_vibration_stop_xinput(i, current_time);
384 		}
385 	}
386 
387 	for (int i = 0; i < JOYPADS_MAX; i++) {
388 
389 		dinput_gamepad *joy = &d_joypads[i];
390 
391 		if (!joy->attached)
392 			continue;
393 
394 		DIJOYSTATE2 js;
395 		hr = joy->di_joy->Poll();
396 		if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
397 			IDirectInputDevice8_Acquire(joy->di_joy);
398 			joy->di_joy->Poll();
399 		}
400 
401 		hr = joy->di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js);
402 		if (FAILED(hr)) {
403 			continue;
404 		}
405 
406 		post_hat(joy->id, js.rgdwPOV[0]);
407 
408 		for (int j = 0; j < 128; j++) {
409 
410 			if (js.rgbButtons[j] & 0x80) {
411 
412 				if (!joy->last_buttons[j]) {
413 
414 					input->joy_button(joy->id, j, true);
415 					joy->last_buttons[j] = true;
416 				}
417 			} else {
418 
419 				if (joy->last_buttons[j]) {
420 
421 					input->joy_button(joy->id, j, false);
422 					joy->last_buttons[j] = false;
423 				}
424 			}
425 		}
426 
427 		// on mingw, these constants are not constants
428 		int count = 8;
429 		unsigned int axes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ, DIJOFS_SLIDER(0), DIJOFS_SLIDER(1) };
430 		int values[] = { js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz, js.rglSlider[0], js.rglSlider[1] };
431 
432 		for (int j = 0; j < joy->joy_axis.size(); j++) {
433 
434 			for (int k = 0; k < count; k++) {
435 				if (joy->joy_axis[j] == axes[k]) {
436 					input->joy_axis(joy->id, j, axis_correct(values[k]));
437 					break;
438 				};
439 			};
440 		};
441 	}
442 	return;
443 }
444 
post_hat(int p_device,DWORD p_dpad)445 void JoypadWindows::post_hat(int p_device, DWORD p_dpad) {
446 
447 	int dpad_val = 0;
448 
449 	// Should be -1 when centered, but according to docs:
450 	// "Some drivers report the centered position of the POV indicator as 65,535. Determine whether the indicator is centered as follows:
451 	//  BOOL POVCentered = (LOWORD(dwPOV) == 0xFFFF);"
452 	// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee416628(v%3Dvs.85)#remarks
453 	if (LOWORD(p_dpad) == 0xFFFF) {
454 		dpad_val = InputDefault::HAT_MASK_CENTER;
455 	}
456 	if (p_dpad == 0) {
457 
458 		dpad_val = InputDefault::HAT_MASK_UP;
459 
460 	} else if (p_dpad == 4500) {
461 
462 		dpad_val = (InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT);
463 
464 	} else if (p_dpad == 9000) {
465 
466 		dpad_val = InputDefault::HAT_MASK_RIGHT;
467 
468 	} else if (p_dpad == 13500) {
469 
470 		dpad_val = (InputDefault::HAT_MASK_RIGHT | InputDefault::HAT_MASK_DOWN);
471 
472 	} else if (p_dpad == 18000) {
473 
474 		dpad_val = InputDefault::HAT_MASK_DOWN;
475 
476 	} else if (p_dpad == 22500) {
477 
478 		dpad_val = (InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT);
479 
480 	} else if (p_dpad == 27000) {
481 
482 		dpad_val = InputDefault::HAT_MASK_LEFT;
483 
484 	} else if (p_dpad == 31500) {
485 
486 		dpad_val = (InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_UP);
487 	}
488 	input->joy_hat(p_device, dpad_val);
489 };
490 
axis_correct(int p_val,bool p_xinput,bool p_trigger,bool p_negate) const491 InputDefault::JoyAxis JoypadWindows::axis_correct(int p_val, bool p_xinput, bool p_trigger, bool p_negate) const {
492 
493 	InputDefault::JoyAxis jx;
494 	if (Math::abs(p_val) < MIN_JOY_AXIS) {
495 		jx.min = p_trigger ? 0 : -1;
496 		jx.value = 0.0f;
497 		return jx;
498 	}
499 	if (p_xinput) {
500 
501 		if (p_trigger) {
502 			jx.min = 0;
503 			jx.value = (float)p_val / MAX_TRIGGER;
504 			return jx;
505 		}
506 		jx.min = -1;
507 		if (p_val < 0) {
508 			jx.value = (float)p_val / MAX_JOY_AXIS;
509 		} else {
510 			jx.value = (float)p_val / (MAX_JOY_AXIS - 1);
511 		}
512 		if (p_negate) {
513 			jx.value = -jx.value;
514 		}
515 		return jx;
516 	}
517 	jx.min = -1;
518 	jx.value = (float)p_val / MAX_JOY_AXIS;
519 	return jx;
520 }
521 
joypad_vibration_start_xinput(int p_device,float p_weak_magnitude,float p_strong_magnitude,float p_duration,uint64_t p_timestamp)522 void JoypadWindows::joypad_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
523 	xinput_gamepad &joy = x_joypads[p_device];
524 	if (joy.attached) {
525 		XINPUT_VIBRATION effect;
526 		effect.wLeftMotorSpeed = (65535 * p_strong_magnitude);
527 		effect.wRightMotorSpeed = (65535 * p_weak_magnitude);
528 		if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
529 			joy.ff_timestamp = p_timestamp;
530 			joy.ff_end_timestamp = p_duration == 0 ? 0 : p_timestamp + (uint64_t)(p_duration * 1000000.0);
531 			joy.vibrating = true;
532 		}
533 	}
534 }
535 
joypad_vibration_stop_xinput(int p_device,uint64_t p_timestamp)536 void JoypadWindows::joypad_vibration_stop_xinput(int p_device, uint64_t p_timestamp) {
537 	xinput_gamepad &joy = x_joypads[p_device];
538 	if (joy.attached) {
539 		XINPUT_VIBRATION effect;
540 		effect.wLeftMotorSpeed = 0;
541 		effect.wRightMotorSpeed = 0;
542 		if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
543 			joy.ff_timestamp = p_timestamp;
544 			joy.vibrating = false;
545 		}
546 	}
547 }
548 
load_xinput()549 void JoypadWindows::load_xinput() {
550 
551 	xinput_get_state = &_xinput_get_state;
552 	xinput_set_state = &_xinput_set_state;
553 	xinput_dll = LoadLibrary("XInput1_4.dll");
554 	if (!xinput_dll) {
555 		xinput_dll = LoadLibrary("XInput1_3.dll");
556 		if (!xinput_dll) {
557 			xinput_dll = LoadLibrary("XInput9_1_0.dll");
558 		}
559 	}
560 
561 	if (!xinput_dll) {
562 		print_verbose("Could not find XInput, using DirectInput only");
563 		return;
564 	}
565 
566 	XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputGetState");
567 	XInputSetState_t set_func = (XInputSetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputSetState");
568 	if (!func || !set_func) {
569 		unload_xinput();
570 		return;
571 	}
572 	xinput_get_state = func;
573 	xinput_set_state = set_func;
574 }
575 
unload_xinput()576 void JoypadWindows::unload_xinput() {
577 
578 	if (xinput_dll) {
579 
580 		FreeLibrary((HMODULE)xinput_dll);
581 	}
582 }
583