Skip to content

Commit

Permalink
Ignore rapid input when button mapping (0003)
Browse files Browse the repository at this point in the history
This introduces a cooldown, which guards against button bounce. Also, when
the user presses two buttons near-simultaneously, only the first will be
mapped preventing the wizard from jumping forward too fast.
  • Loading branch information
garbear committed Jan 14, 2016
1 parent 05d5d99 commit f45bdf5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
30 changes: 24 additions & 6 deletions xbmc/input/joysticks/generic/GenericJoystickButtonMapping.cpp
Expand Up @@ -24,6 +24,7 @@
#include "input/joysticks/JoystickTranslator.h"
#include "input/joysticks/JoystickUtils.h"
#include "threads/SystemClock.h"
#include "utils/log.h"

#include <algorithm>
#include <assert.h>
Expand All @@ -32,12 +33,14 @@
using namespace JOYSTICK;
using namespace XbmcThreads;

#define AXIS_THRESHOLD 0.5f
#define AXIS_HOLDTIME_MS 100
#define MAPPING_COOLDOWN_MS 100 // Guard against rapid input
#define AXIS_THRESHOLD 0.5f // Axis must exceed this value to be mapped
#define AXIS_HOLDTIME_MS 100 // Axis must be activated for this long to be mapped

CGenericJoystickButtonMapping::CGenericJoystickButtonMapping(IJoystickButtonMapper* buttonMapper, IJoystickButtonMap* buttonMap)
: m_buttonMapper(buttonMapper),
m_buttonMap(buttonMap)
m_buttonMap(buttonMap),
m_lastAction(0)
{
assert(m_buttonMapper != NULL);
assert(m_buttonMap != NULL);
Expand All @@ -48,7 +51,8 @@ bool CGenericJoystickButtonMapping::OnButtonMotion(unsigned int buttonIndex, boo
if (bPressed)
{
CDriverPrimitive buttonPrimitive(buttonIndex);
m_buttonMapper->MapPrimitive(m_buttonMap, buttonPrimitive);
if (buttonPrimitive.IsValid())
MapPrimitive(buttonPrimitive);
}

return true;
Expand All @@ -58,7 +62,7 @@ bool CGenericJoystickButtonMapping::OnHatMotion(unsigned int hatIndex, HAT_STATE
{
CDriverPrimitive hatPrimitive(hatIndex, static_cast<HAT_DIRECTION>(state));
if (hatPrimitive.IsValid())
m_buttonMapper->MapPrimitive(m_buttonMap, hatPrimitive);
MapPrimitive(hatPrimitive);

return true;
}
Expand Down Expand Up @@ -102,12 +106,26 @@ void CGenericJoystickButtonMapping::ProcessAxisMotions(void)
if (bHeld)
{
semiaxis.bEmitted = true;
m_buttonMapper->MapPrimitive(m_buttonMap, semiaxis.driverPrimitive);
MapPrimitive(semiaxis.driverPrimitive);
}
}
}
}

void CGenericJoystickButtonMapping::MapPrimitive(const CDriverPrimitive& primitive)
{
bool bTimeoutElapsed = (SystemClockMillis() >= m_lastAction + MAPPING_COOLDOWN_MS);
if (bTimeoutElapsed)
{
m_lastAction = SystemClockMillis();
m_buttonMapper->MapPrimitive(m_buttonMap, primitive);
}
else
{
CLog::Log(LOGDEBUG, "Button mapping: rapid input dropped for profile \"%s\"", m_buttonMapper->ControllerID().c_str());
}
}

void CGenericJoystickButtonMapping::Activate(const CDriverPrimitive& semiaxis)
{
if (!IsActive(semiaxis))
Expand Down
3 changes: 3 additions & 0 deletions xbmc/input/joysticks/generic/GenericJoystickButtonMapping.h
Expand Up @@ -58,6 +58,8 @@ namespace JOYSTICK
virtual void ProcessAxisMotions(void) override;

private:
void MapPrimitive(const CDriverPrimitive& primitive);

void Activate(const CDriverPrimitive& semiAxis);
void Deactivate(const CDriverPrimitive& semiAxis);
bool IsActive(const CDriverPrimitive& semiAxis);
Expand All @@ -73,5 +75,6 @@ namespace JOYSTICK
};

std::vector<ActivatedAxis> m_activatedAxes;
unsigned int m_lastAction;
};
}

0 comments on commit f45bdf5

Please sign in to comment.