Skip to content

Commit

Permalink
[Android][Peripherals] Initialize buttonmap with Android mapping, if …
Browse files Browse the repository at this point in the history
…possible
  • Loading branch information
garbear committed Jan 31, 2024
1 parent 6607a07 commit 98183eb
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 19 deletions.
2 changes: 1 addition & 1 deletion xbmc/peripherals/Peripherals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ void CPeripherals::ResetButtonMaps(const std::string& controllerId)
PeripheralAddonPtr addon;
if (addonBus->GetAddonWithButtonMap(peripheral.get(), addon))
{
CAddonButtonMap buttonMap(peripheral.get(), addon, controllerId);
CAddonButtonMap buttonMap(peripheral.get(), addon, controllerId, *this);
buttonMap.Reset();
}
}
Expand Down
29 changes: 27 additions & 2 deletions xbmc/peripherals/addons/AddonButtonMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "PeripheralAddonTranslator.h"
#include "input/joysticks/JoystickUtils.h"
#include "peripherals/Peripherals.h"
#include "peripherals/devices/Peripheral.h"
#include "utils/log.h"

Expand All @@ -24,8 +25,9 @@ using namespace PERIPHERALS;

CAddonButtonMap::CAddonButtonMap(CPeripheral* device,
const std::weak_ptr<CPeripheralAddon>& addon,
const std::string& strControllerId)
: m_device(device), m_addon(addon), m_strControllerId(strControllerId)
const std::string& strControllerId,
CPeripherals& manager)
: m_device(device), m_addon(addon), m_strControllerId(strControllerId), m_manager(manager)
{
auto peripheralAddon = m_addon.lock();
assert(peripheralAddon != nullptr);
Expand Down Expand Up @@ -59,6 +61,29 @@ bool CAddonButtonMap::Load(void)
bSuccess |= addon->GetIgnoredPrimitives(m_device, ignoredPrimitives);
}

if (features.empty())
{
// Check if we can initialize a buttonmap from the peripheral bus
PeripheralBusPtr peripheralBus = m_manager.GetBusByType(m_device->GetBusType());
if (peripheralBus)
{
CLog::Log(LOGDEBUG,
"Buttonmap not found for {}, attempting to initialize from peripheral bus",
m_device->Location());
if (peripheralBus->InitializeButtonMap(*m_device, *this))
{
bSuccess = true;

if (auto addon = m_addon.lock())
{
addon->GetAppearance(m_device, controllerAppearance);
addon->GetFeatures(m_device, m_strControllerId, features);
addon->GetIgnoredPrimitives(m_device, ignoredPrimitives);
}
}
}
}

// GetFeatures() was changed to always return false if no features were
// retrieved. Check here, just in case its contract is changed or violated in
// the future.
Expand Down
5 changes: 4 additions & 1 deletion xbmc/peripherals/addons/AddonButtonMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PERIPHERALS
{
class CPeripheral;
class CPeripherals;

/*!
* \ingroup peripherals
Expand All @@ -27,7 +28,8 @@ class CAddonButtonMap : public KODI::JOYSTICK::IButtonMap
public:
CAddonButtonMap(CPeripheral* device,
const std::weak_ptr<CPeripheralAddon>& addon,
const std::string& strControllerId);
const std::string& strControllerId,
CPeripherals& manager);

~CAddonButtonMap(void) override;

Expand Down Expand Up @@ -133,6 +135,7 @@ class CAddonButtonMap : public KODI::JOYSTICK::IButtonMap
CPeripheral* const m_device;
const std::weak_ptr<CPeripheralAddon> m_addon;
const std::string m_strControllerId;
CPeripherals& m_manager;

// Button map state
std::string m_controllerAppearance;
Expand Down
2 changes: 1 addition & 1 deletion xbmc/peripherals/addons/AddonButtonMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CAddonButtonMapping::CAddonButtonMapping(CPeripherals& manager,
else
{
const std::string controllerId = mapper->ControllerID();
m_buttonMap.reset(new CAddonButtonMap(peripheral, addon, controllerId));
m_buttonMap = std::make_unique<CAddonButtonMap>(peripheral, addon, controllerId, manager);
if (m_buttonMap->Load())
{
KEYMAP::IKeymap* keymap = peripheral->GetKeymap(controllerId);
Expand Down
24 changes: 17 additions & 7 deletions xbmc/peripherals/addons/AddonInputHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,38 @@ using namespace KODI;
using namespace JOYSTICK;
using namespace PERIPHERALS;

CAddonInputHandling::CAddonInputHandling(CPeripheral* peripheral,
CAddonInputHandling::CAddonInputHandling(CPeripherals& manager,
CPeripheral* peripheral,
std::shared_ptr<CPeripheralAddon> addon,
IInputHandler* handler,
IDriverReceiver* receiver)
: m_peripheral(peripheral),
: m_manager(manager),
m_peripheral(peripheral),
m_addon(std::move(addon)),
m_joystickInputHandler(handler),
m_joystickDriverReceiver(receiver)
{
}

CAddonInputHandling::CAddonInputHandling(CPeripheral* peripheral,
CAddonInputHandling::CAddonInputHandling(CPeripherals& manager,
CPeripheral* peripheral,
std::shared_ptr<CPeripheralAddon> addon,
KEYBOARD::IKeyboardInputHandler* handler)
: m_peripheral(peripheral), m_addon(std::move(addon)), m_keyboardInputHandler(handler)
: m_manager(manager),
m_peripheral(peripheral),
m_addon(std::move(addon)),
m_keyboardInputHandler(handler)
{
}

CAddonInputHandling::CAddonInputHandling(CPeripheral* peripheral,
CAddonInputHandling::CAddonInputHandling(CPeripherals& manager,
CPeripheral* peripheral,
std::shared_ptr<CPeripheralAddon> addon,
MOUSE::IMouseInputHandler* handler)
: m_peripheral(peripheral), m_addon(std::move(addon)), m_mouseInputHandler(handler)
: m_manager(manager),
m_peripheral(peripheral),
m_addon(std::move(addon)),
m_mouseInputHandler(handler)
{
}

Expand All @@ -69,7 +79,7 @@ bool CAddonInputHandling::Load()
controllerId = m_mouseInputHandler->ControllerID();

if (!controllerId.empty())
m_buttonMap = std::make_unique<CAddonButtonMap>(m_peripheral, m_addon, controllerId);
m_buttonMap = std::make_unique<CAddonButtonMap>(m_peripheral, m_addon, controllerId, m_manager);

if (m_buttonMap && m_buttonMap->Load())
{
Expand Down
11 changes: 8 additions & 3 deletions xbmc/peripherals/addons/AddonInputHandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class IMouseInputHandler;
namespace PERIPHERALS
{
class CPeripheral;
class CPeripherals;
class CPeripheralAddon;

/*!
Expand All @@ -49,16 +50,19 @@ class CAddonInputHandling : public KODI::JOYSTICK::IDriverHandler,
public KODI::MOUSE::IMouseDriverHandler
{
public:
CAddonInputHandling(CPeripheral* peripheral,
CAddonInputHandling(CPeripherals& manager,
CPeripheral* peripheral,
std::shared_ptr<CPeripheralAddon> addon,
KODI::JOYSTICK::IInputHandler* handler,
KODI::JOYSTICK::IDriverReceiver* receiver);

CAddonInputHandling(CPeripheral* peripheral,
CAddonInputHandling(CPeripherals& manager,
CPeripheral* peripheral,
std::shared_ptr<CPeripheralAddon> addon,
KODI::KEYBOARD::IKeyboardInputHandler* handler);

CAddonInputHandling(CPeripheral* peripheral,
CAddonInputHandling(CPeripherals& manager,
CPeripheral* peripheral,
std::shared_ptr<CPeripheralAddon> addon,
KODI::MOUSE::IMouseInputHandler* handler);

Expand Down Expand Up @@ -89,6 +93,7 @@ class CAddonInputHandling : public KODI::JOYSTICK::IDriverHandler,

private:
// Construction parameters
CPeripherals& m_manager;
CPeripheral* const m_peripheral;
const std::shared_ptr<CPeripheralAddon> m_addon;
KODI::JOYSTICK::IInputHandler* const m_joystickInputHandler{nullptr};
Expand Down
17 changes: 17 additions & 0 deletions xbmc/peripherals/bus/PeripheralBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@

class CFileItemList;

namespace KODI
{
namespace JOYSTICK
{
class IButtonMap;
} // namespace JOYSTICK
} // namespace KODI

namespace PERIPHERALS
{
class CPeripheral;
Expand Down Expand Up @@ -59,6 +67,15 @@ class CPeripheralBus : protected CThread
*/
virtual bool InitializeProperties(CPeripheral& peripheral);

/*!
* \brief Initialize a joystick buttonmap, if possible
*/
virtual bool InitializeButtonMap(const CPeripheral& peripheral,
KODI::JOYSTICK::IButtonMap& buttonMap) const
{
return false;
}

/*!
* @brief Get the instance of the peripheral at the given location.
* @param strLocation The location.
Expand Down
6 changes: 3 additions & 3 deletions xbmc/peripherals/devices/Peripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ void CPeripheral::RegisterInputHandler(IInputHandler* handler, bool bPromiscuous
if (addon)
{
std::unique_ptr<CAddonInputHandling> addonInput = std::make_unique<CAddonInputHandling>(
this, std::move(addon), handler, GetDriverReceiver());
m_manager, this, std::move(addon), handler, GetDriverReceiver());
if (addonInput->Load())
{
RegisterJoystickDriverHandler(addonInput.get(), bPromiscuous);
Expand Down Expand Up @@ -629,7 +629,7 @@ void CPeripheral::RegisterKeyboardHandler(KEYBOARD::IKeyboardInputHandler* handl
if (addon)
{
std::unique_ptr<CAddonInputHandling> addonInput =
std::make_unique<CAddonInputHandling>(this, std::move(addon), handler);
std::make_unique<CAddonInputHandling>(m_manager, this, std::move(addon), handler);
if (addonInput->Load())
keyboardDriverHandler = std::move(addonInput);
}
Expand Down Expand Up @@ -675,7 +675,7 @@ void CPeripheral::RegisterMouseHandler(MOUSE::IMouseInputHandler* handler, bool
if (addon)
{
std::unique_ptr<CAddonInputHandling> addonInput =
std::make_unique<CAddonInputHandling>(this, std::move(addon), handler);
std::make_unique<CAddonInputHandling>(m_manager, this, std::move(addon), handler);
if (addonInput->Load())
mouseDriverHandler = std::move(addonInput);
}
Expand Down
3 changes: 2 additions & 1 deletion xbmc/peripherals/devices/PeripheralJoystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ bool CPeripheralJoystick::InitialiseFeature(const PeripheralFeature feature)

if (bSuccess)
{
m_buttonMap = std::make_unique<CAddonButtonMap>(this, addon, DEFAULT_CONTROLLER_ID);
m_buttonMap =
std::make_unique<CAddonButtonMap>(this, addon, DEFAULT_CONTROLLER_ID, m_manager);
if (m_buttonMap->Load())
{
InitializeDeadzoneFiltering(*m_buttonMap);
Expand Down
Loading

0 comments on commit 98183eb

Please sign in to comment.