| 1 |
/* |
| 2 |
* Copyright (C) 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 "DefaultButtonMap.h" |
| 10 |
|
| 11 |
#include "DefaultKeyboardTranslator.h" |
| 12 |
#include "DefaultMouseTranslator.h" |
| 13 |
#include "games/controllers/Controller.h" |
| 14 |
#include "games/controllers/ControllerIDs.h" |
| 15 |
#include "input/joysticks/DriverPrimitive.h" |
| 16 |
#include "peripherals/devices/Peripheral.h" |
| 17 |
#include "utils/log.h" |
| 18 |
|
| 19 |
using namespace KODI; |
| 20 |
using namespace GAME; |
| 21 |
|
| 22 |
CDefaultButtonMap::CDefaultButtonMap(PERIPHERALS::CPeripheral* device, std::string strControllerId) |
| 23 |
: m_device(device), m_strControllerId(std::move(strControllerId)) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
CDefaultButtonMap::~CDefaultButtonMap() = default; |
| 28 |
|
| 29 |
std::string CDefaultButtonMap::Location() const |
| 30 |
{ |
| 31 |
return m_device->Location(); |
| 32 |
} |
| 33 |
|
| 34 |
bool CDefaultButtonMap::Load() |
| 35 |
{ |
| 36 |
// Verify we're using the appropriate controller |
| 37 |
switch (m_device->Type()) |
| 38 |
{ |
| 39 |
case PERIPHERALS::PERIPHERAL_KEYBOARD: |
| 40 |
{ |
| 41 |
if (m_strControllerId == DEFAULT_KEYBOARD_ID) |
| 42 |
return true; |
| 43 |
break; |
| 44 |
} |
| 45 |
case PERIPHERALS::PERIPHERAL_MOUSE: |
| 46 |
{ |
| 47 |
if (m_strControllerId == DEFAULT_MOUSE_ID) |
| 48 |
return true; |
| 49 |
break; |
| 50 |
} |
| 51 |
default: |
| 52 |
break; |
| 53 |
} |
| 54 |
|
| 55 |
CLog::Log(LOGDEBUG, "Failed to load default button map for \"{}\" with profile {}", |
| 56 |
m_device->Location(), m_strControllerId); |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
std::string CDefaultButtonMap::GetAppearance() const |
| 61 |
{ |
| 62 |
ControllerPtr controller = m_device->ControllerProfile(); |
| 63 |
if (controller) |
| 64 |
return controller->ID(); |
| 65 |
|
| 66 |
return m_strControllerId; |
| 67 |
} |
| 68 |
|
| 69 |
bool CDefaultButtonMap::GetFeature(const JOYSTICK::CDriverPrimitive& primitive, |
| 70 |
JOYSTICK::FeatureName& feature) |
| 71 |
{ |
| 72 |
switch (primitive.Type()) |
| 73 |
{ |
| 74 |
case JOYSTICK::PRIMITIVE_TYPE::KEY: |
| 75 |
{ |
| 76 |
std::string featureName = CDefaultKeyboardTranslator::TranslateKeycode(primitive.Keycode()); |
| 77 |
if (!featureName.empty()) |
| 78 |
{ |
| 79 |
feature = std::move(featureName); |
| 80 |
return true; |
| 81 |
} |
| 82 |
break; |
| 83 |
} |
| 84 |
case JOYSTICK::PRIMITIVE_TYPE::MOUSE_BUTTON: |
| 85 |
{ |
| 86 |
std::string featureName = |
| 87 |
CDefaultMouseTranslator::TranslateMouseButton(primitive.MouseButton()); |
| 88 |
if (!featureName.empty()) |
| 89 |
{ |
| 90 |
feature = std::move(featureName); |
| 91 |
return true; |
| 92 |
} |
| 93 |
break; |
| 94 |
} |
| 95 |
case JOYSTICK::PRIMITIVE_TYPE::RELATIVE_POINTER: |
| 96 |
{ |
| 97 |
std::string featureName = |
| 98 |
CDefaultMouseTranslator::TranslateMousePointer(primitive.PointerDirection()); |
| 99 |
if (!featureName.empty()) |
| 100 |
{ |
| 101 |
feature = std::move(featureName); |
| 102 |
return true; |
| 103 |
} |
| 104 |
break; |
| 105 |
} |
| 106 |
default: |
| 107 |
break; |
| 108 |
} |
| 109 |
|
| 110 |
return false; |
| 111 |
} |