| 1 |
/* |
| 2 |
* Copyright (C) 2015-2021 Team Kodi (https://kodi.tv) |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* See LICENSE.md for more information. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "InputManager.h" |
| 9 |
#include "ControllerTopology.h" |
| 10 |
#include "LibretroDevice.h" |
| 11 |
#include "LibretroDeviceInput.h" |
| 12 |
#include "libretro/ClientBridge.h" |
| 13 |
#include "libretro-common/libretro.h" |
| 14 |
#include "libretro/LibretroEnvironment.h" |
| 15 |
#include "libretro/LibretroTranslator.h" |
| 16 |
#include "log/Log.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <sstream> |
| 20 |
|
| 21 |
using namespace LIBRETRO; |
| 22 |
|
| 23 |
#define PORT_MAX_COUNT 32 // Large enough |
| 24 |
|
| 25 |
CInputManager& CInputManager::Get(void) |
| 26 |
{ |
| 27 |
static CInputManager _instance; |
| 28 |
return _instance; |
| 29 |
} |
| 30 |
|
| 31 |
libretro_device_caps_t CInputManager::GetDeviceCaps(void) const |
| 32 |
{ |
| 33 |
return 1 << RETRO_DEVICE_JOYPAD | |
| 34 |
1 << RETRO_DEVICE_MOUSE | |
| 35 |
1 << RETRO_DEVICE_KEYBOARD | |
| 36 |
1 << RETRO_DEVICE_LIGHTGUN | |
| 37 |
1 << RETRO_DEVICE_ANALOG | |
| 38 |
1 << RETRO_DEVICE_POINTER; |
| 39 |
} |
| 40 |
|
| 41 |
void CInputManager::SetControllerLayouts(const std::vector<kodi::addon::GameControllerLayout>& controllers) |
| 42 |
{ |
| 43 |
m_controllerLayouts.clear(); |
| 44 |
|
| 45 |
for (const auto &controller : controllers) |
| 46 |
{ |
| 47 |
m_controllerLayouts[controller.controller_id].reset(new CControllerLayout(controller)); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
bool CInputManager::EnableKeyboard(const std::string &controllerId) |
| 52 |
{ |
| 53 |
bool bSuccess = false; |
| 54 |
|
| 55 |
if (!CControllerTopology::GetInstance().SetDevice(GAME_PORT_KEYBOARD, controllerId)) |
| 56 |
{ |
| 57 |
esyslog("Error: Keyboard \"%s\" not supported", controllerId.c_str()); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
DevicePtr device(new CLibretroDevice(controllerId)); |
| 62 |
m_keyboard = std::move(device); |
| 63 |
bSuccess = true; |
| 64 |
} |
| 65 |
|
| 66 |
return bSuccess; |
| 67 |
} |
| 68 |
|
| 69 |
void CInputManager::DisableKeyboard() |
| 70 |
{ |
| 71 |
CControllerTopology::GetInstance().RemoveDevice(GAME_PORT_KEYBOARD); |
| 72 |
m_keyboard.reset(); |
| 73 |
} |
| 74 |
|
| 75 |
bool CInputManager::EnableMouse(const std::string &controllerId) |
| 76 |
{ |
| 77 |
bool bSuccess = false; |
| 78 |
|
| 79 |
if (!CControllerTopology::GetInstance().SetDevice(GAME_PORT_MOUSE, controllerId)) |
| 80 |
{ |
| 81 |
esyslog("Error: Mouse \"%s\" not supported", controllerId.c_str()); |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
DevicePtr device(new CLibretroDevice(controllerId)); |
| 86 |
m_mouse = std::move(device); |
| 87 |
bSuccess = true; |
| 88 |
} |
| 89 |
|
| 90 |
return bSuccess; |
| 91 |
} |
| 92 |
|
| 93 |
void CInputManager::DisableMouse() |
| 94 |
{ |
| 95 |
CControllerTopology::GetInstance().RemoveDevice(GAME_PORT_MOUSE); |
| 96 |
m_mouse.reset(); |
| 97 |
} |
| 98 |
|
| 99 |
libretro_device_t CInputManager::ConnectController(const std::string &address, const std::string &controllerId) |
| 100 |
{ |
| 101 |
unsigned int deviceType = RETRO_DEVICE_NONE; // Unmasked device type |
| 102 |
|
| 103 |
const int port = GetPortIndex(address); |
| 104 |
if (port < 0) |
| 105 |
{ |
| 106 |
esyslog("Failed to connect controller, invalid port address: %s", address.c_str()); |
| 107 |
} |
| 108 |
else if (!controllerId.empty()) |
| 109 |
{ |
| 110 |
auto it = m_controllerLayouts.find(controllerId); |
| 111 |
if (it != m_controllerLayouts.end()) |
| 112 |
{ |
| 113 |
const CControllerLayout &controller = *it->second; |
| 114 |
|
| 115 |
const bool bProvidesInput = controller.ProvidesInput(); |
| 116 |
|
| 117 |
if (!CControllerTopology::GetInstance().SetController(address, controllerId, bProvidesInput)) |
| 118 |
{ |
| 119 |
esyslog("Error: Controller port \"%s\" (libretro port %d) does not accept %s", |
| 120 |
address.c_str(), port, controllerId.c_str()); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
DevicePtr device(new CLibretroDevice(controllerId)); |
| 125 |
|
| 126 |
// Check topology for type/subclass override |
| 127 |
libretro_device_t typeOverride = CControllerTopology::GetInstance().TypeOverride(address, controllerId); |
| 128 |
libretro_subclass_t subclassOverride = CControllerTopology::GetInstance().SubclassOverride(address, controllerId); |
| 129 |
|
| 130 |
if (typeOverride != RETRO_DEVICE_NONE) |
| 131 |
device->SetType(typeOverride); |
| 132 |
if (subclassOverride != RETRO_SUBCLASS_NONE) |
| 133 |
device->SetSubclass(typeOverride); |
| 134 |
|
| 135 |
// Calculate type value to send to libretro |
| 136 |
if (device->Subclass() != RETRO_SUBCLASS_NONE) |
| 137 |
deviceType = RETRO_DEVICE_SUBCLASS(device->Type(), device->Subclass()); |
| 138 |
else |
| 139 |
deviceType = device->Type(); |
| 140 |
|
| 141 |
if (port >= static_cast<int>(m_controllers.size())) |
| 142 |
m_controllers.resize(port + 1); |
| 143 |
|
| 144 |
m_controllers[port] = std::move(device); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return deviceType; |
| 150 |
} |
| 151 |
|
| 152 |
bool CInputManager::DisconnectController(const std::string &address) |
| 153 |
{ |
| 154 |
bool bSuccess = false; |
| 155 |
|
| 156 |
const int port = GetPortIndex(address); |
| 157 |
if (port < 0) |
| 158 |
{ |
| 159 |
esyslog("Failed to disconnect controller, invalid port address: %s", address.c_str()); |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
CControllerTopology::GetInstance().RemoveController(address); |
| 164 |
|
| 165 |
if (port < static_cast<int>(m_controllers.size())) |
| 166 |
m_controllers[port].reset(); |
| 167 |
|
| 168 |
bSuccess = true; |
| 169 |
} |
| 170 |
|
| 171 |
return bSuccess; |
| 172 |
} |
| 173 |
|
| 174 |
int CInputManager::GetPortIndex(const std::string &address) const |
| 175 |
{ |
| 176 |
return CControllerTopology::GetInstance().GetPortIndex(address); |
| 177 |
} |
| 178 |
|
| 179 |
bool CInputManager::GetConnectionPortIndex(const std::string &address, int& connectionPort) const |
| 180 |
{ |
| 181 |
return CControllerTopology::GetInstance().GetConnectionPortIndex(address, connectionPort); |
| 182 |
} |
| 183 |
|
| 184 |
std::string CInputManager::GetAddress(unsigned int port) const |
| 185 |
{ |
| 186 |
return CControllerTopology::GetInstance().GetAddress(port); |
| 187 |
} |
| 188 |
|
| 189 |
libretro_device_t CInputManager::GetDeviceType(const std::string &address) const |
| 190 |
{ |
| 191 |
libretro_device_t deviceType = RETRO_DEVICE_NONE; |
| 192 |
|
| 193 |
int port = GetPortIndex(address); |
| 194 |
if (0 <= port && port < static_cast<int>(m_controllers.size())) |
| 195 |
{ |
| 196 |
const DevicePtr &device = m_controllers[port]; |
| 197 |
|
| 198 |
if (device) |
| 199 |
{ |
| 200 |
libretro_subclass_t subclass = device->Subclass(); |
| 201 |
|
| 202 |
if (subclass == RETRO_SUBCLASS_NONE) |
| 203 |
deviceType = device->Type(); |
| 204 |
else |
| 205 |
deviceType = RETRO_DEVICE_SUBCLASS(device->Type(), subclass); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return deviceType; |
| 210 |
} |
| 211 |
|
| 212 |
void CInputManager::ClosePorts(void) |
| 213 |
{ |
| 214 |
m_controllers.clear(); |
| 215 |
} |
| 216 |
|
| 217 |
void CInputManager::EnableAnalogSensors(unsigned int port, bool bEnabled) |
| 218 |
{ |
| 219 |
// TODO |
| 220 |
} |
| 221 |
|
| 222 |
bool CInputManager::InputEvent(const game_input_event& event) |
| 223 |
{ |
| 224 |
std::string controllerId = event.controller_id != nullptr ? event.controller_id : ""; |
| 225 |
std::string feature = event.feature_name != nullptr ? event.feature_name : ""; |
| 226 |
|
| 227 |
if (controllerId.empty() || feature.empty()) |
| 228 |
return false; |
| 229 |
|
| 230 |
bool bHandled = false; |
| 231 |
|
| 232 |
switch (event.port_type) |
| 233 |
{ |
| 234 |
case GAME_PORT_KEYBOARD: |
| 235 |
{ |
| 236 |
if (m_keyboard) |
| 237 |
bHandled |= m_keyboard->Input().InputEvent(event); |
| 238 |
|
| 239 |
break; |
| 240 |
} |
| 241 |
case GAME_PORT_MOUSE: |
| 242 |
{ |
| 243 |
if (m_mouse) |
| 244 |
bHandled = m_mouse->Input().InputEvent(event); |
| 245 |
|
| 246 |
break; |
| 247 |
} |
| 248 |
case GAME_PORT_CONTROLLER: |
| 249 |
{ |
| 250 |
std::string portAddress = event.port_address != nullptr ? event.port_address : ""; |
| 251 |
int port = GetPortIndex(portAddress); |
| 252 |
|
| 253 |
if (0 <= port && port < PORT_MAX_COUNT) |
| 254 |
{ |
| 255 |
// Resize devices if necessary |
| 256 |
if (port >= static_cast<int>(m_controllers.size())) |
| 257 |
m_controllers.resize(port + 1); |
| 258 |
|
| 259 |
if (m_controllers[port]) |
| 260 |
bHandled = m_controllers[port]->Input().InputEvent(event); |
| 261 |
else |
| 262 |
esyslog("Event from controller %s sent to port with no device!", |
| 263 |
event.controller_id != nullptr ? event.controller_id : ""); |
| 264 |
} |
| 265 |
|
| 266 |
break; |
| 267 |
} |
| 268 |
default: |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
return bHandled; |
| 273 |
} |
| 274 |
|
| 275 |
void CInputManager::LogInputDescriptors(const retro_input_descriptor* descriptors) const |
| 276 |
{ |
| 277 |
dsyslog("Libretro input bindings:"); |
| 278 |
dsyslog("------------------------------------------------------------"); |
| 279 |
|
| 280 |
for (const retro_input_descriptor* descriptor = descriptors; |
| 281 |
descriptor != nullptr && descriptor->description != nullptr && !std::string(descriptor->description).empty(); |
| 282 |
descriptor++) |
| 283 |
{ |
| 284 |
std::string component = LibretroTranslator::GetComponentName(descriptor->device, descriptor->index, descriptor->id); |
| 285 |
|
| 286 |
if (component.empty()) |
| 287 |
{ |
| 288 |
dsyslog("Port: %u, Device: %s, Feature: %s, Description: %s", |
| 289 |
descriptor->port, |
| 290 |
LibretroTranslator::GetDeviceName(descriptor->device), |
| 291 |
LibretroTranslator::GetFeatureName(descriptor->device, descriptor->index, descriptor->id), |
| 292 |
descriptor->description ? descriptor->description : ""); |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
dsyslog("Port: %u, Device: %s, Feature: %s, Component: %s, Description: %s", |
| 297 |
descriptor->port, |
| 298 |
LibretroTranslator::GetDeviceName(descriptor->device), |
| 299 |
LibretroTranslator::GetFeatureName(descriptor->device, descriptor->index, descriptor->id), |
| 300 |
component.c_str(), |
| 301 |
descriptor->description ? descriptor->description : ""); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
dsyslog("------------------------------------------------------------"); |
| 306 |
} |
| 307 |
|
| 308 |
std::string CInputManager::ControllerID(unsigned int port) const |
| 309 |
{ |
| 310 |
std::string controllerId; |
| 311 |
|
| 312 |
if (port < m_controllers.size()) |
| 313 |
{ |
| 314 |
const DevicePtr &device = m_controllers[port]; |
| 315 |
if (device) |
| 316 |
controllerId = device->ControllerID(); |
| 317 |
} |
| 318 |
|
| 319 |
return controllerId; |
| 320 |
} |
| 321 |
|
| 322 |
bool CInputManager::ButtonState(libretro_device_t device, unsigned int port, unsigned int buttonIndex) const |
| 323 |
{ |
| 324 |
bool bState = false; |
| 325 |
|
| 326 |
switch (device) |
| 327 |
{ |
| 328 |
case RETRO_DEVICE_KEYBOARD: |
| 329 |
{ |
| 330 |
if (m_keyboard) |
| 331 |
bState = m_keyboard->Input().ButtonState(buttonIndex); |
| 332 |
|
| 333 |
break; |
| 334 |
} |
| 335 |
case RETRO_DEVICE_MOUSE: |
| 336 |
{ |
| 337 |
if (m_mouse) |
| 338 |
{ |
| 339 |
bState = m_mouse->Input().ButtonState(buttonIndex); |
| 340 |
break; |
| 341 |
} |
| 342 |
|
| 343 |
// Fall through to controller ports |
| 344 |
} |
| 345 |
default: |
| 346 |
{ |
| 347 |
if (port < m_controllers.size()) |
| 348 |
{ |
| 349 |
const DevicePtr &controller = m_controllers[port]; |
| 350 |
if (controller) |
| 351 |
bState = controller->Input().ButtonState(buttonIndex); |
| 352 |
} |
| 353 |
|
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
return bState; |
| 359 |
} |
| 360 |
|
| 361 |
float CInputManager::AnalogButtonState(unsigned int port, unsigned int buttonIndex) const |
| 362 |
{ |
| 363 |
float state = 0.0f; |
| 364 |
|
| 365 |
if (port < m_controllers.size()) |
| 366 |
{ |
| 367 |
const DevicePtr &device = m_controllers[port]; |
| 368 |
if (device) |
| 369 |
state = device->Input().AnalogButtonState(buttonIndex); |
| 370 |
} |
| 371 |
|
| 372 |
return state; |
| 373 |
} |
| 374 |
|
| 375 |
int CInputManager::DeltaX(libretro_device_t device, unsigned int port) |
| 376 |
{ |
| 377 |
int deltaX = 0; |
| 378 |
|
| 379 |
if (device == RETRO_DEVICE_MOUSE && m_mouse) |
| 380 |
{ |
| 381 |
deltaX = m_mouse->Input().RelativePointerDeltaX(); |
| 382 |
} |
| 383 |
else if (port < m_controllers.size()) |
| 384 |
{ |
| 385 |
const DevicePtr &device = m_controllers[port]; |
| 386 |
if (device) |
| 387 |
deltaX = device->Input().RelativePointerDeltaX(); |
| 388 |
} |
| 389 |
|
| 390 |
return deltaX; |
| 391 |
} |
| 392 |
|
| 393 |
int CInputManager::DeltaY(libretro_device_t device, unsigned int port) |
| 394 |
{ |
| 395 |
int deltaY = 0; |
| 396 |
|
| 397 |
if (device == RETRO_DEVICE_MOUSE && m_mouse) |
| 398 |
{ |
| 399 |
deltaY = m_mouse->Input().RelativePointerDeltaY(); |
| 400 |
} |
| 401 |
else if (port < m_controllers.size()) |
| 402 |
{ |
| 403 |
const DevicePtr &device = m_controllers[port]; |
| 404 |
if (device) |
| 405 |
deltaY = device->Input().RelativePointerDeltaY(); |
| 406 |
} |
| 407 |
|
| 408 |
return deltaY; |
| 409 |
} |
| 410 |
|
| 411 |
bool CInputManager::AnalogStickState(unsigned int port, unsigned int analogStickIndex, float& x, float& y) const |
| 412 |
{ |
| 413 |
bool bSuccess = false; |
| 414 |
|
| 415 |
if (port < m_controllers.size()) |
| 416 |
{ |
| 417 |
const DevicePtr &device = m_controllers[port]; |
| 418 |
if (device) |
| 419 |
bSuccess = device->Input().AnalogStickState(analogStickIndex, x, y); |
| 420 |
} |
| 421 |
|
| 422 |
return bSuccess; |
| 423 |
} |
| 424 |
|
| 425 |
bool CInputManager::AbsolutePointerState(unsigned int port, unsigned int pointerIndex, float& x, float& y) const |
| 426 |
{ |
| 427 |
bool bSuccess = false; |
| 428 |
|
| 429 |
if (port < m_controllers.size()) |
| 430 |
{ |
| 431 |
const DevicePtr &device = m_controllers[port]; |
| 432 |
if (device) |
| 433 |
bSuccess = device->Input().AbsolutePointerState(pointerIndex, x, y); |
| 434 |
} |
| 435 |
|
| 436 |
return bSuccess; |
| 437 |
} |
| 438 |
|
| 439 |
bool CInputManager::AccelerometerState(unsigned int port, float& x, float& y, float& z) const |
| 440 |
{ |
| 441 |
bool bSuccess = false; |
| 442 |
|
| 443 |
if (port < m_controllers.size()) |
| 444 |
{ |
| 445 |
const DevicePtr &device = m_controllers[port]; |
| 446 |
if (device) |
| 447 |
bSuccess = device->Input().AccelerometerState(x, y, z); |
| 448 |
} |
| 449 |
|
| 450 |
return bSuccess; |
| 451 |
} |
| 452 |
|
| 453 |
void CInputManager::SetControllerInfo(const retro_controller_info* info) |
| 454 |
{ |
| 455 |
dsyslog("Libretro controller info:"); |
| 456 |
dsyslog("------------------------------------------------------------"); |
| 457 |
|
| 458 |
for (unsigned int i = 0; i < info->num_types; i++) |
| 459 |
{ |
| 460 |
const retro_controller_description& type = info->types[i]; |
| 461 |
|
| 462 |
libretro_device_t baseType = type.id & RETRO_DEVICE_MASK; |
| 463 |
|
| 464 |
std::string description = type.desc ? type.desc : ""; |
| 465 |
|
| 466 |
if (type.id & ~RETRO_DEVICE_MASK) |
| 467 |
{ |
| 468 |
libretro_subclass_t subclass = (type.id >> RETRO_DEVICE_TYPE_SHIFT) - 1; |
| 469 |
dsyslog("Device: %s, Subclass: %u, Description: \"%s\"", |
| 470 |
LibretroTranslator::GetDeviceName(baseType), subclass, description.c_str()); |
| 471 |
} |
| 472 |
else |
| 473 |
{ |
| 474 |
dsyslog("Device: %s, Description: \"%s\"", |
| 475 |
LibretroTranslator::GetDeviceName(baseType), description.c_str()); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
dsyslog("------------------------------------------------------------"); |
| 480 |
} |