RetroArch/input/drivers_joypad/xinput_joypad.c

633 lines
18 KiB
C
Raw Normal View History

2013-09-26 13:13:35 +00:00
/* RetroArch - A frontend for libretro.
2015-01-07 16:46:50 +00:00
* Copyright (C) 2013-2015 - pinumbernumber
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
2013-09-26 13:13:35 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2015-04-02 23:19:51 +00:00
/* Support 360 controllers on Windows.
* Said controllers do show under DInput but they have limitations in this mode;
* The triggers are combined rather than seperate and it is not possible to use
* the guide button.
*
* Some wrappers for other controllers also simulate xinput (as it is easier to implement)
* so this may be useful for those also.
**/
2013-09-26 13:13:35 +00:00
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
2016-09-11 13:08:07 +00:00
#include <boolean.h>
2015-03-15 03:52:46 +00:00
#include <retro_inline.h>
2016-09-11 13:08:07 +00:00
#include <compat/strl.h>
2015-09-05 18:49:57 +00:00
#include <dynamic/dylib.h>
2016-09-11 12:59:18 +00:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include "../../config.def.h"
#include "../../tasks/tasks_internal.h"
#include "../input_driver.h"
2015-09-05 18:49:57 +00:00
2015-11-23 11:14:53 +00:00
#include "../../verbosity.h"
2015-09-05 18:49:57 +00:00
#ifdef HAVE_DINPUT
#include "dinput_joypad.h"
#endif
#if defined(__WINRT__)
#include <Xinput.h>
#endif
/* Check if the definitions do not already exist.
* Official and mingw xinput headers have different include guards.
* Windows 10 API version doesn't have an include guard at all and just uses #pragma once instead
*/
#if ((!_XINPUT_H_) && (!__WINE_XINPUT_H)) && !defined(__WINRT__)
2013-09-26 13:13:35 +00:00
#define XINPUT_GAMEPAD_DPAD_UP 0x0001
#define XINPUT_GAMEPAD_DPAD_DOWN 0x0002
#define XINPUT_GAMEPAD_DPAD_LEFT 0x0004
#define XINPUT_GAMEPAD_DPAD_RIGHT 0x0008
#define XINPUT_GAMEPAD_START 0x0010
#define XINPUT_GAMEPAD_BACK 0x0020
#define XINPUT_GAMEPAD_LEFT_THUMB 0x0040
#define XINPUT_GAMEPAD_RIGHT_THUMB 0x0080
#define XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100
#define XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200
#define XINPUT_GAMEPAD_A 0x1000
#define XINPUT_GAMEPAD_B 0x2000
#define XINPUT_GAMEPAD_X 0x4000
#define XINPUT_GAMEPAD_Y 0x8000
typedef struct
{
uint16_t wButtons;
uint8_t bLeftTrigger;
uint8_t bRightTrigger;
int16_t sThumbLX;
int16_t sThumbLY;
int16_t sThumbRX;
int16_t sThumbRY;
} XINPUT_GAMEPAD;
typedef struct
{
uint32_t dwPacketNumber;
XINPUT_GAMEPAD Gamepad;
} XINPUT_STATE;
typedef struct
{
uint16_t wLeftMotorSpeed;
uint16_t wRightMotorSpeed;
} XINPUT_VIBRATION;
#endif
/* Guide constant is not officially documented. */
2013-09-26 13:13:35 +00:00
#define XINPUT_GAMEPAD_GUIDE 0x0400
#ifndef ERROR_DEVICE_NOT_CONNECTED
#define ERROR_DEVICE_NOT_CONNECTED 1167
#endif
2019-01-05 20:50:58 +00:00
#ifdef HAVE_DINPUT
/* Due to 360 pads showing up under both XInput and DirectInput,
* and since we are going to have to pass through unhandled
* joypad numbers to DirectInput, a slightly ugly
* hack is required here. dinput_joypad_init will fill this.
*
* For each pad index, the appropriate entry will be set to -1 if it is not
* a 360 pad, or the correct XInput user number (0..3 inclusive) if it is.
*/
2015-01-05 00:58:00 +00:00
extern int g_xinput_pad_indexes[MAX_USERS];
2013-09-26 13:13:35 +00:00
extern bool g_xinput_block_pads;
2019-01-05 20:50:58 +00:00
#endif
2013-09-26 13:13:35 +00:00
2018-01-24 00:29:45 +00:00
#ifdef HAVE_DYNAMIC
/* For xinput1_n.dll */
2018-07-12 19:39:18 +00:00
static dylib_t g_xinput_dll = NULL;
2018-01-24 00:29:45 +00:00
#endif
2013-09-26 13:13:35 +00:00
/* Function pointer, to be assigned with dylib_proc */
2013-09-26 13:13:35 +00:00
typedef uint32_t (__stdcall *XInputGetStateEx_t)(uint32_t, XINPUT_STATE*);
static XInputGetStateEx_t g_XInputGetStateEx;
typedef uint32_t (__stdcall *XInputSetState_t)(uint32_t, XINPUT_VIBRATION*);
static XInputSetState_t g_XInputSetState;
/* Guide button may or may not be available */
static bool g_xinput_guide_button_supported = false;
static unsigned g_xinput_num_buttons = 0;
2013-09-26 13:13:35 +00:00
typedef struct
{
XINPUT_STATE xstate;
bool connected;
2015-04-02 23:37:20 +00:00
} xinput_joypad_state;
2013-09-26 13:13:35 +00:00
2020-06-29 00:44:35 +00:00
/* TODO/FIXME - static globals */
2013-09-26 13:13:35 +00:00
static XINPUT_VIBRATION g_xinput_rumble_states[4];
2015-04-02 23:37:20 +00:00
static xinput_joypad_state g_xinput_states[4];
2013-09-26 13:13:35 +00:00
2020-07-20 01:19:14 +00:00
/* Buttons are provided by XInput as bits of a uint16.
* Map from rarch button index (0..10) to a mask to
* bitwise-& the buttons against.
* dpad is handled seperately. */
static const uint16_t button_index_to_bitmap_code[] = {
XINPUT_GAMEPAD_A,
XINPUT_GAMEPAD_B,
XINPUT_GAMEPAD_X,
XINPUT_GAMEPAD_Y,
XINPUT_GAMEPAD_LEFT_SHOULDER,
XINPUT_GAMEPAD_RIGHT_SHOULDER,
XINPUT_GAMEPAD_START,
XINPUT_GAMEPAD_BACK,
XINPUT_GAMEPAD_LEFT_THUMB,
XINPUT_GAMEPAD_RIGHT_THUMB,
XINPUT_GAMEPAD_GUIDE
};
static INLINE int pad_index_to_xuser_index(unsigned pad)
{
2019-01-05 20:50:58 +00:00
#ifdef HAVE_DINPUT
return g_xinput_pad_indexes[pad];
2019-01-05 20:50:58 +00:00
#else
return pad < DEFAULT_MAX_PADS
&& g_xinput_states[pad].connected ? pad : -1;
2019-01-05 20:50:58 +00:00
#endif
}
2013-09-26 13:13:35 +00:00
2020-07-20 11:34:29 +00:00
/* Generic 'XInput' instead of 'Xbox 360', because
* there are some other non-Xbox third party PC
* controllers */
static const char XBOX_CONTROLLER_NAME[] = "XInput Controller";
2016-12-18 18:44:15 +00:00
static const char *xinput_joypad_name(unsigned pad)
2013-09-26 13:13:35 +00:00
{
#ifdef HAVE_DINPUT
2020-07-20 11:34:29 +00:00
/* On platforms with dinput support, we are able
* to get a name from the device itself */
return dinput_joypad.name(pad);
#else
2020-07-20 12:24:18 +00:00
if (pad_index_to_xuser_index(pad) < 0)
return NULL;
2020-07-20 11:34:29 +00:00
/* On platforms without dinput support, no
* device-specific name is available
* > Have to use generic names instead */
return XBOX_CONTROLLER_NAME;
#endif
2013-09-26 13:13:35 +00:00
}
#if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
2018-07-12 19:39:18 +00:00
static bool load_xinput_dll(void)
2013-09-26 13:13:35 +00:00
{
const char *version = "1.4";
/* Find the correct path to load the DLL from.
* Usually this will be from the system directory,
* but occasionally a user may wish to use a third-party
* wrapper DLL (such as x360ce); support these by checking
* the working directory first.
*
* No need to check for existance as we will be checking dylib_load's
* success anyway.
*/
g_xinput_dll = dylib_load("xinput1_4.dll");
2015-04-02 23:37:20 +00:00
if (!g_xinput_dll)
2013-09-26 13:13:35 +00:00
{
2015-09-05 12:10:16 +00:00
g_xinput_dll = dylib_load("xinput1_3.dll");
2013-09-26 13:13:35 +00:00
version = "1.3";
}
2015-04-02 23:37:20 +00:00
if (!g_xinput_dll)
2013-09-26 13:13:35 +00:00
{
2017-07-01 02:37:32 +00:00
RARCH_ERR("[XInput]: Failed to load XInput, ensure DirectX and controller drivers are up to date.\n");
2013-09-26 13:13:35 +00:00
return false;
}
2017-07-01 02:37:32 +00:00
RARCH_LOG("[XInput]: Found XInput v%s.\n", version);
2018-07-12 19:39:18 +00:00
return true;
}
#endif
static bool xinput_joypad_init(void *data)
{
unsigned i, j;
XINPUT_STATE dummy_state;
#if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
2018-07-12 19:39:18 +00:00
if (!g_xinput_dll)
if (!load_xinput_dll())
goto error;
2013-09-26 13:13:35 +00:00
/* If we get here then an xinput DLL is correctly loaded.
* First try to load ordinal 100 (XInputGetStateEx).
*/
2019-06-23 01:23:16 +00:00
g_XInputGetStateEx = (XInputGetStateEx_t)dylib_proc(
g_xinput_dll, (const char*)100);
#elif defined(__WINRT__)
/* XInputGetStateEx is not available on WinRT */
g_XInputGetStateEx = NULL;
2018-01-24 00:29:45 +00:00
#else
g_XInputGetStateEx = (XInputGetStateEx_t)XInputGetStateEx;
#endif
2015-04-02 23:37:20 +00:00
g_xinput_guide_button_supported = true;
2013-09-26 13:13:35 +00:00
if (!g_XInputGetStateEx)
{
/* no ordinal 100. (Presumably a wrapper.) Load the ordinary
* XInputGetState, at the cost of losing guide button support.
*/
2015-04-02 23:37:20 +00:00
g_xinput_guide_button_supported = false;
#if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
2019-06-23 01:23:16 +00:00
g_XInputGetStateEx = (XInputGetStateEx_t)dylib_proc(
g_xinput_dll, "XInputGetState");
2018-01-24 00:29:45 +00:00
#else
g_XInputGetStateEx = (XInputGetStateEx_t)XInputGetState;
#endif
2013-09-26 13:13:35 +00:00
if (!g_XInputGetStateEx)
{
2017-07-01 02:37:32 +00:00
RARCH_ERR("[XInput]: Failed to init: DLL is invalid or corrupt.\n");
#if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
2015-04-09 03:46:07 +00:00
dylib_close(g_xinput_dll);
2018-01-24 00:29:45 +00:00
#endif
2019-06-23 01:23:16 +00:00
/* DLL was loaded but did not contain the correct function. */
goto error;
2013-09-26 13:13:35 +00:00
}
2017-07-01 02:37:32 +00:00
RARCH_WARN("[XInput]: No guide button support.\n");
2013-09-26 13:13:35 +00:00
}
#if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
2019-06-23 01:23:16 +00:00
g_XInputSetState = (XInputSetState_t)dylib_proc(
g_xinput_dll, "XInputSetState");
2018-01-24 00:29:45 +00:00
#else
g_XInputSetState = (XInputSetState_t)XInputSetState;
#endif
2013-09-26 13:13:35 +00:00
if (!g_XInputSetState)
{
2017-07-01 02:37:32 +00:00
RARCH_ERR("[XInput]: Failed to init: DLL is invalid or corrupt.\n");
#if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
2015-04-09 03:46:07 +00:00
dylib_close(g_xinput_dll);
2018-01-24 00:29:45 +00:00
#endif
2019-07-27 02:28:50 +00:00
goto error; /* DLL was loaded but did not contain the correct function. */
2013-09-26 13:13:35 +00:00
}
/* Zero out the states. */
2013-10-22 19:26:33 +00:00
for (i = 0; i < 4; ++i)
2013-09-26 13:13:35 +00:00
{
2020-06-29 00:44:35 +00:00
g_xinput_states[i].xstate.dwPacketNumber = 0;
g_xinput_states[i].xstate.Gamepad.wButtons = 0;
g_xinput_states[i].xstate.Gamepad.bLeftTrigger = 0;
g_xinput_states[i].xstate.Gamepad.bRightTrigger = 0;
g_xinput_states[i].xstate.Gamepad.sThumbLX = 0;
g_xinput_states[i].xstate.Gamepad.sThumbLY = 0;
g_xinput_states[i].xstate.Gamepad.sThumbRX = 0;
g_xinput_states[i].xstate.Gamepad.sThumbRY = 0;
g_xinput_states[i].connected =
!(g_XInputGetStateEx(i, &dummy_state) == ERROR_DEVICE_NOT_CONNECTED);
2013-09-26 13:13:35 +00:00
}
2019-06-23 01:23:16 +00:00
if ( (!g_xinput_states[0].connected) &&
2015-04-02 23:37:20 +00:00
(!g_xinput_states[1].connected) &&
(!g_xinput_states[2].connected) &&
(!g_xinput_states[3].connected))
#ifdef __WINRT__
goto succeeded;
#else
goto error;
#endif
2017-12-05 19:08:18 +00:00
#ifdef HAVE_DINPUT
2019-01-05 20:50:58 +00:00
g_xinput_block_pads = true;
/* We're going to have to be buddies with dinput if we want to be able
* to use XInput and non-XInput controllers together. */
2015-06-04 13:33:22 +00:00
if (!dinput_joypad.init(data))
2013-09-26 13:13:35 +00:00
{
g_xinput_block_pads = false;
goto error;
2013-09-26 13:13:35 +00:00
}
#endif
2013-09-26 13:13:35 +00:00
for (j = 0; j < MAX_USERS; j++)
2013-09-26 13:13:35 +00:00
{
const char *name = xinput_joypad_name(j);
2017-01-13 03:42:26 +00:00
if (pad_index_to_xuser_index(j) > -1)
{
2017-12-06 03:33:54 +00:00
int32_t vid = 0;
int32_t pid = 0;
#ifdef HAVE_DINPUT
2017-12-06 03:33:54 +00:00
int32_t dinput_index = 0;
2019-07-27 02:28:50 +00:00
bool success = dinput_joypad_get_vidpid_from_xinput_index((int32_t)pad_index_to_xuser_index(j), (int32_t*)&vid, (int32_t*)&pid,
(int32_t*)&dinput_index);
/* On success, found VID/PID from dinput index */
#endif
input_autoconfigure_connect(
name,
2016-12-31 06:43:34 +00:00
NULL,
xinput_joypad.ident,
j,
vid,
pid);
}
2013-09-26 13:13:35 +00:00
}
#ifdef __WINRT__
succeeded:
#endif
/* non-hat button. */
g_xinput_num_buttons = g_xinput_guide_button_supported ? 11 : 10;
2013-09-26 13:13:35 +00:00
return true;
error:
/* non-hat button. */
g_xinput_num_buttons = g_xinput_guide_button_supported ? 11 : 10;
return false;
2013-09-26 13:13:35 +00:00
}
2015-04-02 23:37:20 +00:00
static bool xinput_joypad_query_pad(unsigned pad)
2013-09-26 13:13:35 +00:00
{
int xuser = pad_index_to_xuser_index(pad);
2019-07-27 02:28:50 +00:00
if (xuser > -1)
return g_xinput_states[xuser].connected;
#ifdef HAVE_DINPUT
return dinput_joypad.query_pad(pad);
#else
return false;
#endif
2013-09-26 13:13:35 +00:00
}
2015-04-02 23:37:20 +00:00
static void xinput_joypad_destroy(void)
2013-09-26 13:13:35 +00:00
{
2013-10-22 19:26:33 +00:00
unsigned i;
2015-04-02 23:19:51 +00:00
2013-10-22 19:26:33 +00:00
for (i = 0; i < 4; ++i)
2020-06-29 00:44:35 +00:00
{
g_xinput_states[i].xstate.dwPacketNumber = 0;
g_xinput_states[i].xstate.Gamepad.wButtons = 0;
g_xinput_states[i].xstate.Gamepad.bLeftTrigger = 0;
g_xinput_states[i].xstate.Gamepad.bRightTrigger = 0;
g_xinput_states[i].xstate.Gamepad.sThumbLX = 0;
g_xinput_states[i].xstate.Gamepad.sThumbLY = 0;
g_xinput_states[i].xstate.Gamepad.sThumbRX = 0;
g_xinput_states[i].xstate.Gamepad.sThumbRY = 0;
g_xinput_states[i].connected = false;
}
2013-09-26 13:13:35 +00:00
#if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
2015-04-09 03:46:07 +00:00
dylib_close(g_xinput_dll);
2015-04-09 03:46:07 +00:00
g_xinput_dll = NULL;
2018-01-24 00:29:45 +00:00
#endif
2015-04-09 03:46:07 +00:00
g_XInputGetStateEx = NULL;
g_XInputSetState = NULL;
2013-09-26 13:13:35 +00:00
#ifdef HAVE_DINPUT
2013-09-26 13:13:35 +00:00
dinput_joypad.destroy();
2015-04-09 03:46:07 +00:00
2013-09-26 13:13:35 +00:00
g_xinput_block_pads = false;
2019-01-05 20:50:58 +00:00
#endif
2013-09-26 13:13:35 +00:00
}
static int16_t xinput_joypad_button_state(
2020-07-20 01:19:14 +00:00
unsigned xuser, uint16_t btn_word,
unsigned port, uint16_t joykey)
2013-09-26 13:13:35 +00:00
{
unsigned hat_dir = GET_HAT_DIR(joykey);
2019-07-27 02:28:50 +00:00
if (hat_dir)
2013-09-26 13:13:35 +00:00
{
switch (hat_dir)
2013-09-26 13:13:35 +00:00
{
case HAT_UP_MASK:
return (btn_word & XINPUT_GAMEPAD_DPAD_UP);
case HAT_DOWN_MASK:
return (btn_word & XINPUT_GAMEPAD_DPAD_DOWN);
case HAT_LEFT_MASK:
return (btn_word & XINPUT_GAMEPAD_DPAD_LEFT);
case HAT_RIGHT_MASK:
return (btn_word & XINPUT_GAMEPAD_DPAD_RIGHT);
default:
break;
2013-09-26 13:13:35 +00:00
}
/* hat requested and no hat button down */
2013-09-26 13:13:35 +00:00
}
else if (joykey < g_xinput_num_buttons)
return (btn_word & button_index_to_bitmap_code[joykey]);
return 0;
2013-09-26 13:13:35 +00:00
}
static int16_t xinput_joypad_button(unsigned port, uint16_t joykey)
2013-09-26 13:13:35 +00:00
{
2020-07-20 01:19:14 +00:00
int xuser = pad_index_to_xuser_index(port);
uint16_t btn_word = 0;
#ifdef HAVE_DINPUT
2019-07-27 02:28:50 +00:00
if (xuser == -1)
return dinput_joypad.button(port, joykey);
#endif
2019-07-27 02:28:50 +00:00
if (!(g_xinput_states[xuser].connected))
return 0;
2020-07-20 01:19:14 +00:00
btn_word = g_xinput_states[xuser].xstate.Gamepad.wButtons;
return xinput_joypad_button_state(xuser, btn_word, port, joykey);
}
2013-09-26 13:13:35 +00:00
static int16_t xinput_joypad_axis_state(
XINPUT_GAMEPAD *pad,
unsigned port, uint32_t joyaxis)
{
int16_t val = 0;
int axis = -1;
bool is_neg = false;
bool is_pos = false;
2014-09-09 16:15:17 +00:00
/* triggers (axes 4,5) cannot be negative */
if (AXIS_NEG_GET(joyaxis) <= 3)
2013-09-26 13:13:35 +00:00
{
axis = AXIS_NEG_GET(joyaxis);
is_neg = true;
2013-09-26 13:13:35 +00:00
}
else if (AXIS_POS_GET(joyaxis) <= 5)
{
axis = AXIS_POS_GET(joyaxis);
is_pos = true;
2013-09-26 13:13:35 +00:00
}
else
return 0;
2013-09-26 13:13:35 +00:00
switch (axis)
{
2014-09-09 16:15:17 +00:00
case 0:
val = pad->sThumbLX;
break;
case 1:
val = pad->sThumbLY;
break;
case 2:
val = pad->sThumbRX;
break;
case 3:
val = pad->sThumbRY;
break;
case 4:
val = pad->bLeftTrigger * 32767 / 255;
break; /* map 0..255 to 0..32767 */
case 5:
val = pad->bRightTrigger * 32767 / 255;
break;
2013-09-26 13:13:35 +00:00
}
if (is_neg && val > 0)
return 0;
2013-09-26 13:13:35 +00:00
else if (is_pos && val < 0)
return 0;
2014-09-09 16:15:17 +00:00
/* Clamp to avoid overflow error. */
else if (val == -32768)
return -32767;
2013-09-26 13:13:35 +00:00
return val;
}
static int16_t xinput_joypad_axis(unsigned port, uint32_t joyaxis)
{
int xuser = pad_index_to_xuser_index(port);
XINPUT_GAMEPAD *pad = &(g_xinput_states[xuser].xstate.Gamepad);
#ifdef HAVE_DINPUT
if (xuser == -1)
return dinput_joypad.axis(port, joyaxis);
#endif
if (!(g_xinput_states[xuser].connected))
return 0;
return xinput_joypad_axis_state(pad, port, joyaxis);
}
static int16_t xinput_joypad_state_func(
rarch_joypad_info_t *joypad_info,
const struct retro_keybind *binds,
unsigned port)
{
unsigned i;
2020-07-20 01:19:14 +00:00
uint16_t btn_word;
int16_t ret = 0;
2020-07-29 03:31:23 +00:00
uint16_t port_idx = joypad_info->joy_idx;
int xuser = pad_index_to_xuser_index(port_idx);
XINPUT_GAMEPAD *pad = &(g_xinput_states[xuser].xstate.Gamepad);
#ifdef HAVE_DINPUT
if (xuser == -1)
2020-07-29 03:31:23 +00:00
return dinput_joypad.state(joypad_info, binds, port_idx);
#endif
if (!(g_xinput_states[xuser].connected))
return 0;
2020-07-20 01:19:14 +00:00
btn_word = g_xinput_states[xuser].xstate.Gamepad.wButtons;
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
/* Auto-binds are per joypad, not per user. */
const uint64_t joykey = (binds[i].joykey != NO_BTN)
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
if (
(uint16_t)joykey != NO_BTN
&& xinput_joypad_button_state(
2020-07-29 03:31:23 +00:00
xuser, btn_word, port_idx, (uint16_t)joykey))
ret |= ( 1 << i);
else if (joyaxis != AXIS_NONE &&
2020-07-29 03:31:23 +00:00
((float)abs(xinput_joypad_axis_state(pad, port_idx, joyaxis))
/ 0x8000) > joypad_info->axis_threshold)
ret |= (1 << i);
}
return ret;
}
2015-04-02 23:37:20 +00:00
static void xinput_joypad_poll(void)
2013-09-26 13:13:35 +00:00
{
2013-10-22 19:26:33 +00:00
unsigned i;
2013-10-22 19:26:33 +00:00
for (i = 0; i < 4; ++i)
2014-09-09 16:15:17 +00:00
{
bool new_connected = g_XInputGetStateEx(i, &(g_xinput_states[i].xstate)) != ERROR_DEVICE_NOT_CONNECTED;
if (new_connected != g_xinput_states[i].connected)
{
#ifndef HAVE_DINPUT
/* Normally, dinput handles device insertion/removal for us, but
* since dinput is not available on UWP we have to do it ourselves */
/* Also note that on UWP, the controllers are not available on startup
* and are instead 'plugged in' a moment later because Microsoft reasons */
/* TODO: This may be bad for performance? */
if (new_connected)
{
/* This is kinda ugly, but it's the same thing that dinput does */
xinput_joypad_destroy();
xinput_joypad_init(NULL);
return;
}
#endif
g_xinput_states[i].connected = new_connected;
if (!g_xinput_states[i].connected)
input_autoconfigure_disconnect(i, xinput_joypad_name(i));
}
2014-09-09 16:15:17 +00:00
}
2013-09-26 13:13:35 +00:00
#ifdef HAVE_DINPUT
2013-09-26 13:13:35 +00:00
dinput_joypad.poll();
#endif
2013-09-26 13:13:35 +00:00
}
2015-04-02 23:37:20 +00:00
static bool xinput_joypad_rumble(unsigned pad,
2014-09-09 16:15:17 +00:00
enum retro_rumble_effect effect, uint16_t strength)
2013-09-26 13:13:35 +00:00
{
2014-12-05 12:53:49 +00:00
int xuser = pad_index_to_xuser_index(pad);
2014-12-05 12:53:49 +00:00
if (xuser == -1)
2013-09-26 13:13:35 +00:00
{
2019-07-27 02:28:50 +00:00
#ifdef HAVE_DINPUT
2013-09-26 13:13:35 +00:00
if (dinput_joypad.set_rumble)
return dinput_joypad.set_rumble(pad, effect, strength);
2019-07-27 02:28:50 +00:00
#endif
2014-08-27 01:28:22 +00:00
return false;
2013-09-26 13:13:35 +00:00
}
2014-09-09 16:15:17 +00:00
/* Consider the low frequency (left) motor the "strong" one. */
2013-09-26 13:13:35 +00:00
if (effect == RETRO_RUMBLE_STRONG)
2014-12-05 12:53:49 +00:00
g_xinput_rumble_states[xuser].wLeftMotorSpeed = strength;
2013-09-26 13:13:35 +00:00
else if (effect == RETRO_RUMBLE_WEAK)
2014-12-05 12:53:49 +00:00
g_xinput_rumble_states[xuser].wRightMotorSpeed = strength;
2013-09-26 13:13:35 +00:00
2019-07-27 02:28:50 +00:00
if (!g_XInputSetState)
return false;
return (g_XInputSetState(xuser, &g_xinput_rumble_states[xuser])
2015-09-05 12:10:16 +00:00
== 0);
2013-09-26 13:13:35 +00:00
}
input_device_driver_t xinput_joypad = {
2015-04-02 23:37:20 +00:00
xinput_joypad_init,
xinput_joypad_query_pad,
xinput_joypad_destroy,
xinput_joypad_button,
xinput_joypad_state_func,
2015-02-15 00:57:29 +00:00
NULL,
2015-04-02 23:37:20 +00:00
xinput_joypad_axis,
xinput_joypad_poll,
xinput_joypad_rumble,
xinput_joypad_name,
"xinput",
2013-09-26 13:13:35 +00:00
};