Skip to content

Commit

Permalink
[Android][Peripherals] Fix input for extended range of axes
Browse files Browse the repository at this point in the history
This includes axes with unexpected input sources and generic axes on
controllers that Android doesn't know how to map.
  • Loading branch information
garbear committed Jan 29, 2024
1 parent ba24639 commit 0de1136
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
53 changes: 40 additions & 13 deletions xbmc/platform/android/peripherals/AndroidJoystickState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ bool CAndroidJoystickState::Initialize(const CJNIViewInputDevice& inputDevice)
!motionRange.isFromSource(CJNIViewInputDevice::SOURCE_GAMEPAD))
{
CLog::Log(LOGDEBUG,
"CAndroidJoystickState: ignoring axis {} from source {} for input device \"{}\" "
"with ID {}",
motionRange.getAxis(), motionRange.getSource(), deviceName, m_deviceId);
continue;
"CAndroidJoystickState: axis {} has unexpected source {} ({}) for input device "
"\"{}\" with ID {}",
motionRange.getAxis(),
CAndroidJoystickTranslator::TranslateSource(motionRange.getSource()),
motionRange.getSource(), deviceName, m_deviceId);
}

int axisId = motionRange.getAxis();
Expand All @@ -115,15 +116,41 @@ bool CAndroidJoystickState::Initialize(const CJNIViewInputDevice& inputDevice)
motionRange.getRange(),
motionRange.getResolution()};

// check if the axis ID belongs to a D-pad, analogue stick or trigger
if (axisId == AMOTION_EVENT_AXIS_HAT_X || axisId == AMOTION_EVENT_AXIS_HAT_Y ||
axisId == AMOTION_EVENT_AXIS_X || axisId == AMOTION_EVENT_AXIS_Y ||
axisId == AMOTION_EVENT_AXIS_Z || axisId == AMOTION_EVENT_AXIS_RX ||
axisId == AMOTION_EVENT_AXIS_RY || axisId == AMOTION_EVENT_AXIS_RZ ||
axisId == AMOTION_EVENT_AXIS_LTRIGGER || axisId == AMOTION_EVENT_AXIS_RTRIGGER ||
axisId == AMOTION_EVENT_AXIS_GAS || axisId == AMOTION_EVENT_AXIS_BRAKE ||
axisId == AMOTION_EVENT_AXIS_THROTTLE || axisId == AMOTION_EVENT_AXIS_RUDDER ||
axisId == AMOTION_EVENT_AXIS_WHEEL)
// check if the axis ID belongs to a D-pad, analogue stick, trigger or
// generic axis
// clang-format off
if (axisId == AMOTION_EVENT_AXIS_HAT_X ||
axisId == AMOTION_EVENT_AXIS_HAT_Y ||
axisId == AMOTION_EVENT_AXIS_X ||
axisId == AMOTION_EVENT_AXIS_Y ||
axisId == AMOTION_EVENT_AXIS_Z ||
axisId == AMOTION_EVENT_AXIS_RX ||
axisId == AMOTION_EVENT_AXIS_RY ||
axisId == AMOTION_EVENT_AXIS_RZ ||
axisId == AMOTION_EVENT_AXIS_LTRIGGER ||
axisId == AMOTION_EVENT_AXIS_RTRIGGER ||
axisId == AMOTION_EVENT_AXIS_GAS ||
axisId == AMOTION_EVENT_AXIS_BRAKE ||
axisId == AMOTION_EVENT_AXIS_THROTTLE ||
axisId == AMOTION_EVENT_AXIS_RUDDER ||
axisId == AMOTION_EVENT_AXIS_WHEEL ||
axisId == AMOTION_EVENT_AXIS_GENERIC_1 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_2 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_3 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_4 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_5 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_6 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_7 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_8 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_9 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_10 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_11 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_12 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_13 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_14 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_15 ||
axisId == AMOTION_EVENT_AXIS_GENERIC_16)
// clang-format on
{
// check if this axis is already known
if (ContainsAxis(axisId, m_axes))
Expand Down
31 changes: 31 additions & 0 deletions xbmc/platform/android/peripherals/AndroidJoystickTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <android/input.h>
#include <android/keycodes.h>
#include <androidjni/View.h>

using namespace PERIPHERALS;

Expand Down Expand Up @@ -694,3 +695,33 @@ const char* CAndroidJoystickTranslator::TranslateKeyCode(int keyCode)

return "unknown";
}

const char* CAndroidJoystickTranslator::TranslateSource(int source)
{
// clang-format off
static const std::vector<std::pair<int, const char*>> sources{
{CJNIViewInputDevice::SOURCE_DPAD, "SOURCE_DPAD"},
{CJNIViewInputDevice::SOURCE_GAMEPAD, "SOURCE_GAMEPAD"},
{CJNIViewInputDevice::SOURCE_HDMI, "SOURCE_HDMI"},
{CJNIViewInputDevice::SOURCE_JOYSTICK, "SOURCE_JOYSTICK"},
{CJNIViewInputDevice::SOURCE_KEYBOARD, "SOURCE_KEYBOARD"},
{CJNIViewInputDevice::SOURCE_MOUSE, "SOURCE_MOUSE"},
{CJNIViewInputDevice::SOURCE_MOUSE_RELATIVE, "SOURCE_MOUSE_RELATIVE"},
{CJNIViewInputDevice::SOURCE_ROTARY_ENCODER, "SOURCE_ROTARY_ENCODER"},
{CJNIViewInputDevice::SOURCE_STYLUS, "SOURCE_STYLUS"},
{CJNIViewInputDevice::SOURCE_TOUCHPAD, "SOURCE_TOUCHPAD"},
{CJNIViewInputDevice::SOURCE_TOUCHSCREEN, "SOURCE_TOUCHSCREEN"},
{CJNIViewInputDevice::SOURCE_TOUCH_NAVIGATION, "SOURCE_TOUCH_NAVIGATION"},
{CJNIViewInputDevice::SOURCE_TRACKBALL, "SOURCE_TRACKBALL"},
{CJNIViewInputDevice::SOURCE_UNKNOWN, "SOURCE_UNKNOWN"},
};
// clang-format on

for (const auto& it : sources)
{
if (it.first != 0 && it.first == source)
return it.second;
}

return "unknown";
}
9 changes: 9 additions & 0 deletions xbmc/platform/android/peripherals/AndroidJoystickTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ class CAndroidJoystickTranslator
* \return The translated enum label, or "unknown" if unknown
*/
static const char* TranslateKeyCode(int keyCode);

/*!
* \brief Translate a key code to an Android enum suitable for logging
*
* \param keyCode The key code given in <android/keycodes.h>
*
* \return The translated enum label, or "unknown" if unknown
*/
static const char* TranslateSource(int source);
};
} // namespace PERIPHERALS

0 comments on commit 0de1136

Please sign in to comment.