Debian Codegraph @

xbmc/games/addons/input/GameClientJoystick.cpp

raw
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 "GameClientJoystick.h"
10
11 #include "GameClientInput.h"
12 #include "GameClientTopology.h"
13 #include "games/addons/GameClient.h"
14 #include "games/controllers/Controller.h"
15 #include "games/ports/input/PortInput.h"
16 #include "input/joysticks/interfaces/IInputReceiver.h"
17 #include "peripherals/devices/Peripheral.h"
18 #include "utils/log.h"
19
20 #include <assert.h>
21
22 using namespace KODI;
23 using namespace GAME;
24
25 CGameClientJoystick::CGameClientJoystick(CGameClient& gameClient,
26 const std::string& portAddress,
27 const ControllerPtr& controller)
28 : m_gameClient(gameClient),
29 m_portAddress(portAddress),
30 m_controller(controller),
31 m_portInput(new CPortInput(this))
32 {
33 assert(m_controller.get() != NULL);
34 }
35
36 CGameClientJoystick::~CGameClientJoystick() = default;
37
38 void CGameClientJoystick::RegisterInput(JOYSTICK::IInputProvider* inputProvider)
39 {
40 m_portInput->RegisterInput(inputProvider);
41 }
42
43 void CGameClientJoystick::UnregisterInput(JOYSTICK::IInputProvider* inputProvider)
44 {
45 m_portInput->UnregisterInput(inputProvider);
46 }
47
48 std::string CGameClientJoystick::ControllerID(void) const
49 {
50 return m_controller->ID();
51 }
52
53 bool CGameClientJoystick::HasFeature(const std::string& feature) const
54 {
55 return m_gameClient.Input().HasFeature(m_controller->ID(), feature);
56 }
57
58 bool CGameClientJoystick::AcceptsInput(const std::string& feature) const
59 {
60 return m_gameClient.Input().AcceptsInput();
61 }
62
63 bool CGameClientJoystick::OnButtonPress(const std::string& feature, bool bPressed)
64 {
65 game_input_event event;
66
67 std::string controllerId = m_controller->ID();
68
69 event.type = GAME_INPUT_EVENT_DIGITAL_BUTTON;
70 event.controller_id = controllerId.c_str();
71 event.port_type = GAME_PORT_CONTROLLER;
72 event.port_address = m_portAddress.c_str();
73 event.feature_name = feature.c_str();
74 event.digital_button.pressed = bPressed;
75
76 return m_gameClient.Input().InputEvent(event);
77 }
78
79 bool CGameClientJoystick::OnButtonMotion(const std::string& feature,
80 float magnitude,
81 unsigned int motionTimeMs)
82 {
83 game_input_event event;
84
85 std::string controllerId = m_controller->ID();
86
87 event.type = GAME_INPUT_EVENT_ANALOG_BUTTON;
88 event.controller_id = controllerId.c_str();
89 event.port_type = GAME_PORT_CONTROLLER;
90 event.port_address = m_portAddress.c_str();
91 event.feature_name = feature.c_str();
92 event.analog_button.magnitude = magnitude;
93
94 return m_gameClient.Input().InputEvent(event);
95 }
96
97 bool CGameClientJoystick::OnAnalogStickMotion(const std::string& feature,
98 float x,
99 float y,
100 unsigned int motionTimeMs)
101 {
102 game_input_event event;
103
104 std::string controllerId = m_controller->ID();
105
106 event.type = GAME_INPUT_EVENT_ANALOG_STICK;
107 event.controller_id = controllerId.c_str();
108 event.port_type = GAME_PORT_CONTROLLER;
109 event.port_address = m_portAddress.c_str();
110 event.feature_name = feature.c_str();
111 event.analog_stick.x = x;
112 event.analog_stick.y = y;
113
114 return m_gameClient.Input().InputEvent(event);
115 }
116
117 bool CGameClientJoystick::OnAccelerometerMotion(const std::string& feature,
118 float x,
119 float y,
120 float z)
121 {
122 game_input_event event;
123
124 std::string controllerId = m_controller->ID();
125
126 event.type = GAME_INPUT_EVENT_ACCELEROMETER;
127 event.controller_id = controllerId.c_str();
128 event.port_type = GAME_PORT_CONTROLLER;
129 event.port_address = m_portAddress.c_str();
130 event.feature_name = feature.c_str();
131 event.accelerometer.x = x;
132 event.accelerometer.y = y;
133 event.accelerometer.z = z;
134
135 return m_gameClient.Input().InputEvent(event);
136 }
137
138 bool CGameClientJoystick::OnWheelMotion(const std::string& feature,
139 float position,
140 unsigned int motionTimeMs)
141 {
142 game_input_event event;
143
144 std::string controllerId = m_controller->ID();
145
146 event.type = GAME_INPUT_EVENT_AXIS;
147 event.controller_id = controllerId.c_str();
148 event.port_type = GAME_PORT_CONTROLLER;
149 event.port_address = m_portAddress.c_str();
150 event.feature_name = feature.c_str();
151 event.axis.position = position;
152
153 return m_gameClient.Input().InputEvent(event);
154 }
155
156 bool CGameClientJoystick::OnThrottleMotion(const std::string& feature,
157 float position,
158 unsigned int motionTimeMs)
159 {
160 game_input_event event;
161
162 std::string controllerId = m_controller->ID();
163
164 event.type = GAME_INPUT_EVENT_AXIS;
165 event.controller_id = controllerId.c_str();
166 event.port_type = GAME_PORT_CONTROLLER;
167 event.port_address = m_portAddress.c_str();
168 event.feature_name = feature.c_str();
169 event.axis.position = position;
170
171 return m_gameClient.Input().InputEvent(event);
172 }
173
174 std::string CGameClientJoystick::GetControllerAddress() const
175 {
176 return CGameClientTopology::MakeAddress(m_portAddress, m_controller->ID());
177 }
178
179 std::string CGameClientJoystick::GetSourceLocation() const
180 {
181 if (m_sourcePeripheral)
182 return m_sourcePeripheral->Location();
183
184 return "";
185 }
186
187 float CGameClientJoystick::GetActivation() const
188 {
189 return m_portInput->GetActivation();
190 }
191
192 void CGameClientJoystick::SetSource(PERIPHERALS::PeripheralPtr sourcePeripheral)
193 {
194 m_sourcePeripheral = std::move(sourcePeripheral);
195 }
196
197 void CGameClientJoystick::ClearSource()
198 {
199 m_sourcePeripheral.reset();
200 }
201
202 bool CGameClientJoystick::SetRumble(const std::string& feature, float magnitude)
203 {
204 bool bHandled = false;
205
206 if (InputReceiver())
207 bHandled = InputReceiver()->SetRumbleState(feature, magnitude);
208
209 return bHandled;
210 }