Debian Codegraph @

xbmc/games/agents/input/AgentJoystick.cpp

raw
1 /*
2 * Copyright (C) 2023-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 "AgentJoystick.h"
10
11 #include "games/controllers/Controller.h"
12 #include "games/controllers/ControllerIDs.h"
13 #include "games/controllers/input/ControllerActivity.h"
14 #include "input/joysticks/interfaces/IInputProvider.h"
15 #include "peripherals/devices/Peripheral.h"
16
17 using namespace KODI;
18 using namespace GAME;
19
20 CAgentJoystick::CAgentJoystick(PERIPHERALS::PeripheralPtr peripheral)
21 : m_peripheral(std::move(peripheral)),
22 m_controllerActivity(std::make_unique<CControllerActivity>())
23 {
24 }
25
26 CAgentJoystick::~CAgentJoystick() = default;
27
28 void CAgentJoystick::Initialize()
29 {
30 // Record appearance to detect changes
31 m_controllerAppearance = m_peripheral->ControllerProfile();
32
33 // Upcast peripheral to input interface
34 JOYSTICK::IInputProvider* inputProvider = m_peripheral.get();
35
36 // Register input handler to silently observe all input
37 inputProvider->RegisterInputHandler(this, true);
38 }
39
40 void CAgentJoystick::Deinitialize()
41 {
42 // Upcast peripheral to input interface
43 JOYSTICK::IInputProvider* inputProvider = m_peripheral.get();
44
45 // Unregister input handler
46 inputProvider->UnregisterInputHandler(this);
47
48 // Reset appearance
49 m_controllerAppearance.reset();
50 }
51
52 void CAgentJoystick::ClearButtonState()
53 {
54 return m_controllerActivity->ClearButtonState();
55 }
56
57 float CAgentJoystick::GetActivation() const
58 {
59 return m_controllerActivity->GetActivation();
60 }
61
62 std::string CAgentJoystick::ControllerID(void) const
63 {
64 if (m_controllerAppearance)
65 return m_controllerAppearance->ID();
66
67 return DEFAULT_CONTROLLER_ID;
68 }
69
70 bool CAgentJoystick::HasFeature(const std::string& feature) const
71 {
72 return true; // Capture input for all features
73 }
74
75 bool CAgentJoystick::AcceptsInput(const std::string& feature) const
76 {
77 return true; // Accept input for all features
78 }
79
80 bool CAgentJoystick::OnButtonPress(const std::string& feature, bool bPressed)
81 {
82 m_controllerActivity->OnButtonPress(bPressed);
83 return true;
84 }
85
86 void CAgentJoystick::OnButtonHold(const std::string& feature, unsigned int holdTimeMs)
87 {
88 m_controllerActivity->OnButtonPress(true);
89 }
90
91 bool CAgentJoystick::OnButtonMotion(const std::string& feature,
92 float magnitude,
93 unsigned int motionTimeMs)
94 {
95 m_controllerActivity->OnButtonMotion(magnitude);
96 return true;
97 }
98
99 bool CAgentJoystick::OnAnalogStickMotion(const std::string& feature,
100 float x,
101 float y,
102 unsigned int motionTimeMs)
103 {
104 m_controllerActivity->OnAnalogStickMotion(x, y);
105 return true;
106 }
107
108 bool CAgentJoystick::OnWheelMotion(const std::string& feature,
109 float position,
110 unsigned int motionTimeMs)
111 {
112 m_controllerActivity->OnWheelMotion(position);
113 return true;
114 }
115
116 bool CAgentJoystick::OnThrottleMotion(const std::string& feature,
117 float position,
118 unsigned int motionTimeMs)
119 {
120 m_controllerActivity->OnThrottleMotion(position);
121 return true;
122 }
123
124 void CAgentJoystick::OnInputFrame()
125 {
126 m_controllerActivity->OnInputFrame();
127 }