| 1 |
/* |
| 2 |
* Copyright (C) 2014-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 "AddonInputHandling.h" |
| 10 |
|
| 11 |
#include "input/joysticks/generic/DriverReceiving.h" |
| 12 |
#include "input/joysticks/generic/InputHandling.h" |
| 13 |
#include "input/joysticks/interfaces/IDriverReceiver.h" |
| 14 |
#include "input/joysticks/interfaces/IInputHandler.h" |
| 15 |
#include "input/keyboard/generic/KeyboardInputHandling.h" |
| 16 |
#include "input/keyboard/interfaces/IKeyboardInputHandler.h" |
| 17 |
#include "input/mouse/generic/MouseInputHandling.h" |
| 18 |
#include "input/mouse/interfaces/IMouseInputHandler.h" |
| 19 |
#include "peripherals/addons/AddonButtonMap.h" |
| 20 |
#include "peripherals/devices/Peripheral.h" |
| 21 |
#include "utils/log.h" |
| 22 |
|
| 23 |
using namespace KODI; |
| 24 |
using namespace JOYSTICK; |
| 25 |
using namespace PERIPHERALS; |
| 26 |
|
| 27 |
CAddonInputHandling::CAddonInputHandling(CPeripherals& manager, |
| 28 |
CPeripheral* peripheral, |
| 29 |
std::shared_ptr<CPeripheralAddon> addon, |
| 30 |
IInputHandler* handler, |
| 31 |
IDriverReceiver* receiver) |
| 32 |
: m_manager(manager), |
| 33 |
m_peripheral(peripheral), |
| 34 |
m_addon(std::move(addon)), |
| 35 |
m_joystickInputHandler(handler), |
| 36 |
m_joystickDriverReceiver(receiver) |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
CAddonInputHandling::CAddonInputHandling(CPeripherals& manager, |
| 41 |
CPeripheral* peripheral, |
| 42 |
std::shared_ptr<CPeripheralAddon> addon, |
| 43 |
KEYBOARD::IKeyboardInputHandler* handler) |
| 44 |
: m_manager(manager), |
| 45 |
m_peripheral(peripheral), |
| 46 |
m_addon(std::move(addon)), |
| 47 |
m_keyboardInputHandler(handler) |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
CAddonInputHandling::CAddonInputHandling(CPeripherals& manager, |
| 52 |
CPeripheral* peripheral, |
| 53 |
std::shared_ptr<CPeripheralAddon> addon, |
| 54 |
MOUSE::IMouseInputHandler* handler) |
| 55 |
: m_manager(manager), |
| 56 |
m_peripheral(peripheral), |
| 57 |
m_addon(std::move(addon)), |
| 58 |
m_mouseInputHandler(handler) |
| 59 |
{ |
| 60 |
} |
| 61 |
|
| 62 |
CAddonInputHandling::~CAddonInputHandling(void) |
| 63 |
{ |
| 64 |
m_joystickDriverHandler.reset(); |
| 65 |
m_joystickInputReceiver.reset(); |
| 66 |
m_keyboardDriverHandler.reset(); |
| 67 |
m_mouseDriverHandler.reset(); |
| 68 |
m_buttonMap.reset(); |
| 69 |
} |
| 70 |
|
| 71 |
bool CAddonInputHandling::Load() |
| 72 |
{ |
| 73 |
std::string controllerId; |
| 74 |
if (m_joystickInputHandler != nullptr) |
| 75 |
controllerId = m_joystickInputHandler->ControllerID(); |
| 76 |
else if (m_keyboardInputHandler != nullptr) |
| 77 |
controllerId = m_keyboardInputHandler->ControllerID(); |
| 78 |
else if (m_mouseInputHandler != nullptr) |
| 79 |
controllerId = m_mouseInputHandler->ControllerID(); |
| 80 |
|
| 81 |
if (!controllerId.empty()) |
| 82 |
m_buttonMap = std::make_unique<CAddonButtonMap>(m_peripheral, m_addon, controllerId, m_manager); |
| 83 |
|
| 84 |
if (m_buttonMap && m_buttonMap->Load()) |
| 85 |
{ |
| 86 |
if (m_joystickInputHandler != nullptr) |
| 87 |
{ |
| 88 |
m_joystickDriverHandler = |
| 89 |
std::make_unique<CInputHandling>(m_joystickInputHandler, m_buttonMap.get()); |
| 90 |
if (m_joystickDriverReceiver != nullptr) |
| 91 |
{ |
| 92 |
m_joystickInputReceiver = |
| 93 |
std::make_unique<CDriverReceiving>(m_joystickDriverReceiver, m_buttonMap.get()); |
| 94 |
|
| 95 |
// Interfaces are connected here because they share button map as a common resource |
| 96 |
m_joystickInputHandler->SetInputReceiver(m_joystickInputReceiver.get()); |
| 97 |
} |
| 98 |
return true; |
| 99 |
} |
| 100 |
else if (m_keyboardInputHandler != nullptr) |
| 101 |
{ |
| 102 |
m_keyboardDriverHandler = std::make_unique<KEYBOARD::CKeyboardInputHandling>( |
| 103 |
m_keyboardInputHandler, m_buttonMap.get()); |
| 104 |
return true; |
| 105 |
} |
| 106 |
else if (m_mouseInputHandler != nullptr) |
| 107 |
{ |
| 108 |
m_mouseDriverHandler = |
| 109 |
std::make_unique<MOUSE::CMouseInputHandling>(m_mouseInputHandler, m_buttonMap.get()); |
| 110 |
return true; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
bool CAddonInputHandling::OnButtonMotion(unsigned int buttonIndex, bool bPressed) |
| 118 |
{ |
| 119 |
if (m_joystickDriverHandler) |
| 120 |
return m_joystickDriverHandler->OnButtonMotion(buttonIndex, bPressed); |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
bool CAddonInputHandling::OnHatMotion(unsigned int hatIndex, HAT_STATE state) |
| 126 |
{ |
| 127 |
if (m_joystickDriverHandler) |
| 128 |
return m_joystickDriverHandler->OnHatMotion(hatIndex, state); |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
bool CAddonInputHandling::OnAxisMotion(unsigned int axisIndex, |
| 134 |
float position, |
| 135 |
int center, |
| 136 |
unsigned int range) |
| 137 |
{ |
| 138 |
if (m_joystickDriverHandler) |
| 139 |
return m_joystickDriverHandler->OnAxisMotion(axisIndex, position, center, range); |
| 140 |
|
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
void CAddonInputHandling::OnInputFrame(void) |
| 145 |
{ |
| 146 |
if (m_joystickDriverHandler) |
| 147 |
m_joystickDriverHandler->OnInputFrame(); |
| 148 |
} |
| 149 |
|
| 150 |
bool CAddonInputHandling::OnKeyPress(const CKey& key) |
| 151 |
{ |
| 152 |
if (m_keyboardDriverHandler) |
| 153 |
return m_keyboardDriverHandler->OnKeyPress(key); |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
void CAddonInputHandling::OnKeyRelease(const CKey& key) |
| 159 |
{ |
| 160 |
if (m_keyboardDriverHandler) |
| 161 |
m_keyboardDriverHandler->OnKeyRelease(key); |
| 162 |
} |
| 163 |
|
| 164 |
bool CAddonInputHandling::OnPosition(int x, int y) |
| 165 |
{ |
| 166 |
if (m_mouseDriverHandler) |
| 167 |
return m_mouseDriverHandler->OnPosition(x, y); |
| 168 |
|
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
bool CAddonInputHandling::OnButtonPress(MOUSE::BUTTON_ID button) |
| 173 |
{ |
| 174 |
if (m_mouseDriverHandler) |
| 175 |
return m_mouseDriverHandler->OnButtonPress(button); |
| 176 |
|
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
void CAddonInputHandling::OnButtonRelease(MOUSE::BUTTON_ID button) |
| 181 |
{ |
| 182 |
if (m_mouseDriverHandler) |
| 183 |
m_mouseDriverHandler->OnButtonRelease(button); |
| 184 |
} |
| 185 |
|
| 186 |
bool CAddonInputHandling::SetRumbleState(const JOYSTICK::FeatureName& feature, float magnitude) |
| 187 |
{ |
| 188 |
if (m_joystickInputReceiver) |
| 189 |
return m_joystickInputReceiver->SetRumbleState(feature, magnitude); |
| 190 |
|
| 191 |
return false; |
| 192 |
} |