Debian Codegraph @

xbmc/peripherals/devices/PeripheralHID.cpp

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 #include "PeripheralHID.h"
10
11 #include "guilib/LocalizeStrings.h"
12 #include "input/InputManager.h"
13 #include "peripherals/Peripherals.h"
14 #include "utils/log.h"
15
16 using namespace PERIPHERALS;
17
18 CPeripheralHID::CPeripheralHID(CPeripherals& manager,
19 const PeripheralScanResult& scanResult,
20 CPeripheralBus* bus)
21 : CPeripheral(manager, scanResult, bus)
22 {
23 m_strDeviceName = scanResult.m_strDeviceName.empty() ? g_localizeStrings.Get(35001)
24 : scanResult.m_strDeviceName;
25 m_features.push_back(FEATURE_HID);
26 }
27
28 CPeripheralHID::~CPeripheralHID(void)
29 {
30 if (!m_strKeymap.empty() && !GetSettingBool("do_not_use_custom_keymap"))
31 {
32 CLog::Log(LOGDEBUG, "{} - switching active keymapping to: default", __FUNCTION__);
33 m_manager.GetInputManager().RemoveKeymap(m_strKeymap);
34 }
35 }
36
37 bool CPeripheralHID::InitialiseFeature(const PeripheralFeature feature)
38 {
39 if (feature == FEATURE_HID && !m_bInitialised)
40 {
41 m_bInitialised = true;
42
43 if (HasSetting("keymap"))
44 m_strKeymap = GetSettingString("keymap");
45
46 if (m_strKeymap.empty())
47 {
48 m_strKeymap = StringUtils::Format("v{}p{}", VendorIdAsString(), ProductIdAsString());
49 SetSetting("keymap", m_strKeymap);
50 }
51
52 if (!IsSettingVisible("keymap"))
53 SetSettingVisible("do_not_use_custom_keymap", false);
54
55 if (!m_strKeymap.empty())
56 {
57 bool bKeymapEnabled(!GetSettingBool("do_not_use_custom_keymap"));
58 if (bKeymapEnabled)
59 {
60 CLog::Log(LOGDEBUG, "{} - adding keymapping for: {}", __FUNCTION__, m_strKeymap);
61 m_manager.GetInputManager().AddKeymap(m_strKeymap);
62 }
63 else
64 {
65 CLog::Log(LOGDEBUG, "{} - removing keymapping for: {}", __FUNCTION__, m_strKeymap);
66 m_manager.GetInputManager().RemoveKeymap(m_strKeymap);
67 }
68 }
69
70 CLog::Log(LOGDEBUG, "{} - initialised HID device ({}:{})", __FUNCTION__, m_strVendorId,
71 m_strProductId);
72 }
73
74 return CPeripheral::InitialiseFeature(feature);
75 }
76
77 void CPeripheralHID::OnSettingChanged(const std::string& strChangedSetting)
78 {
79 if (m_bInitialised && ((StringUtils::EqualsNoCase(strChangedSetting, "keymap") &&
80 !GetSettingBool("do_not_use_custom_keymap")) ||
81 StringUtils::EqualsNoCase(strChangedSetting, "keymap_enabled")))
82 {
83 m_bInitialised = false;
84 InitialiseFeature(FEATURE_HID);
85 }
86 }