[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / input / ButtonTranslator.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #ifndef BUTTON_TRANSLATOR_H
22 #define BUTTON_TRANSLATOR_H
23
24 #pragma once
25
26 #include <map>
27 #include <vector>
28 #include "system.h" // for HAS_EVENT_SERVER, HAS_SDL_JOYSTICK, HAS_LIRC
29
30 #ifdef HAS_EVENT_SERVER
31 #include "network/EventClient.h"
32 #endif
33
34 class CKey;
35 class CAction;
36 class TiXmlNode;
37
38 struct CButtonAction
39 {
40   int id;
41   CStdString strID; // needed for "XBMC.ActivateWindow()" type actions
42 };
43 ///
44 /// singleton class to map from buttons to actions
45 /// Warning: _not_ threadsafe!
46 class CButtonTranslator
47 {
48 #ifdef HAS_EVENT_SERVER
49   friend class EVENTCLIENT::CEventButtonState;
50 #endif
51
52 private:
53   //private construction, and no assignements; use the provided singleton methods
54   CButtonTranslator();
55   CButtonTranslator(const CButtonTranslator&);
56   CButtonTranslator const& operator=(CButtonTranslator const&);
57   virtual ~CButtonTranslator();
58   bool HasDeviceType(TiXmlNode *pWindow, CStdString type);
59 public:
60   ///access to singleton
61   static CButtonTranslator& GetInstance();
62
63   // Add/remove a HID device with custom mappings
64   void AddDevice(CStdString& strDevice);
65   void RemoveDevice(CStdString& strDevice);
66
67   /// loads Lircmap.xml/IRSSmap.xml (if enabled) and Keymap.xml
68   bool Load(bool AlwaysLoad = false);
69   /// clears the maps
70   void Clear();
71
72   static void GetActions(std::vector<std::string> &actionList);
73   static void GetWindows(std::vector<std::string> &windowList);
74
75   CAction GetAction(int window, const CKey &key, bool fallback = true);
76
77   /*! \brief Translate between a window name and it's id
78    \param window name of the window
79    \return id of the window, or WINDOW_INVALID if not found
80    */
81   static int TranslateWindow(const CStdString &window);
82
83   /*! \brief Translate between a window id and it's name
84    \param window id of the window
85    \return name of the window, or an empty string if not found
86    */
87   static CStdString TranslateWindow(int window);
88
89   static bool TranslateActionString(const char *szAction, int &action);
90
91 #if defined(HAS_LIRC) || defined(HAS_IRSERVERSUITE)
92   int TranslateLircRemoteString(const char* szDevice, const char *szButton);
93 #endif
94 #if defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
95   bool TranslateJoystickString(int window, const char* szDevice, int id,
96                                short inputType, int& action, CStdString& strAction,
97                                bool &fullrange);
98 #endif
99
100 private:
101   typedef std::multimap<uint32_t, CButtonAction> buttonMap; // our button map to fill in
102
103   // m_translatorMap contains all mappings i.e. m_BaseMap + HID device mappings
104   std::map<int, buttonMap> m_translatorMap;
105   // m_deviceList contains the list of connected HID devices
106   std::list<CStdString> m_deviceList;
107
108   int GetActionCode(int window, const CKey &key, CStdString &strAction) const;
109 #if defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
110   typedef std::map<int, std::map<int, std::string> > JoystickMap; // <window, <button/axis, action> >
111   int GetActionCode(int window, int id, const JoystickMap &wmap, CStdString &strAction, bool &fullrange) const;
112 #endif
113   int GetFallbackWindow(int windowID);
114
115   static uint32_t TranslateGamepadString(const char *szButton);
116   static uint32_t TranslateRemoteString(const char *szButton);
117   static uint32_t TranslateUniversalRemoteString(const char *szButton);
118
119   static uint32_t TranslateKeyboardString(const char *szButton);
120   static uint32_t TranslateKeyboardButton(TiXmlElement *pButton);
121
122   static uint32_t TranslateMouseCommand(const char *szButton);
123
124   static uint32_t TranslateAppCommand(const char *szButton);
125
126   void MapWindowActions(TiXmlNode *pWindow, int wWindowID);
127   void MapAction(uint32_t buttonCode, const char *szAction, buttonMap &map);
128
129   bool LoadKeymap(const CStdString &keymapPath);
130 #if defined(HAS_LIRC) || defined(HAS_IRSERVERSUITE)
131   bool LoadLircMap(const CStdString &lircmapPath);
132   void ClearLircButtonMapEntries();
133
134   void MapRemote(TiXmlNode *pRemote, const char* szDevice);
135
136   typedef std::map<CStdString, CStdString> lircButtonMap;
137   std::map<CStdString, lircButtonMap*> lircRemotesMap;
138 #endif
139
140 #if defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
141   void MapJoystickActions(int windowID, TiXmlNode *pJoystick);
142
143   std::map<std::string, JoystickMap> m_joystickButtonMap;      // <joy name, button map>
144   std::map<std::string, JoystickMap> m_joystickAxisMap;        // <joy name, axis map>
145   std::map<std::string, JoystickMap> m_joystickHatMap;        // <joy name, hat map>
146 #endif
147
148   bool m_Loaded;
149 };
150
151 #endif
152