summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/keymaps/KeymapHandling.cpp
blob: b87ea684b5a1202f04bd137e9a273b91408e645a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 *  Copyright (C) 2017-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "KeymapHandling.h"

#include "KeymapHandler.h"
#include "ServiceBroker.h"
#include "input/ButtonTranslator.h"
#include "input/InputManager.h"
#include "input/Keymap.h"
#include "input/joysticks/interfaces/IInputHandler.h"
#include "input/joysticks/interfaces/IInputProvider.h"

#include <algorithm>
#include <utility>

using namespace KODI;
using namespace JOYSTICK;

CKeymapHandling::CKeymapHandling(IInputProvider* inputProvider,
                                 bool pPromiscuous,
                                 const IKeymapEnvironment* environment)
  : m_inputProvider(inputProvider), m_pPromiscuous(pPromiscuous), m_environment(environment)
{
  LoadKeymaps();
  CServiceBroker::GetInputManager().RegisterObserver(this);
}

CKeymapHandling::~CKeymapHandling()
{
  CServiceBroker::GetInputManager().UnregisterObserver(this);
  UnloadKeymaps();
}

IInputReceiver* CKeymapHandling::GetInputReceiver(const std::string& controllerId) const
{
  auto it = std::find_if(m_inputHandlers.begin(), m_inputHandlers.end(),
                         [&controllerId](const std::unique_ptr<IInputHandler>& inputHandler) {
                           return inputHandler->ControllerID() == controllerId;
                         });

  if (it != m_inputHandlers.end())
    return (*it)->InputReceiver();

  return nullptr;
}

IKeymap* CKeymapHandling::GetKeymap(const std::string& controllerId) const
{
  auto it = std::find_if(m_keymaps.begin(), m_keymaps.end(),
                         [&controllerId](const std::unique_ptr<IKeymap>& keymap) {
                           return keymap->ControllerID() == controllerId;
                         });

  if (it != m_keymaps.end())
    return it->get();

  return nullptr;
}

void CKeymapHandling::Notify(const Observable& obs, const ObservableMessage msg)
{
  if (msg == ObservableMessageButtonMapsChanged)
    LoadKeymaps();
}

void CKeymapHandling::LoadKeymaps()
{
  UnloadKeymaps();

  auto& inputManager = CServiceBroker::GetInputManager();

  for (auto& windowKeymap : inputManager.GetJoystickKeymaps())
  {
    // Create keymap
    std::unique_ptr<IKeymap> keymap(new CKeymap(std::move(windowKeymap), m_environment));

    // Create keymap handler
    std::unique_ptr<IInputHandler> inputHandler(new CKeymapHandler(&inputManager, keymap.get()));

    // Register the handler with the input provider
    m_inputProvider->RegisterInputHandler(inputHandler.get(), m_pPromiscuous);

    // Save the keymap and handler
    m_keymaps.emplace_back(std::move(keymap));
    m_inputHandlers.emplace_back(std::move(inputHandler));
  }
}

void CKeymapHandling::UnloadKeymaps()
{
  if (m_inputProvider != nullptr)
  {
    for (auto it = m_inputHandlers.rbegin(); it != m_inputHandlers.rend(); ++it)
      m_inputProvider->UnregisterInputHandler(it->get());
  }
  m_inputHandlers.clear();
  m_keymaps.clear();
}