From 8c258c3eb5f862e91493f4b9adef08118c8f8d6f Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 16 Apr 2016 15:36:51 -0400 Subject: [PATCH] Skip counting controllers that haven't received a single event yet. This closes #38 --- gamepad.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gamepad.cpp b/gamepad.cpp index bad19a1..32a06c5 100644 --- a/gamepad.cpp +++ b/gamepad.cpp @@ -20,6 +20,7 @@ static const unsigned int k_StandardGamepadTriggerButtonIndexes[] = { void MoonlightInstance::PollGamepads() { PP_GamepadsSampleData gamepadData; + short controllerIndex = 0; m_GamepadApi->Sample(pp_instance(), &gamepadData); @@ -31,8 +32,18 @@ void MoonlightInstance::PollGamepads() { continue; } + if (padData.timestamp == 0) { + // On some platforms, Chrome returns "connected" pads that + // really aren't, so timestamp stays at zero. To work around this, + // we'll only count gamepads that have a non-zero timestamp in our + // controller index. + continue; + } + if (padData.timestamp == m_LastPadTimestamps[p]) { - // No change from last poll + // No change from last poll, but this controller is still valid + // so we skip this index. + controllerIndex++; continue; } @@ -75,7 +86,8 @@ void MoonlightInstance::PollGamepads() { rightStickY = -padData.axes[3] * 0x7FFF; } - LiSendMultiControllerEvent(p, buttonFlags, leftTrigger, rightTrigger, + LiSendMultiControllerEvent(controllerIndex, buttonFlags, leftTrigger, rightTrigger, leftStickX, leftStickY, rightStickX, rightStickY); + controllerIndex++; } }