Fix keymap.
[vuplus_xbmc] / xbmc / guilib / Key.cpp
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 #include "system.h"
22 #include "Key.h"
23
24 CKey::CKey(void)
25 {
26   Reset();
27 }
28
29 CKey::~CKey(void)
30 {}
31
32 CKey::CKey(uint32_t buttonCode, uint8_t leftTrigger, uint8_t rightTrigger, float leftThumbX, float leftThumbY, float rightThumbX, float rightThumbY, float repeat)
33 {
34   Reset();
35   m_buttonCode = buttonCode;
36   m_leftTrigger = leftTrigger;
37   m_rightTrigger = rightTrigger;
38   m_leftThumbX = leftThumbX;
39   m_leftThumbY = leftThumbY;
40   m_rightThumbX = rightThumbX;
41   m_rightThumbY = rightThumbY;
42   m_repeat = repeat;
43 }
44
45 CKey::CKey(uint32_t buttonCode, unsigned int held)
46 {
47   Reset();
48   m_buttonCode = buttonCode;
49   m_held = held;
50 }
51
52 CKey::CKey(uint8_t vkey, wchar_t unicode, char ascii, uint32_t modifiers, unsigned int held)
53 {
54   Reset();
55   if (vkey) // FIXME: This needs cleaning up - should we always use the unicode key where available?
56     m_buttonCode = vkey | KEY_VKEY;
57   else
58     m_buttonCode = KEY_UNICODE;
59   m_buttonCode |= modifiers;
60   m_vkey = vkey;
61   m_unicode = unicode;
62   m_ascii = ascii;
63   m_modifiers = modifiers;
64   m_held = held;
65 }
66
67 CKey::CKey(const CKey& key)
68 {
69   *this = key;
70 }
71
72 void CKey::Reset()
73 {
74   m_leftTrigger = 0;
75   m_rightTrigger = 0;
76   m_leftThumbX = 0.0f;
77   m_leftThumbY = 0.0f;
78   m_rightThumbX = 0.0f;
79   m_rightThumbY = 0.0f;
80   m_repeat = 0.0f;
81   m_fromService = false;
82   m_buttonCode = KEY_INVALID;
83   m_vkey = 0;
84   m_unicode = 0;
85   m_ascii = 0;
86   m_modifiers = 0;
87   m_held = 0;
88 }
89
90 CKey& CKey::operator=(const CKey& key)
91 {
92   if (&key == this) return * this;
93   m_leftTrigger  = key.m_leftTrigger;
94   m_rightTrigger = key.m_rightTrigger;
95   m_leftThumbX   = key.m_leftThumbX;
96   m_leftThumbY   = key.m_leftThumbY;
97   m_rightThumbX  = key.m_rightThumbX;
98   m_rightThumbY  = key.m_rightThumbY;
99   m_repeat       = key.m_repeat;
100   m_fromService  = key.m_fromService;
101   m_buttonCode   = key.m_buttonCode;
102   m_vkey         = key.m_vkey;
103   m_unicode     = key.m_unicode;
104   m_ascii       = key.m_ascii;
105   m_modifiers    = key.m_modifiers;
106   m_held         = key.m_held;
107   return *this;
108 }
109
110 BYTE CKey::GetLeftTrigger() const
111 {
112   return m_leftTrigger;
113 }
114
115 BYTE CKey::GetRightTrigger() const
116 {
117   return m_rightTrigger;
118 }
119
120 float CKey::GetLeftThumbX() const
121 {
122   return m_leftThumbX;
123 }
124
125 float CKey::GetLeftThumbY() const
126 {
127   return m_leftThumbY;
128 }
129
130
131 float CKey::GetRightThumbX() const
132 {
133   return m_rightThumbX;
134 }
135
136 float CKey::GetRightThumbY() const
137 {
138   return m_rightThumbY;
139 }
140
141 bool CKey::FromKeyboard() const
142 {
143   return (m_buttonCode >= KEY_VKEY && m_buttonCode != KEY_INVALID);
144 }
145
146 bool CKey::IsAnalogButton() const
147 {
148   if ((GetButtonCode() > 261 && GetButtonCode() < 270) || (GetButtonCode() > 279 && GetButtonCode() < 284))
149     return true;
150
151   return false;
152 }
153
154 bool CKey::IsIRRemote() const
155 {
156   if (GetButtonCode() < 256)
157     return true;
158   return false;
159 }
160
161 float CKey::GetRepeat() const
162 {
163   return m_repeat;
164 }
165
166 void CKey::SetFromService(bool fromService)
167 {
168   if (fromService && (m_buttonCode & KEY_ASCII))
169     m_unicode = m_buttonCode - KEY_ASCII;
170     
171   m_fromService = fromService;
172 }
173
174 CAction::CAction(int actionID, float amount1 /* = 1.0f */, float amount2 /* = 0.0f */, const CStdString &name /* = "" */, unsigned int holdTime /*= 0*/)
175 {
176   m_id = actionID;
177   m_amount[0] = amount1;
178   m_amount[1] = amount2;
179   for (unsigned int i = 2; i < max_amounts; i++)
180     m_amount[i] = 0;  
181   m_name = name;
182   m_repeat = 0;
183   m_buttonCode = 0;
184   m_unicode = 0;
185   m_holdTime = holdTime;
186 }
187
188 CAction::CAction(int actionID, unsigned int state, float posX, float posY, float offsetX, float offsetY, const CStdString &name)
189 {
190   m_id = actionID;
191   m_amount[0] = posX;
192   m_amount[1] = posY;
193   m_amount[2] = offsetX;
194   m_amount[3] = offsetY;
195   for (unsigned int i = 4; i < max_amounts; i++)
196     m_amount[i] = 0;  
197   m_name = name;
198   m_repeat = 0;
199   m_buttonCode = 0;
200   m_unicode = 0;
201   m_holdTime = state;
202 }
203
204 CAction::CAction(int actionID, wchar_t unicode)
205 {
206   m_id = actionID;
207   for (unsigned int i = 0; i < max_amounts; i++)
208     m_amount[i] = 0;  
209   m_repeat = 0;
210   m_buttonCode = 0;
211   m_unicode = unicode;
212   m_holdTime = 0;
213 }
214
215 CAction::CAction(int actionID, const CStdString &name, const CKey &key)
216 {
217   m_id = actionID;
218   m_name = name;
219   m_amount[0] = 1; // digital button (could change this for repeat acceleration)
220   for (unsigned int i = 1; i < max_amounts; i++)
221     m_amount[i] = 0;
222   m_repeat = key.GetRepeat();
223   m_buttonCode = key.GetButtonCode();
224   m_unicode = 0;
225   m_holdTime = key.GetHeld();
226   // get the action amounts of the analog buttons
227   if (key.GetButtonCode() == KEY_BUTTON_LEFT_ANALOG_TRIGGER)
228     m_amount[0] = (float)key.GetLeftTrigger() / 255.0f;
229   else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_ANALOG_TRIGGER)
230     m_amount[0] = (float)key.GetRightTrigger() / 255.0f;
231   else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK)
232   {
233     m_amount[0] = key.GetLeftThumbX();
234     m_amount[1] = key.GetLeftThumbY();
235   }
236   else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK)
237   {
238     m_amount[0] = key.GetRightThumbX();
239     m_amount[1] = key.GetRightThumbY();
240   }
241   else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_UP)
242     m_amount[0] = key.GetLeftThumbY();
243   else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_DOWN)
244     m_amount[0] = -key.GetLeftThumbY();
245   else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_LEFT)
246     m_amount[0] = -key.GetLeftThumbX();
247   else if (key.GetButtonCode() == KEY_BUTTON_LEFT_THUMB_STICK_RIGHT)
248     m_amount[0] = key.GetLeftThumbX();
249   else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_UP)
250     m_amount[0] = key.GetRightThumbY();
251   else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_DOWN)
252     m_amount[0] = -key.GetRightThumbY();
253   else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_LEFT)
254     m_amount[0] = -key.GetRightThumbX();
255   else if (key.GetButtonCode() == KEY_BUTTON_RIGHT_THUMB_STICK_RIGHT)
256     m_amount[0] = key.GetRightThumbX();
257 }
258
259 CAction::CAction(int actionID, const std::string &name)
260 {
261   m_id = actionID;
262   m_name = name;
263   for (unsigned int i = 0; i < max_amounts; i++)
264     m_amount[i] = 0;
265   m_repeat = 0;
266   m_buttonCode = 0;
267   m_unicode = 0;
268   m_holdTime = 0;
269 }