| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 "PeripheralKeyboard.h" |
| 10 |
|
| 11 |
#include "games/GameServices.h" |
| 12 |
#include "games/controllers/Controller.h" |
| 13 |
#include "games/controllers/ControllerManager.h" |
| 14 |
#include "input/InputManager.h" |
| 15 |
#include "peripherals/Peripherals.h" |
| 16 |
|
| 17 |
#include <mutex> |
| 18 |
#include <sstream> |
| 19 |
|
| 20 |
using namespace KODI; |
| 21 |
using namespace PERIPHERALS; |
| 22 |
|
| 23 |
CPeripheralKeyboard::CPeripheralKeyboard(CPeripherals& manager, |
| 24 |
const PeripheralScanResult& scanResult, |
| 25 |
CPeripheralBus* bus) |
| 26 |
: CPeripheral(manager, scanResult, bus) |
| 27 |
{ |
| 28 |
// Initialize CPeripheral |
| 29 |
m_features.push_back(FEATURE_KEYBOARD); |
| 30 |
} |
| 31 |
|
| 32 |
CPeripheralKeyboard::~CPeripheralKeyboard(void) |
| 33 |
{ |
| 34 |
m_manager.GetInputManager().UnregisterKeyboardDriverHandler(this); |
| 35 |
} |
| 36 |
|
| 37 |
bool CPeripheralKeyboard::InitialiseFeature(const PeripheralFeature feature) |
| 38 |
{ |
| 39 |
bool bSuccess = false; |
| 40 |
|
| 41 |
if (CPeripheral::InitialiseFeature(feature)) |
| 42 |
{ |
| 43 |
switch (feature) |
| 44 |
{ |
| 45 |
case FEATURE_KEYBOARD: |
| 46 |
{ |
| 47 |
m_manager.GetInputManager().RegisterKeyboardDriverHandler(this); |
| 48 |
break; |
| 49 |
} |
| 50 |
default: |
| 51 |
break; |
| 52 |
} |
| 53 |
|
| 54 |
bSuccess = true; |
| 55 |
} |
| 56 |
|
| 57 |
return bSuccess; |
| 58 |
} |
| 59 |
|
| 60 |
void CPeripheralKeyboard::RegisterKeyboardDriverHandler( |
| 61 |
KODI::KEYBOARD::IKeyboardDriverHandler* handler, bool bPromiscuous) |
| 62 |
{ |
| 63 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 64 |
|
| 65 |
KeyboardHandle handle{handler, bPromiscuous}; |
| 66 |
m_keyboardHandlers.insert(m_keyboardHandlers.begin(), handle); |
| 67 |
} |
| 68 |
|
| 69 |
void CPeripheralKeyboard::UnregisterKeyboardDriverHandler( |
| 70 |
KODI::KEYBOARD::IKeyboardDriverHandler* handler) |
| 71 |
{ |
| 72 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 73 |
|
| 74 |
auto it = |
| 75 |
std::find_if(m_keyboardHandlers.begin(), m_keyboardHandlers.end(), |
| 76 |
[handler](const KeyboardHandle& handle) { return handle.handler == handler; }); |
| 77 |
|
| 78 |
if (it != m_keyboardHandlers.end()) |
| 79 |
m_keyboardHandlers.erase(it); |
| 80 |
} |
| 81 |
|
| 82 |
void CPeripheralKeyboard::SetLastActive(const CDateTime& lastActive) |
| 83 |
{ |
| 84 |
// Update state |
| 85 |
m_lastActive = lastActive; |
| 86 |
|
| 87 |
// Update ancestor |
| 88 |
CPeripheral::SetLastActive(lastActive); |
| 89 |
} |
| 90 |
|
| 91 |
GAME::ControllerPtr CPeripheralKeyboard::ControllerProfile() const |
| 92 |
{ |
| 93 |
if (m_controllerProfile) |
| 94 |
return m_controllerProfile; |
| 95 |
|
| 96 |
return m_manager.GetControllerProfiles().GetDefaultKeyboard(); |
| 97 |
} |
| 98 |
|
| 99 |
bool CPeripheralKeyboard::OnKeyPress(const CKey& key) |
| 100 |
{ |
| 101 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 102 |
|
| 103 |
// Update state |
| 104 |
SetLastActive(CDateTime::GetCurrentDateTime()); |
| 105 |
|
| 106 |
bool bHandled = false; |
| 107 |
|
| 108 |
// Process promiscuous handlers |
| 109 |
for (const KeyboardHandle& handle : m_keyboardHandlers) |
| 110 |
{ |
| 111 |
if (handle.bPromiscuous) |
| 112 |
handle.handler->OnKeyPress(key); |
| 113 |
} |
| 114 |
|
| 115 |
// Process handlers until one is handled |
| 116 |
for (const KeyboardHandle& handle : m_keyboardHandlers) |
| 117 |
{ |
| 118 |
if (!handle.bPromiscuous) |
| 119 |
{ |
| 120 |
bHandled = handle.handler->OnKeyPress(key); |
| 121 |
if (bHandled) |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return bHandled; |
| 127 |
} |
| 128 |
|
| 129 |
void CPeripheralKeyboard::OnKeyRelease(const CKey& key) |
| 130 |
{ |
| 131 |
std::unique_lock<CCriticalSection> lock(m_mutex); |
| 132 |
|
| 133 |
for (const KeyboardHandle& handle : m_keyboardHandlers) |
| 134 |
handle.handler->OnKeyRelease(key); |
| 135 |
} |