| 1 |
/* |
| 2 |
* Copyright (C) 2014-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 "AddonButtonMapping.h" |
| 10 |
|
| 11 |
#include "input/joysticks/generic/ButtonMapping.h" |
| 12 |
#include "input/joysticks/interfaces/IButtonMapper.h" |
| 13 |
#include "peripherals/Peripherals.h" |
| 14 |
#include "peripherals/addons/AddonButtonMap.h" |
| 15 |
#include "utils/log.h" |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
|
| 19 |
using namespace KODI; |
| 20 |
using namespace JOYSTICK; |
| 21 |
using namespace PERIPHERALS; |
| 22 |
|
| 23 |
CAddonButtonMapping::CAddonButtonMapping(CPeripherals& manager, |
| 24 |
CPeripheral* peripheral, |
| 25 |
IButtonMapper* mapper) |
| 26 |
{ |
| 27 |
PeripheralAddonPtr addon = manager.GetAddonWithButtonMap(peripheral); |
| 28 |
|
| 29 |
if (!addon) |
| 30 |
{ |
| 31 |
CLog::Log(LOGDEBUG, "Failed to locate add-on for \"{}\"", peripheral->DeviceName()); |
| 32 |
} |
| 33 |
else |
| 34 |
{ |
| 35 |
const std::string controllerId = mapper->ControllerID(); |
| 36 |
m_buttonMap = std::make_unique<CAddonButtonMap>(peripheral, addon, controllerId, manager); |
| 37 |
if (m_buttonMap->Load()) |
| 38 |
{ |
| 39 |
KEYMAP::IKeymap* keymap = peripheral->GetKeymap(controllerId); |
| 40 |
m_buttonMapping = std::make_unique<CButtonMapping>(mapper, m_buttonMap.get(), keymap); |
| 41 |
|
| 42 |
// Allow the mapper to save our button map |
| 43 |
mapper->SetButtonMapCallback(peripheral->Location(), this); |
| 44 |
} |
| 45 |
else |
| 46 |
m_buttonMap.reset(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
CAddonButtonMapping::~CAddonButtonMapping(void) |
| 51 |
{ |
| 52 |
m_buttonMapping.reset(); |
| 53 |
m_buttonMap.reset(); |
| 54 |
} |
| 55 |
|
| 56 |
bool CAddonButtonMapping::OnButtonMotion(unsigned int buttonIndex, bool bPressed) |
| 57 |
{ |
| 58 |
if (m_buttonMapping) |
| 59 |
return m_buttonMapping->OnButtonMotion(buttonIndex, bPressed); |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
bool CAddonButtonMapping::OnHatMotion(unsigned int hatIndex, HAT_STATE state) |
| 65 |
{ |
| 66 |
if (m_buttonMapping) |
| 67 |
return m_buttonMapping->OnHatMotion(hatIndex, state); |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
bool CAddonButtonMapping::OnAxisMotion(unsigned int axisIndex, |
| 73 |
float position, |
| 74 |
int center, |
| 75 |
unsigned int range) |
| 76 |
{ |
| 77 |
if (m_buttonMapping) |
| 78 |
return m_buttonMapping->OnAxisMotion(axisIndex, position, center, range); |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
void CAddonButtonMapping::OnInputFrame(void) |
| 84 |
{ |
| 85 |
if (m_buttonMapping) |
| 86 |
m_buttonMapping->OnInputFrame(); |
| 87 |
} |
| 88 |
|
| 89 |
bool CAddonButtonMapping::OnKeyPress(const CKey& key) |
| 90 |
{ |
| 91 |
if (m_buttonMapping) |
| 92 |
return m_buttonMapping->OnKeyPress(key); |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
void CAddonButtonMapping::OnKeyRelease(const CKey& key) |
| 98 |
{ |
| 99 |
if (m_buttonMapping) |
| 100 |
m_buttonMapping->OnKeyRelease(key); |
| 101 |
} |
| 102 |
|
| 103 |
bool CAddonButtonMapping::OnPosition(int x, int y) |
| 104 |
{ |
| 105 |
if (m_buttonMapping) |
| 106 |
return m_buttonMapping->OnPosition(x, y); |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
bool CAddonButtonMapping::OnButtonPress(MOUSE::BUTTON_ID button) |
| 112 |
{ |
| 113 |
if (m_buttonMapping) |
| 114 |
return m_buttonMapping->OnButtonPress(button); |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
void CAddonButtonMapping::OnButtonRelease(MOUSE::BUTTON_ID button) |
| 120 |
{ |
| 121 |
if (m_buttonMapping) |
| 122 |
m_buttonMapping->OnButtonRelease(button); |
| 123 |
} |
| 124 |
|
| 125 |
void CAddonButtonMapping::SaveButtonMap() |
| 126 |
{ |
| 127 |
if (m_buttonMapping) |
| 128 |
m_buttonMapping->SaveButtonMap(); |
| 129 |
} |
| 130 |
|
| 131 |
void CAddonButtonMapping::ResetIgnoredPrimitives() |
| 132 |
{ |
| 133 |
if (m_buttonMapping) |
| 134 |
m_buttonMapping->ResetIgnoredPrimitives(); |
| 135 |
} |
| 136 |
|
| 137 |
void CAddonButtonMapping::RevertButtonMap() |
| 138 |
{ |
| 139 |
if (m_buttonMapping) |
| 140 |
m_buttonMapping->RevertButtonMap(); |
| 141 |
} |