Debian Codegraph @

xbmc/input/keyboard/KeyboardStat.cpp

raw
1 /*
2 * Copyright (C) 2007-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 // C++ Implementation: CKeyboard
10
11 // Comment OUT, if not really debugging!!!:
12 //#define DEBUG_KEYBOARD_GETCHAR
13
14 #include "KeyboardStat.h"
15
16 #include "ServiceBroker.h"
17 #include "input/keyboard/KeyboardTranslator.h"
18 #include "input/keyboard/KeyboardTypes.h"
19 #include "input/keyboard/XBMC_keytable.h"
20 #include "input/keyboard/XBMC_vkeys.h"
21 #include "peripherals/Peripherals.h"
22 #include "peripherals/devices/PeripheralHID.h"
23 #include "utils/log.h"
24 #include "windowing/XBMC_events.h"
25
26 using namespace KODI;
27 using namespace KEYBOARD;
28
29 bool operator==(const XBMC_keysym& lhs, const XBMC_keysym& rhs)
30 {
31 return lhs.mod == rhs.mod && lhs.scancode == rhs.scancode && lhs.sym == rhs.sym &&
32 lhs.unicode == rhs.unicode;
33 }
34
35 CKeyboardStat::CKeyboardStat()
36 {
37 memset(&m_lastKeysym, 0, sizeof(m_lastKeysym));
38 m_lastKeyTime = {};
39 }
40
41 void CKeyboardStat::Initialize()
42 {
43 }
44
45 bool CKeyboardStat::LookupSymAndUnicodePeripherals(XBMC_keysym& keysym, uint8_t* key, char* unicode)
46 {
47 using namespace PERIPHERALS;
48
49 PeripheralVector hidDevices;
50 if (CServiceBroker::GetPeripherals().GetPeripheralsWithFeature(hidDevices, FEATURE_HID))
51 {
52 for (auto& peripheral : hidDevices)
53 {
54 std::shared_ptr<CPeripheralHID> hidDevice =
55 std::static_pointer_cast<CPeripheralHID>(peripheral);
56 if (hidDevice->LookupSymAndUnicode(keysym, key, unicode))
57 return true;
58 }
59 }
60 return false;
61 }
62
63 CKey CKeyboardStat::TranslateKey(XBMC_keysym& keysym) const
64 {
65 uint32_t keycode;
66 uint8_t vkey;
67 wchar_t unicode;
68 char ascii;
69 uint32_t modifiers;
70 uint32_t lockingModifiers;
71 std::chrono::milliseconds held;
72 XBMCKEYTABLE keytable;
73
74 modifiers = 0;
75 if (keysym.mod & XBMCKMOD_CTRL)
76 modifiers |= CKey::MODIFIER_CTRL;
77 if (keysym.mod & XBMCKMOD_SHIFT)
78 modifiers |= CKey::MODIFIER_SHIFT;
79 if (keysym.mod & XBMCKMOD_ALT)
80 modifiers |= CKey::MODIFIER_ALT;
81 if (keysym.mod & XBMCKMOD_SUPER)
82 modifiers |= CKey::MODIFIER_SUPER;
83 if (keysym.mod & XBMCKMOD_META)
84 modifiers |= CKey::MODIFIER_META;
85
86 lockingModifiers = 0;
87 if (keysym.mod & XBMCKMOD_NUM)
88 lockingModifiers |= CKey::MODIFIER_NUMLOCK;
89 if (keysym.mod & XBMCKMOD_CAPS)
90 lockingModifiers |= CKey::MODIFIER_CAPSLOCK;
91 if (keysym.mod & XBMCKMOD_MODE)
92 lockingModifiers |= CKey::MODIFIER_SCROLLLOCK;
93
94 CLog::Log(LOGDEBUG,
95 "Keyboard: scancode: {:#02x}, sym: {:#04x} ({}), unicode: {:#04x}, modifier: 0x{:x}",
96 keysym.scancode, keysym.sym, CKeyboardTranslator::TranslateKeycode(keysym.sym),
97 keysym.unicode, keysym.mod);
98
99 // The keysym.unicode is usually valid, even if it is zero. A zero
100 // unicode just means this is a non-printing keypress. The ascii and
101 // vkey will be set below.
102 keycode = keysym.sym;
103 unicode = keysym.unicode;
104 ascii = 0;
105 vkey = 0;
106 held = std::chrono::milliseconds(0);
107
108 // Start by check whether any of the HID peripherals wants to translate this keypress
109 if (LookupSymAndUnicodePeripherals(keysym, &vkey, &ascii))
110 {
111 CLog::Log(LOGDEBUG, "{} - keypress translated by a HID peripheral", __FUNCTION__);
112 }
113
114 // Continue by trying to match both the sym and unicode. This will identify
115 // the majority of keypresses
116 else if (KeyTable::LookupSymAndUnicode(keysym.sym, keysym.unicode, &keytable))
117 {
118 vkey = keytable.vkey;
119 ascii = keytable.ascii;
120 }
121
122 // If we failed to match the sym and unicode try just the unicode. This
123 // will match keys like \ that are on different keys on regional keyboards.
124 else if (KeyTable::LookupUnicode(keysym.unicode, &keytable))
125 {
126 if (keycode == 0)
127 keycode = keytable.sym;
128 vkey = keytable.vkey;
129 ascii = keytable.ascii;
130 }
131
132 // If there is still no match try the sym
133 else if (KeyTable::LookupSym(keysym.sym, &keytable))
134 {
135 vkey = keytable.vkey;
136
137 // Occasionally we get non-printing keys that have a non-zero value in
138 // the keysym.unicode. Check for this here and replace any rogue unicode
139 // values.
140 if (keytable.unicode == 0 && unicode != 0)
141 unicode = 0;
142 else if (keysym.unicode > 32 && keysym.unicode < 128)
143 ascii = unicode & 0x7f;
144 }
145
146 // The keysym.sym is unknown ...
147 else
148 {
149 if (!vkey && !ascii)
150 {
151 if (keysym.mod & XBMCKMOD_LSHIFT)
152 vkey = 0xa0;
153 else if (keysym.mod & XBMCKMOD_RSHIFT)
154 vkey = 0xa1;
155 else if (keysym.mod & XBMCKMOD_LALT)
156 vkey = 0xa4;
157 else if (keysym.mod & XBMCKMOD_RALT)
158 vkey = 0xa5;
159 else if (keysym.mod & XBMCKMOD_LCTRL)
160 vkey = 0xa2;
161 else if (keysym.mod & XBMCKMOD_RCTRL)
162 vkey = 0xa3;
163 else if (keysym.unicode > 32 && keysym.unicode < 128)
164 // only TRUE ASCII! (Otherwise XBMC crashes! No unicode not even latin 1!)
165 ascii = (char)(keysym.unicode & 0xff);
166 }
167 }
168
169 if (keysym == m_lastKeysym)
170 {
171 auto now = std::chrono::steady_clock::now();
172
173 held = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastKeyTime);
174 if (held.count() > KEY_HOLD_TRESHOLD)
175 modifiers |= CKey::MODIFIER_LONG;
176 }
177
178 // For all shift-X keys except shift-A to shift-Z and shift-F1 to shift-F24 the
179 // shift modifier is ignored. This so that, for example, the * keypress (shift-8)
180 // is seen as <asterisk> not <asterisk mod="shift">.
181 // The A-Z keys are exempted because shift-A-Z is used for navigation in lists.
182 // The function keys are exempted because function keys have no shifted value and
183 // the Nyxboard remote uses keys like Shift-F3 for some buttons.
184 if (modifiers == CKey::MODIFIER_SHIFT)
185 if ((unicode < 'A' || unicode > 'Z') && (unicode < 'a' || unicode > 'z') &&
186 (vkey < XBMCVK_F1 || vkey > XBMCVK_F24))
187 modifiers = 0;
188
189 // Create and return a CKey
190
191 CKey key(keycode, vkey, unicode, ascii, modifiers, lockingModifiers, held.count());
192
193 return key;
194 }
195
196 void CKeyboardStat::ProcessKeyDown(XBMC_keysym& keysym)
197 {
198 if (!(m_lastKeysym == keysym))
199 {
200 m_lastKeysym = keysym;
201 m_lastKeyTime = std::chrono::steady_clock::now();
202 }
203 }
204
205 void CKeyboardStat::ProcessKeyUp(void)
206 {
207 memset(&m_lastKeysym, 0, sizeof(m_lastKeysym));
208 m_lastKeyTime = {};
209 }
210
211 // Return the key name given a key ID
212 // Used to make the debug log more intelligible
213 // The KeyID includes the flags for ctrl, alt etc
214
215 std::string CKeyboardStat::GetKeyName(int KeyID)
216 {
217 int keyid;
218 std::string keyname;
219 XBMCKEYTABLE keytable;
220
221 keyname.clear();
222
223 // Get modifiers
224
225 if (KeyID & CKey::MODIFIER_CTRL)
226 keyname.append("ctrl-");
227 if (KeyID & CKey::MODIFIER_SHIFT)
228 keyname.append("shift-");
229 if (KeyID & CKey::MODIFIER_ALT)
230 keyname.append("alt-");
231 if (KeyID & CKey::MODIFIER_SUPER)
232 keyname.append("win-");
233 if (KeyID & CKey::MODIFIER_META)
234 keyname.append("meta-");
235 if (KeyID & CKey::MODIFIER_LONG)
236 keyname.append("long-");
237
238 // Now get the key name
239
240 keyid = KeyID & 0xFF;
241 bool VKeyFound = KeyTable::LookupVKeyName(keyid, &keytable);
242 if (VKeyFound)
243 keyname.append(keytable.keyname);
244 else
245 keyname += std::to_string(keyid);
246
247 // in case this might be an universalremote keyid
248 // we also print the possible corresponding obc code
249 // so users can easily find it in their universalremote
250 // map xml
251 if (VKeyFound || keyid > 255)
252 keyname += StringUtils::Format(" ({:#02x})", KeyID);
253 else // obc keys are 255 -rawid
254 keyname += StringUtils::Format(" ({:#02x}, obc{})", KeyID, 255 - KeyID);
255
256 return keyname;
257 }