Skip to content

Commit a6d765d

Browse files
authored
Remove interval for XInput rumbles (#18313)
1 parent a019b92 commit a6d765d

File tree

2 files changed

+27
-60
lines changed

2 files changed

+27
-60
lines changed

input/drivers_joypad/xinput_hybrid_joypad.c

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <stdlib.h>
3232
#include <stddef.h>
3333
#include <string.h>
34-
#include <time.h>
3534

3635
#include <boolean.h>
3736
#include <retro_inline.h>
@@ -69,8 +68,6 @@ typedef struct
6968
bool connected;
7069
} xinput_joypad_state;
7170

72-
#define RUMBLE_INTERVAL 0.005
73-
7471
/* TODO/FIXME - static globals */
7572
static int g_xinput_pad_indexes[MAX_USERS];
7673
static unsigned g_last_xinput_pad_idx = 0;
@@ -94,7 +91,6 @@ static XINPUT_VIBRATION g_xinput_rumble_states[4];
9491
#endif
9592
static xinput_joypad_state g_xinput_states[4];
9693
static bool xinput_active_port[4] = {0};
97-
static clock_t last_rumble_time[4] = {0};
9894

9995
static unsigned xinput_hotplug_index = 0;
10096
static unsigned xinput_poll_counter = 0;
@@ -582,7 +578,9 @@ static int16_t xinput_joypad_state_func(
582578
static void xinput_joypad_poll(void)
583579
{
584580
int i;
581+
#ifdef __WINRT__
585582
bool has_active_ports = false;
583+
#endif
586584

587585
/* Hotplugging detection: scanning one port at a time every few frames,
588586
* to avoid polling overload and framerate drops. */
@@ -613,11 +611,13 @@ static void xinput_joypad_poll(void)
613611
xinput_hotplug_index = (xinput_hotplug_index + 1) % 4;
614612
}
615613

614+
#ifdef __WINRT__
616615
for (i = 0; i < 4; ++i)
617616
{
618617
if (xinput_active_port[i])
619618
has_active_ports = true;
620619
}
620+
#endif
621621

622622
for (i = 0; i < 4; ++i)
623623
{
@@ -714,43 +714,27 @@ static void xinput_joypad_poll(void)
714714
static bool xinput_joypad_rumble(unsigned pad,
715715
enum retro_rumble_effect effect, uint16_t strength)
716716
{
717-
clock_t now;
718-
double time_since_last_rumble;
719-
XINPUT_VIBRATION new_state, *state;
717+
XINPUT_VIBRATION *state, prev;
720718
int xuser = PAD_INDEX_TO_XUSER_INDEX(pad);
721719

722720
if (xuser == -1)
723721
return dinput_joypad_set_rumble(pad, effect, strength);
724722

725-
state = &g_xinput_rumble_states[xuser];
726-
new_state = *state;
723+
state = &g_xinput_rumble_states[xuser];
724+
prev = *state;
727725

728726
/* Consider the low frequency (left) motor the "strong" one. */
729727
if (effect == RETRO_RUMBLE_STRONG)
730-
new_state.wLeftMotorSpeed = strength;
728+
state->wLeftMotorSpeed = strength;
731729
else if (effect == RETRO_RUMBLE_WEAK)
732-
new_state.wRightMotorSpeed = strength;
730+
state->wRightMotorSpeed = strength;
733731

734732
/* Rumble state unchanged? */
735-
if ( (new_state.wLeftMotorSpeed == state->wLeftMotorSpeed)
736-
&& (new_state.wRightMotorSpeed == state->wRightMotorSpeed))
737-
return true;
738-
739-
now = clock();
740-
time_since_last_rumble = (double)(now - last_rumble_time[xuser]) / CLOCKS_PER_SEC;
741-
742-
/* Rumble interval unelapsed? */
743-
if (time_since_last_rumble < RUMBLE_INTERVAL)
733+
if ( (state->wLeftMotorSpeed == prev.wLeftMotorSpeed)
734+
&& (state->wRightMotorSpeed == prev.wRightMotorSpeed))
744735
return true;
745736

746-
if (g_XInputSetState)
747-
{
748-
*state = new_state;
749-
last_rumble_time[xuser] = now;
750-
if (g_XInputSetState(xuser, state) == ERROR_SUCCESS)
751-
return true;
752-
}
753-
return false;
737+
return g_XInputSetState && (g_XInputSetState(xuser, state) == ERROR_SUCCESS);
754738
}
755739

756740
static void xinput_joypad_destroy(void)

input/drivers_joypad/xinput_joypad.c

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <stdlib.h>
2626
#include <stddef.h>
2727
#include <string.h>
28-
#include <time.h>
2928

3029
#include <boolean.h>
3130
#include <retro_inline.h>
@@ -52,8 +51,6 @@ typedef struct
5251
bool connected;
5352
} xinput_joypad_state;
5453

55-
#define RUMBLE_INTERVAL 0.005
56-
5754
/* Function pointer, to be assigned with dylib_proc */
5855
typedef uint32_t (__stdcall *XInputGetStateEx_t)(uint32_t, XINPUT_STATE*);
5956
typedef uint32_t (__stdcall *XInputSetState_t)(uint32_t, XINPUT_VIBRATION*);
@@ -75,7 +72,6 @@ static XINPUT_VIBRATION g_xinput_rumble_states[4];
7572
#endif
7673
static xinput_joypad_state g_xinput_states[4];
7774
static bool xinput_active_port[4] = {0};
78-
static clock_t last_rumble_time[4] = {0};
7975

8076
static unsigned xinput_hotplug_index = 0;
8177
static unsigned xinput_poll_counter = 0;
@@ -351,7 +347,9 @@ static int16_t xinput_joypad_state_func(
351347
static void xinput_joypad_poll(void)
352348
{
353349
int i;
350+
#ifdef __WINRT__
354351
bool has_active_ports = false;
352+
#endif
355353

356354
/* Hotplugging detection: scanning one port at a time every few frames,
357355
* to avoid polling overload and framerate drops. */
@@ -382,11 +380,13 @@ static void xinput_joypad_poll(void)
382380
xinput_hotplug_index = (xinput_hotplug_index + 1) % 4;
383381
}
384382

383+
#ifdef __WINRT__
385384
for (i = 0; i < 4; ++i)
386385
{
387386
if (xinput_active_port[i])
388387
has_active_ports = true;
389388
}
389+
#endif
390390

391391
for (i = 0; i < 4; ++i)
392392
{
@@ -397,8 +397,8 @@ static void xinput_joypad_poll(void)
397397
* If no ports are currently active, we need to poll all ports
398398
* to catch any late arriving controllers. */
399399
#ifdef __WINRT__
400-
if (!xinput_active_port[i] && has_active_ports)
401-
continue;
400+
if (!xinput_active_port[i] && has_active_ports)
401+
continue;
402402
#else
403403
if (!xinput_active_port[i])
404404
continue;
@@ -434,44 +434,27 @@ if (!xinput_active_port[i] && has_active_ports)
434434
static bool xinput_joypad_rumble(unsigned pad,
435435
enum retro_rumble_effect effect, uint16_t strength)
436436
{
437-
clock_t now;
438-
double time_since_last_rumble;
439-
XINPUT_VIBRATION *state, new_state;
440-
int xuser = pad_index_to_xuser_index(pad);
437+
XINPUT_VIBRATION *state, prev;
438+
int xuser = pad_index_to_xuser_index(pad);
441439

442440
if (xuser == -1)
443441
return false;
444442

445-
state = &g_xinput_rumble_states[xuser];
446-
new_state = *state;
443+
state = &g_xinput_rumble_states[xuser];
444+
prev = *state;
447445

448446
/* Consider the low frequency (left) motor the "strong" one. */
449447
if (effect == RETRO_RUMBLE_STRONG)
450-
new_state.wLeftMotorSpeed = strength;
448+
state->wLeftMotorSpeed = strength;
451449
else if (effect == RETRO_RUMBLE_WEAK)
452-
new_state.wRightMotorSpeed = strength;
450+
state->wRightMotorSpeed = strength;
453451

454452
/* Rumble state unchanged? */
455-
if ( (new_state.wLeftMotorSpeed == state->wLeftMotorSpeed)
456-
&& (new_state.wRightMotorSpeed == state->wRightMotorSpeed))
453+
if ( (state->wLeftMotorSpeed == prev.wLeftMotorSpeed)
454+
&& (state->wRightMotorSpeed == prev.wRightMotorSpeed))
457455
return true;
458456

459-
now = clock();
460-
time_since_last_rumble = (double)(now - last_rumble_time[xuser]) / CLOCKS_PER_SEC;
461-
462-
/* Rumble interval unelapsed? */
463-
if (time_since_last_rumble < RUMBLE_INTERVAL)
464-
return true;
465-
466-
if (g_XInputSetState)
467-
{
468-
*state = new_state;
469-
last_rumble_time[xuser] = now;
470-
if (g_XInputSetState(xuser, state) == ERROR_SUCCESS)
471-
return true;
472-
}
473-
474-
return false;
457+
return g_XInputSetState && (g_XInputSetState(xuser, state) == ERROR_SUCCESS);
475458
}
476459

477460
input_device_driver_t xinput_joypad = {

0 commit comments

Comments
 (0)