| 1 |
/* |
| 2 |
* Copyright (C) 2015-2018 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 "GameClientKeyboard.h" |
| 10 |
|
| 11 |
#include "GameClientInput.h" |
| 12 |
#include "addons/kodi-dev-kit/include/kodi/addon-instance/Game.h" |
| 13 |
#include "games/addons/GameClient.h" |
| 14 |
#include "games/addons/GameClientTranslator.h" |
| 15 |
#include "games/controllers/input/ControllerActivity.h" |
| 16 |
#include "input/keyboard/interfaces/IKeyboardInputProvider.h" |
| 17 |
#include "utils/log.h" |
| 18 |
|
| 19 |
#include <utility> |
| 20 |
|
| 21 |
using namespace KODI; |
| 22 |
using namespace GAME; |
| 23 |
|
| 24 |
#define BUTTON_INDEX_MASK 0x01ff |
| 25 |
|
| 26 |
CGameClientKeyboard::CGameClientKeyboard(CGameClient& gameClient, |
| 27 |
std::string controllerId, |
| 28 |
KEYBOARD::IKeyboardInputProvider* inputProvider) |
| 29 |
: m_gameClient(gameClient), |
| 30 |
m_controllerId(std::move(controllerId)), |
| 31 |
m_inputProvider(inputProvider), |
| 32 |
m_keyboardActivity(std::make_unique<CControllerActivity>()) |
| 33 |
{ |
| 34 |
m_inputProvider->RegisterKeyboardHandler(this, false, false); |
| 35 |
} |
| 36 |
|
| 37 |
CGameClientKeyboard::~CGameClientKeyboard() |
| 38 |
{ |
| 39 |
m_inputProvider->UnregisterKeyboardHandler(this); |
| 40 |
} |
| 41 |
|
| 42 |
std::string CGameClientKeyboard::ControllerID() const |
| 43 |
{ |
| 44 |
return m_controllerId; |
| 45 |
} |
| 46 |
|
| 47 |
bool CGameClientKeyboard::HasKey(const KEYBOARD::KeyName& key) const |
| 48 |
{ |
| 49 |
return m_gameClient.Input().HasFeature(ControllerID(), key); |
| 50 |
} |
| 51 |
|
| 52 |
bool CGameClientKeyboard::OnKeyPress(const KEYBOARD::KeyName& key, |
| 53 |
KEYBOARD::Modifier mod, |
| 54 |
uint32_t unicode) |
| 55 |
{ |
| 56 |
m_keyboardActivity->OnKeyPress(key); |
| 57 |
m_keyboardActivity->OnInputFrame(); |
| 58 |
|
| 59 |
// Only allow activated input in fullscreen game |
| 60 |
if (!m_gameClient.Input().AcceptsInput()) |
| 61 |
{ |
| 62 |
CLog::Log(LOGDEBUG, "GAME: key press ignored, not in fullscreen game"); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
game_input_event event; |
| 67 |
|
| 68 |
event.type = GAME_INPUT_EVENT_KEY; |
| 69 |
event.controller_id = m_controllerId.c_str(); |
| 70 |
event.port_type = GAME_PORT_KEYBOARD; |
| 71 |
event.port_address = KEYBOARD_PORT_ADDRESS; |
| 72 |
event.feature_name = key.c_str(); |
| 73 |
event.key.pressed = true; |
| 74 |
event.key.unicode = unicode; |
| 75 |
event.key.modifiers = CGameClientTranslator::GetModifiers(mod); |
| 76 |
|
| 77 |
return m_gameClient.Input().InputEvent(event); |
| 78 |
} |
| 79 |
|
| 80 |
void CGameClientKeyboard::OnKeyRelease(const KEYBOARD::KeyName& key, |
| 81 |
KEYBOARD::Modifier mod, |
| 82 |
uint32_t unicode) |
| 83 |
{ |
| 84 |
m_keyboardActivity->OnKeyRelease(key); |
| 85 |
m_keyboardActivity->OnInputFrame(); |
| 86 |
|
| 87 |
game_input_event event; |
| 88 |
|
| 89 |
event.type = GAME_INPUT_EVENT_KEY; |
| 90 |
event.controller_id = m_controllerId.c_str(); |
| 91 |
event.port_type = GAME_PORT_KEYBOARD; |
| 92 |
event.port_address = KEYBOARD_PORT_ADDRESS; |
| 93 |
event.feature_name = key.c_str(); |
| 94 |
event.key.pressed = false; |
| 95 |
event.key.unicode = unicode; |
| 96 |
event.key.modifiers = CGameClientTranslator::GetModifiers(mod); |
| 97 |
|
| 98 |
m_gameClient.Input().InputEvent(event); |
| 99 |
} |
| 100 |
|
| 101 |
float CGameClientKeyboard::GetActivation() const |
| 102 |
{ |
| 103 |
return m_keyboardActivity->GetActivation(); |
| 104 |
} |
| 105 |
|
| 106 |
void CGameClientKeyboard::SetSource(PERIPHERALS::PeripheralPtr sourcePeripheral) |
| 107 |
{ |
| 108 |
m_sourcePeripheral = std::move(sourcePeripheral); |
| 109 |
} |
| 110 |
|
| 111 |
void CGameClientKeyboard::ClearSource() |
| 112 |
{ |
| 113 |
m_sourcePeripheral.reset(); |
| 114 |
} |