joypad_windows.cpp 19 KB

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