| 1 | /* |
| 2 | * Copyright (C) 2014-2021 Garrett Brown |
| 3 | * Copyright (C) 2014-2021 Team Kodi |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | * See LICENSE.md for more information. |
| 7 | */ |
| 8 | |
| 9 | #include "JoystickInterfaceXInput.h" |
| 10 | #include "JoystickXInput.h" |
| 11 | #include "XInputDLL.h" |
| 12 | #include "api/JoystickTypes.h" |
| 13 | |
| 14 | #include <array> |
| 15 | #include <cstring> |
| 16 | #include <utility> |
| 17 | #include <Xinput.h> |
| 18 | |
| 19 | using namespace JOYSTICK; |
| 20 | |
| 21 | #define MAX_JOYSTICKS 4 |
| 22 | |
| 23 | ButtonMap CJoystickInterfaceXInput::m_buttonMap = { |
| 24 | std::make_pair("game.controller.default", FeatureVector{ |
| 25 | kodi::addon::JoystickFeature("leftmotor", JOYSTICK_FEATURE_TYPE_MOTOR), |
| 26 | kodi::addon::JoystickFeature("rightmotor", JOYSTICK_FEATURE_TYPE_MOTOR), |
| 27 | }), |
| 28 | std::make_pair("game.controller.ps", FeatureVector{ |
| 29 | kodi::addon::JoystickFeature("strongmotor", JOYSTICK_FEATURE_TYPE_MOTOR), |
| 30 | kodi::addon::JoystickFeature("weakmotor", JOYSTICK_FEATURE_TYPE_MOTOR), |
| 31 | }), |
| 32 | }; |
| 33 | |
| 34 | EJoystickInterface CJoystickInterfaceXInput::Type(void) const |
| 35 | { |
| 36 | return EJoystickInterface::XINPUT; |
| 37 | } |
| 38 | |
| 39 | bool CJoystickInterfaceXInput::Initialize(void) |
| 40 | { |
| 41 | return CXInputDLL::Get().Load(); |
| 42 | } |
| 43 | |
| 44 | void CJoystickInterfaceXInput::Deinitialize(void) |
| 45 | { |
| 46 | CXInputDLL::Get().Unload(); |
| 47 | } |
| 48 | |
| 49 | bool CJoystickInterfaceXInput::SupportsPowerOff(void) const |
| 50 | { |
| 51 | return CXInputDLL::Get().SupportsPowerOff(); |
| 52 | } |
| 53 | |
| 54 | bool CJoystickInterfaceXInput::ScanForJoysticks(JoystickVector& joysticks) |
| 55 | { |
| 56 | // No need to memset, only checking for controller existence |
| 57 | XINPUT_STATE controllerState; |
| 58 | XINPUT_STATE_EX controllerStateWithGuide; |
| 59 | |
| 60 | for (unsigned int i = 0; i < MAX_JOYSTICKS; i++) |
| 61 | { |
| 62 | if (CXInputDLL::Get().HasGuideButton()) |
| 63 | { |
| 64 | if (!CXInputDLL::Get().GetStateWithGuide(i, controllerStateWithGuide)) |
| 65 | continue; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | if (!CXInputDLL::Get().GetState(i, controllerState)) |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | joysticks.push_back(JoystickPtr(new CJoystickXInput(i))); |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | const ButtonMap& CJoystickInterfaceXInput::GetButtonMap() |
| 80 | { |
| 81 | auto& dflt = m_buttonMap["game.controller.default"]; |
| 82 | dflt[CJoystickXInput::MOTOR_LEFT].SetPrimitive(JOYSTICK_MOTOR_PRIMITIVE, kodi::addon::DriverPrimitive::CreateMotor(CJoystickXInput::MOTOR_LEFT)); |
| 83 | dflt[CJoystickXInput::MOTOR_RIGHT].SetPrimitive(JOYSTICK_MOTOR_PRIMITIVE, kodi::addon::DriverPrimitive::CreateMotor(CJoystickXInput::MOTOR_RIGHT)); |
| 84 | |
| 85 | auto& ps = m_buttonMap["game.controller.ps"]; |
| 86 | ps[CJoystickXInput::MOTOR_LEFT].SetPrimitive(JOYSTICK_MOTOR_PRIMITIVE, kodi::addon::DriverPrimitive::CreateMotor(CJoystickXInput::MOTOR_LEFT)); |
| 87 | ps[CJoystickXInput::MOTOR_RIGHT].SetPrimitive(JOYSTICK_MOTOR_PRIMITIVE, kodi::addon::DriverPrimitive::CreateMotor(CJoystickXInput::MOTOR_RIGHT)); |
| 88 | |
| 89 | return m_buttonMap; |
| 90 | } |