kodi
AndroidJoystickState.h
1 /*
2  * Copyright (C) 2016-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 #pragma once
10 
11 #include "addons/kodi-dev-kit/include/kodi/addon-instance/peripheral/PeripheralUtils.h"
12 #include "threads/CriticalSection.h"
13 
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 struct AInputEvent;
19 class CJNIViewInputDevice;
20 
21 namespace PERIPHERALS
22 {
24 {
25 public:
26  CAndroidJoystickState() = default;
28  virtual ~CAndroidJoystickState();
29 
30  int GetDeviceId() const { return m_deviceId; }
31 
32  unsigned int GetButtonCount() const { return static_cast<unsigned int>(m_buttons.size()); }
33  unsigned int GetAxisCount() const { return static_cast<unsigned int>(m_axes.size()); }
34 
39  bool Initialize(const CJNIViewInputDevice& inputDevice);
40 
45  void Deinitialize();
46 
50  bool ProcessEvent(const AInputEvent* event);
51 
55  void GetEvents(std::vector<kodi::addon::PeripheralEvent>& events);
56 
57 private:
58  bool SetButtonValue(int axisId, JOYSTICK_STATE_BUTTON buttonValue);
59  bool SetAxisValue(const std::vector<int>& axisIds, JOYSTICK_STATE_AXIS axisValue);
60 
61  void GetButtonEvents(std::vector<kodi::addon::PeripheralEvent>& events);
62  void GetAxisEvents(std::vector<kodi::addon::PeripheralEvent>& events) const;
63 
64  static float Contain(float value, float min, float max);
65  static float Scale(float value, float max, float scaledMax);
66  static float Deadzone(float value, float deadzone);
67 
68  struct JoystickAxis
69  {
70  std::vector<int> ids;
71  float flat = 0.0f;
72  float fuzz = 0.0f;
73  float min = 0.0f;
74  float max = 0.0f;
75  float range = 0.0f;
76  float resolution = 0.0f;
77  };
78 
79  using JoystickAxes = std::vector<JoystickAxis>;
80 
81  static JoystickAxes::const_iterator GetAxis(const std::vector<int>& axisIds,
82  const JoystickAxes& axes);
83  static bool ContainsAxis(int axisId, const JoystickAxes& axes);
84  static bool GetAxesIndex(const std::vector<int>& axisIds,
85  const JoystickAxes& axes,
86  size_t& axesIndex);
87 
88  int m_deviceId = -1;
89 
90  JoystickAxes m_buttons;
91  JoystickAxes m_axes;
92 
93  std::vector<JOYSTICK_STATE_AXIS> m_analogState;
94 
95  CCriticalSection m_eventMutex;
96  std::vector<kodi::addon::PeripheralEvent> m_digitalEvents;
97 };
98 } // namespace PERIPHERALS
void GetEvents(std::vector< kodi::addon::PeripheralEvent > &events)
Definition: AndroidJoystickState.cpp:266
Definition: RetroPlayerInput.h:15
bool ProcessEvent(const AInputEvent *event)
Definition: AndroidJoystickState.cpp:203
JOYSTICK_STATE_BUTTON
Definition: peripheral.h:147
void Deinitialize()
Definition: AndroidJoystickState.cpp:194
Definition: AndroidJoystickState.h:23
bool Initialize(const CJNIViewInputDevice &inputDevice)
Definition: AndroidJoystickState.cpp:84