2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* joypad_windows.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2019-02-12 15:43:54 +01:00
|
|
|
#include "joypad_windows.h"
|
2019-01-28 12:16:12 +01:00
|
|
|
|
2015-12-18 06:12:53 +01:00
|
|
|
#include <oleauto.h>
|
|
|
|
#include <wbemidl.h>
|
|
|
|
|
2020-01-16 12:07:58 +01:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
// Workaround GCC warning from -Wcast-function-type.
|
|
|
|
#define GetProcAddress (void *)GetProcAddress
|
|
|
|
#endif
|
|
|
|
|
2015-12-18 06:12:53 +01:00
|
|
|
DWORD WINAPI _xinput_get_state(DWORD dwUserIndex, XINPUT_STATE *pState) {
|
|
|
|
return ERROR_DEVICE_NOT_CONNECTED;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2016-06-15 14:40:57 +02:00
|
|
|
DWORD WINAPI _xinput_set_state(DWORD dwUserIndex, XINPUT_VIBRATION *pVibration) {
|
|
|
|
return ERROR_DEVICE_NOT_CONNECTED;
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
JoypadWindows::JoypadWindows() {
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 16:56:48 +01:00
|
|
|
JoypadWindows::JoypadWindows(HWND *hwnd) {
|
2020-04-28 15:19:37 +02:00
|
|
|
input = Input::get_singleton();
|
2015-12-18 19:15:32 +01:00
|
|
|
hWnd = hwnd;
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad_count = 0;
|
2020-04-02 01:20:12 +02:00
|
|
|
dinput = nullptr;
|
|
|
|
xinput_dll = nullptr;
|
|
|
|
xinput_get_state = nullptr;
|
|
|
|
xinput_set_state = nullptr;
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
load_xinput();
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
for (int i = 0; i < JOYPADS_MAX; i++) {
|
2017-01-08 21:05:51 +01:00
|
|
|
attached_joypads[i] = false;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2020-05-29 11:15:43 +02:00
|
|
|
HRESULT result = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&dinput, nullptr);
|
|
|
|
if (result == DI_OK) {
|
|
|
|
probe_joypads();
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("Couldn't initialize DirectInput. Error: " + itos(result));
|
|
|
|
if (result == DIERR_OUTOFMEMORY) {
|
|
|
|
ERR_PRINT("The Windows DirectInput subsystem could not allocate sufficient memory.");
|
|
|
|
ERR_PRINT("Rebooting your PC may solve this issue.");
|
|
|
|
}
|
2020-05-29 17:39:58 +02:00
|
|
|
// Ensure dinput is still a nullptr.
|
|
|
|
dinput = nullptr;
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
JoypadWindows::~JoypadWindows() {
|
2017-01-08 21:05:51 +01:00
|
|
|
close_joypad();
|
2020-05-29 17:39:58 +02:00
|
|
|
if (dinput) {
|
|
|
|
dinput->Release();
|
|
|
|
}
|
2015-12-18 19:15:32 +01:00
|
|
|
unload_xinput();
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
bool JoypadWindows::have_device(const GUID &p_guid) {
|
2017-01-08 21:05:51 +01:00
|
|
|
for (int i = 0; i < JOYPADS_MAX; i++) {
|
|
|
|
if (d_joypads[i].guid == p_guid) {
|
|
|
|
d_joypads[i].confirmed = true;
|
2015-12-18 19:15:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// adapted from SDL2, works a lot better than the MSDN version
|
2017-01-08 23:52:38 +01:00
|
|
|
bool JoypadWindows::is_xinput_device(const GUID *p_guid) {
|
2021-12-16 07:54:03 +01:00
|
|
|
static GUID IID_ValveStreamingGamepad = { MAKELONG(0x28DE, 0x11FF), 0x28DE, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
|
2015-12-26 13:14:13 +01:00
|
|
|
static GUID IID_X360WiredGamepad = { MAKELONG(0x045E, 0x02A1), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
|
|
|
|
static GUID IID_X360WirelessGamepad = { MAKELONG(0x045E, 0x028E), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
|
2023-01-21 04:22:05 +01:00
|
|
|
static GUID IID_XSWirelessGamepad = { MAKELONG(0x045E, 0x0B13), 0x0000, 0x0000, { 0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44 } };
|
2015-12-26 13:14:13 +01:00
|
|
|
|
2021-12-16 07:54:03 +01:00
|
|
|
if (memcmp(p_guid, &IID_ValveStreamingGamepad, sizeof(*p_guid)) == 0 ||
|
|
|
|
memcmp(p_guid, &IID_X360WiredGamepad, sizeof(*p_guid)) == 0 ||
|
2023-01-21 04:22:05 +01:00
|
|
|
memcmp(p_guid, &IID_X360WirelessGamepad, sizeof(*p_guid)) == 0 ||
|
|
|
|
memcmp(p_guid, &IID_XSWirelessGamepad, sizeof(*p_guid)) == 0)
|
2015-12-26 13:14:13 +01:00
|
|
|
return true;
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
PRAWINPUTDEVICELIST dev_list = nullptr;
|
2015-12-18 19:15:32 +01:00
|
|
|
unsigned int dev_list_count = 0;
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
if (GetRawInputDeviceList(nullptr, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) {
|
2015-12-18 19:15:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-10 19:31:44 +01:00
|
|
|
dev_list = (PRAWINPUTDEVICELIST)memalloc(sizeof(RAWINPUTDEVICELIST) * dev_list_count);
|
|
|
|
ERR_FAIL_NULL_V_MSG(dev_list, false, "Out of memory.");
|
2015-12-18 19:15:32 +01:00
|
|
|
|
2019-06-15 12:11:30 +02:00
|
|
|
if (GetRawInputDeviceList(dev_list, &dev_list_count, sizeof(RAWINPUTDEVICELIST)) == (UINT)-1) {
|
2021-03-10 19:31:44 +01:00
|
|
|
memfree(dev_list);
|
2015-12-18 19:15:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-15 12:11:30 +02:00
|
|
|
for (unsigned int i = 0; i < dev_list_count; i++) {
|
2015-12-18 19:15:32 +01:00
|
|
|
RID_DEVICE_INFO rdi;
|
|
|
|
char dev_name[128];
|
|
|
|
UINT rdiSize = sizeof(rdi);
|
|
|
|
UINT nameSize = sizeof(dev_name);
|
|
|
|
|
|
|
|
rdi.cbSize = rdiSize;
|
|
|
|
if ((dev_list[i].dwType == RIM_TYPEHID) &&
|
|
|
|
(GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1) &&
|
|
|
|
(MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) == (LONG)p_guid->Data1) &&
|
|
|
|
(GetRawInputDeviceInfoA(dev_list[i].hDevice, RIDI_DEVICENAME, &dev_name, &nameSize) != (UINT)-1) &&
|
2020-04-02 01:20:12 +02:00
|
|
|
(strstr(dev_name, "IG_") != nullptr)) {
|
2021-03-10 19:31:44 +01:00
|
|
|
memfree(dev_list);
|
2015-12-18 19:15:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 19:31:44 +01:00
|
|
|
memfree(dev_list);
|
2015-12-18 19:15:32 +01:00
|
|
|
return false;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
bool JoypadWindows::setup_dinput_joypad(const DIDEVICEINSTANCE *instance) {
|
2020-05-29 17:39:58 +02:00
|
|
|
ERR_FAIL_NULL_V_MSG(dinput, false, "DirectInput not initialized. Rebooting your PC may solve this issue.");
|
2015-12-18 19:15:32 +01:00
|
|
|
HRESULT hr;
|
2017-02-21 17:02:49 +01:00
|
|
|
int num = input->get_unused_joy_id();
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (have_device(instance->guidInstance) || num == -1) {
|
2015-12-18 19:15:32 +01:00
|
|
|
return false;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2020-07-31 20:16:51 +02:00
|
|
|
d_joypads[num] = dinput_gamepad();
|
|
|
|
dinput_gamepad *joy = &d_joypads[num];
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
const DWORD devtype = (instance->dwDevType & 0xFF);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2020-12-04 13:28:57 +01:00
|
|
|
if ((devtype != DI8DEVTYPE_JOYSTICK) && (devtype != DI8DEVTYPE_GAMEPAD) && (devtype != DI8DEVTYPE_1STPERSON) && (devtype != DI8DEVTYPE_DRIVING)) {
|
2015-12-18 19:15:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
hr = dinput->CreateDevice(instance->guidInstance, &joy->di_joy, nullptr);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
const GUID &guid = instance->guidProduct;
|
|
|
|
char uid[128];
|
2020-05-25 18:29:34 +02:00
|
|
|
|
2022-11-01 15:29:38 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(memcmp(&guid.Data4[2], "PIDVID", 6), false, "DirectInput device not recognized.");
|
2020-05-25 18:29:34 +02:00
|
|
|
WORD type = BSWAP16(0x03);
|
|
|
|
WORD vendor = BSWAP16(LOWORD(guid.Data1));
|
|
|
|
WORD product = BSWAP16(HIWORD(guid.Data1));
|
|
|
|
WORD version = 0;
|
|
|
|
sprintf_s(uid, "%04x%04x%04x%04x%04x%04x%04x%04x", type, 0, vendor, 0, product, 0, version, 0);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2020-07-31 20:16:51 +02:00
|
|
|
id_to_change = num;
|
2020-06-20 11:03:15 +02:00
|
|
|
slider_count = 0;
|
2015-12-26 13:14:13 +01:00
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
joy->di_joy->SetDataFormat(&c_dfDIJoystick2);
|
2015-12-18 19:15:32 +01:00
|
|
|
joy->di_joy->SetCooperativeLevel(*hWnd, DISCL_FOREGROUND);
|
2018-10-17 08:46:50 +02:00
|
|
|
joy->di_joy->EnumObjects(objectsCallback, this, 0);
|
2015-12-18 19:15:32 +01:00
|
|
|
joy->joy_axis.sort();
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
joy->guid = instance->guidInstance;
|
|
|
|
input->joy_connection_changed(num, true, instance->tszProductName, uid);
|
|
|
|
joy->attached = true;
|
|
|
|
joy->id = num;
|
2017-01-08 21:05:51 +01:00
|
|
|
attached_joypads[num] = true;
|
2015-12-18 19:15:32 +01:00
|
|
|
joy->confirmed = true;
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad_count++;
|
2015-12-18 19:15:32 +01:00
|
|
|
return true;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
void JoypadWindows::setup_joypad_object(const DIDEVICEOBJECTINSTANCE *ob, int p_joy_id) {
|
2015-12-18 19:15:32 +01:00
|
|
|
if (ob->dwType & DIDFT_AXIS) {
|
|
|
|
HRESULT res;
|
|
|
|
DIPROPRANGE prop_range;
|
|
|
|
DIPROPDWORD dilong;
|
2020-11-06 11:17:28 +01:00
|
|
|
LONG ofs;
|
2022-02-16 13:56:32 +01:00
|
|
|
if (ob->guidType == GUID_XAxis) {
|
2015-12-18 19:15:32 +01:00
|
|
|
ofs = DIJOFS_X;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (ob->guidType == GUID_YAxis) {
|
2015-12-18 19:15:32 +01:00
|
|
|
ofs = DIJOFS_Y;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (ob->guidType == GUID_ZAxis) {
|
2015-12-18 19:15:32 +01:00
|
|
|
ofs = DIJOFS_Z;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (ob->guidType == GUID_RxAxis) {
|
2015-12-18 19:15:32 +01:00
|
|
|
ofs = DIJOFS_RX;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (ob->guidType == GUID_RyAxis) {
|
2015-12-18 19:15:32 +01:00
|
|
|
ofs = DIJOFS_RY;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (ob->guidType == GUID_RzAxis) {
|
2015-12-18 19:15:32 +01:00
|
|
|
ofs = DIJOFS_RZ;
|
2022-02-16 13:56:32 +01:00
|
|
|
} else if (ob->guidType == GUID_Slider) {
|
2020-06-20 11:03:15 +02:00
|
|
|
if (slider_count < 2) {
|
|
|
|
ofs = DIJOFS_SLIDER(slider_count);
|
|
|
|
slider_count++;
|
2020-06-22 12:13:22 +02:00
|
|
|
} else {
|
|
|
|
return;
|
2020-06-20 11:03:15 +02:00
|
|
|
}
|
2022-02-16 13:56:32 +01:00
|
|
|
} else {
|
2015-12-18 19:15:32 +01:00
|
|
|
return;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 19:15:32 +01:00
|
|
|
prop_range.diph.dwSize = sizeof(DIPROPRANGE);
|
|
|
|
prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
|
|
|
|
prop_range.diph.dwObj = ob->dwType;
|
|
|
|
prop_range.diph.dwHow = DIPH_BYID;
|
|
|
|
prop_range.lMin = -MAX_JOY_AXIS;
|
|
|
|
prop_range.lMax = +MAX_JOY_AXIS;
|
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
dinput_gamepad &joy = d_joypads[p_joy_id];
|
2015-12-18 19:15:32 +01:00
|
|
|
|
2015-12-26 13:14:13 +01:00
|
|
|
res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_RANGE, &prop_range.diph);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (FAILED(res)) {
|
2015-12-18 19:15:32 +01:00
|
|
|
return;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 19:15:32 +01:00
|
|
|
|
|
|
|
dilong.diph.dwSize = sizeof(dilong);
|
|
|
|
dilong.diph.dwHeaderSize = sizeof(dilong.diph);
|
|
|
|
dilong.diph.dwObj = ob->dwType;
|
|
|
|
dilong.diph.dwHow = DIPH_BYID;
|
|
|
|
dilong.dwData = 0;
|
|
|
|
|
|
|
|
res = IDirectInputDevice8_SetProperty(joy.di_joy, DIPROP_DEADZONE, &dilong.diph);
|
2022-02-16 13:56:32 +01:00
|
|
|
if (FAILED(res)) {
|
2015-12-18 19:15:32 +01:00
|
|
|
return;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 19:15:32 +01:00
|
|
|
|
|
|
|
joy.joy_axis.push_back(ofs);
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
BOOL CALLBACK JoypadWindows::enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context) {
|
2022-04-05 12:40:26 +02:00
|
|
|
JoypadWindows *self = static_cast<JoypadWindows *>(p_context);
|
2017-08-11 21:10:05 +02:00
|
|
|
if (self->is_xinput_device(&p_instance->guidProduct)) {
|
2015-12-18 19:15:32 +01:00
|
|
|
return DIENUM_CONTINUE;
|
|
|
|
}
|
2017-08-11 21:10:05 +02:00
|
|
|
self->setup_dinput_joypad(p_instance);
|
2015-12-18 19:15:32 +01:00
|
|
|
return DIENUM_CONTINUE;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 12:40:26 +02:00
|
|
|
BOOL CALLBACK JoypadWindows::objectsCallback(const DIDEVICEOBJECTINSTANCE *p_instance, void *p_context) {
|
|
|
|
JoypadWindows *self = static_cast<JoypadWindows *>(p_context);
|
|
|
|
self->setup_joypad_object(p_instance, self->id_to_change);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
return DIENUM_CONTINUE;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
void JoypadWindows::close_joypad(int id) {
|
2015-12-18 19:15:32 +01:00
|
|
|
if (id == -1) {
|
2017-01-08 21:05:51 +01:00
|
|
|
for (int i = 0; i < JOYPADS_MAX; i++) {
|
|
|
|
close_joypad(i);
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!d_joypads[id].attached) {
|
2017-01-08 21:05:51 +01:00
|
|
|
return;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
d_joypads[id].di_joy->Unacquire();
|
|
|
|
d_joypads[id].di_joy->Release();
|
|
|
|
d_joypads[id].attached = false;
|
|
|
|
attached_joypads[d_joypads[id].id] = false;
|
|
|
|
d_joypads[id].guid.Data1 = d_joypads[id].guid.Data2 = d_joypads[id].guid.Data3 = 0;
|
|
|
|
input->joy_connection_changed(d_joypads[id].id, false, "");
|
|
|
|
joypad_count--;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
void JoypadWindows::probe_joypads() {
|
2020-05-29 17:39:58 +02:00
|
|
|
ERR_FAIL_NULL_MSG(dinput, "DirectInput not initialized. Rebooting your PC may solve this issue.");
|
2015-12-18 19:15:32 +01:00
|
|
|
DWORD dwResult;
|
|
|
|
for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
|
2017-01-08 21:05:51 +01:00
|
|
|
ZeroMemory(&x_joypads[i].state, sizeof(XINPUT_STATE));
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
dwResult = xinput_get_state(i, &x_joypads[i].state);
|
2015-12-18 19:15:32 +01:00
|
|
|
if (dwResult == ERROR_SUCCESS) {
|
2017-02-21 17:02:49 +01:00
|
|
|
int id = input->get_unused_joy_id();
|
2017-01-08 21:05:51 +01:00
|
|
|
if (id != -1 && !x_joypads[i].attached) {
|
|
|
|
x_joypads[i].attached = true;
|
|
|
|
x_joypads[i].id = id;
|
|
|
|
x_joypads[i].ff_timestamp = 0;
|
|
|
|
x_joypads[i].ff_end_timestamp = 0;
|
|
|
|
x_joypads[i].vibrating = false;
|
|
|
|
attached_joypads[id] = true;
|
2015-12-18 19:15:32 +01:00
|
|
|
input->joy_connection_changed(id, true, "XInput Gamepad", "__XINPUT_DEVICE__");
|
|
|
|
}
|
2017-01-08 21:05:51 +01:00
|
|
|
} else if (x_joypads[i].attached) {
|
|
|
|
x_joypads[i].attached = false;
|
|
|
|
attached_joypads[x_joypads[i].id] = false;
|
|
|
|
input->joy_connection_changed(x_joypads[i].id, false, "");
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
for (int i = 0; i < joypad_count; i++) {
|
|
|
|
d_joypads[i].confirmed = false;
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
dinput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, this, DIEDFL_ATTACHEDONLY);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
for (int i = 0; i < joypad_count; i++) {
|
|
|
|
if (!d_joypads[i].confirmed) {
|
|
|
|
close_joypad(i);
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-26 15:59:13 +02:00
|
|
|
void JoypadWindows::process_joypads() {
|
2015-12-18 19:15:32 +01:00
|
|
|
HRESULT hr;
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
for (int i = 0; i < XUSER_MAX_COUNT; i++) {
|
2017-01-08 21:05:51 +01:00
|
|
|
xinput_gamepad &joy = x_joypads[i];
|
2015-12-18 19:15:32 +01:00
|
|
|
if (!joy.attached) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ZeroMemory(&joy.state, sizeof(XINPUT_STATE));
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
xinput_get_state(i, &joy.state);
|
|
|
|
if (joy.state.dwPacketNumber != joy.last_packet) {
|
|
|
|
int button_mask = XINPUT_GAMEPAD_DPAD_UP;
|
2019-06-17 18:01:09 +02:00
|
|
|
for (int j = 0; j <= 16; j++) {
|
2021-03-25 21:56:12 +01:00
|
|
|
input->joy_button(joy.id, (JoyButton)j, joy.state.Gamepad.wButtons & button_mask);
|
2015-12-18 19:15:32 +01:00
|
|
|
button_mask = button_mask * 2;
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2021-08-13 23:31:57 +02:00
|
|
|
input->joy_axis(joy.id, JoyAxis::LEFT_X, axis_correct(joy.state.Gamepad.sThumbLX, true));
|
|
|
|
input->joy_axis(joy.id, JoyAxis::LEFT_Y, axis_correct(joy.state.Gamepad.sThumbLY, true, false, true));
|
|
|
|
input->joy_axis(joy.id, JoyAxis::RIGHT_X, axis_correct(joy.state.Gamepad.sThumbRX, true));
|
|
|
|
input->joy_axis(joy.id, JoyAxis::RIGHT_Y, axis_correct(joy.state.Gamepad.sThumbRY, true, false, true));
|
|
|
|
input->joy_axis(joy.id, JoyAxis::TRIGGER_LEFT, axis_correct(joy.state.Gamepad.bLeftTrigger, true, true));
|
|
|
|
input->joy_axis(joy.id, JoyAxis::TRIGGER_RIGHT, axis_correct(joy.state.Gamepad.bRightTrigger, true, true));
|
2015-12-18 19:15:32 +01:00
|
|
|
joy.last_packet = joy.state.dwPacketNumber;
|
|
|
|
}
|
2016-06-15 14:40:57 +02:00
|
|
|
uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
|
|
|
|
if (timestamp > joy.ff_timestamp) {
|
|
|
|
Vector2 strength = input->get_joy_vibration_strength(joy.id);
|
|
|
|
float duration = input->get_joy_vibration_duration(joy.id);
|
|
|
|
if (strength.x == 0 && strength.y == 0) {
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad_vibration_stop_xinput(i, timestamp);
|
2016-06-15 14:40:57 +02:00
|
|
|
} else {
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad_vibration_start_xinput(i, strength.x, strength.y, duration, timestamp);
|
2016-06-15 14:40:57 +02:00
|
|
|
}
|
|
|
|
} else if (joy.vibrating && joy.ff_end_timestamp != 0) {
|
|
|
|
uint64_t current_time = OS::get_singleton()->get_ticks_usec();
|
2022-02-16 13:56:32 +01:00
|
|
|
if (current_time >= joy.ff_end_timestamp) {
|
2017-01-08 21:05:51 +01:00
|
|
|
joypad_vibration_stop_xinput(i, current_time);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2016-06-15 14:40:57 +02:00
|
|
|
}
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-01-08 21:05:51 +01:00
|
|
|
for (int i = 0; i < JOYPADS_MAX; i++) {
|
|
|
|
dinput_gamepad *joy = &d_joypads[i];
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
if (!joy->attached) {
|
2015-12-18 19:15:32 +01:00
|
|
|
continue;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
DIJOYSTATE2 js;
|
|
|
|
hr = joy->di_joy->Poll();
|
|
|
|
if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED) {
|
|
|
|
IDirectInputDevice8_Acquire(joy->di_joy);
|
|
|
|
joy->di_joy->Poll();
|
|
|
|
}
|
2017-09-06 23:50:18 +02:00
|
|
|
|
|
|
|
hr = joy->di_joy->GetDeviceState(sizeof(DIJOYSTATE2), &js);
|
|
|
|
if (FAILED(hr)) {
|
2015-12-18 19:15:32 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2017-03-26 15:59:13 +02:00
|
|
|
post_hat(joy->id, js.rgdwPOV[0]);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
for (int j = 0; j < 128; j++) {
|
|
|
|
if (js.rgbButtons[j] & 0x80) {
|
|
|
|
if (!joy->last_buttons[j]) {
|
2021-03-25 21:56:12 +01:00
|
|
|
input->joy_button(joy->id, (JoyButton)j, true);
|
2015-12-18 19:15:32 +01:00
|
|
|
joy->last_buttons[j] = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (joy->last_buttons[j]) {
|
2021-03-25 21:56:12 +01:00
|
|
|
input->joy_button(joy->id, (JoyButton)j, false);
|
2015-12-18 19:15:32 +01:00
|
|
|
joy->last_buttons[j] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2016-06-15 14:40:57 +02:00
|
|
|
// on mingw, these constants are not constants
|
2020-06-20 11:03:15 +02:00
|
|
|
int count = 8;
|
2022-04-05 12:40:26 +02:00
|
|
|
const LONG axes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_Z, DIJOFS_RX, DIJOFS_RY, DIJOFS_RZ, (LONG)DIJOFS_SLIDER(0), (LONG)DIJOFS_SLIDER(1) };
|
2020-06-20 11:03:15 +02:00
|
|
|
int values[] = { js.lX, js.lY, js.lZ, js.lRx, js.lRy, js.lRz, js.rglSlider[0], js.rglSlider[1] };
|
2015-12-18 06:12:53 +01:00
|
|
|
|
|
|
|
for (int j = 0; j < joy->joy_axis.size(); j++) {
|
|
|
|
for (int k = 0; k < count; k++) {
|
|
|
|
if (joy->joy_axis[j] == axes[k]) {
|
2021-03-25 21:56:12 +01:00
|
|
|
input->joy_axis(joy->id, (JoyAxis)j, axis_correct(values[k]));
|
2015-12-18 06:12:53 +01:00
|
|
|
break;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
2017-03-26 15:59:13 +02:00
|
|
|
return;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-26 15:59:13 +02:00
|
|
|
void JoypadWindows::post_hat(int p_device, DWORD p_dpad) {
|
2023-01-08 00:55:54 +01:00
|
|
|
BitField<HatMask> dpad_val;
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2019-06-15 12:11:30 +02:00
|
|
|
// Should be -1 when centered, but according to docs:
|
|
|
|
// "Some drivers report the centered position of the POV indicator as 65,535. Determine whether the indicator is centered as follows:
|
|
|
|
// BOOL POVCentered = (LOWORD(dwPOV) == 0xFFFF);"
|
|
|
|
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee416628(v%3Dvs.85)#remarks
|
|
|
|
if (LOWORD(p_dpad) == 0xFFFF) {
|
2023-01-08 00:55:54 +01:00
|
|
|
// Do nothing.
|
|
|
|
// dpad_val.set_flag(HatMask::CENTER);
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
|
|
|
if (p_dpad == 0) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::UP);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
} else if (p_dpad == 4500) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::UP);
|
|
|
|
dpad_val.set_flag(HatMask::RIGHT);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
} else if (p_dpad == 9000) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::RIGHT);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
} else if (p_dpad == 13500) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::RIGHT);
|
|
|
|
dpad_val.set_flag(HatMask::DOWN);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
} else if (p_dpad == 18000) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::DOWN);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
} else if (p_dpad == 22500) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::DOWN);
|
|
|
|
dpad_val.set_flag(HatMask::LEFT);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
} else if (p_dpad == 27000) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::LEFT);
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2015-12-18 19:15:32 +01:00
|
|
|
} else if (p_dpad == 31500) {
|
2023-01-08 00:55:54 +01:00
|
|
|
dpad_val.set_flag(HatMask::LEFT);
|
|
|
|
dpad_val.set_flag(HatMask::UP);
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
2017-03-26 15:59:13 +02:00
|
|
|
input->joy_hat(p_device, dpad_val);
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
|
2021-12-25 10:29:08 +01:00
|
|
|
float JoypadWindows::axis_correct(int p_val, bool p_xinput, bool p_trigger, bool p_negate) const {
|
2015-12-18 19:15:32 +01:00
|
|
|
if (Math::abs(p_val) < MIN_JOY_AXIS) {
|
2021-12-25 10:29:08 +01:00
|
|
|
return p_trigger ? -1.0f : 0.0f;
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
2021-12-25 10:29:08 +01:00
|
|
|
if (!p_xinput) {
|
|
|
|
return (float)p_val / MAX_JOY_AXIS;
|
|
|
|
}
|
|
|
|
if (p_trigger) {
|
|
|
|
// Convert to a value between -1.0f and 1.0f.
|
|
|
|
return 2.0f * p_val / MAX_TRIGGER - 1.0f;
|
|
|
|
}
|
|
|
|
float value;
|
|
|
|
if (p_val < 0) {
|
|
|
|
value = (float)p_val / MAX_JOY_AXIS;
|
|
|
|
} else {
|
|
|
|
value = (float)p_val / (MAX_JOY_AXIS - 1);
|
|
|
|
}
|
|
|
|
if (p_negate) {
|
|
|
|
value = -value;
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
2021-12-25 10:29:08 +01:00
|
|
|
return value;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
void JoypadWindows::joypad_vibration_start_xinput(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
|
2017-01-08 21:05:51 +01:00
|
|
|
xinput_gamepad &joy = x_joypads[p_device];
|
2016-06-15 14:40:57 +02:00
|
|
|
if (joy.attached) {
|
|
|
|
XINPUT_VIBRATION effect;
|
|
|
|
effect.wLeftMotorSpeed = (65535 * p_strong_magnitude);
|
|
|
|
effect.wRightMotorSpeed = (65535 * p_weak_magnitude);
|
|
|
|
if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
|
|
|
|
joy.ff_timestamp = p_timestamp;
|
|
|
|
joy.ff_end_timestamp = p_duration == 0 ? 0 : p_timestamp + (uint64_t)(p_duration * 1000000.0);
|
|
|
|
joy.vibrating = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
void JoypadWindows::joypad_vibration_stop_xinput(int p_device, uint64_t p_timestamp) {
|
2017-01-08 21:05:51 +01:00
|
|
|
xinput_gamepad &joy = x_joypads[p_device];
|
2016-06-15 14:40:57 +02:00
|
|
|
if (joy.attached) {
|
|
|
|
XINPUT_VIBRATION effect;
|
|
|
|
effect.wLeftMotorSpeed = 0;
|
|
|
|
effect.wRightMotorSpeed = 0;
|
|
|
|
if (xinput_set_state(p_device, &effect) == ERROR_SUCCESS) {
|
|
|
|
joy.ff_timestamp = p_timestamp;
|
|
|
|
joy.vibrating = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
void JoypadWindows::load_xinput() {
|
2015-12-18 19:15:32 +01:00
|
|
|
xinput_get_state = &_xinput_get_state;
|
2016-06-15 14:40:57 +02:00
|
|
|
xinput_set_state = &_xinput_set_state;
|
2023-01-29 22:53:23 +01:00
|
|
|
bool legacy_xinput = false;
|
2015-12-18 19:15:32 +01:00
|
|
|
xinput_dll = LoadLibrary("XInput1_4.dll");
|
|
|
|
if (!xinput_dll) {
|
|
|
|
xinput_dll = LoadLibrary("XInput1_3.dll");
|
|
|
|
if (!xinput_dll) {
|
|
|
|
xinput_dll = LoadLibrary("XInput9_1_0.dll");
|
2023-01-29 22:53:23 +01:00
|
|
|
legacy_xinput = true;
|
2015-12-18 19:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!xinput_dll) {
|
2018-08-24 08:47:34 +02:00
|
|
|
print_verbose("Could not find XInput, using DirectInput only");
|
2015-12-18 19:15:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-29 22:53:23 +01:00
|
|
|
// (LPCSTR)100 is the magic number to get XInputGetStateEx, which also provides the state for the guide button
|
|
|
|
LPCSTR get_state_func_name = legacy_xinput ? "XInputGetState" : (LPCSTR)100;
|
|
|
|
XInputGetState_t func = (XInputGetState_t)GetProcAddress((HMODULE)xinput_dll, get_state_func_name);
|
2016-06-15 14:40:57 +02:00
|
|
|
XInputSetState_t set_func = (XInputSetState_t)GetProcAddress((HMODULE)xinput_dll, "XInputSetState");
|
|
|
|
if (!func || !set_func) {
|
2015-12-18 19:15:32 +01:00
|
|
|
unload_xinput();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
xinput_get_state = func;
|
2016-06-15 14:40:57 +02:00
|
|
|
xinput_set_state = set_func;
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:52:38 +01:00
|
|
|
void JoypadWindows::unload_xinput() {
|
2015-12-18 19:15:32 +01:00
|
|
|
if (xinput_dll) {
|
|
|
|
FreeLibrary((HMODULE)xinput_dll);
|
|
|
|
}
|
2015-12-18 06:12:53 +01:00
|
|
|
}
|