2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "addons/kodi-dev-kit/include/kodi/addon-instance/peripheral/PeripheralUtils.h"
12 #include "threads/CriticalSection.h"
19 class CJNIViewInputDevice
;
26 } // namespace JOYSTICK
31 class CAndroidJoystickState
34 CAndroidJoystickState() = default;
35 CAndroidJoystickState(CAndroidJoystickState
&& other
) noexcept
;
36 virtual ~CAndroidJoystickState();
38 int GetDeviceId() const { return m_deviceId
; }
40 unsigned int GetButtonCount() const { return static_cast<unsigned int>(m_buttons
.size()); }
41 unsigned int GetAxisCount() const { return static_cast<unsigned int>(m_axes
.size()); }
44 * \brief Initialize the joystick object
46 * Joystick will be initialized before the first call to GetEvents().
48 bool Initialize(const CJNIViewInputDevice
& inputDevice
);
51 * \brief Initialize a joystick buttonmap, if possible
53 * Android has a large database of buttonmaps, which it uses to provide
54 * mapped button keycodes such as AKEYCODE_BUTTON_A. We can take advantage of
55 * this to initialize a default buttonmap based on these mappings.
57 * If Android can't map the buttons, it will use generic button keycodes such
58 * as AKEYCODE_BUTTON_1, in which case we can't initialize the buttonmap.
60 bool InitializeButtonMap(KODI::JOYSTICK::IButtonMap
& buttonMap
) const;
63 * \brief Deinitialize the joystick object
65 * GetEvents() will not be called after deinitialization.
70 * \brief Processes the given input event.
72 bool ProcessEvent(const AInputEvent
* event
);
75 * \brief Get events that have occurred since the last call to GetEvents()
77 void GetEvents(std::vector
<kodi::addon::PeripheralEvent
>& events
);
80 bool SetButtonValue(int axisId
, JOYSTICK_STATE_BUTTON buttonValue
);
81 bool SetAxisValue(const std::vector
<int>& axisIds
, JOYSTICK_STATE_AXIS axisValue
);
83 void GetButtonEvents(std::vector
<kodi::addon::PeripheralEvent
>& events
);
84 void GetAxisEvents(std::vector
<kodi::addon::PeripheralEvent
>& events
) const;
86 bool MapButton(KODI::JOYSTICK::IButtonMap
& buttonMap
, int buttonKeycode
) const;
87 bool MapTrigger(KODI::JOYSTICK::IButtonMap
& buttonMap
,
89 const std::string
& triggerName
) const;
90 bool MapDpad(KODI::JOYSTICK::IButtonMap
& buttonMap
, int horizAxisId
, int vertAxisId
) const;
91 bool MapAnalogStick(KODI::JOYSTICK::IButtonMap
& buttonMap
,
94 const std::string
& analogStickName
) const;
96 static float Contain(float value
, float min
, float max
);
97 static float Scale(float value
, float max
, float scaledMax
);
98 static float Deadzone(float value
, float deadzone
);
102 std::vector
<int> ids
;
108 float resolution
= 0.0f
;
111 using JoystickAxes
= std::vector
<JoystickAxis
>;
113 static JoystickAxes::const_iterator
GetAxis(const std::vector
<int>& axisIds
,
114 const JoystickAxes
& axes
);
115 static bool ContainsAxis(int axisId
, const JoystickAxes
& axes
);
116 static bool GetAxesIndex(const std::vector
<int>& axisIds
,
117 const JoystickAxes
& axes
,
122 JoystickAxes m_buttons
;
125 std::vector
<JOYSTICK_STATE_AXIS
> m_analogState
;
127 CCriticalSection m_eventMutex
;
128 std::vector
<kodi::addon::PeripheralEvent
> m_digitalEvents
;
130 } // namespace PERIPHERALS