Merge pull request #4775 from jmarshallnz/empty_episode_playcount
[vuplus_xbmc] / xbmc / input / ButtonTranslator.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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 #include "utils/StdString.h"
34
35 class CKey;
36 class CAction;
37 class TiXmlNode;
38
39 struct CButtonAction
40 {
41   int id;
42   CStdString strID; // needed for "XBMC.ActivateWindow()" type actions
43 };
44 ///
45 /// singleton class to map from buttons to actions
46 /// Warning: _not_ threadsafe!
47 class CButtonTranslator
48 {
49 #ifdef HAS_EVENT_SERVER
50   friend class EVENTCLIENT::CEventButtonState;
51 #endif
52
53 private:
54   //private construction, and no assignements; use the provided singleton methods
55   CButtonTranslator();
56   CButtonTranslator(const CButtonTranslator&);
57   CButtonTranslator const& operator=(CButtonTranslator const&);
58   virtual ~CButtonTranslator();
59   bool HasDeviceType(TiXmlNode *pWindow, CStdString type);
60 public:
61   ///access to singleton
62   static CButtonTranslator& GetInstance();
63
64   // Add/remove a HID device with custom mappings
65   void AddDevice(CStdString& strDevice);
66   void RemoveDevice(CStdString& strDevice);
67
68   /// loads Lircmap.xml/IRSSmap.xml (if enabled) and Keymap.xml
69   bool Load(bool AlwaysLoad = false);
70   /// clears the maps
71   void Clear();
72
73   static void GetActions(std::vector<std::string> &actionList);
74   static void GetWindows(std::vector<std::string> &windowList);
75
76   CAction GetAction(int window, const CKey &key, bool fallback = true);
77
78   /*! \brief Translate between a window name and it's id
79    \param window name of the window
80    \return id of the window, or WINDOW_INVALID if not found
81    */
82   static int TranslateWindow(const CStdString &window);
83
84   /*! \brief Translate between a window id and it's name
85    \param window id of the window
86    \return name of the window, or an empty string if not found
87    */
88   static CStdString TranslateWindow(int window);
89
90   static bool TranslateActionString(const char *szAction, int &action);
91
92 #if defined(HAS_LIRC) || defined(HAS_IRSERVERSUITE)
93   int TranslateLircRemoteString(const char* szDevice, const char *szButton);
94 #endif
95 #if defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
96   bool TranslateJoystickString(int window, const char* szDevice, int id,
97                                short inputType, int& action, CStdString& strAction,
98                                bool &fullrange);
99 #endif
100
101   bool TranslateTouchAction(int window, int touchAction, int touchPointers, int &action);
102
103 private:
104   typedef std::multimap<uint32_t, CButtonAction> buttonMap; // our button map to fill in
105
106   // m_translatorMap contains all mappings i.e. m_BaseMap + HID device mappings
107   std::map<int, buttonMap> m_translatorMap;
108   // m_deviceList contains the list of connected HID devices
109   std::list<CStdString> m_deviceList;
110
111   int GetActionCode(int window, int action);
112   int GetActionCode(int window, const CKey &key, CStdString &strAction) const;
113 #if defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
114   typedef std::map<int, std::map<int, std::string> > JoystickMap; // <window, <button/axis, action> >
115   int GetActionCode(int window, int id, const JoystickMap &wmap, CStdString &strAction, bool &fullrange) const;
116 #endif
117   int GetFallbackWindow(int windowID);
118
119   static uint32_t TranslateGamepadString(const char *szButton);
120   static uint32_t TranslateRemoteString(const char *szButton);
121   static uint32_t TranslateUniversalRemoteString(const char *szButton);
122
123   static uint32_t TranslateKeyboardString(const char *szButton);
124   static uint32_t TranslateKeyboardButton(TiXmlElement *pButton);
125
126   static uint32_t TranslateMouseCommand(const char *szButton);
127
128   static uint32_t TranslateAppCommand(const char *szButton);
129
130   void MapWindowActions(TiXmlNode *pWindow, int wWindowID);
131   void MapAction(uint32_t buttonCode, const char *szAction, buttonMap &map);
132
133   bool LoadKeymap(const CStdString &keymapPath);
134 #if defined(HAS_LIRC) || defined(HAS_IRSERVERSUITE)
135   bool LoadLircMap(const CStdString &lircmapPath);
136   void ClearLircButtonMapEntries();
137
138   void MapRemote(TiXmlNode *pRemote, const char* szDevice);
139
140   typedef std::map<CStdString, CStdString> lircButtonMap;
141   std::map<CStdString, lircButtonMap*> lircRemotesMap;
142 #endif
143
144 #if defined(HAS_SDL_JOYSTICK) || defined(HAS_EVENT_SERVER)
145   void MapJoystickActions(int windowID, TiXmlNode *pJoystick);
146
147   std::map<std::string, JoystickMap> m_joystickButtonMap;      // <joy name, button map>
148   std::map<std::string, JoystickMap> m_joystickAxisMap;        // <joy name, axis map>
149   std::map<std::string, JoystickMap> m_joystickHatMap;        // <joy name, hat map>
150 #endif
151
152   void MapTouchActions(int windowID, TiXmlNode *pTouch);
153   static uint32_t TranslateTouchCommand(TiXmlElement *pButton, CButtonAction &action);
154   int GetTouchActionCode(int window, int action);
155
156   std::map<int, buttonMap> m_touchMap;
157
158   bool m_Loaded;
159 };
160
161 #endif
162