summaryrefslogtreecommitdiffstats
path: root/xbmc/games/addons/input/GameClientJoystick.cpp
blob: d4f083ba08228e2bf3405f431b4f2db95b12ce2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 *  Copyright (C) 2015-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GameClientJoystick.h"

#include "GameClientInput.h"
#include "GameClientTopology.h"
#include "games/addons/GameClient.h"
#include "games/controllers/Controller.h"
#include "games/ports/input/PortInput.h"
#include "input/joysticks/interfaces/IInputReceiver.h"
#include "peripherals/devices/Peripheral.h"
#include "utils/log.h"

#include <assert.h>

using namespace KODI;
using namespace GAME;

CGameClientJoystick::CGameClientJoystick(CGameClient& gameClient,
                                         const std::string& portAddress,
                                         const ControllerPtr& controller)
  : m_gameClient(gameClient),
    m_portAddress(portAddress),
    m_controller(controller),
    m_portInput(new CPortInput(this))
{
  assert(m_controller.get() != NULL);
}

CGameClientJoystick::~CGameClientJoystick() = default;

void CGameClientJoystick::RegisterInput(JOYSTICK::IInputProvider* inputProvider)
{
  m_portInput->RegisterInput(inputProvider);
}

void CGameClientJoystick::UnregisterInput(JOYSTICK::IInputProvider* inputProvider)
{
  m_portInput->UnregisterInput(inputProvider);
}

std::string CGameClientJoystick::ControllerID(void) const
{
  return m_controller->ID();
}

bool CGameClientJoystick::HasFeature(const std::string& feature) const
{
  return m_gameClient.Input().HasFeature(m_controller->ID(), feature);
}

bool CGameClientJoystick::AcceptsInput(const std::string& feature) const
{
  return m_gameClient.Input().AcceptsInput();
}

bool CGameClientJoystick::OnButtonPress(const std::string& feature, bool bPressed)
{
  game_input_event event;

  std::string controllerId = m_controller->ID();

  event.type = GAME_INPUT_EVENT_DIGITAL_BUTTON;
  event.controller_id = controllerId.c_str();
  event.port_type = GAME_PORT_CONTROLLER;
  event.port_address = m_portAddress.c_str();
  event.feature_name = feature.c_str();
  event.digital_button.pressed = bPressed;

  return m_gameClient.Input().InputEvent(event);
}

bool CGameClientJoystick::OnButtonMotion(const std::string& feature,
                                         float magnitude,
                                         unsigned int motionTimeMs)
{
  game_input_event event;

  std::string controllerId = m_controller->ID();

  event.type = GAME_INPUT_EVENT_ANALOG_BUTTON;
  event.controller_id = controllerId.c_str();
  event.port_type = GAME_PORT_CONTROLLER;
  event.port_address = m_portAddress.c_str();
  event.feature_name = feature.c_str();
  event.analog_button.magnitude = magnitude;

  return m_gameClient.Input().InputEvent(event);
}

bool CGameClientJoystick::OnAnalogStickMotion(const std::string& feature,
                                              float x,
                                              float y,
                                              unsigned int motionTimeMs)
{
  game_input_event event;

  std::string controllerId = m_controller->ID();

  event.type = GAME_INPUT_EVENT_ANALOG_STICK;
  event.controller_id = controllerId.c_str();
  event.port_type = GAME_PORT_CONTROLLER;
  event.port_address = m_portAddress.c_str();
  event.feature_name = feature.c_str();
  event.analog_stick.x = x;
  event.analog_stick.y = y;

  return m_gameClient.Input().InputEvent(event);
}

bool CGameClientJoystick::OnAccelerometerMotion(const std::string& feature,
                                                float x,
                                                float y,
                                                float z)
{
  game_input_event event;

  std::string controllerId = m_controller->ID();

  event.type = GAME_INPUT_EVENT_ACCELEROMETER;
  event.controller_id = controllerId.c_str();
  event.port_type = GAME_PORT_CONTROLLER;
  event.port_address = m_portAddress.c_str();
  event.feature_name = feature.c_str();
  event.accelerometer.x = x;
  event.accelerometer.y = y;
  event.accelerometer.z = z;

  return m_gameClient.Input().InputEvent(event);
}

bool CGameClientJoystick::OnWheelMotion(const std::string& feature,
                                        float position,
                                        unsigned int motionTimeMs)
{
  game_input_event event;

  std::string controllerId = m_controller->ID();

  event.type = GAME_INPUT_EVENT_AXIS;
  event.controller_id = controllerId.c_str();
  event.port_type = GAME_PORT_CONTROLLER;
  event.port_address = m_portAddress.c_str();
  event.feature_name = feature.c_str();
  event.axis.position = position;

  return m_gameClient.Input().InputEvent(event);
}

bool CGameClientJoystick::OnThrottleMotion(const std::string& feature,
                                           float position,
                                           unsigned int motionTimeMs)
{
  game_input_event event;

  std::string controllerId = m_controller->ID();

  event.type = GAME_INPUT_EVENT_AXIS;
  event.controller_id = controllerId.c_str();
  event.port_type = GAME_PORT_CONTROLLER;
  event.port_address = m_portAddress.c_str();
  event.feature_name = feature.c_str();
  event.axis.position = position;

  return m_gameClient.Input().InputEvent(event);
}

std::string CGameClientJoystick::GetControllerAddress() const
{
  return CGameClientTopology::MakeAddress(m_portAddress, m_controller->ID());
}

std::string CGameClientJoystick::GetSourceLocation() const
{
  if (m_sourcePeripheral)
    return m_sourcePeripheral->Location();

  return "";
}

void CGameClientJoystick::SetSource(PERIPHERALS::PeripheralPtr sourcePeripheral)
{
  m_sourcePeripheral = std::move(sourcePeripheral);
}

void CGameClientJoystick::ClearSource()
{
  m_sourcePeripheral.reset();
}

bool CGameClientJoystick::SetRumble(const std::string& feature, float magnitude)
{
  bool bHandled = false;

  if (InputReceiver())
    bHandled = InputReceiver()->SetRumbleState(feature, magnitude);

  return bHandled;
}