joystick.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*************************************************************************/
  2. /* joystick.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. joystick_windows::joystick_windows() {
  39. }
  40. joystick_windows::joystick_windows(InputDefault* _input, HWND* hwnd) {
  41. input = _input;
  42. hWnd = hwnd;
  43. joystick_count = 0;
  44. dinput = NULL;
  45. xinput_dll = NULL;
  46. xinput_get_state = NULL;
  47. load_xinput();
  48. for (int i = 0; i < JOYSTICKS_MAX; i++)
  49. attached_joysticks[i] = false;
  50. HRESULT result;
  51. result = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dinput, NULL);
  52. if (FAILED(result)) {
  53. printf("failed init DINPUT: %ld\n", result);
  54. }
  55. probe_joysticks();
  56. }
  57. joystick_windows::~joystick_windows() {
  58. close_joystick();
  59. dinput->Release();
  60. unload_xinput();
  61. }
  62. bool joystick_windows::have_device(const GUID &p_guid) {
  63. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  64. if (d_joysticks[i].guid == p_guid) {
  65. d_joysticks[i].confirmed = true;
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. int joystick_windows::check_free_joy_slot() const {
  72. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  73. if (!attached_joysticks[i])
  74. return i;
  75. }
  76. return -1;
  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 = check_free_joy_slot();
  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(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 = check_free_joy_slot();
  230. if (id != -1 && !x_joysticks[i].attached) {
  231. x_joysticks[i].attached = true;
  232. x_joysticks[i].id = id;
  233. attached_joysticks[id] = true;
  234. input->joy_connection_changed(id, true, "XInput Gamepad","__XINPUT_DEVICE__");
  235. }
  236. }
  237. else if (x_joysticks[i].attached) {
  238. x_joysticks[i].attached = false;
  239. attached_joysticks[x_joysticks[i].id] = false;
  240. input->joy_connection_changed(x_joysticks[i].id, false, "");
  241. }
  242. }
  243. for (int i = 0; i < joystick_count; i++) {
  244. d_joysticks[i].confirmed = false;
  245. }
  246. dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY);
  247. for (int i = 0; i < joystick_count; i++) {
  248. if (!d_joysticks[i].confirmed) {
  249. close_joystick(i);
  250. }
  251. }
  252. }
  253. unsigned int joystick_windows::process_joysticks(unsigned int p_last_id) {
  254. HRESULT hr;
  255. for (int i = 0; i < XUSER_MAX_COUNT; i++) {
  256. xinput_gamepad &joy = x_joysticks[i];
  257. if (!joy.attached) {
  258. continue;
  259. }
  260. ZeroMemory(&joy.state, sizeof(XINPUT_STATE));
  261. xinput_get_state(i, &joy.state);
  262. if (joy.state.dwPacketNumber != joy.last_packet) {
  263. int button_mask = XINPUT_GAMEPAD_DPAD_UP;
  264. for (int i = 0; i <= 16; i++) {
  265. p_last_id = input->joy_button(p_last_id, joy.id, i, joy.state.Gamepad.wButtons & button_mask);
  266. button_mask = button_mask * 2;
  267. }
  268. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_0, axis_correct(joy.state.Gamepad.sThumbLX, true));
  269. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_1, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true));
  270. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_2, axis_correct(joy.state.Gamepad.sThumbRX, true));
  271. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_3, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true));
  272. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_4, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true));
  273. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_5, axis_correct(joy.state.Gamepad.bRightTrigger, true, true));
  274. joy.last_packet = joy.state.dwPacketNumber;
  275. }
  276. }
  277. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  278. dinput_gamepad* joy = &d_joysticks[i];
  279. if (!joy->attached)
  280. continue;
  281. DIJOYSTATE2 js;
  282. hr = joy->di_joy->Poll();
  283. if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
  284. IDirectInputDevice8_Acquire(joy->di_joy);
  285. joy->di_joy->Poll();
  286. }
  287. if (FAILED(hr = joy->di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js))) {
  288. //printf("failed to read joy #%d\n", i);
  289. continue;
  290. }
  291. p_last_id = post_hat(p_last_id, joy->id, js.rgdwPOV[0]);
  292. for (int j = 0; j < 128; j++) {
  293. if (js.rgbButtons[j] & 0x80) {
  294. if (!joy->last_buttons[j]) {
  295. p_last_id = input->joy_button(p_last_id, joy->id, j, true);
  296. joy->last_buttons[j] = true;
  297. }
  298. }
  299. else {
  300. if (joy->last_buttons[j]) {
  301. p_last_id = input->joy_button(p_last_id, joy->id, j, false);
  302. joy->last_buttons[j] = false;
  303. }
  304. }
  305. }
  306. // on mingw, these constants are not constants
  307. int count = 6;
  308. int axes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ };
  309. int values[] = { js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz };
  310. for (int j = 0; j < joy->joy_axis.size(); j++) {
  311. for (int k=0; k<count; k++) {
  312. if (joy->joy_axis[j] == axes[k]) {
  313. p_last_id = input->joy_axis(p_last_id, joy->id, j, axis_correct(values[k]));
  314. break;
  315. };
  316. };
  317. };
  318. }
  319. return p_last_id;
  320. }
  321. unsigned int joystick_windows::post_hat(unsigned int p_last_id, int p_device, DWORD p_dpad) {
  322. int dpad_val = 0;
  323. if (p_dpad == -1) {
  324. dpad_val = InputDefault::HAT_MASK_CENTER;
  325. }
  326. if (p_dpad == 0) {
  327. dpad_val = InputDefault::HAT_MASK_UP;
  328. }
  329. else if (p_dpad == 4500) {
  330. dpad_val = (InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT);
  331. }
  332. else if (p_dpad == 9000) {
  333. dpad_val = InputDefault::HAT_MASK_RIGHT;
  334. }
  335. else if (p_dpad == 13500) {
  336. dpad_val = (InputDefault::HAT_MASK_RIGHT | InputDefault::HAT_MASK_DOWN);
  337. }
  338. else if (p_dpad == 18000) {
  339. dpad_val = InputDefault::HAT_MASK_DOWN;
  340. }
  341. else if (p_dpad == 22500) {
  342. dpad_val = (InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT);
  343. }
  344. else if (p_dpad == 27000) {
  345. dpad_val = InputDefault::HAT_MASK_LEFT;
  346. }
  347. else if (p_dpad == 31500) {
  348. dpad_val = (InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_UP);
  349. }
  350. return input->joy_hat(p_last_id, p_device, dpad_val);
  351. };
  352. InputDefault::JoyAxis joystick_windows::axis_correct(int p_val, bool p_xinput, bool p_trigger, bool p_negate) const {
  353. InputDefault::JoyAxis jx;
  354. if (Math::abs(p_val) < MIN_JOY_AXIS) {
  355. jx.min = -1;
  356. jx.value = 0.0f;
  357. return jx;
  358. }
  359. if (p_xinput) {
  360. if (p_trigger) {
  361. jx.min = 0;
  362. jx.value = (float)p_val / MAX_TRIGGER;
  363. return jx;
  364. }
  365. jx.min = -1;
  366. if (p_val < 0) {
  367. jx.value = (float)p_val / MAX_JOY_AXIS;
  368. }
  369. else {
  370. jx.value = (float)p_val / (MAX_JOY_AXIS - 1);
  371. }
  372. if (p_negate) {
  373. jx.value = -jx.value;
  374. }
  375. return jx;
  376. }
  377. jx.min = -1;
  378. jx.value = (float)p_val / MAX_JOY_AXIS;
  379. return jx;
  380. }
  381. void joystick_windows::load_xinput() {
  382. xinput_get_state = &_xinput_get_state;
  383. xinput_dll = LoadLibrary( "XInput1_4.dll" );
  384. if (!xinput_dll) {
  385. xinput_dll = LoadLibrary("XInput1_3.dll");
  386. if (!xinput_dll) {
  387. xinput_dll = LoadLibrary("XInput9_1_0.dll");
  388. }
  389. }
  390. if (!xinput_dll) {
  391. if (OS::get_singleton()->is_stdout_verbose()) {
  392. print_line("Could not find XInput, using DirectInput only");
  393. }
  394. return;
  395. }
  396. XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputGetState");
  397. if (!func) {
  398. unload_xinput();
  399. return;
  400. }
  401. xinput_get_state = func;
  402. return;
  403. }
  404. void joystick_windows::unload_xinput() {
  405. if (xinput_dll) {
  406. FreeLibrary((HMODULE)xinput_dll);
  407. }
  408. }