| 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 "GameClientMouse.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/controllers/input/ControllerActivity.h" |
| 15 |
#include "input/mouse/interfaces/IMouseInputProvider.h" |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
using namespace KODI; |
| 20 |
using namespace GAME; |
| 21 |
|
| 22 |
CGameClientMouse::CGameClientMouse(CGameClient& gameClient, |
| 23 |
std::string controllerId, |
| 24 |
MOUSE::IMouseInputProvider* inputProvider) |
| 25 |
: m_gameClient(gameClient), |
| 26 |
m_controllerId(std::move(controllerId)), |
| 27 |
m_inputProvider(inputProvider), |
| 28 |
m_mouseActivity(std::make_unique<CControllerActivity>()) |
| 29 |
{ |
| 30 |
inputProvider->RegisterMouseHandler(this, false, false); |
| 31 |
} |
| 32 |
|
| 33 |
CGameClientMouse::~CGameClientMouse() |
| 34 |
{ |
| 35 |
m_inputProvider->UnregisterMouseHandler(this); |
| 36 |
} |
| 37 |
|
| 38 |
std::string CGameClientMouse::ControllerID(void) const |
| 39 |
{ |
| 40 |
return m_controllerId; |
| 41 |
} |
| 42 |
|
| 43 |
bool CGameClientMouse::OnMotion(const std::string& relpointer, int dx, int dy) |
| 44 |
{ |
| 45 |
//! @todo Allow mouse motion to activate controller |
| 46 |
//! This will follow a larger refactoring of the mouse input system |
| 47 |
//m_mouseActivity->OnMouseMotion(relpointer, differenceX, differenceY); |
| 48 |
|
| 49 |
// Only allow activated input in fullscreen game |
| 50 |
if (!m_gameClient.Input().AcceptsInput()) |
| 51 |
{ |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
const std::string controllerId = ControllerID(); |
| 56 |
|
| 57 |
game_input_event event; |
| 58 |
|
| 59 |
event.type = GAME_INPUT_EVENT_RELATIVE_POINTER; |
| 60 |
event.controller_id = m_controllerId.c_str(); |
| 61 |
event.port_type = GAME_PORT_MOUSE; |
| 62 |
event.port_address = MOUSE_PORT_ADDRESS; |
| 63 |
event.feature_name = relpointer.c_str(); |
| 64 |
event.rel_pointer.x = dx; |
| 65 |
event.rel_pointer.y = dy; |
| 66 |
|
| 67 |
return m_gameClient.Input().InputEvent(event); |
| 68 |
} |
| 69 |
|
| 70 |
bool CGameClientMouse::OnButtonPress(const std::string& button) |
| 71 |
{ |
| 72 |
m_mouseActivity->OnMouseButtonPress(button); |
| 73 |
m_mouseActivity->OnInputFrame(); |
| 74 |
|
| 75 |
// Only allow activated input in fullscreen game |
| 76 |
if (!m_gameClient.Input().AcceptsInput()) |
| 77 |
{ |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
game_input_event event; |
| 82 |
|
| 83 |
event.type = GAME_INPUT_EVENT_DIGITAL_BUTTON; |
| 84 |
event.controller_id = m_controllerId.c_str(); |
| 85 |
event.port_type = GAME_PORT_MOUSE; |
| 86 |
event.port_address = MOUSE_PORT_ADDRESS; |
| 87 |
event.feature_name = button.c_str(); |
| 88 |
event.digital_button.pressed = true; |
| 89 |
|
| 90 |
return m_gameClient.Input().InputEvent(event); |
| 91 |
} |
| 92 |
|
| 93 |
void CGameClientMouse::OnButtonRelease(const std::string& button) |
| 94 |
{ |
| 95 |
m_mouseActivity->OnMouseButtonRelease(button); |
| 96 |
m_mouseActivity->OnInputFrame(); |
| 97 |
|
| 98 |
game_input_event event; |
| 99 |
|
| 100 |
event.type = GAME_INPUT_EVENT_DIGITAL_BUTTON; |
| 101 |
event.controller_id = m_controllerId.c_str(); |
| 102 |
event.port_type = GAME_PORT_MOUSE; |
| 103 |
event.port_address = MOUSE_PORT_ADDRESS; |
| 104 |
event.feature_name = button.c_str(); |
| 105 |
event.digital_button.pressed = false; |
| 106 |
|
| 107 |
m_gameClient.Input().InputEvent(event); |
| 108 |
} |
| 109 |
|
| 110 |
void CGameClientMouse::OnInputFrame() |
| 111 |
{ |
| 112 |
m_mouseActivity->OnInputFrame(); |
| 113 |
} |
| 114 |
|
| 115 |
float CGameClientMouse::GetActivation() const |
| 116 |
{ |
| 117 |
return m_mouseActivity->GetActivation(); |
| 118 |
} |
| 119 |
|
| 120 |
void CGameClientMouse::SetSource(PERIPHERALS::PeripheralPtr sourcePeripheral) |
| 121 |
{ |
| 122 |
m_sourcePeripheral = std::move(sourcePeripheral); |
| 123 |
} |
| 124 |
|
| 125 |
void CGameClientMouse::ClearSource() |
| 126 |
{ |
| 127 |
m_sourcePeripheral.reset(); |
| 128 |
} |