| 1 |
/* |
| 2 |
* Copyright (C) 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 "AgentKeyboard.h" |
| 10 |
|
| 11 |
#include "games/controllers/Controller.h" |
| 12 |
#include "games/controllers/ControllerIDs.h" |
| 13 |
#include "games/controllers/input/ControllerActivity.h" |
| 14 |
#include "input/keyboard/interfaces/IKeyboardInputProvider.h" |
| 15 |
#include "peripherals/devices/Peripheral.h" |
| 16 |
|
| 17 |
using namespace KODI; |
| 18 |
using namespace GAME; |
| 19 |
|
| 20 |
CAgentKeyboard::CAgentKeyboard(PERIPHERALS::PeripheralPtr peripheral) |
| 21 |
: m_peripheral(std::move(peripheral)), m_keyboardActivity(std::make_unique<CControllerActivity>()) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
CAgentKeyboard::~CAgentKeyboard() = default; |
| 26 |
|
| 27 |
void CAgentKeyboard::Initialize() |
| 28 |
{ |
| 29 |
// Record appearance to detect changes |
| 30 |
m_controllerAppearance = m_peripheral->ControllerProfile(); |
| 31 |
|
| 32 |
// Upcast peripheral to input provider interface |
| 33 |
KEYBOARD::IKeyboardInputProvider* inputProvider = m_peripheral.get(); |
| 34 |
|
| 35 |
// Register input handler to silently observe all input |
| 36 |
inputProvider->RegisterKeyboardHandler(this, true, true); |
| 37 |
} |
| 38 |
|
| 39 |
void CAgentKeyboard::Deinitialize() |
| 40 |
{ |
| 41 |
// Upcast peripheral to input interface |
| 42 |
KEYBOARD::IKeyboardInputProvider* inputProvider = m_peripheral.get(); |
| 43 |
|
| 44 |
// Unregister input handler |
| 45 |
inputProvider->UnregisterKeyboardHandler(this); |
| 46 |
|
| 47 |
// Reset appearance |
| 48 |
m_controllerAppearance.reset(); |
| 49 |
} |
| 50 |
|
| 51 |
void CAgentKeyboard::ClearButtonState() |
| 52 |
{ |
| 53 |
return m_keyboardActivity->ClearButtonState(); |
| 54 |
} |
| 55 |
|
| 56 |
float CAgentKeyboard::GetActivation() const |
| 57 |
{ |
| 58 |
return m_keyboardActivity->GetActivation(); |
| 59 |
} |
| 60 |
|
| 61 |
std::string CAgentKeyboard::ControllerID(void) const |
| 62 |
{ |
| 63 |
return DEFAULT_KEYBOARD_ID; |
| 64 |
} |
| 65 |
|
| 66 |
bool CAgentKeyboard::HasKey(const KEYBOARD::KeyName& key) const |
| 67 |
{ |
| 68 |
return true; // Capture all keys |
| 69 |
} |
| 70 |
|
| 71 |
bool CAgentKeyboard::OnKeyPress(const KEYBOARD::KeyName& key, |
| 72 |
KEYBOARD::Modifier mod, |
| 73 |
uint32_t unicode) |
| 74 |
{ |
| 75 |
m_keyboardActivity->OnKeyPress(key); |
| 76 |
m_keyboardActivity->OnInputFrame(); |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
void CAgentKeyboard::OnKeyRelease(const KEYBOARD::KeyName& key, |
| 82 |
KEYBOARD::Modifier mod, |
| 83 |
uint32_t unicode) |
| 84 |
{ |
| 85 |
m_keyboardActivity->OnKeyRelease(key); |
| 86 |
m_keyboardActivity->OnInputFrame(); |
| 87 |
} |