joystick.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. PRAWINPUTDEVICELIST dev_list = NULL;
  81. unsigned int dev_list_count = 0;
  82. if (GetRawInputDeviceList(NULL, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) {
  83. return false;
  84. }
  85. dev_list = (PRAWINPUTDEVICELIST) malloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count);
  86. if (!dev_list) return false;
  87. if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == -1) {
  88. free(dev_list);
  89. return false;
  90. }
  91. for (int i = 0; i < dev_list_count; i++) {
  92. RID_DEVICE_INFO rdi;
  93. char dev_name[128];
  94. UINT rdiSize = sizeof(rdi);
  95. UINT nameSize = sizeof(dev_name);
  96. rdi.cbSize = rdiSize;
  97. if ( (dev_list[i].dwType == RIM_TYPEHID) &&
  98. (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1) &&
  99. (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)p_guid->Data1) &&
  100. (GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) &&
  101. (strstr(dev_name, "IG_") != NULL)) {
  102. free(dev_list);
  103. return true;
  104. }
  105. }
  106. free(dev_list);
  107. return false;
  108. }
  109. bool joystick_windows::setup_dinput_joystick(const DIDEVICEINSTANCE* instance) {
  110. HRESULT hr;
  111. int num = check_free_joy_slot();
  112. if (have_device(instance->guidInstance) || num == -1)
  113. return false;
  114. d_joysticks[joystick_count] = dinput_gamepad();
  115. dinput_gamepad* joy = &d_joysticks[num];
  116. const DWORD devtype = (instance->dwDevType & 0xFF);
  117. if ((devtype != DI8DEVTYPE_JOYSTICK) && (devtype != DI8DEVTYPE_GAMEPAD) && (devtype != DI8DEVTYPE_1STPERSON)) {
  118. //printf("ignore device %s, type %x\n", instance->tszProductName, devtype);
  119. return false;
  120. }
  121. hr = dinput->CreateDevice(instance->guidInstance, &joy->di_joy, NULL);
  122. if (FAILED(hr)) {
  123. //std::wcout << "failed to create device: " << instance->tszProductName << std::endl;
  124. return false;
  125. }
  126. const GUID &guid = instance->guidProduct;
  127. char uid[128];
  128. sprintf(uid, "%08lx%04hx%04hx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
  129. __builtin_bswap32(guid.Data1), guid.Data2, guid.Data3,
  130. guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
  131. guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
  132. id_to_change = num;
  133. joy->di_joy->SetDataFormat(&c_dfDIJoystick2);
  134. joy->di_joy->SetCooperativeLevel(*hWnd, DISCL_FOREGROUND);
  135. joy->di_joy->EnumObjects(objectsCallback, this, NULL);
  136. joy->joy_axis.sort();
  137. joy->guid = instance->guidInstance;
  138. input->joy_connection_changed(num, true, instance->tszProductName, uid);
  139. joy->attached = true;
  140. joy->id = num;
  141. attached_joysticks[num] = true;
  142. joy->confirmed = true;
  143. joystick_count++;
  144. return true;
  145. }
  146. void joystick_windows::setup_joystick_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id) {
  147. if (ob->dwType & DIDFT_AXIS) {
  148. HRESULT res;
  149. DIPROPRANGE prop_range;
  150. DIPROPDWORD dilong;
  151. DWORD ofs;
  152. if (ob->guidType == GUID_XAxis)
  153. ofs = DIJOFS_X;
  154. else if (ob->guidType == GUID_YAxis)
  155. ofs = DIJOFS_Y;
  156. else if (ob->guidType == GUID_ZAxis)
  157. ofs = DIJOFS_Z;
  158. else if (ob->guidType == GUID_RxAxis)
  159. ofs = DIJOFS_RX;
  160. else if (ob->guidType == GUID_RyAxis)
  161. ofs = DIJOFS_RY;
  162. else if (ob->guidType == GUID_RzAxis)
  163. ofs = DIJOFS_RZ;
  164. else if (ob->guidType == GUID_Slider)
  165. ofs = DIJOFS_SLIDER(0);
  166. else
  167. return;
  168. prop_range.diph.dwSize = sizeof(DIPROPRANGE);
  169. prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  170. prop_range.diph.dwObj = ob->dwType;
  171. prop_range.diph.dwHow = DIPH_BYID;
  172. prop_range.lMin = -MAX_JOY_AXIS;
  173. prop_range.lMax = +MAX_JOY_AXIS;
  174. dinput_gamepad &joy = d_joysticks[p_joy_id];
  175. res = joy.di_joy->SetProperty(DIPROP_RANGE, &prop_range.diph);
  176. if (FAILED(res))
  177. return;
  178. dilong.diph.dwSize = sizeof(dilong);
  179. dilong.diph.dwHeaderSize = sizeof(dilong.diph);
  180. dilong.diph.dwObj = ob->dwType;
  181. dilong.diph.dwHow = DIPH_BYID;
  182. dilong.dwData = 0;
  183. res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_DEADZONE, &dilong.diph);
  184. if (FAILED(res))
  185. return;
  186. joy.joy_axis.push_back(ofs);
  187. }
  188. }
  189. BOOL CALLBACK joystick_windows::enumCallback(const DIDEVICEINSTANCE* instance, void* pContext) {
  190. joystick_windows* self = (joystick_windows*)pContext;
  191. if (self->is_xinput_device(&instance->guidProduct)) {;
  192. return DIENUM_CONTINUE;
  193. }
  194. self->setup_dinput_joystick(instance);
  195. return DIENUM_CONTINUE;
  196. }
  197. BOOL CALLBACK joystick_windows::objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context) {
  198. joystick_windows* self = (joystick_windows*)context;
  199. self->setup_joystick_object(instance, self->id_to_change);
  200. return DIENUM_CONTINUE;
  201. }
  202. void joystick_windows::close_joystick(int id) {
  203. if (id == -1) {
  204. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  205. close_joystick(i);
  206. }
  207. return;
  208. }
  209. if (!d_joysticks[id].attached) return;
  210. d_joysticks[id].di_joy->Unacquire();
  211. d_joysticks[id].di_joy->Release();
  212. d_joysticks[id].attached = false;
  213. attached_joysticks[d_joysticks[id].id] = false;
  214. d_joysticks[id].guid.Data1 = d_joysticks[id].guid.Data2 = d_joysticks[id].guid.Data3 = 0;
  215. input->joy_connection_changed(id, false, "");
  216. joystick_count--;
  217. }
  218. void joystick_windows::probe_joysticks() {
  219. DWORD dwResult;
  220. for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
  221. ZeroMemory(&x_joysticks[i].state, sizeof(XINPUT_STATE));
  222. dwResult = xinput_get_state(i, &x_joysticks[i].state);
  223. if ( dwResult == ERROR_SUCCESS) {
  224. int id = check_free_joy_slot();
  225. if (id != -1 && !x_joysticks[i].attached) {
  226. x_joysticks[i].attached = true;
  227. x_joysticks[i].id = id;
  228. attached_joysticks[id] = true;
  229. input->joy_connection_changed(id, true, "XInput Gamepad","__XINPUT_DEVICE__");
  230. }
  231. }
  232. else if (x_joysticks[i].attached) {
  233. x_joysticks[i].attached = false;
  234. attached_joysticks[x_joysticks[i].id] = false;
  235. input->joy_connection_changed(x_joysticks[i].id, false, "");
  236. }
  237. }
  238. for (int i = 0; i < joystick_count; i++) {
  239. d_joysticks[i].confirmed = false;
  240. }
  241. dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY);
  242. for (int i = 0; i < joystick_count; i++) {
  243. if (!d_joysticks[i].confirmed) {
  244. close_joystick(i);
  245. }
  246. }
  247. }
  248. unsigned int joystick_windows::process_joysticks(unsigned int p_last_id) {
  249. HRESULT hr;
  250. for (int i = 0; i < XUSER_MAX_COUNT; i++) {
  251. xinput_gamepad &joy = x_joysticks[i];
  252. if (!joy.attached) {
  253. continue;
  254. }
  255. ZeroMemory(&joy.state, sizeof(XINPUT_STATE));
  256. xinput_get_state(i, &joy.state);
  257. if (joy.state.dwPacketNumber != joy.last_packet) {
  258. int button_mask = XINPUT_GAMEPAD_DPAD_UP;
  259. for (int i = 0; i <= 16; i++) {
  260. p_last_id = input->joy_button(p_last_id, joy.id, i, joy.state.Gamepad.wButtons & button_mask);
  261. button_mask = button_mask * 2;
  262. }
  263. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_0, axis_correct(joy.state.Gamepad.sThumbLX, true));
  264. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_1, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true));
  265. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_2, axis_correct(joy.state.Gamepad.sThumbRX, true));
  266. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_3, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true));
  267. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_4, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true));
  268. p_last_id = input->joy_axis(p_last_id, joy.id, JOY_AXIS_5, axis_correct(joy.state.Gamepad.bRightTrigger, true, true));
  269. joy.last_packet = joy.state.dwPacketNumber;
  270. }
  271. }
  272. for (int i = 0; i < JOYSTICKS_MAX; i++) {
  273. dinput_gamepad* joy = &d_joysticks[i];
  274. if (!joy->attached)
  275. continue;
  276. DIJOYSTATE2 js;
  277. hr = joy->di_joy->Poll();
  278. if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
  279. IDirectInputDevice8_Acquire(joy->di_joy);
  280. joy->di_joy->Poll();
  281. }
  282. if (FAILED(hr = d_joysticks[i].di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js))) {
  283. //printf("failed to read joy #%d\n", i);
  284. continue;
  285. }
  286. p_last_id = post_hat(p_last_id, i, js.rgdwPOV[0]);
  287. for (int j = 0; j < 128; j++) {
  288. if (js.rgbButtons[j] & 0x80) {
  289. if (!joy->last_buttons[j]) {
  290. p_last_id = input->joy_button(p_last_id, i, j, true);
  291. joy->last_buttons[j] = true;
  292. }
  293. }
  294. else {
  295. if (joy->last_buttons[j]) {
  296. p_last_id = input->joy_button(p_last_id, i, j, false);
  297. joy->last_buttons[j] = false;
  298. }
  299. }
  300. }
  301. // on mingw, these constants are not constants
  302. int count = 6;
  303. int axes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ };
  304. int values[] = { js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz };
  305. for (int j = 0; j < joy->joy_axis.size(); j++) {
  306. for (int k=0; k<count; k++) {
  307. if (joy->joy_axis[j] == axes[k]) {
  308. p_last_id = input->joy_axis(p_last_id, i, j, axis_correct(values[k]));
  309. break;
  310. };
  311. };
  312. };
  313. }
  314. return p_last_id;
  315. }
  316. unsigned int joystick_windows::post_hat(unsigned int p_last_id, int p_device, DWORD p_dpad) {
  317. int dpad_val = 0;
  318. if (p_dpad == -1) {
  319. dpad_val = InputDefault::HAT_MASK_CENTER;
  320. }
  321. if (p_dpad == 0) {
  322. dpad_val = InputDefault::HAT_MASK_UP;
  323. }
  324. else if (p_dpad == 4500) {
  325. dpad_val = (InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT);
  326. }
  327. else if (p_dpad == 9000) {
  328. dpad_val = InputDefault::HAT_MASK_RIGHT;
  329. }
  330. else if (p_dpad == 13500) {
  331. dpad_val = (InputDefault::HAT_MASK_RIGHT | InputDefault::HAT_MASK_DOWN);
  332. }
  333. else if (p_dpad == 18000) {
  334. dpad_val = InputDefault::HAT_MASK_DOWN;
  335. }
  336. else if (p_dpad == 22500) {
  337. dpad_val = (InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT);
  338. }
  339. else if (p_dpad == 27000) {
  340. dpad_val = InputDefault::HAT_MASK_LEFT;
  341. }
  342. else if (p_dpad == 31500) {
  343. dpad_val = (InputDefault::HAT_MASK_LEFT | InputDefault::HAT_MASK_UP);
  344. }
  345. return input->joy_hat(p_last_id, p_device, dpad_val);
  346. };
  347. InputDefault::JoyAxis joystick_windows::axis_correct(int p_val, bool p_xinput, bool p_trigger, bool p_negate) const {
  348. InputDefault::JoyAxis jx;
  349. if (Math::abs(p_val) < MIN_JOY_AXIS) {
  350. jx.min = -1;
  351. jx.value = 0.0f;
  352. return jx;
  353. }
  354. if (p_xinput) {
  355. if (p_trigger) {
  356. jx.min = 0;
  357. jx.value = (float)p_val / MAX_TRIGGER;
  358. return jx;
  359. }
  360. jx.min = -1;
  361. if (p_val < 0) {
  362. jx.value = (float)p_val / MAX_JOY_AXIS;
  363. }
  364. else {
  365. jx.value = (float)p_val / (MAX_JOY_AXIS - 1);
  366. }
  367. if (p_negate) {
  368. jx.value = -jx.value;
  369. }
  370. return jx;
  371. }
  372. jx.min = -1;
  373. jx.value = (float)p_val / MAX_JOY_AXIS;
  374. return jx;
  375. }
  376. void joystick_windows::load_xinput() {
  377. xinput_get_state = &_xinput_get_state;
  378. xinput_dll = LoadLibrary( "XInput1_4.dll" );
  379. if (!xinput_dll) {
  380. xinput_dll = LoadLibrary("XInput1_3.dll");
  381. if (!xinput_dll) {
  382. xinput_dll = LoadLibrary("XInput9_1_0.dll");
  383. }
  384. }
  385. if (!xinput_dll) {
  386. if (OS::get_singleton()->is_stdout_verbose()) {
  387. print_line("Could not find XInput, using DirectInput only");
  388. }
  389. return;
  390. }
  391. XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputGetState");
  392. if (!func) {
  393. unload_xinput();
  394. return;
  395. }
  396. xinput_get_state = func;
  397. return;
  398. }
  399. void joystick_windows::unload_xinput() {
  400. if (xinput_dll) {
  401. FreeLibrary((HMODULE)xinput_dll);
  402. }
  403. }