| 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 "AgentInput.h" |
| 10 |
|
| 11 |
#include "AgentController.h" |
| 12 |
#include "games/addons/GameClient.h" |
| 13 |
#include "games/addons/input/GameClientInput.h" |
| 14 |
#include "games/addons/input/GameClientJoystick.h" |
| 15 |
#include "games/controllers/Controller.h" |
| 16 |
#include "input/InputManager.h" |
| 17 |
#include "peripherals/Peripherals.h" |
| 18 |
#include "peripherals/devices/Peripheral.h" |
| 19 |
#include "peripherals/devices/PeripheralJoystick.h" |
| 20 |
#include "peripherals/events/EventLockHandle.h" |
| 21 |
#include "utils/log.h" |
| 22 |
|
| 23 |
#include <array> |
| 24 |
|
| 25 |
using namespace KODI; |
| 26 |
using namespace GAME; |
| 27 |
|
| 28 |
CAgentInput::CAgentInput(PERIPHERALS::CPeripherals& peripheralManager, CInputManager& inputManager) |
| 29 |
: m_peripheralManager(peripheralManager), m_inputManager(inputManager) |
| 30 |
{ |
| 31 |
// Register callbacks |
| 32 |
m_peripheralManager.RegisterObserver(this); |
| 33 |
m_inputManager.RegisterKeyboardDriverHandler(this); |
| 34 |
m_inputManager.RegisterMouseDriverHandler(this); |
| 35 |
} |
| 36 |
|
| 37 |
CAgentInput::~CAgentInput() |
| 38 |
{ |
| 39 |
// Unregister callbacks in reverse order |
| 40 |
m_inputManager.UnregisterMouseDriverHandler(this); |
| 41 |
m_inputManager.UnregisterKeyboardDriverHandler(this); |
| 42 |
m_peripheralManager.UnregisterObserver(this); |
| 43 |
} |
| 44 |
|
| 45 |
void CAgentInput::Start(GameClientPtr gameClient) |
| 46 |
{ |
| 47 |
// Initialize state |
| 48 |
m_gameClient = std::move(gameClient); |
| 49 |
|
| 50 |
// Register callbacks |
| 51 |
if (m_gameClient) |
| 52 |
m_gameClient->Input().RegisterObserver(this); |
| 53 |
|
| 54 |
// Perform initial refresh |
| 55 |
Refresh(); |
| 56 |
} |
| 57 |
|
| 58 |
void CAgentInput::Stop() |
| 59 |
{ |
| 60 |
// Unregister callbacks in reverse order |
| 61 |
if (m_gameClient) |
| 62 |
m_gameClient->Input().UnregisterObserver(this); |
| 63 |
|
| 64 |
// Close open joysticks |
| 65 |
{ |
| 66 |
PERIPHERALS::EventLockHandlePtr inputHandlingLock; |
| 67 |
|
| 68 |
for (const auto& [inputProvider, joystick] : m_portMap) |
| 69 |
{ |
| 70 |
if (!inputHandlingLock) |
| 71 |
inputHandlingLock = m_peripheralManager.RegisterEventLock(); |
| 72 |
|
| 73 |
joystick->UnregisterInput(inputProvider); |
| 74 |
|
| 75 |
SetChanged(true); |
| 76 |
} |
| 77 |
|
| 78 |
m_portMap.clear(); |
| 79 |
m_peripheralMap.clear(); |
| 80 |
m_disconnectedPeripherals.clear(); |
| 81 |
} |
| 82 |
|
| 83 |
// Close open keyboard |
| 84 |
if (!m_keyboardPort.empty()) |
| 85 |
{ |
| 86 |
m_keyboardPort.clear(); |
| 87 |
SetChanged(true); |
| 88 |
} |
| 89 |
|
| 90 |
// Close open mouse |
| 91 |
if (!m_mousePort.empty()) |
| 92 |
{ |
| 93 |
m_mousePort.clear(); |
| 94 |
SetChanged(true); |
| 95 |
} |
| 96 |
|
| 97 |
// Notify observers if anything changed |
| 98 |
NotifyObservers(ObservableMessageAgentControllersChanged); |
| 99 |
|
| 100 |
// Reset state |
| 101 |
m_gameClient.reset(); |
| 102 |
} |
| 103 |
|
| 104 |
void CAgentInput::Refresh() |
| 105 |
{ |
| 106 |
if (m_gameClient) |
| 107 |
{ |
| 108 |
// Process keyboard |
| 109 |
ProcessKeyboard(); |
| 110 |
|
| 111 |
// Process mouse |
| 112 |
ProcessMouse(); |
| 113 |
|
| 114 |
// Open/close joysticks |
| 115 |
PERIPHERALS::EventLockHandlePtr inputHandlingLock; |
| 116 |
ProcessJoysticks(inputHandlingLock); |
| 117 |
} |
| 118 |
|
| 119 |
// Notify observers if anything changed |
| 120 |
NotifyObservers(ObservableMessageAgentControllersChanged); |
| 121 |
} |
| 122 |
|
| 123 |
void CAgentInput::Notify(const Observable& obs, const ObservableMessage msg) |
| 124 |
{ |
| 125 |
switch (msg) |
| 126 |
{ |
| 127 |
case ObservableMessageGamePortsChanged: |
| 128 |
case ObservableMessagePeripheralsChanged: |
| 129 |
{ |
| 130 |
Refresh(); |
| 131 |
break; |
| 132 |
} |
| 133 |
default: |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
bool CAgentInput::OnKeyPress(const CKey& key) |
| 139 |
{ |
| 140 |
if (!m_bHasKeyboard) |
| 141 |
{ |
| 142 |
m_bHasKeyboard = true; |
| 143 |
ProcessKeyboard(); |
| 144 |
NotifyObservers(ObservableMessageAgentControllersChanged); |
| 145 |
} |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
bool CAgentInput::OnPosition(int x, int y) |
| 150 |
{ |
| 151 |
if (!m_bHasMouse) |
| 152 |
{ |
| 153 |
// Only process mouse if the position has changed |
| 154 |
if (m_initialMouseX == -1 && m_initialMouseY == -1) |
| 155 |
{ |
| 156 |
m_initialMouseX = x; |
| 157 |
m_initialMouseY = y; |
| 158 |
} |
| 159 |
|
| 160 |
if (m_initialMouseX != x || m_initialMouseY != y) |
| 161 |
{ |
| 162 |
m_bHasMouse = true; |
| 163 |
ProcessMouse(); |
| 164 |
NotifyObservers(ObservableMessageAgentControllersChanged); |
| 165 |
} |
| 166 |
} |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
bool CAgentInput::OnButtonPress(MOUSE::BUTTON_ID button) |
| 171 |
{ |
| 172 |
if (!m_bHasMouse) |
| 173 |
{ |
| 174 |
m_bHasMouse = true; |
| 175 |
ProcessMouse(); |
| 176 |
NotifyObservers(ObservableMessageAgentControllersChanged); |
| 177 |
} |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
std::vector<std::shared_ptr<const CAgentController>> CAgentInput::GetControllers() const |
| 182 |
{ |
| 183 |
std::lock_guard<std::mutex> lock(m_controllerMutex); |
| 184 |
|
| 185 |
std::vector<std::shared_ptr<const CAgentController>> controllers{m_controllers.size()}; |
| 186 |
std::copy(m_controllers.begin(), m_controllers.end(), controllers.begin()); |
| 187 |
|
| 188 |
return controllers; |
| 189 |
} |
| 190 |
|
| 191 |
std::string CAgentInput::GetPortAddress(JOYSTICK::IInputProvider* inputProvider) const |
| 192 |
{ |
| 193 |
auto it = m_portMap.find(inputProvider); |
| 194 |
if (it != m_portMap.end()) |
| 195 |
return it->second->GetPortAddress(); |
| 196 |
|
| 197 |
return ""; |
| 198 |
} |
| 199 |
|
| 200 |
std::string CAgentInput::GetKeyboardAddress(KEYBOARD::IKeyboardInputProvider* inputProvider) const |
| 201 |
{ |
| 202 |
auto it = m_keyboardPort.find(inputProvider); |
| 203 |
if (it != m_keyboardPort.end()) |
| 204 |
return it->second; |
| 205 |
|
| 206 |
return ""; |
| 207 |
} |
| 208 |
|
| 209 |
std::string CAgentInput::GetMouseAddress(MOUSE::IMouseInputProvider* inputProvider) const |
| 210 |
{ |
| 211 |
auto it = m_mousePort.find(inputProvider); |
| 212 |
if (it != m_mousePort.end()) |
| 213 |
return it->second; |
| 214 |
|
| 215 |
return ""; |
| 216 |
} |
| 217 |
|
| 218 |
std::vector<std::string> CAgentInput::GetGameInputPorts() const |
| 219 |
{ |
| 220 |
std::vector<std::string> inputPorts; |
| 221 |
|
| 222 |
if (m_gameClient) |
| 223 |
{ |
| 224 |
CControllerTree controllerTree = m_gameClient->Input().GetActiveControllerTree(); |
| 225 |
controllerTree.GetInputPorts(inputPorts); |
| 226 |
} |
| 227 |
|
| 228 |
return inputPorts; |
| 229 |
} |
| 230 |
|
| 231 |
float CAgentInput::GetGamePortActivation(const std::string& portAddress) const |
| 232 |
{ |
| 233 |
float activation = 0.0f; |
| 234 |
|
| 235 |
if (m_gameClient) |
| 236 |
activation = m_gameClient->Input().GetPortActivation(portAddress); |
| 237 |
|
| 238 |
return activation; |
| 239 |
} |
| 240 |
|
| 241 |
float CAgentInput::GetPeripheralActivation(const std::string& peripheralLocation) const |
| 242 |
{ |
| 243 |
std::lock_guard<std::mutex> lock(m_controllerMutex); |
| 244 |
|
| 245 |
for (const std::shared_ptr<CAgentController>& controller : m_controllers) |
| 246 |
{ |
| 247 |
if (controller->GetPeripheralLocation() == peripheralLocation) |
| 248 |
return controller->GetActivation(); |
| 249 |
} |
| 250 |
|
| 251 |
return 0.0f; |
| 252 |
} |
| 253 |
|
| 254 |
void CAgentInput::ProcessJoysticks(PERIPHERALS::EventLockHandlePtr& inputHandlingLock) |
| 255 |
{ |
| 256 |
// Get system joysticks. |
| 257 |
// |
| 258 |
// It's important to hold these shared pointers for the function scope |
| 259 |
// because we call into the input handlers in m_portMap. |
| 260 |
// |
| 261 |
// The input handlers are upcasted from their peripheral object, so the |
| 262 |
// peripheral object must persist through this function. |
| 263 |
// |
| 264 |
PERIPHERALS::PeripheralVector joysticks; |
| 265 |
m_peripheralManager.GetPeripheralsWithFeature(joysticks, PERIPHERALS::FEATURE_JOYSTICK); |
| 266 |
|
| 267 |
// Remove "virtual" Android joysticks |
| 268 |
// |
| 269 |
// The heuristic used to identify these is to check if the device name is all |
| 270 |
// lowercase letters and dashes (and contains at least one dash). The |
| 271 |
// following virtual devices have been observed: |
| 272 |
// |
| 273 |
// shield-ask-remote |
| 274 |
// sunxi-ir-uinput |
| 275 |
// virtual-search |
| 276 |
// |
| 277 |
// Additionally, we specifically allow the following devices: |
| 278 |
// |
| 279 |
// virtual-remote |
| 280 |
// |
| 281 |
joysticks.erase( |
| 282 |
std::remove_if(joysticks.begin(), joysticks.end(), |
| 283 |
[](const PERIPHERALS::PeripheralPtr& joystick) |
| 284 |
{ |
| 285 |
const std::string& joystickName = joystick->DeviceName(); |
| 286 |
|
| 287 |
// Skip joysticks in the allowlist |
| 288 |
static const std::array<std::string, 1> peripheralAllowlist = { |
| 289 |
"virtual-remote", |
| 290 |
}; |
| 291 |
if (std::find_if(peripheralAllowlist.begin(), peripheralAllowlist.end(), |
| 292 |
[&joystickName](const std::string& allowedJoystick) { |
| 293 |
return allowedJoystick == joystickName; |
| 294 |
}) != peripheralAllowlist.end()) |
| 295 |
{ |
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
// Require at least one dash |
| 300 |
if (std::find_if(joystickName.begin(), joystickName.end(), |
| 301 |
[](char c) { return c == '-'; }) == joystickName.end()) |
| 302 |
{ |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
// Require all lowercase letters or dashes |
| 307 |
if (std::find_if(joystickName.begin(), joystickName.end(), |
| 308 |
[](char c) |
| 309 |
{ |
| 310 |
const bool isLowercase = ('a' <= c && c <= 'z'); |
| 311 |
const bool isDash = (c == '-'); |
| 312 |
return !(isLowercase || isDash); |
| 313 |
}) != joystickName.end()) |
| 314 |
{ |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
// Joystick matches the pattern, remove it |
| 319 |
return true; |
| 320 |
}), |
| 321 |
joysticks.end()); |
| 322 |
|
| 323 |
// Update agent controllers |
| 324 |
ProcessAgentControllers(joysticks, inputHandlingLock, true); |
| 325 |
|
| 326 |
if (!m_gameClient) |
| 327 |
return; |
| 328 |
|
| 329 |
// Update expired joysticks |
| 330 |
UpdateExpiredJoysticks(joysticks, inputHandlingLock); |
| 331 |
|
| 332 |
// Perform the port mapping |
| 333 |
PortMap newPortMap = |
| 334 |
MapJoysticks(joysticks, m_gameClient->Input().GetJoystickMap(), m_currentPorts, |
| 335 |
m_currentPeripherals, m_gameClient->Input().GetPlayerLimit()); |
| 336 |
|
| 337 |
// Update connected joysticks |
| 338 |
std::set<PERIPHERALS::PeripheralPtr> disconnectedPeripherals; |
| 339 |
UpdateConnectedJoysticks(joysticks, newPortMap, inputHandlingLock, disconnectedPeripherals); |
| 340 |
|
| 341 |
// Rebuild peripheral map |
| 342 |
PeripheralMap peripheralMap; |
| 343 |
for (const auto& [inputProvider, joystick] : m_portMap) |
| 344 |
peripheralMap[joystick->GetControllerAddress()] = joystick->GetSource(); |
| 345 |
|
| 346 |
// Log peripheral map if there were any changes |
| 347 |
if (peripheralMap != m_peripheralMap || disconnectedPeripherals != m_disconnectedPeripherals) |
| 348 |
{ |
| 349 |
m_peripheralMap = std::move(peripheralMap); |
| 350 |
m_disconnectedPeripherals = std::move(disconnectedPeripherals); |
| 351 |
LogPeripheralMap(m_peripheralMap, m_disconnectedPeripherals); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
void CAgentInput::ProcessKeyboard() |
| 356 |
{ |
| 357 |
PERIPHERALS::PeripheralVector keyboards; |
| 358 |
m_peripheralManager.GetPeripheralsWithFeature(keyboards, PERIPHERALS::FEATURE_KEYBOARD); |
| 359 |
|
| 360 |
if (!keyboards.empty()) |
| 361 |
{ |
| 362 |
// Update agent controllers |
| 363 |
PERIPHERALS::EventLockHandlePtr inputHandlingLock; |
| 364 |
ProcessAgentControllers(keyboards, inputHandlingLock, false); |
| 365 |
|
| 366 |
// Process keyboard input |
| 367 |
if (m_gameClient && m_gameClient->Input().SupportsKeyboard() && |
| 368 |
!m_gameClient->Input().IsKeyboardOpen()) |
| 369 |
{ |
| 370 |
// Get controller in keyboard port |
| 371 |
const CControllerTree controllers = m_gameClient->Input().GetActiveControllerTree(); |
| 372 |
auto it = std::find_if(controllers.GetPorts().begin(), controllers.GetPorts().end(), |
| 373 |
[](const CPortNode& port) |
| 374 |
{ return port.GetPortType() == PORT_TYPE::KEYBOARD; }); |
| 375 |
|
| 376 |
// Open keyboard input |
| 377 |
PERIPHERALS::PeripheralPtr keyboard = std::move(keyboards.at(0)); |
| 378 |
m_gameClient->Input().OpenKeyboard(it->GetActiveController().GetController(), keyboard); |
| 379 |
|
| 380 |
// Save keyboard port |
| 381 |
m_keyboardPort[static_cast<KEYBOARD::IKeyboardInputProvider*>(keyboard.get())] = |
| 382 |
it->GetAddress(); |
| 383 |
|
| 384 |
SetChanged(true); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
void CAgentInput::ProcessMouse() |
| 390 |
{ |
| 391 |
PERIPHERALS::PeripheralVector mice; |
| 392 |
m_peripheralManager.GetPeripheralsWithFeature(mice, PERIPHERALS::FEATURE_MOUSE); |
| 393 |
|
| 394 |
if (!mice.empty()) |
| 395 |
{ |
| 396 |
// Update agent controllers |
| 397 |
PERIPHERALS::EventLockHandlePtr inputHandlingLock; |
| 398 |
ProcessAgentControllers(mice, inputHandlingLock, false); |
| 399 |
|
| 400 |
// Process mouse input |
| 401 |
if (m_gameClient && m_gameClient->Input().SupportsMouse() && |
| 402 |
!m_gameClient->Input().IsMouseOpen()) |
| 403 |
{ |
| 404 |
// Get controller in mouse port |
| 405 |
CControllerTree controllers = m_gameClient->Input().GetActiveControllerTree(); |
| 406 |
|
| 407 |
auto it = std::find_if(controllers.GetPorts().begin(), controllers.GetPorts().end(), |
| 408 |
[](const CPortNode& port) |
| 409 |
{ return port.GetPortType() == PORT_TYPE::MOUSE; }); |
| 410 |
|
| 411 |
// Open mouse input |
| 412 |
PERIPHERALS::PeripheralPtr mouse = std::move(mice.at(0)); |
| 413 |
m_gameClient->Input().OpenMouse(it->GetActiveController().GetController(), mouse); |
| 414 |
|
| 415 |
// Save mouse port |
| 416 |
m_mousePort[static_cast<MOUSE::IMouseInputProvider*>(mouse.get())] = it->GetAddress(); |
| 417 |
|
| 418 |
SetChanged(true); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
void CAgentInput::ProcessAgentControllers(const PERIPHERALS::PeripheralVector& peripherals, |
| 424 |
PERIPHERALS::EventLockHandlePtr& inputHandlingLock, |
| 425 |
bool updateJoysticks) |
| 426 |
{ |
| 427 |
std::lock_guard<std::mutex> lock(m_controllerMutex); |
| 428 |
|
| 429 |
// Handle new and existing controllers |
| 430 |
for (const auto& peripheral : peripherals) |
| 431 |
{ |
| 432 |
// Skip peripherals that have never been active |
| 433 |
if (!peripheral->LastActive().IsValid()) |
| 434 |
continue; |
| 435 |
|
| 436 |
// Check if controller already exists |
| 437 |
auto it = std::find_if(m_controllers.begin(), m_controllers.end(), |
| 438 |
[&peripheral](const std::shared_ptr<CAgentController>& controller) { |
| 439 |
return controller->GetPeripheralLocation() == peripheral->Location(); |
| 440 |
}); |
| 441 |
|
| 442 |
if (it == m_controllers.end()) |
| 443 |
{ |
| 444 |
// Handle new controller |
| 445 |
std::shared_ptr<CAgentController> agentController = |
| 446 |
std::make_shared<CAgentController>(peripheral); |
| 447 |
agentController->Initialize(); |
| 448 |
m_controllers.emplace_back(std::move(agentController)); |
| 449 |
|
| 450 |
SetChanged(true); |
| 451 |
} |
| 452 |
else |
| 453 |
{ |
| 454 |
// Check if appearance has changed |
| 455 |
CAgentController& agentController = **it; |
| 456 |
|
| 457 |
ControllerPtr oldController = agentController.GetController(); |
| 458 |
ControllerPtr newController = peripheral->ControllerProfile(); |
| 459 |
|
| 460 |
std::string oldControllerId = oldController ? oldController->ID() : ""; |
| 461 |
std::string newControllerId = newController ? newController->ID() : ""; |
| 462 |
|
| 463 |
if (oldControllerId != newControllerId) |
| 464 |
{ |
| 465 |
if (!inputHandlingLock) |
| 466 |
inputHandlingLock = m_peripheralManager.RegisterEventLock(); |
| 467 |
|
| 468 |
// Reinitialize agent's controller |
| 469 |
agentController.Deinitialize(); |
| 470 |
agentController.Initialize(); |
| 471 |
|
| 472 |
SetChanged(true); |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
// If we're processing joysticks, remove expired joysticks |
| 478 |
if (updateJoysticks) |
| 479 |
{ |
| 480 |
std::vector<std::string> expiredJoysticks; |
| 481 |
for (const auto& agentController : m_controllers) |
| 482 |
{ |
| 483 |
if (agentController->GetPeripheral()->Type() != PERIPHERALS::PERIPHERAL_JOYSTICK) |
| 484 |
continue; |
| 485 |
|
| 486 |
auto it = |
| 487 |
std::find_if(peripherals.begin(), peripherals.end(), |
| 488 |
[&agentController](const PERIPHERALS::PeripheralPtr& peripheral) { |
| 489 |
return agentController->GetPeripheralLocation() == peripheral->Location(); |
| 490 |
}); |
| 491 |
|
| 492 |
if (it == peripherals.end()) |
| 493 |
expiredJoysticks.emplace_back(agentController->GetPeripheralLocation()); |
| 494 |
} |
| 495 |
for (const std::string& expiredJoystick : expiredJoysticks) |
| 496 |
{ |
| 497 |
auto it = std::find_if(m_controllers.begin(), m_controllers.end(), |
| 498 |
[&expiredJoystick](const std::shared_ptr<CAgentController>& controller) |
| 499 |
{ return controller->GetPeripheralLocation() == expiredJoystick; }); |
| 500 |
if (it != m_controllers.end()) |
| 501 |
{ |
| 502 |
if (!inputHandlingLock) |
| 503 |
inputHandlingLock = m_peripheralManager.RegisterEventLock(); |
| 504 |
|
| 505 |
// Deinitialize agent |
| 506 |
(*it)->Deinitialize(); |
| 507 |
|
| 508 |
// Remove from list |
| 509 |
m_controllers.erase(it); |
| 510 |
|
| 511 |
SetChanged(true); |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
// Sort controllers in the order: |
| 517 |
// |
| 518 |
// - Keyboard, if game client accepts keyboard input |
| 519 |
// - Mouse, if game client accepts mouse input |
| 520 |
// - Joysticks, in order of last button press |
| 521 |
// - Keyboard, if game client doesn't accept keyboard input |
| 522 |
// - Mouse, if game client doesn't accept mouse input |
| 523 |
// |
| 524 |
std::sort(m_controllers.begin(), m_controllers.end(), |
| 525 |
[this](const std::shared_ptr<CAgentController>& lhs, |
| 526 |
const std::shared_ptr<CAgentController>& rhs) |
| 527 |
{ |
| 528 |
const PERIPHERALS::PeripheralPtr& lhsPeripheral = lhs->GetPeripheral(); |
| 529 |
const PERIPHERALS::PeripheralPtr& rhsPeripheral = rhs->GetPeripheral(); |
| 530 |
|
| 531 |
if (m_gameClient && m_gameClient->Input().SupportsKeyboard()) |
| 532 |
{ |
| 533 |
if (lhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_KEYBOARD && |
| 534 |
rhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_KEYBOARD) |
| 535 |
return true; |
| 536 |
if (lhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_KEYBOARD && |
| 537 |
rhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_KEYBOARD) |
| 538 |
return false; |
| 539 |
} |
| 540 |
|
| 541 |
if (m_gameClient && m_gameClient->Input().SupportsMouse()) |
| 542 |
{ |
| 543 |
if (lhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_MOUSE && |
| 544 |
rhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_MOUSE) |
| 545 |
return true; |
| 546 |
if (lhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_MOUSE && |
| 547 |
rhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_MOUSE) |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
if (lhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_JOYSTICK && |
| 552 |
rhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_JOYSTICK) |
| 553 |
{ |
| 554 |
if (lhsPeripheral->LastActive().IsValid() && !rhsPeripheral->LastActive().IsValid()) |
| 555 |
return true; |
| 556 |
if (!lhsPeripheral->LastActive().IsValid() && rhsPeripheral->LastActive().IsValid()) |
| 557 |
return false; |
| 558 |
|
| 559 |
return lhsPeripheral->LastActive() > rhsPeripheral->LastActive(); |
| 560 |
} |
| 561 |
|
| 562 |
if (lhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_JOYSTICK && |
| 563 |
rhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_JOYSTICK) |
| 564 |
return true; |
| 565 |
if (lhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_JOYSTICK && |
| 566 |
rhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_JOYSTICK) |
| 567 |
return false; |
| 568 |
|
| 569 |
if (lhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_KEYBOARD && |
| 570 |
rhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_KEYBOARD) |
| 571 |
return true; |
| 572 |
if (lhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_KEYBOARD && |
| 573 |
rhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_KEYBOARD) |
| 574 |
return false; |
| 575 |
|
| 576 |
return lhsPeripheral->Type() == PERIPHERALS::PERIPHERAL_MOUSE && |
| 577 |
rhsPeripheral->Type() != PERIPHERALS::PERIPHERAL_MOUSE; |
| 578 |
}); |
| 579 |
} |
| 580 |
|
| 581 |
void CAgentInput::UpdateExpiredJoysticks(const PERIPHERALS::PeripheralVector& joysticks, |
| 582 |
PERIPHERALS::EventLockHandlePtr& inputHandlingLock) |
| 583 |
{ |
| 584 |
// Make a copy - expired joysticks are removed from m_portMap |
| 585 |
PortMap portMapCopy = m_portMap; |
| 586 |
|
| 587 |
for (const auto& [inputProvider, joystick] : portMapCopy) |
| 588 |
{ |
| 589 |
// Structured binding cannot be captured, so make a copy |
| 590 |
JOYSTICK::IInputProvider* const inputProviderCopy = inputProvider; |
| 591 |
|
| 592 |
// Search peripheral vector for input provider |
| 593 |
auto it2 = std::find_if(joysticks.begin(), joysticks.end(), |
| 594 |
[inputProviderCopy](const PERIPHERALS::PeripheralPtr& joystick) |
| 595 |
{ |
| 596 |
// Upcast peripheral to input interface |
| 597 |
JOYSTICK::IInputProvider* peripheralInput = joystick.get(); |
| 598 |
|
| 599 |
// Compare |
| 600 |
return inputProviderCopy == peripheralInput; |
| 601 |
}); |
| 602 |
|
| 603 |
// If peripheral wasn't found, then it was disconnected |
| 604 |
const bool bDisconnected = (it2 == joysticks.end()); |
| 605 |
|
| 606 |
// Erase joystick from port map if its peripheral becomes disconnected |
| 607 |
if (bDisconnected) |
| 608 |
{ |
| 609 |
// Must use nullptr because peripheral has likely fallen out of scope, |
| 610 |
// destroying the object |
| 611 |
joystick->UnregisterInput(nullptr); |
| 612 |
|
| 613 |
if (!inputHandlingLock) |
| 614 |
inputHandlingLock = m_peripheralManager.RegisterEventLock(); |
| 615 |
m_portMap.erase(inputProvider); |
| 616 |
|
| 617 |
SetChanged(true); |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
void CAgentInput::UpdateConnectedJoysticks( |
| 623 |
const PERIPHERALS::PeripheralVector& joysticks, |
| 624 |
const PortMap& newPortMap, |
| 625 |
PERIPHERALS::EventLockHandlePtr& inputHandlingLock, |
| 626 |
std::set<PERIPHERALS::PeripheralPtr>& disconnectedPeripherals) |
| 627 |
{ |
| 628 |
for (auto& peripheralJoystick : joysticks) |
| 629 |
{ |
| 630 |
// Upcast peripheral to input interface |
| 631 |
JOYSTICK::IInputProvider* inputProvider = peripheralJoystick.get(); |
| 632 |
|
| 633 |
// Get connection states |
| 634 |
auto itConnectedPort = newPortMap.find(inputProvider); |
| 635 |
auto itDisconnectedPort = m_portMap.find(inputProvider); |
| 636 |
|
| 637 |
// Get possibly connected joystick |
| 638 |
std::shared_ptr<CGameClientJoystick> newJoystick; |
| 639 |
if (itConnectedPort != newPortMap.end()) |
| 640 |
newJoystick = itConnectedPort->second; |
| 641 |
|
| 642 |
// Get possibly disconnected joystick |
| 643 |
std::shared_ptr<CGameClientJoystick> oldJoystick; |
| 644 |
if (itDisconnectedPort != m_portMap.end()) |
| 645 |
oldJoystick = itDisconnectedPort->second; |
| 646 |
|
| 647 |
// Check for a change in joysticks |
| 648 |
if (oldJoystick != newJoystick) |
| 649 |
{ |
| 650 |
// Unregister old input handler |
| 651 |
if (oldJoystick != nullptr) |
| 652 |
{ |
| 653 |
oldJoystick->UnregisterInput(inputProvider); |
| 654 |
|
| 655 |
if (!inputHandlingLock) |
| 656 |
inputHandlingLock = m_peripheralManager.RegisterEventLock(); |
| 657 |
m_portMap.erase(itDisconnectedPort); |
| 658 |
|
| 659 |
SetChanged(true); |
| 660 |
} |
| 661 |
|
| 662 |
// Register new handler |
| 663 |
if (newJoystick != nullptr) |
| 664 |
{ |
| 665 |
newJoystick->RegisterInput(inputProvider); |
| 666 |
|
| 667 |
m_portMap[inputProvider] = std::move(newJoystick); |
| 668 |
|
| 669 |
SetChanged(true); |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
// Record disconnected peripherals |
| 675 |
for (const auto& peripheral : joysticks) |
| 676 |
{ |
| 677 |
// Upcast peripheral to input interface |
| 678 |
JOYSTICK::IInputProvider* inputProvider = peripheral.get(); |
| 679 |
|
| 680 |
// Check if peripheral is disconnected |
| 681 |
if (m_portMap.find(inputProvider) == m_portMap.end()) |
| 682 |
disconnectedPeripherals.emplace(peripheral); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
CAgentInput::PortMap CAgentInput::MapJoysticks( |
| 687 |
const PERIPHERALS::PeripheralVector& peripheralJoysticks, |
| 688 |
const JoystickMap& gameClientjoysticks, |
| 689 |
CurrentPortMap& currentPorts, |
| 690 |
CurrentPeripheralMap& currentPeripherals, |
| 691 |
int playerLimit) |
| 692 |
{ |
| 693 |
PortMap result; |
| 694 |
|
| 695 |
// First, create a map of the current ports to attempt to preserve |
| 696 |
// player numbers |
| 697 |
for (const auto& [portAddress, joystick] : gameClientjoysticks) |
| 698 |
{ |
| 699 |
std::string sourceLocation = joystick->GetSourceLocation(); |
| 700 |
if (!sourceLocation.empty()) |
| 701 |
currentPorts[portAddress] = std::move(sourceLocation); |
| 702 |
} |
| 703 |
|
| 704 |
// Allow reverse lookups by peripheral location |
| 705 |
for (const auto& [portAddress, sourceLocation] : currentPorts) |
| 706 |
currentPeripherals[sourceLocation] = portAddress; |
| 707 |
|
| 708 |
// Next, create a list of joystick peripherals sorted by order of last |
| 709 |
// button press. Joysticks without a current port are assigned in this |
| 710 |
// order. |
| 711 |
PERIPHERALS::PeripheralVector availableJoysticks = peripheralJoysticks; |
| 712 |
std::sort(availableJoysticks.begin(), availableJoysticks.end(), |
| 713 |
[](const PERIPHERALS::PeripheralPtr& lhs, const PERIPHERALS::PeripheralPtr& rhs) |
| 714 |
{ |
| 715 |
if (lhs->LastActive().IsValid() && !rhs->LastActive().IsValid()) |
| 716 |
return true; |
| 717 |
if (!lhs->LastActive().IsValid() && rhs->LastActive().IsValid()) |
| 718 |
return false; |
| 719 |
|
| 720 |
return lhs->LastActive() > rhs->LastActive(); |
| 721 |
}); |
| 722 |
|
| 723 |
// Loop through the active ports and assign joysticks |
| 724 |
unsigned int iJoystick = 0; |
| 725 |
for (const auto& [portAddress, gameClientJoystick] : gameClientjoysticks) |
| 726 |
{ |
| 727 |
const unsigned int joystickCount = ++iJoystick; |
| 728 |
|
| 729 |
// Check if we're out of joystick peripherals or over the topology limit |
| 730 |
if (availableJoysticks.empty() || |
| 731 |
(playerLimit >= 0 && static_cast<int>(joystickCount) > playerLimit)) |
| 732 |
{ |
| 733 |
gameClientJoystick->ClearSource(); |
| 734 |
continue; |
| 735 |
} |
| 736 |
|
| 737 |
PERIPHERALS::PeripheralVector::iterator itJoystick = availableJoysticks.end(); |
| 738 |
|
| 739 |
// Attempt to preserve player numbers |
| 740 |
auto itCurrentPort = currentPorts.find(portAddress); |
| 741 |
if (itCurrentPort != currentPorts.end()) |
| 742 |
{ |
| 743 |
const PeripheralLocation& currentPeripheral = itCurrentPort->second; |
| 744 |
|
| 745 |
// Find peripheral with matching source location |
| 746 |
itJoystick = std::find_if(availableJoysticks.begin(), availableJoysticks.end(), |
| 747 |
[¤tPeripheral](const PERIPHERALS::PeripheralPtr& joystick) |
| 748 |
{ return joystick->Location() == currentPeripheral; }); |
| 749 |
} |
| 750 |
|
| 751 |
if (itJoystick == availableJoysticks.end()) |
| 752 |
{ |
| 753 |
// Get the next most recently active joystick that doesn't have a current port |
| 754 |
itJoystick = std::find_if( |
| 755 |
availableJoysticks.begin(), availableJoysticks.end(), |
| 756 |
[¤tPeripherals, &gameClientjoysticks](const PERIPHERALS::PeripheralPtr& joystick) |
| 757 |
{ |
| 758 |
const PeripheralLocation& joystickLocation = joystick->Location(); |
| 759 |
|
| 760 |
// If joystick doesn't have a current port, use it |
| 761 |
auto itPeripheral = currentPeripherals.find(joystickLocation); |
| 762 |
if (itPeripheral == currentPeripherals.end()) |
| 763 |
return true; |
| 764 |
|
| 765 |
// Get the address of the last port this joystick was connected to |
| 766 |
const PortAddress& portAddress = itPeripheral->second; |
| 767 |
|
| 768 |
// If port is disconnected, use this joystick |
| 769 |
if (gameClientjoysticks.find(portAddress) == gameClientjoysticks.end()) |
| 770 |
return true; |
| 771 |
|
| 772 |
return false; |
| 773 |
}); |
| 774 |
} |
| 775 |
|
| 776 |
// If found, assign the port and remove from the lists |
| 777 |
if (itJoystick != availableJoysticks.end()) |
| 778 |
{ |
| 779 |
// Dereference iterator |
| 780 |
PERIPHERALS::PeripheralPtr peripheralJoystick = *itJoystick; |
| 781 |
|
| 782 |
// Map joystick |
| 783 |
MapJoystick(std::move(peripheralJoystick), gameClientJoystick, result); |
| 784 |
|
| 785 |
// Remove from availableJoysticks list |
| 786 |
availableJoysticks.erase(itJoystick); |
| 787 |
} |
| 788 |
else |
| 789 |
{ |
| 790 |
// No joystick found, clear the port |
| 791 |
gameClientJoystick->ClearSource(); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
return result; |
| 796 |
} |
| 797 |
|
| 798 |
void CAgentInput::MapJoystick(PERIPHERALS::PeripheralPtr peripheralJoystick, |
| 799 |
std::shared_ptr<CGameClientJoystick> gameClientJoystick, |
| 800 |
PortMap& result) |
| 801 |
{ |
| 802 |
// Upcast peripheral joystick to input provider |
| 803 |
JOYSTICK::IInputProvider* inputProvider = peripheralJoystick.get(); |
| 804 |
|
| 805 |
// Update game joystick's source peripheral |
| 806 |
gameClientJoystick->SetSource(std::move(peripheralJoystick)); |
| 807 |
|
| 808 |
// Map input provider to input handler |
| 809 |
result[inputProvider] = std::move(gameClientJoystick); |
| 810 |
} |
| 811 |
|
| 812 |
void CAgentInput::LogPeripheralMap( |
| 813 |
const PeripheralMap& peripheralMap, |
| 814 |
const std::set<PERIPHERALS::PeripheralPtr>& disconnectedPeripherals) |
| 815 |
{ |
| 816 |
CLog::Log(LOGDEBUG, "===== Peripheral Map ====="); |
| 817 |
|
| 818 |
unsigned int line = 0; |
| 819 |
|
| 820 |
if (!peripheralMap.empty()) |
| 821 |
{ |
| 822 |
for (const auto& [controllerAddress, peripheral] : peripheralMap) |
| 823 |
{ |
| 824 |
if (line != 0) |
| 825 |
CLog::Log(LOGDEBUG, ""); |
| 826 |
CLog::Log(LOGDEBUG, "{}:", controllerAddress); |
| 827 |
CLog::Log(LOGDEBUG, " {} [{}]", peripheral->Location(), peripheral->DeviceName()); |
| 828 |
|
| 829 |
++line; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
if (!disconnectedPeripherals.empty()) |
| 834 |
{ |
| 835 |
if (line != 0) |
| 836 |
CLog::Log(LOGDEBUG, ""); |
| 837 |
CLog::Log(LOGDEBUG, "Disconnected:"); |
| 838 |
|
| 839 |
// Sort by peripheral location |
| 840 |
std::map<std::string, std::string> disconnectedPeripheralMap; |
| 841 |
for (const auto& peripheral : disconnectedPeripherals) |
| 842 |
disconnectedPeripheralMap[peripheral->Location()] = peripheral->DeviceName(); |
| 843 |
|
| 844 |
// Log location and device name for disconnected peripherals |
| 845 |
for (const auto& [location, deviceName] : disconnectedPeripheralMap) |
| 846 |
CLog::Log(LOGDEBUG, " {} [{}]", location, deviceName); |
| 847 |
} |
| 848 |
|
| 849 |
CLog::Log(LOGDEBUG, "=========================="); |
| 850 |
} |