joypad_windows.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*************************************************************************/
  2. /* joypad_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "joypad_windows.h"
  31. #include <oleauto.h>
  32. #include <wbemidl.h>
  33. #if defined(__GNUC__)
  34. // Workaround GCC warning from -Wcast-function-type.
  35. #define GetProcAddress (void *)GetProcAddress
  36. #endif
  37. DWORD WINAPI _xinput_get_state(DWORD dwUserIndex, XINPUT_STATE *pState) {
  38. return ERROR_DEVICE_NOT_CONNECTED;
  39. }
  40. DWORD WINAPI _xinput_set_state(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration) {
  41. return ERROR_DEVICE_NOT_CONNECTED;
  42. }
  43. JoypadWindows::JoypadWindows() {
  44. }
  45. JoypadWindows::JoypadWindows(HWND *hwnd) {
  46. input = Input::get_singleton();
  47. hWnd = hwnd;
  48. joypad_count = 0;
  49. dinput = nullptr;
  50. xinput_dll = nullptr;
  51. xinput_get_state = nullptr;
  52. xinput_set_state = nullptr;
  53. load_xinput();
  54. for (int i = 0; i < JOYPADS_MAX; i++) {
  55. attached_joypads[i] = false;
  56. }
  57. HRESULT result = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&dinput, nullptr);
  58. if (result == DI_OK) {
  59. probe_joypads();
  60. } else {
  61. ERR_PRINT("Couldn't initialize DirectInput. Error: " + itos(result));
  62. if (result == DIERR_OUTOFMEMORY) {
  63. ERR_PRINT("The Windows DirectInput subsystem could not allocate sufficient memory.");
  64. ERR_PRINT("Rebooting your PC may solve this issue.");
  65. }
  66. // Ensure dinput is still a nullptr.
  67. dinput = nullptr;
  68. }
  69. }
  70. JoypadWindows::~JoypadWindows() {
  71. close_joypad();
  72. if (dinput) {
  73. dinput->Release();
  74. }
  75. unload_xinput();
  76. }
  77. bool JoypadWindows::have_device(const GUID &p_guid) {
  78. for (int i = 0; i < JOYPADS_MAX; i++) {
  79. if (d_joypads[i].guid == p_guid) {
  80. d_joypads[i].confirmed = true;
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. // adapted from SDL2, works a lot better than the MSDN version
  87. bool JoypadWindows::is_xinput_device(const GUID *p_guid) {
  88. static GUID IID_ValveStreamingGamepad = { MAKELONG(0x28DE, 0x11FF), 0x28DE, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
  89. static GUID IID_X360WiredGamepad = { MAKELONG(0x045E, 0x02A1), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
  90. static GUID IID_X360WirelessGamepad = { MAKELONG(0x045E, 0x028E), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
  91. if (memcmp(p_guid, &IID_ValveStreamingGamepad, sizeof(*p_guid)) == 0 ||
  92. memcmp(p_guid, &IID_X360WiredGamepad, sizeof(*p_guid)) == 0 ||
  93. memcmp(p_guid, &IID_X360WirelessGamepad, sizeof(*p_guid)) == 0)
  94. return true;
  95. PRAWINPUTDEVICELIST dev_list = nullptr;
  96. unsigned int dev_list_count = 0;
  97. if (GetRawInputDeviceList(nullptr, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) {
  98. return false;
  99. }
  100. dev_list = (PRAWINPUTDEVICELIST)memalloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count);
  101. ERR_FAIL_NULL_V_MSG(dev_list, false, "Out of memory.");
  102. if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) {
  103. memfree(dev_list);
  104. return false;
  105. }
  106. for (unsigned int i = 0; i < dev_list_count; i++) {
  107. RID_DEVICE_INFO rdi;
  108. char dev_name[128];
  109. UINT rdiSize = sizeof(rdi);
  110. UINT nameSize = sizeof(dev_name);
  111. rdi.cbSize = rdiSize;
  112. if ((dev_list[i].dwType == RIM_TYPEHID) &&
  113. (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1) &&
  114. (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)p_guid->Data1) &&
  115. (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) &&
  116. (strstr(dev_name, "IG_") != nullptr)) {
  117. memfree(dev_list);
  118. return true;
  119. }
  120. }
  121. memfree(dev_list);
  122. return false;
  123. }
  124. bool JoypadWindows::setup_dinput_joypad(const DIDEVICEINSTANCE *instance) {
  125. ERR_FAIL_NULL_V_MSG(dinput, false, "DirectInput not initialized. Rebooting your PC may solve this issue.");
  126. HRESULT hr;
  127. int num = input->get_unused_joy_id();
  128. if (have_device(instance->guidInstance) || num == -1) {
  129. return false;
  130. }
  131. d_joypads[num] = dinput_gamepad();
  132. dinput_gamepad *joy = &d_joypads[num];
  133. const DWORD devtype = (instance->dwDevType & 0xFF);
  134. if ((devtype != DI8DEVTYPE_JOYSTICK) && (devtype != DI8DEVTYPE_GAMEPAD) && (devtype != DI8DEVTYPE_1STPERSON) && (devtype != DI8DEVTYPE_DRIVING)) {
  135. return false;
  136. }
  137. hr = dinput->CreateDevice(instance->guidInstance, &joy->di_joy, nullptr);
  138. if (FAILED(hr)) {
  139. return false;
  140. }
  141. const GUID &guid = instance->guidProduct;
  142. char uid[128];
  143. ERR_FAIL_COND_V_MSG(memcmp(&guid.Data4[2], "PIDVID", 6), false, "DirectInput device not recognised.");
  144. WORD type = BSWAP16(0x03);
  145. WORD vendor = BSWAP16(LOWORD(guid.Data1));
  146. WORD product = BSWAP16(HIWORD(guid.Data1));
  147. WORD version = 0;
  148. sprintf_s(uid, "%04x%04x%04x%04x%04x%04x%04x%04x", type, 0, vendor, 0, product, 0, version, 0);
  149. id_to_change = num;
  150. slider_count = 0;
  151. joy->di_joy->SetDataFormat(&c_dfDIJoystick2);
  152. joy->di_joy->SetCooperativeLevel(*hWnd, DISCL_FOREGROUND);
  153. joy->di_joy->EnumObjects(objectsCallback, this, 0);
  154. joy->joy_axis.sort();
  155. joy->guid = instance->guidInstance;
  156. input->joy_connection_changed(num, true, instance->tszProductName, uid);
  157. joy->attached = true;
  158. joy->id = num;
  159. attached_joypads[num] = true;
  160. joy->confirmed = true;
  161. joypad_count++;
  162. return true;
  163. }
  164. void JoypadWindows::setup_joypad_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id) {
  165. if (ob->dwType & DIDFT_AXIS) {
  166. HRESULT res;
  167. DIPROPRANGE prop_range;
  168. DIPROPDWORD dilong;
  169. LONG ofs;
  170. if (ob->guidType == GUID_XAxis) {
  171. ofs = DIJOFS_X;
  172. } else if (ob->guidType == GUID_YAxis) {
  173. ofs = DIJOFS_Y;
  174. } else if (ob->guidType == GUID_ZAxis) {
  175. ofs = DIJOFS_Z;
  176. } else if (ob->guidType == GUID_RxAxis) {
  177. ofs = DIJOFS_RX;
  178. } else if (ob->guidType == GUID_RyAxis) {
  179. ofs = DIJOFS_RY;
  180. } else if (ob->guidType == GUID_RzAxis) {
  181. ofs = DIJOFS_RZ;
  182. } else if (ob->guidType == GUID_Slider) {
  183. if (slider_count < 2) {
  184. ofs = DIJOFS_SLIDER(slider_count);
  185. slider_count++;
  186. } else {
  187. return;
  188. }
  189. } else {
  190. return;
  191. }
  192. prop_range.diph.dwSize = sizeof(DIPROPRANGE);
  193. prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  194. prop_range.diph.dwObj = ob->dwType;
  195. prop_range.diph.dwHow = DIPH_BYID;
  196. prop_range.lMin = -MAX_JOY_AXIS;
  197. prop_range.lMax = +MAX_JOY_AXIS;
  198. dinput_gamepad &joy = d_joypads[p_joy_id];
  199. res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_RANGE, &prop_range.diph);
  200. if (FAILED(res)) {
  201. return;
  202. }
  203. dilong.diph.dwSize = sizeof(dilong);
  204. dilong.diph.dwHeaderSize = sizeof(dilong.diph);
  205. dilong.diph.dwObj = ob->dwType;
  206. dilong.diph.dwHow = DIPH_BYID;
  207. dilong.dwData = 0;
  208. res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_DEADZONE, &dilong.diph);
  209. if (FAILED(res)) {
  210. return;
  211. }
  212. joy.joy_axis.push_back(ofs);
  213. }
  214. }
  215. BOOL CALLBACK JoypadWindows::enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context) {
  216. JoypadWindows *self = static_cast<JoypadWindows *>(p_context);
  217. if (self->is_xinput_device(&p_instance->guidProduct)) {
  218. return DIENUM_CONTINUE;
  219. }
  220. self->setup_dinput_joypad(p_instance);
  221. return DIENUM_CONTINUE;
  222. }
  223. BOOL CALLBACK JoypadWindows::objectsCallback(const DIDEVICEOBJECTINSTANCE *p_instance, void *p_context) {
  224. JoypadWindows *self = static_cast<JoypadWindows *>(p_context);
  225. self->setup_joypad_object(p_instance, self->id_to_change);
  226. return DIENUM_CONTINUE;
  227. }
  228. void JoypadWindows::close_joypad(int id) {
  229. if (id == -1) {
  230. for (int i = 0; i < JOYPADS_MAX; i++) {
  231. close_joypad(i);
  232. }
  233. return;
  234. }
  235. if (!d_joypads[id].attached) {
  236. return;
  237. }
  238. d_joypads[id].di_joy->Unacquire();
  239. d_joypads[id].di_joy->Release();
  240. d_joypads[id].attached = false;
  241. attached_joypads[d_joypads[id].id] = false;
  242. d_joypads[id].guid.Data1 = d_joypads[id].guid.Data2 = d_joypads[id].guid.Data3 = 0;
  243. input->joy_connection_changed(d_joypads[id].id, false, "");
  244. joypad_count--;
  245. }
  246. void JoypadWindows::probe_joypads() {
  247. ERR_FAIL_NULL_MSG(dinput, "DirectInput not initialized. Rebooting your PC may solve this issue.");
  248. DWORD dwResult;
  249. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
  250. ZeroMemory(&x_joypads[i].state, sizeof(XINPUT_STATE));
  251. dwResult = xinput_get_state(i, &x_joypads[i].state);
  252. if (dwResult == ERROR_SUCCESS) {
  253. int id = input->get_unused_joy_id();
  254. if (id != -1 && !x_joypads[i].attached) {
  255. x_joypads[i].attached = true;
  256. x_joypads[i].id = id;
  257. x_joypads[i].ff_timestamp = 0;
  258. x_joypads[i].ff_end_timestamp = 0;
  259. x_joypads[i].vibrating = false;
  260. attached_joypads[id] = true;
  261. input->joy_connection_changed(id, true, "XInput Gamepad", "__XINPUT_DEVICE__");
  262. }
  263. } else if (x_joypads[i].attached) {
  264. x_joypads[i].attached = false;
  265. attached_joypads[x_joypads[i].id] = false;
  266. input->joy_connection_changed(x_joypads[i].id, false, "");
  267. }
  268. }
  269. for (int i = 0; i < joypad_count; i++) {
  270. d_joypads[i].confirmed = false;
  271. }
  272. dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY);
  273. for (int i = 0; i < joypad_count; i++) {
  274. if (!d_joypads[i].confirmed) {
  275. close_joypad(i);
  276. }
  277. }
  278. }
  279. void JoypadWindows::process_joypads() {
  280. HRESULT hr;
  281. for (int i = 0; i < XUSER_MAX_COUNT; i++) {
  282. xinput_gamepad &joy = x_joypads[i];
  283. if (!joy.attached) {
  284. continue;
  285. }
  286. ZeroMemory(&joy.state, sizeof(XINPUT_STATE));
  287. xinput_get_state(i, &joy.state);
  288. if (joy.state.dwPacketNumber != joy.last_packet) {
  289. int button_mask = XINPUT_GAMEPAD_DPAD_UP;
  290. for (int j = 0; j <= 16; j++) {
  291. input->joy_button(joy.id, (JoyButton)j, joy.state.Gamepad.wButtons & button_mask);
  292. button_mask = button_mask * 2;
  293. }
  294. input->joy_axis(joy.id, JoyAxis::LEFT_X, axis_correct(joy.state.Gamepad.sThumbLX, true));
  295. input->joy_axis(joy.id, JoyAxis::LEFT_Y, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true));
  296. input->joy_axis(joy.id, JoyAxis::RIGHT_X, axis_correct(joy.state.Gamepad.sThumbRX, true));
  297. input->joy_axis(joy.id, JoyAxis::RIGHT_Y, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true));
  298. input->joy_axis(joy.id, JoyAxis::TRIGGER_LEFT, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true));
  299. input->joy_axis(joy.id, JoyAxis::TRIGGER_RIGHT, axis_correct(joy.state.Gamepad.bRightTrigger, true, true));
  300. joy.last_packet = joy.state.dwPacketNumber;
  301. }
  302. uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
  303. if (timestamp > joy.ff_timestamp) {
  304. Vector2 strength = input->get_joy_vibration_strength(joy.id);
  305. float duration = input->get_joy_vibration_duration(joy.id);
  306. if (strength.x == 0 && strength.y == 0) {
  307. joypad_vibration_stop_xinput(i, timestamp);
  308. } else {
  309. joypad_vibration_start_xinput(i, strength.x, strength.y, duration, timestamp);
  310. }
  311. } else if (joy.vibrating && joy.ff_end_timestamp != 0) {
  312. uint64_t current_time = OS::get_singleton()->get_ticks_usec();
  313. if (current_time >= joy.ff_end_timestamp) {
  314. joypad_vibration_stop_xinput(i, current_time);
  315. }
  316. }
  317. }
  318. for (int i = 0; i < JOYPADS_MAX; i++) {
  319. dinput_gamepad *joy = &d_joypads[i];
  320. if (!joy->attached) {
  321. continue;
  322. }
  323. DIJOYSTATE2 js;
  324. hr = joy->di_joy->Poll();
  325. if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
  326. IDirectInputDevice8_Acquire(joy->di_joy);
  327. joy->di_joy->Poll();
  328. }
  329. hr = joy->di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js);
  330. if (FAILED(hr)) {
  331. continue;
  332. }
  333. post_hat(joy->id, js.rgdwPOV[0]);
  334. for (int j = 0; j < 128; j++) {
  335. if (js.rgbButtons[j] & 0x80) {
  336. if (!joy->last_buttons[j]) {
  337. input->joy_button(joy->id, (JoyButton)j, true);
  338. joy->last_buttons[j] = true;
  339. }
  340. } else {
  341. if (joy->last_buttons[j]) {
  342. input->joy_button(joy->id, (JoyButton)j, false);
  343. joy->last_buttons[j] = false;
  344. }
  345. }
  346. }
  347. // on mingw, these constants are not constants
  348. int count = 8;
  349. const LONG axes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ, (LONG)DIJOFS_SLIDER(0), (LONG)DIJOFS_SLIDER(1) };
  350. int values[] = { js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz, js.rglSlider[0], js.rglSlider[1] };
  351. for (int j = 0; j < joy->joy_axis.size(); j++) {
  352. for (int k = 0; k < count; k++) {
  353. if (joy->joy_axis[j] == axes[k]) {
  354. input->joy_axis(joy->id, (JoyAxis)j, axis_correct(values[k]));
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. return;
  361. }
  362. void JoypadWindows::post_hat(int p_device, DWORD p_dpad) {
  363. HatMask dpad_val = (HatMask)0;
  364. // Should be -1 when centered, but according to docs:
  365. // "Some drivers report the centered position of the POV indicator as 65,535. Determine whether the indicator is centered as follows:
  366. // BOOL POVCentered = (LOWORD(dwPOV) == 0xFFFF);"
  367. // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee416628(v%3Dvs.85)#remarks
  368. if (LOWORD(p_dpad) == 0xFFFF) {
  369. dpad_val = (HatMask)HatMask::CENTER;
  370. }
  371. if (p_dpad == 0) {
  372. dpad_val = (HatMask)HatMask::UP;
  373. } else if (p_dpad == 4500) {
  374. dpad_val = (HatMask)(HatMask::UP | HatMask::RIGHT);
  375. } else if (p_dpad == 9000) {
  376. dpad_val = (HatMask)HatMask::RIGHT;
  377. } else if (p_dpad == 13500) {
  378. dpad_val = (HatMask)(HatMask::RIGHT | HatMask::DOWN);
  379. } else if (p_dpad == 18000) {
  380. dpad_val = (HatMask)HatMask::DOWN;
  381. } else if (p_dpad == 22500) {
  382. dpad_val = (HatMask)(HatMask::DOWN | HatMask::LEFT);
  383. } else if (p_dpad == 27000) {
  384. dpad_val = (HatMask)HatMask::LEFT;
  385. } else if (p_dpad == 31500) {
  386. dpad_val = (HatMask)(HatMask::LEFT | HatMask::UP);
  387. }
  388. input->joy_hat(p_device, dpad_val);
  389. }
  390. float JoypadWindows::axis_correct(int p_val, bool p_xinput, bool p_trigger, bool p_negate) const {
  391. if (Math::abs(p_val) < MIN_JOY_AXIS) {
  392. return p_trigger ? -1.0f : 0.0f;
  393. }
  394. if (!p_xinput) {
  395. return (float)p_val / MAX_JOY_AXIS;
  396. }
  397. if (p_trigger) {
  398. // Convert to a value between -1.0f and 1.0f.
  399. return 2.0f * p_val / MAX_TRIGGER - 1.0f;
  400. }
  401. float value;
  402. if (p_val < 0) {
  403. value = (float)p_val / MAX_JOY_AXIS;
  404. } else {
  405. value = (float)p_val / (MAX_JOY_AXIS - 1);
  406. }
  407. if (p_negate) {
  408. value = -value;
  409. }
  410. return value;
  411. }
  412. void JoypadWindows::joypad_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
  413. xinput_gamepad &joy = x_joypads[p_device];
  414. if (joy.attached) {
  415. XINPUT_VIBRATION effect;
  416. effect.wLeftMotorSpeed = (65535 * p_strong_magnitude);
  417. effect.wRightMotorSpeed = (65535 * p_weak_magnitude);
  418. if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
  419. joy.ff_timestamp = p_timestamp;
  420. joy.ff_end_timestamp = p_duration == 0 ? 0 : p_timestamp + (uint64_t)(p_duration * 1000000.0);
  421. joy.vibrating = true;
  422. }
  423. }
  424. }
  425. void JoypadWindows::joypad_vibration_stop_xinput(int p_device, uint64_t p_timestamp) {
  426. xinput_gamepad &joy = x_joypads[p_device];
  427. if (joy.attached) {
  428. XINPUT_VIBRATION effect;
  429. effect.wLeftMotorSpeed = 0;
  430. effect.wRightMotorSpeed = 0;
  431. if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
  432. joy.ff_timestamp = p_timestamp;
  433. joy.vibrating = false;
  434. }
  435. }
  436. }
  437. void JoypadWindows::load_xinput() {
  438. xinput_get_state = &_xinput_get_state;
  439. xinput_set_state = &_xinput_set_state;
  440. xinput_dll = LoadLibrary("XInput1_4.dll");
  441. if (!xinput_dll) {
  442. xinput_dll = LoadLibrary("XInput1_3.dll");
  443. if (!xinput_dll) {
  444. xinput_dll = LoadLibrary("XInput9_1_0.dll");
  445. }
  446. }
  447. if (!xinput_dll) {
  448. print_verbose("Could not find XInput, using DirectInput only");
  449. return;
  450. }
  451. XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputGetState");
  452. XInputSetState_t set_func = (XInputSetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputSetState");
  453. if (!func || !set_func) {
  454. unload_xinput();
  455. return;
  456. }
  457. xinput_get_state = func;
  458. xinput_set_state = set_func;
  459. }
  460. void JoypadWindows::unload_xinput() {
  461. if (xinput_dll) {
  462. FreeLibrary((HMODULE)xinput_dll);
  463. }
  464. }