summaryrefslogtreecommitdiffstats
path: root/xbmc/games/addons/input/GameClientKeyboard.cpp
blob: e6f48c9cf42f1b84097c330eac0cd0163bf8c299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 *  Copyright (C) 2015-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GameClientKeyboard.h"

#include "GameClientInput.h"
#include "addons/kodi-dev-kit/include/kodi/addon-instance/Game.h"
#include "games/addons/GameClient.h"
#include "games/addons/GameClientTranslator.h"
#include "input/keyboard/interfaces/IKeyboardInputProvider.h"
#include "utils/log.h"

#include <utility>

using namespace KODI;
using namespace GAME;

#define BUTTON_INDEX_MASK 0x01ff

CGameClientKeyboard::CGameClientKeyboard(CGameClient& gameClient,
                                         std::string controllerId,
                                         KEYBOARD::IKeyboardInputProvider* inputProvider)
  : m_gameClient(gameClient),
    m_controllerId(std::move(controllerId)),
    m_inputProvider(inputProvider)
{
  m_inputProvider->RegisterKeyboardHandler(this, false);
}

CGameClientKeyboard::~CGameClientKeyboard()
{
  m_inputProvider->UnregisterKeyboardHandler(this);
}

std::string CGameClientKeyboard::ControllerID() const
{
  return m_controllerId;
}

bool CGameClientKeyboard::HasKey(const KEYBOARD::KeyName& key) const
{
  return m_gameClient.Input().HasFeature(ControllerID(), key);
}

bool CGameClientKeyboard::OnKeyPress(const KEYBOARD::KeyName& key,
                                     KEYBOARD::Modifier mod,
                                     uint32_t unicode)
{
  // Only allow activated input in fullscreen game
  if (!m_gameClient.Input().AcceptsInput())
  {
    CLog::Log(LOGDEBUG, "GAME: key press ignored, not in fullscreen game");
    return false;
  }

  game_input_event event;

  event.type = GAME_INPUT_EVENT_KEY;
  event.controller_id = m_controllerId.c_str();
  event.port_type = GAME_PORT_KEYBOARD;
  event.port_address = ""; // Not used
  event.feature_name = key.c_str();
  event.key.pressed = true;
  event.key.unicode = unicode;
  event.key.modifiers = CGameClientTranslator::GetModifiers(mod);

  return m_gameClient.Input().InputEvent(event);
}

void CGameClientKeyboard::OnKeyRelease(const KEYBOARD::KeyName& key,
                                       KEYBOARD::Modifier mod,
                                       uint32_t unicode)
{
  game_input_event event;

  event.type = GAME_INPUT_EVENT_KEY;
  event.controller_id = m_controllerId.c_str();
  event.port_type = GAME_PORT_KEYBOARD;
  event.port_address = ""; // Not used
  event.feature_name = key.c_str();
  event.key.pressed = false;
  event.key.unicode = unicode;
  event.key.modifiers = CGameClientTranslator::GetModifiers(mod);

  m_gameClient.Input().InputEvent(event);
}

void CGameClientKeyboard::SetSource(PERIPHERALS::PeripheralPtr sourcePeripheral)
{
  m_sourcePeripheral = std::move(sourcePeripheral);
}

void CGameClientKeyboard::ClearSource()
{
  m_sourcePeripheral.reset();
}