summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/generic/InputHandling.cpp
blob: f6cae6d4db455332d69b9548a5adefcf642379f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *  Copyright (C) 2014-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "InputHandling.h"

#include "input/joysticks/DriverPrimitive.h"
#include "input/joysticks/JoystickUtils.h"
#include "input/joysticks/dialogs/GUIDialogNewJoystick.h"
#include "input/joysticks/interfaces/IButtonMap.h"
#include "input/joysticks/interfaces/IInputHandler.h"
#include "utils/log.h"

#include <array>
#include <cmath>
#include <tuple>

using namespace KODI;
using namespace JOYSTICK;

CGUIDialogNewJoystick* const CInputHandling::m_dialog = new CGUIDialogNewJoystick;

CInputHandling::CInputHandling(IInputHandler* handler, IButtonMap* buttonMap)
  : m_handler(handler), m_buttonMap(buttonMap)
{
}

CInputHandling::~CInputHandling(void) = default;

bool CInputHandling::OnButtonMotion(unsigned int buttonIndex, bool bPressed)
{
  return OnDigitalMotion(CDriverPrimitive(PRIMITIVE_TYPE::BUTTON, buttonIndex), bPressed);
}

bool CInputHandling::OnHatMotion(unsigned int hatIndex, HAT_STATE state)
{
  bool bHandled = false;

  bHandled |=
      OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::UP), state & HAT_DIRECTION::UP);
  bHandled |= OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::RIGHT),
                              state & HAT_DIRECTION::RIGHT);
  bHandled |=
      OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::DOWN), state & HAT_DIRECTION::DOWN);
  bHandled |=
      OnDigitalMotion(CDriverPrimitive(hatIndex, HAT_DIRECTION::LEFT), state & HAT_DIRECTION::LEFT);

  return bHandled;
}

bool CInputHandling::OnAxisMotion(unsigned int axisIndex,
                                  float position,
                                  int center,
                                  unsigned int range)
{
  bool bHandled = false;

  if (center != 0)
  {
    float translatedPostion = std::min((position - center) / range, 1.0f);

    // Calculate the direction the trigger travels from the center point
    SEMIAXIS_DIRECTION dir;
    if (center > 0)
      dir = SEMIAXIS_DIRECTION::NEGATIVE;
    else
      dir = SEMIAXIS_DIRECTION::POSITIVE;

    CDriverPrimitive offsetSemiaxis(axisIndex, center, dir, range);

    bHandled = OnAnalogMotion(offsetSemiaxis, translatedPostion);
  }
  else
  {
    CDriverPrimitive positiveSemiaxis(axisIndex, 0, SEMIAXIS_DIRECTION::POSITIVE, 1);
    CDriverPrimitive negativeSemiaxis(axisIndex, 0, SEMIAXIS_DIRECTION::NEGATIVE, 1);

    bHandled |= OnAnalogMotion(positiveSemiaxis, position > 0.0f ? position : 0.0f);
    bHandled |= OnAnalogMotion(negativeSemiaxis, position < 0.0f ? -position : 0.0f);
  }

  return bHandled;
}

void CInputHandling::OnInputFrame(void)
{
  // Handle driver input
  for (auto& it : m_features)
    it.second->ProcessMotions();

  // Handle higher-level controller input
  m_handler->OnInputFrame();
}

bool CInputHandling::OnDigitalMotion(const CDriverPrimitive& source, bool bPressed)
{
  bool bHandled = false;

  FeatureName featureName;
  if (m_buttonMap->GetFeature(source, featureName))
  {
    auto it = m_features.find(featureName);
    if (it == m_features.end())
    {
      FeaturePtr feature(CreateFeature(featureName));
      if (feature)
        std::tie(it, std::ignore) = m_features.insert({featureName, std::move(feature)});
    }

    if (it != m_features.end())
      bHandled = it->second->OnDigitalMotion(source, bPressed);
  }
  else if (bPressed)
  {
    // If button didn't resolve to a feature, check if the button map is empty
    // and ask the user if they would like to start mapping the controller
    if (m_buttonMap->IsEmpty())
    {
      CLog::Log(LOGDEBUG, "Empty button map detected for {}", m_buttonMap->ControllerID());
      m_dialog->ShowAsync();
    }
  }

  return bHandled;
}

bool CInputHandling::OnAnalogMotion(const CDriverPrimitive& source, float magnitude)
{
  bool bHandled = false;

  FeatureName featureName;
  if (m_buttonMap->GetFeature(source, featureName))
  {
    auto it = m_features.find(featureName);
    if (it == m_features.end())
    {
      FeaturePtr feature(CreateFeature(featureName));
      if (feature)
        std::tie(it, std::ignore) = m_features.insert({featureName, std::move(feature)});
    }

    if (it != m_features.end())
      bHandled = it->second->OnAnalogMotion(source, magnitude);
  }

  return bHandled;
}

CJoystickFeature* CInputHandling::CreateFeature(const FeatureName& featureName)
{
  CJoystickFeature* feature = nullptr;

  switch (m_buttonMap->GetFeatureType(featureName))
  {
    case FEATURE_TYPE::SCALAR:
    {
      feature = new CScalarFeature(featureName, m_handler, m_buttonMap);
      break;
    }
    case FEATURE_TYPE::ANALOG_STICK:
    {
      feature = new CAnalogStick(featureName, m_handler, m_buttonMap);
      break;
    }
    case FEATURE_TYPE::ACCELEROMETER:
    {
      feature = new CAccelerometer(featureName, m_handler, m_buttonMap);
      break;
    }
    default:
      break;
  }

  return feature;
}