| 1 |
/* |
| 2 |
* Copyright (C) 2017-2024 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "KeymapHandling.h" |
| 10 |
|
| 11 |
#include "KeymapHandler.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "input/InputManager.h" |
| 14 |
#include "input/joysticks/interfaces/IInputHandler.h" |
| 15 |
#include "input/joysticks/interfaces/IInputProvider.h" |
| 16 |
#include "input/keymaps/ButtonTranslator.h" |
| 17 |
#include "input/keymaps/Keymap.h" |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <utility> |
| 21 |
|
| 22 |
using namespace KODI; |
| 23 |
using namespace KEYMAP; |
| 24 |
|
| 25 |
CKeymapHandling::CKeymapHandling(JOYSTICK::IInputProvider* inputProvider, |
| 26 |
bool pPromiscuous, |
| 27 |
const IKeymapEnvironment* environment) |
| 28 |
: m_inputProvider(inputProvider), m_pPromiscuous(pPromiscuous), m_environment(environment) |
| 29 |
{ |
| 30 |
LoadKeymaps(); |
| 31 |
CServiceBroker::GetInputManager().RegisterObserver(this); |
| 32 |
} |
| 33 |
|
| 34 |
CKeymapHandling::~CKeymapHandling() |
| 35 |
{ |
| 36 |
CServiceBroker::GetInputManager().UnregisterObserver(this); |
| 37 |
UnloadKeymaps(); |
| 38 |
} |
| 39 |
|
| 40 |
JOYSTICK::IInputReceiver* CKeymapHandling::GetInputReceiver(const std::string& controllerId) const |
| 41 |
{ |
| 42 |
auto it = |
| 43 |
std::find_if(m_inputHandlers.begin(), m_inputHandlers.end(), |
| 44 |
[&controllerId](const std::unique_ptr<JOYSTICK::IInputHandler>& inputHandler) |
| 45 |
{ return inputHandler->ControllerID() == controllerId; }); |
| 46 |
|
| 47 |
if (it != m_inputHandlers.end()) |
| 48 |
return (*it)->InputReceiver(); |
| 49 |
|
| 50 |
return nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
IKeymap* CKeymapHandling::GetKeymap(const std::string& controllerId) const |
| 54 |
{ |
| 55 |
auto it = std::find_if(m_keymaps.begin(), m_keymaps.end(), |
| 56 |
[&controllerId](const std::unique_ptr<IKeymap>& keymap) |
| 57 |
{ return keymap->ControllerID() == controllerId; }); |
| 58 |
|
| 59 |
if (it != m_keymaps.end()) |
| 60 |
return it->get(); |
| 61 |
|
| 62 |
return nullptr; |
| 63 |
} |
| 64 |
|
| 65 |
void CKeymapHandling::Notify(const Observable& obs, const ObservableMessage msg) |
| 66 |
{ |
| 67 |
if (msg == ObservableMessageButtonMapsChanged) |
| 68 |
LoadKeymaps(); |
| 69 |
} |
| 70 |
|
| 71 |
void CKeymapHandling::LoadKeymaps() |
| 72 |
{ |
| 73 |
UnloadKeymaps(); |
| 74 |
|
| 75 |
auto& inputManager = CServiceBroker::GetInputManager(); |
| 76 |
|
| 77 |
for (auto& windowKeymap : inputManager.GetJoystickKeymaps()) |
| 78 |
{ |
| 79 |
// Create keymap |
| 80 |
std::unique_ptr<IKeymap> keymap(new CKeymap(std::move(windowKeymap), m_environment)); |
| 81 |
|
| 82 |
// Create keymap handler |
| 83 |
std::unique_ptr<JOYSTICK::IInputHandler> inputHandler( |
| 84 |
new CKeymapHandler(&inputManager, keymap.get())); |
| 85 |
|
| 86 |
// Register the handler with the input provider |
| 87 |
m_inputProvider->RegisterInputHandler(inputHandler.get(), m_pPromiscuous); |
| 88 |
|
| 89 |
// Save the keymap and handler |
| 90 |
m_keymaps.emplace_back(std::move(keymap)); |
| 91 |
m_inputHandlers.emplace_back(std::move(inputHandler)); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void CKeymapHandling::UnloadKeymaps() |
| 96 |
{ |
| 97 |
if (m_inputProvider != nullptr) |
| 98 |
{ |
| 99 |
for (auto it = m_inputHandlers.rbegin(); it != m_inputHandlers.rend(); ++it) |
| 100 |
m_inputProvider->UnregisterInputHandler(it->get()); |
| 101 |
} |
| 102 |
m_inputHandlers.clear(); |
| 103 |
m_keymaps.clear(); |
| 104 |
} |