Debian Codegraph @

xbmc/input/keymaps/ButtonTranslator.h

raw
1 /*
2 * Copyright (C) 2005-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 #pragma once
10
11 #include "input/actions/Action.h"
12 #include "network/EventClient.h"
13
14 #include <map>
15 #include <set>
16 #include <string>
17
18 class CKey;
19
20 namespace tinyxml2
21 {
22 class XMLNode;
23 }
24
25 namespace KODI
26 {
27 namespace KEYMAP
28 {
29 class CCustomControllerTranslator;
30 class CTouchTranslator;
31 class IKeyMapper;
32 class IWindowKeymap;
33
34 /*!
35 * \ingroup keymap
36 *
37 * \brief Singleton class to map from buttons to actions
38 *
39 * Warning: _not_ threadsafe!
40 */
41 class CButtonTranslator
42 {
43 friend class EVENTCLIENT::CEventButtonState;
44
45 public:
46 CButtonTranslator() = default;
47 CButtonTranslator(const CButtonTranslator&) = delete;
48 CButtonTranslator const& operator=(CButtonTranslator const&) = delete;
49 virtual ~CButtonTranslator() = default;
50
51 // Add/remove a HID device with custom mappings
52 bool AddDevice(const std::string& strDevice);
53 bool RemoveDevice(const std::string& strDevice);
54
55 /// loads Keymap.xml
56 bool Load();
57
58 /// clears the maps
59 void Clear();
60
61 /*! \brief Finds out if a longpress mapping exists for this key
62 \param window id of the current window
63 \param key to search a mapping for
64 \return true if a longpress mapping exists
65 */
66 bool HasLongpressMapping(int window, const CKey& key);
67
68 /*! \brief Obtain the action configured for a given window and key
69 \param window the window id
70 \param key the key to query the action for
71 \param fallback if no action is directly configured for the given window, obtain the action from
72 fallback window, if exists or from global config as last resort
73 \return the action matching the key
74 */
75 CAction GetAction(int window, const CKey& key, bool fallback = true);
76
77 void RegisterMapper(const std::string& device, IKeyMapper* mapper);
78 void UnregisterMapper(const IKeyMapper* mapper);
79
80 static uint32_t TranslateString(const std::string& strMap, const std::string& strButton);
81
82 private:
83 struct CButtonAction
84 {
85 unsigned int id;
86 std::string strID; // needed for "ActivateWindow()" type actions
87 };
88
89 typedef std::multimap<uint32_t, CButtonAction> buttonMap; // our button map to fill in
90
91 // m_translatorMap contains all mappings i.e. m_BaseMap + HID device mappings
92 std::map<int, buttonMap> m_translatorMap;
93
94 // m_deviceList contains the list of connected HID devices
95 std::set<std::string> m_deviceList;
96
97 unsigned int GetActionCode(int window, const CKey& key, std::string& strAction) const;
98
99 void MapWindowActions(const tinyxml2::XMLNode* pWindow, int wWindowID);
100 void MapAction(uint32_t buttonCode, const std::string& szAction, buttonMap& map);
101
102 bool LoadKeymap(const std::string& keymapPath);
103
104 bool HasLongpressMapping_Internal(int window, const CKey& key);
105
106 std::map<std::string, IKeyMapper*> m_buttonMappers;
107 };
108 } // namespace KEYMAP
109 } // namespace KODI