| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 "PortInput.h" |
| 10 |
|
| 11 |
#include "games/addons/GameClient.h" |
| 12 |
#include "games/controllers/input/ControllerActivity.h" |
| 13 |
#include "games/controllers/input/InputSink.h" |
| 14 |
#include "guilib/WindowIDs.h" |
| 15 |
#include "input/keymaps/joysticks/KeymapHandling.h" |
| 16 |
#include "peripherals/devices/Peripheral.h" |
| 17 |
|
| 18 |
#include <memory> |
| 19 |
|
| 20 |
using namespace KODI; |
| 21 |
using namespace GAME; |
| 22 |
|
| 23 |
CPortInput::CPortInput(JOYSTICK::IInputHandler* gameInput) |
| 24 |
: m_gameInput(gameInput), |
| 25 |
m_inputSink(new CInputSink(gameInput)), |
| 26 |
m_controllerActivity(std::make_unique<CControllerActivity>()) |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
CPortInput::~CPortInput() = default; |
| 31 |
|
| 32 |
void CPortInput::RegisterInput(JOYSTICK::IInputProvider* provider) |
| 33 |
{ |
| 34 |
// Give input sink the lowest priority by registering it before the other |
| 35 |
// input handlers |
| 36 |
provider->RegisterInputHandler(m_inputSink.get(), false); |
| 37 |
|
| 38 |
// Register input handler |
| 39 |
provider->RegisterInputHandler(this, false); |
| 40 |
|
| 41 |
// Register GUI input |
| 42 |
m_appInput = std::make_unique<KEYMAP::CKeymapHandling>(provider, false, this); |
| 43 |
} |
| 44 |
|
| 45 |
void CPortInput::UnregisterInput(JOYSTICK::IInputProvider* provider) |
| 46 |
{ |
| 47 |
// Unregister in reverse order |
| 48 |
if (provider == nullptr && m_appInput) |
| 49 |
m_appInput->UnregisterInputProvider(); |
| 50 |
m_appInput.reset(); |
| 51 |
|
| 52 |
if (provider != nullptr) |
| 53 |
{ |
| 54 |
provider->UnregisterInputHandler(this); |
| 55 |
provider->UnregisterInputHandler(m_inputSink.get()); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
float CPortInput::GetActivation() const |
| 60 |
{ |
| 61 |
return m_controllerActivity->GetActivation(); |
| 62 |
} |
| 63 |
|
| 64 |
std::string CPortInput::ControllerID() const |
| 65 |
{ |
| 66 |
return m_gameInput->ControllerID(); |
| 67 |
} |
| 68 |
|
| 69 |
bool CPortInput::AcceptsInput(const std::string& feature) const |
| 70 |
{ |
| 71 |
return m_gameInput->AcceptsInput(feature); |
| 72 |
} |
| 73 |
|
| 74 |
bool CPortInput::OnButtonPress(const std::string& feature, bool bPressed) |
| 75 |
{ |
| 76 |
m_controllerActivity->OnButtonPress(bPressed); |
| 77 |
|
| 78 |
if (bPressed && !m_gameInput->AcceptsInput(feature)) |
| 79 |
return false; |
| 80 |
|
| 81 |
return m_gameInput->OnButtonPress(feature, bPressed); |
| 82 |
} |
| 83 |
|
| 84 |
void CPortInput::OnButtonHold(const std::string& feature, unsigned int holdTimeMs) |
| 85 |
{ |
| 86 |
m_controllerActivity->OnButtonPress(true); |
| 87 |
|
| 88 |
m_gameInput->OnButtonHold(feature, holdTimeMs); |
| 89 |
} |
| 90 |
|
| 91 |
bool CPortInput::OnButtonMotion(const std::string& feature, |
| 92 |
float magnitude, |
| 93 |
unsigned int motionTimeMs) |
| 94 |
{ |
| 95 |
m_controllerActivity->OnButtonMotion(magnitude); |
| 96 |
|
| 97 |
if (magnitude > 0.0f && !m_gameInput->AcceptsInput(feature)) |
| 98 |
return false; |
| 99 |
|
| 100 |
return m_gameInput->OnButtonMotion(feature, magnitude, motionTimeMs); |
| 101 |
} |
| 102 |
|
| 103 |
bool CPortInput::OnAnalogStickMotion(const std::string& feature, |
| 104 |
float x, |
| 105 |
float y, |
| 106 |
unsigned int motionTimeMs) |
| 107 |
{ |
| 108 |
m_controllerActivity->OnAnalogStickMotion(x, y); |
| 109 |
|
| 110 |
if ((x != 0.0f || y != 0.0f) && !m_gameInput->AcceptsInput(feature)) |
| 111 |
return false; |
| 112 |
|
| 113 |
return m_gameInput->OnAnalogStickMotion(feature, x, y, motionTimeMs); |
| 114 |
} |
| 115 |
|
| 116 |
bool CPortInput::OnAccelerometerMotion(const std::string& feature, float x, float y, float z) |
| 117 |
{ |
| 118 |
if (!m_gameInput->AcceptsInput(feature)) |
| 119 |
return false; |
| 120 |
|
| 121 |
return m_gameInput->OnAccelerometerMotion(feature, x, y, z); |
| 122 |
} |
| 123 |
|
| 124 |
bool CPortInput::OnWheelMotion(const std::string& feature, |
| 125 |
float position, |
| 126 |
unsigned int motionTimeMs) |
| 127 |
{ |
| 128 |
m_controllerActivity->OnWheelMotion(position); |
| 129 |
|
| 130 |
if ((position != 0.0f) && !m_gameInput->AcceptsInput(feature)) |
| 131 |
return false; |
| 132 |
|
| 133 |
return m_gameInput->OnWheelMotion(feature, position, motionTimeMs); |
| 134 |
} |
| 135 |
|
| 136 |
bool CPortInput::OnThrottleMotion(const std::string& feature, |
| 137 |
float position, |
| 138 |
unsigned int motionTimeMs) |
| 139 |
{ |
| 140 |
m_controllerActivity->OnThrottleMotion(position); |
| 141 |
|
| 142 |
if ((position != 0.0f) && !m_gameInput->AcceptsInput(feature)) |
| 143 |
return false; |
| 144 |
|
| 145 |
return m_gameInput->OnThrottleMotion(feature, position, motionTimeMs); |
| 146 |
} |
| 147 |
|
| 148 |
void CPortInput::OnInputFrame() |
| 149 |
{ |
| 150 |
m_controllerActivity->OnInputFrame(); |
| 151 |
} |
| 152 |
|
| 153 |
int CPortInput::GetWindowID() const |
| 154 |
{ |
| 155 |
return WINDOW_FULLSCREEN_GAME; |
| 156 |
} |