| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 "AgentController.h" |
| 10 |
|
| 11 |
#include "AgentJoystick.h" |
| 12 |
#include "AgentKeyboard.h" |
| 13 |
#include "AgentMouse.h" |
| 14 |
#include "games/controllers/Controller.h" |
| 15 |
#include "games/controllers/ControllerLayout.h" |
| 16 |
#include "peripherals/devices/Peripheral.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
|
| 20 |
using namespace KODI; |
| 21 |
using namespace GAME; |
| 22 |
|
| 23 |
CAgentController::CAgentController(PERIPHERALS::PeripheralPtr peripheral) |
| 24 |
: m_peripheral(std::move(peripheral)) |
| 25 |
{ |
| 26 |
switch (m_peripheral->Type()) |
| 27 |
{ |
| 28 |
case PERIPHERALS::PERIPHERAL_JOYSTICK: |
| 29 |
{ |
| 30 |
m_joystick = std::make_unique<CAgentJoystick>(m_peripheral); |
| 31 |
break; |
| 32 |
} |
| 33 |
case PERIPHERALS::PERIPHERAL_KEYBOARD: |
| 34 |
{ |
| 35 |
m_keyboard = std::make_unique<CAgentKeyboard>(m_peripheral); |
| 36 |
break; |
| 37 |
} |
| 38 |
case PERIPHERALS::PERIPHERAL_MOUSE: |
| 39 |
{ |
| 40 |
m_mouse = std::make_unique<CAgentMouse>(m_peripheral); |
| 41 |
break; |
| 42 |
} |
| 43 |
default: |
| 44 |
break; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
CAgentController::~CAgentController() |
| 49 |
{ |
| 50 |
Deinitialize(); |
| 51 |
} |
| 52 |
|
| 53 |
void CAgentController::Initialize() |
| 54 |
{ |
| 55 |
if (m_joystick) |
| 56 |
m_joystick->Initialize(); |
| 57 |
if (m_keyboard) |
| 58 |
m_keyboard->Initialize(); |
| 59 |
if (m_mouse) |
| 60 |
m_mouse->Initialize(); |
| 61 |
} |
| 62 |
|
| 63 |
void CAgentController::Deinitialize() |
| 64 |
{ |
| 65 |
if (m_mouse) |
| 66 |
m_mouse->Deinitialize(); |
| 67 |
if (m_keyboard) |
| 68 |
m_keyboard->Deinitialize(); |
| 69 |
if (m_joystick) |
| 70 |
m_joystick->Deinitialize(); |
| 71 |
} |
| 72 |
|
| 73 |
std::string CAgentController::GetPeripheralName() const |
| 74 |
{ |
| 75 |
std::string deviceName = m_peripheral->DeviceName(); |
| 76 |
|
| 77 |
// Handle unknown device name |
| 78 |
if (deviceName.empty()) |
| 79 |
{ |
| 80 |
ControllerPtr controller = m_peripheral->ControllerProfile(); |
| 81 |
if (controller) |
| 82 |
deviceName = controller->Layout().Label(); |
| 83 |
} |
| 84 |
|
| 85 |
return deviceName; |
| 86 |
} |
| 87 |
|
| 88 |
const std::string& CAgentController::GetPeripheralLocation() const |
| 89 |
{ |
| 90 |
return m_peripheral->Location(); |
| 91 |
} |
| 92 |
|
| 93 |
ControllerPtr CAgentController::GetController() const |
| 94 |
{ |
| 95 |
// Use joystick controller if joystick is initialized |
| 96 |
if (m_joystick) |
| 97 |
{ |
| 98 |
ControllerPtr controller = m_joystick->Appearance(); |
| 99 |
if (controller) |
| 100 |
return controller; |
| 101 |
} |
| 102 |
|
| 103 |
// Use peripheral controller if joystick is deinitialized |
| 104 |
return m_peripheral->ControllerProfile(); |
| 105 |
} |
| 106 |
|
| 107 |
CDateTime CAgentController::LastActive() const |
| 108 |
{ |
| 109 |
return m_peripheral->LastActive(); |
| 110 |
} |
| 111 |
|
| 112 |
float CAgentController::GetActivation() const |
| 113 |
{ |
| 114 |
// Return the maximum activation of all joystick, keyboard and mice input providers |
| 115 |
float activation = 0.0f; |
| 116 |
|
| 117 |
if (m_joystick) |
| 118 |
activation = std::max(activation, m_joystick->GetActivation()); |
| 119 |
if (m_keyboard) |
| 120 |
activation = std::max(activation, m_keyboard->GetActivation()); |
| 121 |
if (m_mouse) |
| 122 |
activation = std::max(activation, m_mouse->GetActivation()); |
| 123 |
|
| 124 |
return activation; |
| 125 |
} |
| 126 |
|
| 127 |
void CAgentController::ClearButtonState() |
| 128 |
{ |
| 129 |
if (m_joystick) |
| 130 |
m_joystick->ClearButtonState(); |
| 131 |
if (m_keyboard) |
| 132 |
m_keyboard->ClearButtonState(); |
| 133 |
if (m_mouse) |
| 134 |
m_mouse->ClearButtonState(); |
| 135 |
} |