diff --git a/xbmc/games/addons/GameClientJoystick.cpp b/xbmc/games/addons/GameClientJoystick.cpp index 104883f4adb73..28dc2ded718ef 100644 --- a/xbmc/games/addons/GameClientJoystick.cpp +++ b/xbmc/games/addons/GameClientJoystick.cpp @@ -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; diff --git a/xbmc/games/addons/GameClientJoystick.h b/xbmc/games/addons/GameClientJoystick.h index c0d56d498909a..db5bede7904d6 100644 --- a/xbmc/games/addons/GameClientJoystick.h +++ b/xbmc/games/addons/GameClientJoystick.h @@ -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 { } diff --git a/xbmc/games/ports/InputSink.cpp b/xbmc/games/ports/InputSink.cpp index 2db5c186c38f6..9962613f30983 100644 --- a/xbmc/games/ports/InputSink.cpp +++ b/xbmc/games/ports/InputSink.cpp @@ -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; -} diff --git a/xbmc/games/ports/InputSink.h b/xbmc/games/ports/InputSink.h index 8ae47ad2ab167..1add30580c4b3 100644 --- a/xbmc/games/ports/InputSink.h +++ b/xbmc/games/ports/InputSink.h @@ -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 { } diff --git a/xbmc/input/joysticks/DefaultJoystick.cpp b/xbmc/input/joysticks/DefaultJoystick.cpp index b00b4e979450c..d37b1c4041ff3 100644 --- a/xbmc/input/joysticks/DefaultJoystick.cpp +++ b/xbmc/input/joysticks/DefaultJoystick.cpp @@ -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()); @@ -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) @@ -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) @@ -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; @@ -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; } } diff --git a/xbmc/input/joysticks/DefaultJoystick.h b/xbmc/input/joysticks/DefaultJoystick.h index 09937c9ddce21..8808ce8fc86f9 100644 --- a/xbmc/input/joysticks/DefaultJoystick.h +++ b/xbmc/input/joysticks/DefaultJoystick.h @@ -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; @@ -118,7 +117,6 @@ namespace JOYSTICK IKeymapHandler* const m_handler; // State variables used to process joystick input - std::map m_holdStartTimes; // Key ID -> hold start time (ms) std::map m_currentDirections; // Analog stick name -> direction }; } diff --git a/xbmc/input/joysticks/IInputHandler.h b/xbmc/input/joysticks/IInputHandler.h index dc2388af7e539..7e30c5d1cf621 100644 --- a/xbmc/input/joysticks/IInputHandler.h +++ b/xbmc/input/joysticks/IInputHandler.h @@ -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 * diff --git a/xbmc/input/joysticks/IKeymapHandler.h b/xbmc/input/joysticks/IKeymapHandler.h index be68712439f31..151473f98b921 100644 --- a/xbmc/input/joysticks/IKeymapHandler.h +++ b/xbmc/input/joysticks/IKeymapHandler.h @@ -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 * diff --git a/xbmc/input/joysticks/KeymapHandler.cpp b/xbmc/input/joysticks/KeymapHandler.cpp index 86fe98af5e537..b56771a811019 100644 --- a/xbmc/input/joysticks/KeymapHandler.cpp +++ b/xbmc/input/joysticks/KeymapHandler.cpp @@ -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 @@ -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); @@ -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(); diff --git a/xbmc/input/joysticks/KeymapHandler.h b/xbmc/input/joysticks/KeymapHandler.h index 8639f7e21efd5..cecd2d0f72918 100644 --- a/xbmc/input/joysticks/KeymapHandler.h +++ b/xbmc/input/joysticks/KeymapHandler.h @@ -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; @@ -59,6 +59,7 @@ namespace JOYSTICK unsigned int m_lastButtonPress; unsigned int m_lastDigitalActionMs; std::vector m_pressedButtons; + std::map m_holdStartTimes; // Key ID -> hold start time (ms) }; } } diff --git a/xbmc/input/joysticks/generic/FeatureHandling.cpp b/xbmc/input/joysticks/generic/FeatureHandling.cpp index 631bb5137b233..db3f2ac66bc1d 100644 --- a/xbmc/input/joysticks/generic/FeatureHandling.cpp +++ b/xbmc/input/joysticks/generic/FeatureHandling.cpp @@ -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 @@ -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), @@ -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) diff --git a/xbmc/input/joysticks/generic/FeatureHandling.h b/xbmc/input/joysticks/generic/FeatureHandling.h index d30e0f6cbc5e9..819091318fbbe 100644 --- a/xbmc/input/joysticks/generic/FeatureHandling.h +++ b/xbmc/input/joysticks/generic/FeatureHandling.h @@ -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;