Skip to content

Commit

Permalink
Joysticks: Remove input type from interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
garbear committed Jun 21, 2017
1 parent b3d2aa5 commit 89a5cb9
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 124 deletions.
5 changes: 0 additions & 5 deletions xbmc/games/addons/GameClientJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ bool CGameClientJoystick::AcceptsInput(void)
return m_gameClient->AcceptsInput();
}

JOYSTICK::INPUT_TYPE CGameClientJoystick::GetInputType(const std::string& feature) const
{
return m_controller->GetInputType(feature);
}

bool CGameClientJoystick::OnButtonPress(const std::string& feature, bool bPressed)
{
bool bHandled = false;
Expand Down
1 change: 0 additions & 1 deletion xbmc/games/addons/GameClientJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ namespace GAME
virtual std::string ControllerID(void) const override;
virtual bool HasFeature(const std::string& feature) const override;
virtual bool AcceptsInput(void) override;
virtual JOYSTICK::INPUT_TYPE GetInputType(const std::string& feature) const override;
virtual unsigned int GetDelayMs(const std::string& feature) const override { return 0; }
virtual bool OnButtonPress(const std::string& feature, bool bPressed) override;
virtual void OnButtonHold(const std::string& feature, unsigned int holdTimeMs) override { }
Expand Down
8 changes: 0 additions & 8 deletions xbmc/games/ports/InputSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,3 @@ bool CInputSink::AcceptsInput(void)
{
return m_gameClient.AcceptsInput();
}

JOYSTICK::INPUT_TYPE CInputSink::GetInputType(const std::string& feature) const
{
// Convert all input to analog. This is done to simplify this function
// and avoid any extra dependencies. Analog is chosen to avoid any
// thresholding effects.
return JOYSTICK::INPUT_TYPE::ANALOG;
}
1 change: 0 additions & 1 deletion xbmc/games/ports/InputSink.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ namespace GAME
virtual std::string ControllerID(void) const override;
virtual bool HasFeature(const std::string& feature) const override { return true; }
virtual bool AcceptsInput(void) override;
virtual JOYSTICK::INPUT_TYPE GetInputType(const std::string& feature) const override;
virtual unsigned int GetDelayMs(const std::string& feature) const override { return 0; }
virtual bool OnButtonPress(const std::string& feature, bool bPressed) override { return true; }
virtual void OnButtonHold(const std::string& feature, unsigned int holdTimeMs) override { }
Expand Down
67 changes: 9 additions & 58 deletions xbmc/input/joysticks/DefaultJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ bool CDefaultJoystick::AcceptsInput()
return g_application.IsAppFocused();
}

INPUT_TYPE CDefaultJoystick::GetInputType(const FeatureName& feature) const
{
return m_handler->GetInputType(GetKeyID(feature), GetWindowID(), GetFallthrough());
}

unsigned int CDefaultJoystick::GetDelayMs(const FeatureName& feature) const
{
return m_handler->GetHoldTimeMs(GetKeyID(feature), GetWindowID(), GetFallthrough());
Expand All @@ -91,13 +86,8 @@ bool CDefaultJoystick::OnButtonPress(const FeatureName& feature, bool bPressed)
const int windowId = GetWindowID();
const bool bFallthrough = GetFallthrough();

if (m_handler->GetInputType(keyId, windowId, bFallthrough) == INPUT_TYPE::DIGITAL)
{
m_handler->OnDigitalKey(keyId, windowId, bFallthrough, bPressed);
return true;
}

return false;
m_handler->OnDigitalKey(keyId, windowId, bFallthrough, bPressed);
return true;
}

void CDefaultJoystick::OnButtonHold(const FeatureName& feature, unsigned int holdTimeMs)
Expand All @@ -115,13 +105,8 @@ bool CDefaultJoystick::OnButtonMotion(const FeatureName& feature, float magnitud
const int windowId = GetWindowID();
const bool bFallthrough = GetFallthrough();

if (m_handler->GetInputType(keyId, windowId, bFallthrough) == INPUT_TYPE::ANALOG)
{
m_handler->OnAnalogKey(keyId, windowId, bFallthrough, magnitude, motionTimeMs);
return true;
}

return false;
m_handler->OnAnalogKey(keyId, windowId, bFallthrough, magnitude, motionTimeMs);
return true;
}

bool CDefaultJoystick::OnAnalogStickMotion(const FeatureName& feature, float x, float y, unsigned int motionTimeMs)
Expand Down Expand Up @@ -178,34 +163,9 @@ bool CDefaultJoystick::ActivateDirection(const FeatureName& feature, float magni
const unsigned int keyId = GetKeyID(feature, dir);
const int windowId = GetWindowID();
const bool bFallthrough = GetFallthrough();
const INPUT_TYPE inputType = m_handler->GetInputType(keyId, windowId, bFallthrough);

if (inputType == INPUT_TYPE::DIGITAL)
{
unsigned int holdTimeMs = 0;

const bool bIsPressed = (magnitude >= ANALOG_DIGITAL_THRESHOLD);
if (bIsPressed)
{
const bool bIsHeld = (m_holdStartTimes.find(keyId) != m_holdStartTimes.end());
if (bIsHeld)
holdTimeMs = motionTimeMs - m_holdStartTimes[keyId];
else
m_holdStartTimes[keyId] = motionTimeMs;
}
else
{
m_holdStartTimes.erase(keyId);
}

m_handler->OnDigitalKey(keyId, windowId, bFallthrough, bIsPressed, holdTimeMs);
bHandled = true;
}
else if (inputType == INPUT_TYPE::ANALOG)
{
m_handler->OnAnalogKey(keyId, windowId, bFallthrough, magnitude);
bHandled = true;
}
m_handler->OnAnalogKey(keyId, windowId, bFallthrough, magnitude, motionTimeMs);
bHandled = true;

if (bHandled)
m_currentDirections[feature] = dir;
Expand All @@ -221,18 +181,9 @@ void CDefaultJoystick::DeactivateDirection(const FeatureName& feature, ANALOG_ST
const unsigned int keyId = GetKeyID(feature, dir);
const int windowId = GetWindowID();
const bool bFallthrough = GetFallthrough();
const INPUT_TYPE inputType = m_handler->GetInputType(keyId, windowId, bFallthrough);

if (inputType == INPUT_TYPE::DIGITAL)
{
m_handler->OnDigitalKey(keyId, windowId, bFallthrough, false, 0);
}
else if (inputType == INPUT_TYPE::ANALOG)
{
m_handler->OnAnalogKey(keyId, windowId, bFallthrough, 0.0f, 0);
}

m_holdStartTimes.erase(keyId);

m_handler->OnAnalogKey(keyId, windowId, bFallthrough, 0.0f, 0);

m_currentDirections[feature] = ANALOG_STICK_DIRECTION::UNKNOWN;
}
}
Expand Down
2 changes: 0 additions & 2 deletions xbmc/input/joysticks/DefaultJoystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace JOYSTICK
virtual std::string ControllerID(void) const override;
virtual bool HasFeature(const FeatureName& feature) const override;
virtual bool AcceptsInput(void) override;
virtual INPUT_TYPE GetInputType(const FeatureName& feature) const override;
virtual unsigned int GetDelayMs(const FeatureName& feature) const override;
virtual bool OnButtonPress(const FeatureName& feature, bool bPressed) override;
virtual void OnButtonHold(const FeatureName& feature, unsigned int holdTimeMs) override;
Expand Down Expand Up @@ -118,7 +117,6 @@ namespace JOYSTICK
IKeymapHandler* const m_handler;

// State variables used to process joystick input
std::map<unsigned int, unsigned int> m_holdStartTimes; // Key ID -> hold start time (ms)
std::map<FeatureName, ANALOG_STICK_DIRECTION> m_currentDirections; // Analog stick name -> direction
};
}
Expand Down
8 changes: 0 additions & 8 deletions xbmc/input/joysticks/IInputHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ namespace JOYSTICK
*/
virtual bool AcceptsInput(void) = 0;

/*!
* \brief Get the type of input handled by the specified feature
*
* \return INPUT_TYPE::DIGITAL for digital buttons, INPUT::ANALOG for analog
* buttons, or INPUT::UNKNOWN otherwise
*/
virtual INPUT_TYPE GetInputType(const FeatureName& feature) const = 0;

/*!
* \brief Get the delay before this feature should be processed
*
Expand Down
12 changes: 0 additions & 12 deletions xbmc/input/joysticks/IKeymapHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ namespace JOYSTICK
public:
virtual ~IKeymapHandler() = default;

/*!
* \brief Get the type of action mapped to the specified key ID
*
* \param keyId The key ID from Key.h
* \param windowId The window ID from WindowIDs.h
* \param bFallthrough Use a key from an underlying window or global keymap
*
* \return The type of action mapped to keyId, or INPUT_TYPE::UNKNOWN if
* no action is mapped to the specified key
*/
virtual INPUT_TYPE GetInputType(unsigned int keyId, int windowId, bool bFallthrough) const = 0;

/*!
* \brief Get the action ID mapped to the specified key ID
*
Expand Down
68 changes: 43 additions & 25 deletions xbmc/input/joysticks/KeymapHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
using namespace KODI;
using namespace JOYSTICK;

#define DIGITAL_ANALOG_THRESHOLD 0.5f

#define HOLD_TIMEOUT_MS 500
#define REPEAT_TIMEOUT_MS 50

Expand All @@ -45,24 +47,6 @@ CKeymapHandler::~CKeymapHandler(void)
{
}

INPUT_TYPE CKeymapHandler::GetInputType(unsigned int keyId, int windowId, bool bFallthrough) const
{
CAction action(ACTION_NONE);

if (keyId != 0)
action = CButtonTranslator::GetInstance().GetAction(windowId, CKey(keyId), bFallthrough);

if (action.GetID() > ACTION_NONE)
{
if (action.IsAnalog())
return INPUT_TYPE::ANALOG;
else
return INPUT_TYPE::DIGITAL;
}

return INPUT_TYPE::UNKNOWN;
}

unsigned int CKeymapHandler::GetActionID(unsigned int keyId, int windowId, bool bFallthrough) const
{
CAction action(ACTION_NONE);
Expand All @@ -84,30 +68,64 @@ unsigned int CKeymapHandler::GetHoldTimeMs(unsigned int keyId, int windowId, boo

void CKeymapHandler::OnDigitalKey(unsigned int keyId, int windowId, bool bFallthrough, bool bPressed, unsigned int holdTimeMs /* = 0 */)
{
CAction action(ACTION_NONE);

if (keyId != 0)
action = CButtonTranslator::GetInstance().GetAction(windowId, CKey(keyId, holdTimeMs), bFallthrough);

if (action.GetID() != ACTION_NONE)
{
if (bPressed)
if (action.IsAnalog())
{
CAction action(CButtonTranslator::GetInstance().GetAction(windowId, CKey(keyId, holdTimeMs), bFallthrough));
SendAction(action);
OnAnalogKey(keyId, windowId, bFallthrough, bPressed ? 1.0f : 0.0f, 0);
}
else
{
ProcessButtonRelease(keyId);
if (bPressed)
SendDigitalAction(action);
else
ProcessButtonRelease(keyId);
}
}
}

void CKeymapHandler::OnAnalogKey(unsigned int keyId, int windowId, bool bFallthrough, float magnitude, unsigned int motionTimeMs)
{
CAction action(ACTION_NONE);

if (keyId != 0)
action = CButtonTranslator::GetInstance().GetAction(windowId, CKey(keyId), bFallthrough);

if (action.GetID() != ACTION_NONE)
{
CAction action(CButtonTranslator::GetInstance().GetAction(windowId, CKey(keyId), bFallthrough));
m_actionHandler->SendAnalogAction(action, magnitude);
if (action.IsAnalog())
{
m_actionHandler->SendAnalogAction(action, magnitude);
}
else
{
unsigned int holdTimeMs = 0;

const bool bIsPressed = (magnitude >= DIGITAL_ANALOG_THRESHOLD);
if (bIsPressed)
{
const bool bIsHeld = (m_holdStartTimes.find(keyId) != m_holdStartTimes.end());
if (bIsHeld)
holdTimeMs = motionTimeMs - m_holdStartTimes[keyId];
else
m_holdStartTimes[keyId] = motionTimeMs;
}
else
{
m_holdStartTimes.erase(keyId);
}

OnDigitalKey(keyId, windowId, bFallthrough, bIsPressed, holdTimeMs);
}
}
}

void CKeymapHandler::SendAction(const CAction& action)
void CKeymapHandler::SendDigitalAction(const CAction& action)
{
const unsigned int keyId = action.GetButtonCode();
const unsigned int holdTimeMs = action.GetHoldTime();
Expand Down
5 changes: 3 additions & 2 deletions xbmc/input/joysticks/KeymapHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ namespace JOYSTICK
virtual ~CKeymapHandler(void);

// implementation of IKeymapHandler
virtual INPUT_TYPE GetInputType(unsigned int keyId, int windowId, bool bFallthrough) const override;
virtual unsigned int GetActionID(unsigned int keyId, int windowId, bool bFallthrough) const override;
virtual unsigned int GetHoldTimeMs(unsigned int keyId, int windowId, bool bFallthrough) const override;
virtual void OnDigitalKey(unsigned int keyId, int windowId, bool bFallthrough, bool bPressed, unsigned int holdTimeMs = 0) override;
virtual void OnAnalogKey(unsigned int keyId, int windowId, bool bFallthrough, float magnitude, unsigned int motionTimeMs) override;

private:
void SendAction(const CAction& action);
static INPUT_TYPE GetInputType(unsigned int keyId, int windowId, bool bFallthrough);
void SendDigitalAction(const CAction& action);
void ProcessButtonRelease(unsigned int keyId);
bool IsPressed(unsigned int keyId) const;

Expand All @@ -59,6 +59,7 @@ namespace JOYSTICK
unsigned int m_lastButtonPress;
unsigned int m_lastDigitalActionMs;
std::vector<unsigned int> m_pressedButtons;
std::map<unsigned int, unsigned int> m_holdStartTimes; // Key ID -> hold start time (ms)
};
}
}
7 changes: 6 additions & 1 deletion xbmc/input/joysticks/generic/FeatureHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
*/

#include "FeatureHandling.h"
#include "games/controllers/Controller.h"
#include "games/GameServices.h"
#include "input/joysticks/DriverPrimitive.h"
#include "input/joysticks/IButtonMap.h"
#include "input/joysticks/IInputHandler.h"
#include "threads/SystemClock.h"
#include "utils/log.h"
#include "ServiceBroker.h"

#include <vector>

Expand Down Expand Up @@ -61,7 +64,6 @@ bool CJoystickFeature::AcceptsInput(bool bActivation)

CScalarFeature::CScalarFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap) :
CJoystickFeature(name, handler, buttonMap),
m_inputType(handler->GetInputType(name)),
m_bDigitalState(false),
m_bDigitalHandled(false),
m_bDigitalPressSent(false),
Expand All @@ -70,6 +72,9 @@ CScalarFeature::CScalarFeature(const FeatureName& name, IInputHandler* handler,
m_analogEvent(false),
m_bDiscrete(true)
{
GAME::ControllerPtr controller = CServiceBroker::GetGameServices().GetController(handler->ControllerID());
if (controller)
m_inputType = controller->GetInputType(name);
}

bool CScalarFeature::OnDigitalMotion(const CDriverPrimitive& source, bool bPressed)
Expand Down
2 changes: 1 addition & 1 deletion xbmc/input/joysticks/generic/FeatureHandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace JOYSTICK
void OnDigitalMotion(bool bPressed);
void OnAnalogMotion(float magnitude);

const INPUT_TYPE m_inputType;
INPUT_TYPE m_inputType = INPUT_TYPE::UNKNOWN;
bool m_bDigitalState;
bool m_bDigitalHandled;
bool m_bDigitalPressSent;
Expand Down

0 comments on commit 89a5cb9

Please sign in to comment.