| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 "GUIDialogIgnoreInput.h" |
| 10 |
|
| 11 |
#include "guilib/LocalizeStrings.h" |
| 12 |
#include "input/joysticks/JoystickTranslator.h" |
| 13 |
#include "input/joysticks/interfaces/IButtonMap.h" |
| 14 |
#include "input/joysticks/interfaces/IButtonMapCallback.h" |
| 15 |
#include "utils/StringUtils.h" |
| 16 |
#include "utils/log.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <iterator> |
| 20 |
|
| 21 |
using namespace KODI; |
| 22 |
using namespace GAME; |
| 23 |
|
| 24 |
bool CGUIDialogIgnoreInput::AcceptsPrimitive(JOYSTICK::PRIMITIVE_TYPE type) const |
| 25 |
{ |
| 26 |
switch (type) |
| 27 |
{ |
| 28 |
case JOYSTICK::PRIMITIVE_TYPE::BUTTON: |
| 29 |
case JOYSTICK::PRIMITIVE_TYPE::SEMIAXIS: |
| 30 |
return true; |
| 31 |
default: |
| 32 |
break; |
| 33 |
} |
| 34 |
|
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
std::string CGUIDialogIgnoreInput::GetDialogText() |
| 39 |
{ |
| 40 |
// "Some controllers have buttons and axes that interfere with mapping. Press |
| 41 |
// these now to disable them:[CR]%s" |
| 42 |
const std::string& dialogText = g_localizeStrings.Get(35014); |
| 43 |
|
| 44 |
std::vector<std::string> primitives; |
| 45 |
|
| 46 |
std::transform(m_capturedPrimitives.begin(), m_capturedPrimitives.end(), |
| 47 |
std::back_inserter(primitives), |
| 48 |
[](const JOYSTICK::CDriverPrimitive& primitive) |
| 49 |
{ return JOYSTICK::CJoystickTranslator::GetPrimitiveName(primitive); }); |
| 50 |
|
| 51 |
return StringUtils::Format(dialogText, StringUtils::Join(primitives, " | ")); |
| 52 |
} |
| 53 |
|
| 54 |
std::string CGUIDialogIgnoreInput::GetDialogHeader() |
| 55 |
{ |
| 56 |
|
| 57 |
return g_localizeStrings.Get(35019); // "Ignore input" |
| 58 |
} |
| 59 |
|
| 60 |
bool CGUIDialogIgnoreInput::MapPrimitiveInternal(JOYSTICK::IButtonMap* buttonMap, |
| 61 |
KEYMAP::IKeymap* keymap, |
| 62 |
const JOYSTICK::CDriverPrimitive& primitive) |
| 63 |
{ |
| 64 |
// Check if we have already started capturing primitives for a device |
| 65 |
const bool bHasDevice = !m_location.empty(); |
| 66 |
|
| 67 |
// If a primitive comes from a different device, ignore it |
| 68 |
if (bHasDevice && m_location != buttonMap->Location()) |
| 69 |
{ |
| 70 |
CLog::Log(LOGDEBUG, "{}: ignoring input from device {}", buttonMap->ControllerID(), |
| 71 |
buttonMap->Location()); |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
if (!bHasDevice) |
| 76 |
{ |
| 77 |
CLog::Log(LOGDEBUG, "{}: capturing input for device {}", buttonMap->ControllerID(), |
| 78 |
buttonMap->Location()); |
| 79 |
m_location = buttonMap->Location(); |
| 80 |
} |
| 81 |
|
| 82 |
if (AddPrimitive(primitive)) |
| 83 |
{ |
| 84 |
buttonMap->SetIgnoredPrimitives(m_capturedPrimitives); |
| 85 |
m_captureEvent.Set(); |
| 86 |
} |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
void CGUIDialogIgnoreInput::OnClose(bool bAccepted) |
| 92 |
{ |
| 93 |
for (auto& callback : ButtonMapCallbacks()) |
| 94 |
{ |
| 95 |
if (bAccepted) |
| 96 |
{ |
| 97 |
// See documentation of IButtonMapCallback::ResetIgnoredPrimitives() |
| 98 |
// for why this call is needed |
| 99 |
if (m_location.empty()) |
| 100 |
callback.second->ResetIgnoredPrimitives(); |
| 101 |
|
| 102 |
if (m_location.empty() || m_location == callback.first) |
| 103 |
callback.second->SaveButtonMap(); |
| 104 |
} |
| 105 |
else |
| 106 |
callback.second->RevertButtonMap(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
bool CGUIDialogIgnoreInput::AddPrimitive(const JOYSTICK::CDriverPrimitive& primitive) |
| 111 |
{ |
| 112 |
bool bValid = false; |
| 113 |
|
| 114 |
if (primitive.Type() == JOYSTICK::PRIMITIVE_TYPE::BUTTON || |
| 115 |
primitive.Type() == JOYSTICK::PRIMITIVE_TYPE::SEMIAXIS) |
| 116 |
{ |
| 117 |
auto PrimitiveMatch = [&primitive](const JOYSTICK::CDriverPrimitive& other) |
| 118 |
{ return primitive.Type() == other.Type() && primitive.Index() == other.Index(); }; |
| 119 |
|
| 120 |
bValid = std::find_if(m_capturedPrimitives.begin(), m_capturedPrimitives.end(), |
| 121 |
PrimitiveMatch) == m_capturedPrimitives.end(); |
| 122 |
} |
| 123 |
|
| 124 |
if (bValid) |
| 125 |
{ |
| 126 |
m_capturedPrimitives.emplace_back(primitive); |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
return false; |
| 131 |
} |