| 1 |
/* |
| 2 |
* Copyright (C) 2014-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 "GUIConfigurationWizard.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "games/controllers/Controller.h" |
| 13 |
#include "games/controllers/dialogs/GUIDialogAxisDetection.h" |
| 14 |
#include "games/controllers/guicontrols/GUIFeatureButton.h" |
| 15 |
#include "games/controllers/input/PhysicalFeature.h" |
| 16 |
#include "input/InputManager.h" |
| 17 |
#include "input/actions/ActionIDs.h" |
| 18 |
#include "input/joysticks/JoystickUtils.h" |
| 19 |
#include "input/joysticks/interfaces/IButtonMap.h" |
| 20 |
#include "input/joysticks/interfaces/IButtonMapCallback.h" |
| 21 |
#include "input/keyboard/KeyboardTranslator.h" |
| 22 |
#include "input/keymaps/interfaces/IKeymap.h" |
| 23 |
#include "input/keymaps/keyboard/KeyboardActionMap.h" |
| 24 |
#include "peripherals/Peripherals.h" |
| 25 |
#include "threads/SingleLock.h" |
| 26 |
#include "utils/log.h" |
| 27 |
|
| 28 |
#include <mutex> |
| 29 |
|
| 30 |
using namespace KODI; |
| 31 |
using namespace GAME; |
| 32 |
using namespace std::chrono_literals; |
| 33 |
|
| 34 |
namespace |
| 35 |
{ |
| 36 |
|
| 37 |
#define ESC_KEY_CODE 27 |
| 38 |
#define SKIPPING_DETECTION_MS 200 |
| 39 |
|
| 40 |
// Duration to wait for axes to neutralize after mapping is finished |
| 41 |
constexpr auto POST_MAPPING_WAIT_TIME_MS = 5000ms; |
| 42 |
|
| 43 |
} // namespace |
| 44 |
|
| 45 |
CGUIConfigurationWizard::CGUIConfigurationWizard() |
| 46 |
: CThread("GUIConfigurationWizard"), m_actionMap(new KEYMAP::CKeyboardActionMap) |
| 47 |
{ |
| 48 |
InitializeState(); |
| 49 |
} |
| 50 |
|
| 51 |
CGUIConfigurationWizard::~CGUIConfigurationWizard(void) = default; |
| 52 |
|
| 53 |
void CGUIConfigurationWizard::InitializeState(void) |
| 54 |
{ |
| 55 |
m_currentButton = nullptr; |
| 56 |
m_cardinalDirection = INPUT::CARDINAL_DIRECTION::NONE; |
| 57 |
m_wheelDirection = JOYSTICK::WHEEL_DIRECTION::NONE; |
| 58 |
m_throttleDirection = JOYSTICK::THROTTLE_DIRECTION::NONE; |
| 59 |
m_history.clear(); |
| 60 |
m_lateAxisDetected = false; |
| 61 |
m_location.clear(); |
| 62 |
} |
| 63 |
|
| 64 |
void CGUIConfigurationWizard::Run(const std::string& strControllerId, |
| 65 |
const std::vector<IFeatureButton*>& buttons) |
| 66 |
{ |
| 67 |
Abort(); |
| 68 |
|
| 69 |
{ |
| 70 |
std::unique_lock<CCriticalSection> lock(m_stateMutex); |
| 71 |
|
| 72 |
// Set Run() parameters |
| 73 |
m_strControllerId = strControllerId; |
| 74 |
m_buttons = buttons; |
| 75 |
|
| 76 |
// Reset synchronization variables |
| 77 |
m_inputEvent.Reset(); |
| 78 |
m_motionlessEvent.Reset(); |
| 79 |
m_bInMotion.clear(); |
| 80 |
|
| 81 |
// Initialize state variables |
| 82 |
InitializeState(); |
| 83 |
} |
| 84 |
|
| 85 |
Create(); |
| 86 |
} |
| 87 |
|
| 88 |
void CGUIConfigurationWizard::OnUnfocus(IFeatureButton* button) |
| 89 |
{ |
| 90 |
std::unique_lock<CCriticalSection> lock(m_stateMutex); |
| 91 |
|
| 92 |
if (button == m_currentButton) |
| 93 |
Abort(false); |
| 94 |
} |
| 95 |
|
| 96 |
bool CGUIConfigurationWizard::Abort(bool bWait /* = true */) |
| 97 |
{ |
| 98 |
bool bWasRunning = !m_bStop; |
| 99 |
|
| 100 |
StopThread(false); |
| 101 |
|
| 102 |
m_inputEvent.Set(); |
| 103 |
m_motionlessEvent.Set(); |
| 104 |
|
| 105 |
if (bWait) |
| 106 |
StopThread(true); |
| 107 |
|
| 108 |
return bWasRunning; |
| 109 |
} |
| 110 |
|
| 111 |
void CGUIConfigurationWizard::RegisterKey(const CPhysicalFeature& key) |
| 112 |
{ |
| 113 |
if (key.Keycode() != XBMCK_UNKNOWN) |
| 114 |
m_keyMap[KEYBOARD::CKeyboardTranslator::TranslateKeycode(key.Keycode())] = key; |
| 115 |
} |
| 116 |
|
| 117 |
void CGUIConfigurationWizard::UnregisterKeys() |
| 118 |
{ |
| 119 |
m_keyMap.clear(); |
| 120 |
} |
| 121 |
|
| 122 |
void CGUIConfigurationWizard::Process(void) |
| 123 |
{ |
| 124 |
CLog::Log(LOGDEBUG, "Starting configuration wizard"); |
| 125 |
|
| 126 |
InstallHooks(); |
| 127 |
|
| 128 |
bool bLateAxisDetected = false; |
| 129 |
|
| 130 |
{ |
| 131 |
std::unique_lock<CCriticalSection> lock(m_stateMutex); |
| 132 |
for (IFeatureButton* button : m_buttons) |
| 133 |
{ |
| 134 |
// Allow other threads to access the button we're using |
| 135 |
m_currentButton = button; |
| 136 |
|
| 137 |
while (!button->IsFinished()) |
| 138 |
{ |
| 139 |
// Allow other threads to access which direction the prompt is on |
| 140 |
m_cardinalDirection = button->GetCardinalDirection(); |
| 141 |
m_wheelDirection = button->GetWheelDirection(); |
| 142 |
m_throttleDirection = button->GetThrottleDirection(); |
| 143 |
|
| 144 |
// Wait for input |
| 145 |
{ |
| 146 |
using namespace JOYSTICK; |
| 147 |
|
| 148 |
CSingleExit exit(m_stateMutex); |
| 149 |
|
| 150 |
if (button->Feature().Type() == FEATURE_TYPE::UNKNOWN) |
| 151 |
CLog::Log(LOGDEBUG, "{}: Waiting for input", m_strControllerId); |
| 152 |
else |
| 153 |
CLog::Log(LOGDEBUG, "{}: Waiting for input for feature \"{}\"", m_strControllerId, |
| 154 |
button->Feature().Name()); |
| 155 |
|
| 156 |
if (!button->PromptForInput(m_inputEvent)) |
| 157 |
Abort(false); |
| 158 |
} |
| 159 |
|
| 160 |
if (m_bStop) |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
button->Reset(); |
| 165 |
|
| 166 |
if (m_bStop) |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
bLateAxisDetected = m_lateAxisDetected; |
| 171 |
|
| 172 |
// Finished mapping |
| 173 |
InitializeState(); |
| 174 |
} |
| 175 |
|
| 176 |
for (auto callback : ButtonMapCallbacks()) |
| 177 |
callback.second->SaveButtonMap(); |
| 178 |
|
| 179 |
if (bLateAxisDetected) |
| 180 |
{ |
| 181 |
CGUIDialogAxisDetection dialog; |
| 182 |
dialog.Show(); |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
// Wait for motion to stop to avoid sending analog actions for the button |
| 187 |
// that is pressed immediately after button mapping finishes. |
| 188 |
bool bInMotion; |
| 189 |
|
| 190 |
{ |
| 191 |
std::unique_lock<CCriticalSection> lock(m_motionMutex); |
| 192 |
bInMotion = !m_bInMotion.empty(); |
| 193 |
} |
| 194 |
|
| 195 |
if (bInMotion) |
| 196 |
{ |
| 197 |
CLog::Log(LOGDEBUG, "Configuration wizard: waiting {}ms for axes to neutralize", |
| 198 |
POST_MAPPING_WAIT_TIME_MS.count()); |
| 199 |
m_motionlessEvent.Wait(POST_MAPPING_WAIT_TIME_MS); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
RemoveHooks(); |
| 204 |
|
| 205 |
CLog::Log(LOGDEBUG, "Configuration wizard ended"); |
| 206 |
} |
| 207 |
|
| 208 |
bool CGUIConfigurationWizard::MapPrimitive(JOYSTICK::IButtonMap* buttonMap, |
| 209 |
KEYMAP::IKeymap* keymap, |
| 210 |
const JOYSTICK::CDriverPrimitive& primitive) |
| 211 |
{ |
| 212 |
using namespace INPUT; |
| 213 |
using namespace JOYSTICK; |
| 214 |
|
| 215 |
bool bHandled = false; |
| 216 |
|
| 217 |
// Abort if another controller cancels the prompt |
| 218 |
if (IsMapping() && !IsMapping(buttonMap->Location())) |
| 219 |
{ |
| 220 |
//! @todo This only succeeds for game.controller.default; no actions are |
| 221 |
// currently defined for other controllers |
| 222 |
if (keymap) |
| 223 |
{ |
| 224 |
std::string feature; |
| 225 |
if (buttonMap->GetFeature(primitive, feature)) |
| 226 |
{ |
| 227 |
const auto& actions = keymap->GetActions(CJoystickUtils::MakeKeyName(feature)).actions; |
| 228 |
if (!actions.empty()) |
| 229 |
{ |
| 230 |
//! @todo Handle multiple actions mapped to the same key |
| 231 |
OnAction(actions.begin()->actionId); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// Discard input |
| 237 |
bHandled = true; |
| 238 |
} |
| 239 |
else if (m_history.find(primitive) != m_history.end()) |
| 240 |
{ |
| 241 |
// Primitive has already been mapped this round, ignore it |
| 242 |
bHandled = true; |
| 243 |
} |
| 244 |
else if (buttonMap->IsIgnored(primitive)) |
| 245 |
{ |
| 246 |
bHandled = true; |
| 247 |
} |
| 248 |
else |
| 249 |
{ |
| 250 |
// Get the current state of the thread |
| 251 |
IFeatureButton* currentButton; |
| 252 |
CARDINAL_DIRECTION cardinalDirection; |
| 253 |
WHEEL_DIRECTION wheelDirection; |
| 254 |
THROTTLE_DIRECTION throttleDirection; |
| 255 |
{ |
| 256 |
std::unique_lock<CCriticalSection> lock(m_stateMutex); |
| 257 |
currentButton = m_currentButton; |
| 258 |
cardinalDirection = m_cardinalDirection; |
| 259 |
wheelDirection = m_wheelDirection; |
| 260 |
throttleDirection = m_throttleDirection; |
| 261 |
} |
| 262 |
|
| 263 |
if (currentButton) |
| 264 |
{ |
| 265 |
// Check if we were expecting a keyboard key |
| 266 |
if (currentButton->NeedsKey()) |
| 267 |
{ |
| 268 |
if (primitive.Type() == PRIMITIVE_TYPE::KEY) |
| 269 |
{ |
| 270 |
auto it = |
| 271 |
m_keyMap.find(KEYBOARD::CKeyboardTranslator::TranslateKeycode(primitive.Keycode())); |
| 272 |
if (it != m_keyMap.end()) |
| 273 |
{ |
| 274 |
const CPhysicalFeature& key = it->second; |
| 275 |
currentButton->SetKey(key); |
| 276 |
m_inputEvent.Set(); |
| 277 |
} |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
//! @todo Check if primitive is a cancel or motion action |
| 282 |
} |
| 283 |
bHandled = true; |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
const CPhysicalFeature& feature = currentButton->Feature(); |
| 288 |
|
| 289 |
if (primitive.Type() == PRIMITIVE_TYPE::RELATIVE_POINTER && |
| 290 |
feature.Type() != FEATURE_TYPE::RELPOINTER) |
| 291 |
{ |
| 292 |
// Don't allow relative pointers to map to other features |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
CLog::Log(LOGDEBUG, "{}: mapping feature \"{}\" for device {} to \"{}\"", |
| 297 |
m_strControllerId, feature.Name(), buttonMap->Location(), primitive.ToString()); |
| 298 |
|
| 299 |
switch (feature.Type()) |
| 300 |
{ |
| 301 |
case FEATURE_TYPE::SCALAR: |
| 302 |
{ |
| 303 |
buttonMap->AddScalar(feature.Name(), primitive); |
| 304 |
bHandled = true; |
| 305 |
break; |
| 306 |
} |
| 307 |
case FEATURE_TYPE::ANALOG_STICK: |
| 308 |
{ |
| 309 |
buttonMap->AddAnalogStick(feature.Name(), cardinalDirection, primitive); |
| 310 |
bHandled = true; |
| 311 |
break; |
| 312 |
} |
| 313 |
case FEATURE_TYPE::RELPOINTER: |
| 314 |
{ |
| 315 |
buttonMap->AddRelativePointer(feature.Name(), cardinalDirection, primitive); |
| 316 |
bHandled = true; |
| 317 |
break; |
| 318 |
} |
| 319 |
case FEATURE_TYPE::WHEEL: |
| 320 |
{ |
| 321 |
buttonMap->AddWheel(feature.Name(), wheelDirection, primitive); |
| 322 |
bHandled = true; |
| 323 |
break; |
| 324 |
} |
| 325 |
case FEATURE_TYPE::THROTTLE: |
| 326 |
{ |
| 327 |
buttonMap->AddThrottle(feature.Name(), throttleDirection, primitive); |
| 328 |
bHandled = true; |
| 329 |
break; |
| 330 |
} |
| 331 |
case FEATURE_TYPE::KEY: |
| 332 |
{ |
| 333 |
buttonMap->AddKey(feature.Name(), primitive); |
| 334 |
bHandled = true; |
| 335 |
break; |
| 336 |
} |
| 337 |
default: |
| 338 |
break; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
if (bHandled) |
| 343 |
{ |
| 344 |
m_history.insert(primitive); |
| 345 |
|
| 346 |
// Don't record motion for relative pointers |
| 347 |
if (primitive.Type() != PRIMITIVE_TYPE::RELATIVE_POINTER) |
| 348 |
OnMotion(buttonMap); |
| 349 |
|
| 350 |
m_inputEvent.Set(); |
| 351 |
|
| 352 |
if (m_location.empty()) |
| 353 |
{ |
| 354 |
m_location = buttonMap->Location(); |
| 355 |
m_bIsKeyboard = (primitive.Type() == PRIMITIVE_TYPE::KEY); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
return bHandled; |
| 363 |
} |
| 364 |
|
| 365 |
void CGUIConfigurationWizard::OnEventFrame(const JOYSTICK::IButtonMap* buttonMap, bool bMotion) |
| 366 |
{ |
| 367 |
std::unique_lock<CCriticalSection> lock(m_motionMutex); |
| 368 |
|
| 369 |
if (m_bInMotion.find(buttonMap) != m_bInMotion.end() && !bMotion) |
| 370 |
OnMotionless(buttonMap); |
| 371 |
} |
| 372 |
|
| 373 |
void CGUIConfigurationWizard::OnLateAxis(const JOYSTICK::IButtonMap* buttonMap, |
| 374 |
unsigned int axisIndex) |
| 375 |
{ |
| 376 |
std::unique_lock<CCriticalSection> lock(m_stateMutex); |
| 377 |
|
| 378 |
m_lateAxisDetected = true; |
| 379 |
Abort(false); |
| 380 |
} |
| 381 |
|
| 382 |
void CGUIConfigurationWizard::OnMotion(const JOYSTICK::IButtonMap* buttonMap) |
| 383 |
{ |
| 384 |
std::unique_lock<CCriticalSection> lock(m_motionMutex); |
| 385 |
|
| 386 |
m_motionlessEvent.Reset(); |
| 387 |
m_bInMotion.insert(buttonMap); |
| 388 |
} |
| 389 |
|
| 390 |
void CGUIConfigurationWizard::OnMotionless(const JOYSTICK::IButtonMap* buttonMap) |
| 391 |
{ |
| 392 |
m_bInMotion.erase(buttonMap); |
| 393 |
if (m_bInMotion.empty()) |
| 394 |
m_motionlessEvent.Set(); |
| 395 |
} |
| 396 |
|
| 397 |
bool CGUIConfigurationWizard::OnKeyPress(const CKey& key) |
| 398 |
{ |
| 399 |
bool bHandled = false; |
| 400 |
|
| 401 |
if (!m_bStop) |
| 402 |
{ |
| 403 |
// Only allow key to abort the prompt if we know for sure that we're mapping |
| 404 |
// a controller |
| 405 |
const bool bIsMappingController = (IsMapping() && !m_bIsKeyboard); |
| 406 |
|
| 407 |
if (bIsMappingController) |
| 408 |
{ |
| 409 |
bHandled = OnAction(m_actionMap->GetActionID(key)); |
| 410 |
} |
| 411 |
else |
| 412 |
{ |
| 413 |
// Allow key press to fall through to the button mapper |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
return bHandled; |
| 418 |
} |
| 419 |
|
| 420 |
bool CGUIConfigurationWizard::OnAction(unsigned int actionId) |
| 421 |
{ |
| 422 |
bool bHandled = false; |
| 423 |
|
| 424 |
switch (actionId) |
| 425 |
{ |
| 426 |
case ACTION_MOVE_LEFT: |
| 427 |
case ACTION_MOVE_RIGHT: |
| 428 |
case ACTION_MOVE_UP: |
| 429 |
case ACTION_MOVE_DOWN: |
| 430 |
case ACTION_PAGE_UP: |
| 431 |
case ACTION_PAGE_DOWN: |
| 432 |
// Abort and allow motion |
| 433 |
Abort(false); |
| 434 |
bHandled = false; |
| 435 |
break; |
| 436 |
|
| 437 |
case ACTION_PARENT_DIR: |
| 438 |
case ACTION_PREVIOUS_MENU: |
| 439 |
case ACTION_STOP: |
| 440 |
case ACTION_NAV_BACK: |
| 441 |
// Abort and prevent action |
| 442 |
Abort(false); |
| 443 |
bHandled = true; |
| 444 |
break; |
| 445 |
|
| 446 |
default: |
| 447 |
// Absorb keypress |
| 448 |
bHandled = true; |
| 449 |
break; |
| 450 |
} |
| 451 |
|
| 452 |
return bHandled; |
| 453 |
} |
| 454 |
|
| 455 |
bool CGUIConfigurationWizard::IsMapping() const |
| 456 |
{ |
| 457 |
return !m_location.empty(); |
| 458 |
} |
| 459 |
|
| 460 |
bool CGUIConfigurationWizard::IsMapping(const std::string& location) const |
| 461 |
{ |
| 462 |
return m_location == location; |
| 463 |
} |
| 464 |
|
| 465 |
void CGUIConfigurationWizard::InstallHooks(void) |
| 466 |
{ |
| 467 |
// Install button mapper with lowest priority |
| 468 |
CServiceBroker::GetPeripherals().RegisterJoystickButtonMapper(this); |
| 469 |
|
| 470 |
// Install hook to reattach button mapper when peripherals change |
| 471 |
CServiceBroker::GetPeripherals().RegisterObserver(this); |
| 472 |
|
| 473 |
// Install hook to cancel the button mapper |
| 474 |
CServiceBroker::GetInputManager().RegisterKeyboardDriverHandler(this); |
| 475 |
} |
| 476 |
|
| 477 |
void CGUIConfigurationWizard::RemoveHooks(void) |
| 478 |
{ |
| 479 |
CServiceBroker::GetInputManager().UnregisterKeyboardDriverHandler(this); |
| 480 |
CServiceBroker::GetPeripherals().UnregisterObserver(this); |
| 481 |
CServiceBroker::GetPeripherals().UnregisterJoystickButtonMapper(this); |
| 482 |
} |
| 483 |
|
| 484 |
void CGUIConfigurationWizard::Notify(const Observable& obs, const ObservableMessage msg) |
| 485 |
{ |
| 486 |
switch (msg) |
| 487 |
{ |
| 488 |
case ObservableMessagePeripheralsChanged: |
| 489 |
{ |
| 490 |
CServiceBroker::GetPeripherals().UnregisterJoystickButtonMapper(this); |
| 491 |
CServiceBroker::GetPeripherals().RegisterJoystickButtonMapper(this); |
| 492 |
break; |
| 493 |
} |
| 494 |
default: |
| 495 |
break; |
| 496 |
} |
| 497 |
} |