joystick.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*************************************************************************/
  2. /* joystick.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. //author: Andreas Haas <hondres, [email protected]>
  30. #include "joystick.h"
  31. #include <iostream>
  32. #include <wbemidl.h>
  33. #include <oleauto.h>
  34. #ifndef __GNUC__
  35. #define __builtin_bswap32 _byteswap_ulong
  36. #endif
  37. DWORD WINAPI _xinput_get_state(DWORD dwUserIndex, XINPUT_STATE* pState) { return ERROR_DEVICE_NOT_CONNECTED; }
  38. DWORD WINAPI _xinput_set_state(DWORD dwUserIndex, XINPUT_VIBRATION* pVibration) { return ERROR_DEVICE_NOT_CONNECTED; }
  39. joystick_windows::joystick_windows() {
  40. }
  41. joystick_windows::joystick_windows(InputDefault* _input, HWND* hwnd) {
  42. input = _input;
  43. hWnd = hwnd;
  44. joystick_count = 0;
  45. dinput = NULL;
  46. xinput_dll = NULL;
  47. xinput_get_state = NULL;
  48. xinput_set_state = NULL;
  49. load_xinput();
  50. for (int i = 0; i < JOYSTICKS_MAX; i++)
  51. attached_joysticks[i] = false;
  52. HRESULT result;
  53. result = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL);
  54. if (FAILED(result)) {
  55. printf("failed init DINPUT: %ld\n", result);
  56. }
  57. probe_joysticks();
  58. }
  59. joystick_windows::~joystick_windows() {
  60. close_joystick();
  61. dinput->Release();
  62. unload_xinput();
  63. }
  64. bool joystick_windows::have_device(const GUID &p_guid) {
  65. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  66. if (d_joysticks[i].guid == p_guid) {
  67. d_joysticks[i].confirmed = true;
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. int joystick_windows::check_free_joy_slot() const {
  74. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  75. if (!attached_joysticks[i])
  76. return i;
  77. }
  78. return -1;
  79. }
  80. // adapted from SDL2, works a lot better than the MSDN version
  81. bool joystick_windows::is_xinput_device(const GUID *p_guid) {
  82. static GUID IID_ValveStreamingGamepad = { MAKELONG(0x28DE, 0x11FF), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
  83. static GUID IID_X360WiredGamepad = { MAKELONG(0x045E, 0x02A1), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
  84. static GUID IID_X360WirelessGamepad = { MAKELONG(0x045E, 0x028E), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
  85. if (p_guid == &IID_ValveStreamingGamepad || p_guid == &IID_X360WiredGamepad || p_guid == &IID_X360WirelessGamepad)
  86. return true;
  87. PRAWINPUTDEVICELIST dev_list = NULL;
  88. unsigned int dev_list_count = 0;
  89. if (GetRawInputDeviceList(NULL, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) {
  90. return false;
  91. }
  92. dev_list = (PRAWINPUTDEVICELIST) malloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count);
  93. if (!dev_list) return false;
  94. if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) {
  95. free(dev_list);
  96. return false;
  97. }
  98. for (int i = 0; i < dev_list_count; i++) {
  99. RID_DEVICE_INFO rdi;
  100. char dev_name[128];
  101. UINT rdiSize = sizeof(rdi);
  102. UINT nameSize = sizeof(dev_name);
  103. rdi.cbSize = rdiSize;
  104. if ( (dev_list[i].dwType == RIM_TYPEHID) &&
  105. (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1) &&
  106. (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)p_guid->Data1) &&
  107. (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) &&
  108. (strstr(dev_name, "IG_") != NULL)) {
  109. free(dev_list);
  110. return true;
  111. }
  112. }
  113. free(dev_list);
  114. return false;
  115. }
  116. bool joystick_windows::setup_dinput_joystick(const DIDEVICEINSTANCE* instance) {
  117. HRESULT hr;
  118. int num = check_free_joy_slot();
  119. if (have_device(instance->guidInstance) || num == -1)
  120. return false;
  121. d_joysticks[joystick_count] = dinput_gamepad();
  122. dinput_gamepad* joy = &d_joysticks[joystick_count];
  123. const DWORD devtype = (instance->dwDevType & 0xFF);
  124. if ((devtype != DI8DEVTYPE_JOYSTICK) && (devtype != DI8DEVTYPE_GAMEPAD) && (devtype != DI8DEVTYPE_1STPERSON)) {
  125. //printf("ignore device %s, type %x\n", instance->tszProductName, devtype);
  126. return false;
  127. }
  128. hr = dinput->CreateDevice(instance->guidInstance, &joy->di_joy, NULL);
  129. if (FAILED(hr)) {
  130. //std::wcout << "failed to create device: " << instance->tszProductName << std::endl;
  131. return false;
  132. }
  133. const GUID &guid = instance->guidProduct;
  134. char uid[128];
  135. sprintf(uid, "%08lx%04hx%04hx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
  136. __builtin_bswap32(guid.Data1), guid.Data2, guid.Data3,
  137. guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
  138. guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
  139. id_to_change = joystick_count;
  140. joy->di_joy->SetDataFormat(&c_dfDIJoystick2);
  141. joy->di_joy->SetCooperativeLevel(*hWnd, DISCL_FOREGROUND);
  142. joy->di_joy->EnumObjects(objectsCallback, this, NULL);
  143. joy->joy_axis.sort();
  144. joy->guid = instance->guidInstance;
  145. input->joy_connection_changed(num, true, instance->tszProductName, uid);
  146. joy->attached = true;
  147. joy->id = num;
  148. attached_joysticks[num] = true;
  149. joy->confirmed = true;
  150. joystick_count++;
  151. return true;
  152. }
  153. void joystick_windows::setup_joystick_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id) {
  154. if (ob->dwType & DIDFT_AXIS) {
  155. HRESULT res;
  156. DIPROPRANGE prop_range;
  157. DIPROPDWORD dilong;
  158. DWORD ofs;
  159. if (ob->guidType == GUID_XAxis)
  160. ofs = DIJOFS_X;
  161. else if (ob->guidType == GUID_YAxis)
  162. ofs = DIJOFS_Y;
  163. else if (ob->guidType == GUID_ZAxis)
  164. ofs = DIJOFS_Z;
  165. else if (ob->guidType == GUID_RxAxis)
  166. ofs = DIJOFS_RX;
  167. else if (ob->guidType == GUID_RyAxis)
  168. ofs = DIJOFS_RY;
  169. else if (ob->guidType == GUID_RzAxis)
  170. ofs = DIJOFS_RZ;
  171. else if (ob->guidType == GUID_Slider)
  172. ofs = DIJOFS_SLIDER(0);
  173. else
  174. return;
  175. prop_range.diph.dwSize = sizeof(DIPROPRANGE);
  176. prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  177. prop_range.diph.dwObj = ob->dwType;
  178. prop_range.diph.dwHow = DIPH_BYID;
  179. prop_range.lMin = -MAX_JOY_AXIS;
  180. prop_range.lMax = +MAX_JOY_AXIS;
  181. dinput_gamepad &joy = d_joysticks[p_joy_id];
  182. res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_RANGE, &prop_range.diph);
  183. if (FAILED(res))
  184. return;
  185. dilong.diph.dwSize = sizeof(dilong);
  186. dilong.diph.dwHeaderSize = sizeof(dilong.diph);
  187. dilong.diph.dwObj = ob->dwType;
  188. dilong.diph.dwHow = DIPH_BYID;
  189. dilong.dwData = 0;
  190. res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_DEADZONE, &dilong.diph);
  191. if (FAILED(res))
  192. return;
  193. joy.joy_axis.push_back(ofs);
  194. }
  195. }
  196. BOOL CALLBACK joystick_windows::enumCallback(const DIDEVICEINSTANCE* instance, void* pContext) {
  197. joystick_windows* self = (joystick_windows*)pContext;
  198. if (self->is_xinput_device(&instance->guidProduct)) {;
  199. return DIENUM_CONTINUE;
  200. }
  201. self->setup_dinput_joystick(instance);
  202. return DIENUM_CONTINUE;
  203. }
  204. BOOL CALLBACK joystick_windows::objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context) {
  205. joystick_windows* self = (joystick_windows*)context;
  206. self->setup_joystick_object(instance, self->id_to_change);
  207. return DIENUM_CONTINUE;
  208. }
  209. void joystick_windows::close_joystick(int id) {
  210. if (id == -1) {
  211. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  212. close_joystick(i);
  213. }
  214. return;
  215. }
  216. if (!d_joysticks[id].attached) return;
  217. d_joysticks[id].di_joy->Unacquire();
  218. d_joysticks[id].di_joy->Release();
  219. d_joysticks[id].attached = false;
  220. attached_joysticks[d_joysticks[id].id] = false;
  221. d_joysticks[id].guid.Data1 = d_joysticks[id].guid.Data2 = d_joysticks[id].guid.Data3 = 0;
  222. input->joy_connection_changed(d_joysticks[id].id, false, "");
  223. joystick_count--;
  224. }
  225. void joystick_windows::probe_joysticks() {
  226. DWORD dwResult;
  227. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
  228. ZeroMemory(&x_joysticks[i].state, sizeof(XINPUT_STATE));
  229. dwResult = xinput_get_state(i, &x_joysticks[i].state);
  230. if ( dwResult == ERROR_SUCCESS) {
  231. int id = check_free_joy_slot();
  232. if (id != -1 && !x_joysticks[i].attached) {
  233. x_joysticks[i].attached = true;
  234. x_joysticks[i].id = id;
  235. x_joysticks[i].ff_timestamp = 0;
  236. x_joysticks[i].ff_end_timestamp = 0;
  237. x_joysticks[i].vibrating = false;
  238. attached_joysticks[id] = true;
  239. input->joy_connection_changed(id, true, "XInput Gamepad","__XINPUT_DEVICE__");
  240. }
  241. }
  242. else if (x_joysticks[i].attached) {
  243. x_joysticks[i].attached = false;
  244. attached_joysticks[x_joysticks[i].id] = false;
  245. input->joy_connection_changed(x_joysticks[i].id, false, "");
  246. }
  247. }
  248. for (int i = 0; i < joystick_count; i++) {
  249. d_joysticks[i].confirmed = false;
  250. }
  251. dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY);
  252. for (int i = 0; i < joystick_count; i++) {
  253. if (!d_joysticks[i].confirmed) {
  254. close_joystick(i);
  255. }
  256. }
  257. }
  258. unsigned int joystick_windows::process_joysticks(unsigned int p_last_id) {
  259. HRESULT hr;
  260. for (int i = 0; i < XUSER_MAX_COUNT; i++) {
  261. xinput_gamepad &joy = x_joysticks[i];
  262. if (!joy.attached) {
  263. continue;
  264. }
  265. ZeroMemory(&joy.state, sizeof(XINPUT_STATE));
  266. xinput_get_state(i, &joy.state);
  267. if (joy.state.dwPacketNumber != joy.last_packet) {
  268. int button_mask = XINPUT_GAMEPAD_DPAD_UP;
  269. for (int i = 0; i <= 16; i++) {
  270. p_last_id = input->joy_button(p_last_id, joy.id, i, joy.state.Gamepad.wButtons & button_mask);
  271. button_mask = button_mask * 2;
  272. }
  273. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_0, axis_correct(joy.state.Gamepad.sThumbLX, true));
  274. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_1, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true));
  275. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_2, axis_correct(joy.state.Gamepad.sThumbRX, true));
  276. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_3, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true));
  277. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_4, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true));
  278. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_5, axis_correct(joy.state.Gamepad.bRightTrigger, true, true));
  279. joy.last_packet = joy.state.dwPacketNumber;
  280. }
  281. uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
  282. if (timestamp > joy.ff_timestamp) {
  283. Vector2 strength = input->get_joy_vibration_strength(joy.id);
  284. float duration = input->get_joy_vibration_duration(joy.id);
  285. if (strength.x == 0 && strength.y == 0) {
  286. joystick_vibration_stop_xinput(i, timestamp);
  287. } else {
  288. joystick_vibration_start_xinput(i, strength.x, strength.y, duration, timestamp);
  289. }
  290. } else if (joy.vibrating && joy.ff_end_timestamp != 0) {
  291. uint64_t current_time = OS::get_singleton()->get_ticks_usec();
  292. if (current_time >= joy.ff_end_timestamp)
  293. joystick_vibration_stop_xinput(i, current_time);
  294. }
  295. }
  296. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  297. dinput_gamepad* joy = &d_joysticks[i];
  298. if (!joy->attached)
  299. continue;
  300. DIJOYSTATE2 js;
  301. hr = joy->di_joy->Poll();
  302. if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
  303. IDirectInputDevice8_Acquire(joy->di_joy);
  304. joy->di_joy->Poll();
  305. }
  306. if (FAILED(hr = joy->di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js))) {
  307. //printf("failed to read joy #%d\n", i);
  308. continue;
  309. }
  310. p_last_id = post_hat(p_last_id, joy->id, js.rgdwPOV[0]);
  311. for (int j = 0; j < 128; j++) {
  312. if (js.rgbButtons[j] & 0x80) {
  313. if (!joy->last_buttons[j]) {
  314. p_last_id = input->joy_button(p_last_id, joy->id, j, true);
  315. joy->last_buttons[j] = true;
  316. }
  317. }
  318. else {
  319. if (joy->last_buttons[j]) {
  320. p_last_id = input->joy_button(p_last_id, joy->id, j, false);
  321. joy->last_buttons[j] = false;
  322. }
  323. }
  324. }
  325. // on mingw, these constants are not constants
  326. int count = 6;
  327. int axes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ };
  328. int values[] = { js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz };
  329. for (int j = 0; j < joy->joy_axis.size(); j++) {
  330. for (int k=0; k<count; k++) {
  331. if (joy->joy_axis[j] == axes[k]) {
  332. p_last_id = input->joy_axis(p_last_id, joy->id, j, axis_correct(values[k]));
  333. break;
  334. };
  335. };
  336. };
  337. }
  338. return p_last_id;
  339. }
  340. unsigned int joystick_windows::post_hat(unsigned int p_last_id, int p_device, DWORD p_dpad) {
  341. int dpad_val = 0;
  342. if (p_dpad == -1) {
  343. dpad_val = InputDefault::HAT_MASK_CENTER;
  344. }
  345. if (p_dpad == 0) {
  346. dpad_val = InputDefault::HAT_MASK_UP;
  347. }
  348. else if (p_dpad == 4500) {
  349. dpad_val = (InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT);
  350. }
  351. else if (p_dpad == 9000) {
  352. dpad_val = InputDefault::HAT_MASK_RIGHT;
  353. }
  354. else if (p_dpad == 13500) {
  355. dpad_val = (InputDefault::HAT_MASK_RIGHT | InputDefault::HAT_MASK_DOWN);
  356. }
  357. else if (p_dpad == 18000) {
  358. dpad_val = InputDefault::HAT_MASK_DOWN;
  359. }
  360. else if (p_dpad == 22500) {
  361. dpad_val = (InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT);
  362. }
  363. else if (p_dpad == 27000) {
  364. dpad_val = InputDefault::HAT_MASK_LEFT;
  365. }
  366. else if (p_dpad == 31500) {
  367. dpad_val = (InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_UP);
  368. }
  369. return input->joy_hat(p_last_id, p_device, dpad_val);
  370. };
  371. InputDefault::JoyAxis joystick_windows::axis_correct(int p_val, bool p_xinput, bool p_trigger, bool p_negate) const {
  372. InputDefault::JoyAxis jx;
  373. if (Math::abs(p_val) < MIN_JOY_AXIS) {
  374. jx.min = p_trigger ? 0 : -1;
  375. jx.value = 0.0f;
  376. return jx;
  377. }
  378. if (p_xinput) {
  379. if (p_trigger) {
  380. jx.min = 0;
  381. jx.value = (float)p_val / MAX_TRIGGER;
  382. return jx;
  383. }
  384. jx.min = -1;
  385. if (p_val < 0) {
  386. jx.value = (float)p_val / MAX_JOY_AXIS;
  387. }
  388. else {
  389. jx.value = (float)p_val / (MAX_JOY_AXIS - 1);
  390. }
  391. if (p_negate) {
  392. jx.value = -jx.value;
  393. }
  394. return jx;
  395. }
  396. jx.min = -1;
  397. jx.value = (float)p_val / MAX_JOY_AXIS;
  398. return jx;
  399. }
  400. void joystick_windows::joystick_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
  401. xinput_gamepad &joy = x_joysticks[p_device];
  402. if (joy.attached) {
  403. XINPUT_VIBRATION effect;
  404. effect.wLeftMotorSpeed = (65535 * p_strong_magnitude);
  405. effect.wRightMotorSpeed = (65535 * p_weak_magnitude);
  406. if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
  407. joy.ff_timestamp = p_timestamp;
  408. joy.ff_end_timestamp = p_duration == 0 ? 0 : p_timestamp + (uint64_t)(p_duration * 1000000.0);
  409. joy.vibrating = true;
  410. }
  411. }
  412. }
  413. void joystick_windows::joystick_vibration_stop_xinput(int p_device, uint64_t p_timestamp) {
  414. xinput_gamepad &joy = x_joysticks[p_device];
  415. if (joy.attached) {
  416. XINPUT_VIBRATION effect;
  417. effect.wLeftMotorSpeed = 0;
  418. effect.wRightMotorSpeed = 0;
  419. if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
  420. joy.ff_timestamp = p_timestamp;
  421. joy.vibrating = false;
  422. }
  423. }
  424. }
  425. void joystick_windows::load_xinput() {
  426. xinput_get_state = &_xinput_get_state;
  427. xinput_set_state = &_xinput_set_state;
  428. xinput_dll = LoadLibrary( "XInput1_4.dll" );
  429. if (!xinput_dll) {
  430. xinput_dll = LoadLibrary("XInput1_3.dll");
  431. if (!xinput_dll) {
  432. xinput_dll = LoadLibrary("XInput9_1_0.dll");
  433. }
  434. }
  435. if (!xinput_dll) {
  436. if (OS::get_singleton()->is_stdout_verbose()) {
  437. print_line("Could not find XInput, using DirectInput only");
  438. }
  439. return;
  440. }
  441. XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputGetState");
  442. XInputSetState_t set_func = (XInputSetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputSetState");
  443. if (!func || !set_func) {
  444. unload_xinput();
  445. return;
  446. }
  447. xinput_get_state = func;
  448. xinput_set_state = set_func;
  449. }
  450. void joystick_windows::unload_xinput() {
  451. if (xinput_dll) {
  452. FreeLibrary((HMODULE)xinput_dll);
  453. }
  454. }