joystick.cpp 17 KB

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