From a9a571cd6af04a80277e8923c1480a97a7b695d8 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 4 Apr 2021 02:05:59 +1000 Subject: [PATCH] Android: Support binding half axes to buttons --- android/app/src/cpp/android_host_interface.cpp | 18 ++++++++++++++++++ .../duckstation/AndroidHostInterface.java | 5 +++++ .../duckstation/ControllerBindingDialog.java | 8 +++++--- .../ControllerBindingPreference.java | 5 +++-- .../ControllerSettingsActivity.java | 15 ++++++++------- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/android/app/src/cpp/android_host_interface.cpp b/android/app/src/cpp/android_host_interface.cpp index 0bb87d991..d722ad278 100644 --- a/android/app/src/cpp/android_host_interface.cpp +++ b/android/app/src/cpp/android_host_interface.cpp @@ -1191,6 +1191,24 @@ DEFINE_JNI_ARGS_METHOD(jint, AndroidHostInterface_getControllerAxisCode, jobject return code.value_or(-1); } +DEFINE_JNI_ARGS_METHOD(jint, AndroidHostInterface_getControllerAxisType, jobject unused, jstring controller_type, + jstring axis_name) +{ + std::optional type = + Settings::ParseControllerTypeName(AndroidHelpers::JStringToString(env, controller_type).c_str()); + if (!type) + return -1; + + const std::string axis_name_str(AndroidHelpers::JStringToString(env, axis_name)); + for (const auto& [name, code, type] : Controller::GetAxisNames(type.value())) + { + if (name == axis_name_str) + return static_cast(type); + } + + return -1; +} + DEFINE_JNI_ARGS_METHOD(jobjectArray, AndroidHostInterface_getControllerButtonNames, jobject unused, jstring controller_type) { diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/AndroidHostInterface.java b/android/app/src/main/java/com/github/stenzek/duckstation/AndroidHostInterface.java index 0130a1a95..a64d13009 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/AndroidHostInterface.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/AndroidHostInterface.java @@ -18,6 +18,9 @@ public class AndroidHostInterface { public final static int DISPLAY_ALIGNMENT_CENTER = 1; public final static int DISPLAY_ALIGNMENT_RIGHT_OR_BOTTOM = 2; + public final static int CONTROLLER_AXIS_TYPE_FULL = 0; + public final static int CONTROLLER_AXIS_TYPE_HALF = 1; + private long mNativePointer; private Context mContext; @@ -77,6 +80,8 @@ public class AndroidHostInterface { public static native int getControllerAxisCode(String controllerType, String axisName); + public static native int getControllerAxisType(String controllerType, String axisName); + public static native String[] getControllerButtonNames(String controllerType); public static native String[] getControllerAxisNames(String controllerType); diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingDialog.java b/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingDialog.java index d19c0c742..8b6344c25 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingDialog.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingDialog.java @@ -78,7 +78,7 @@ public class ControllerBindingDialog extends AlertDialog { return super.onKeyDown(keyCode, event); } - if (mType == ControllerBindingPreference.Type.BUTTON) { + if (mType == ControllerBindingPreference.Type.BUTTON || mType == ControllerBindingPreference.Type.HALF_AXIS) { mCurrentBinding = String.format("%s/Button%d", device.getDescriptor(), event.getKeyCode()); } else if (mType == ControllerBindingPreference.Type.VIBRATION) { if (device.getVibrator() == null || !device.getVibrator().hasVibrator()) { @@ -105,7 +105,7 @@ public class ControllerBindingDialog extends AlertDialog { mUpdatedAxisCode = axisCode; final int controllerIndex = 0; - if (mType == ControllerBindingPreference.Type.AXIS) + if (mType == ControllerBindingPreference.Type.AXIS || mType == ControllerBindingPreference.Type.HALF_AXIS) mCurrentBinding = String.format("%s/Axis%d", device.getDescriptor(), axisCode); else mCurrentBinding = String.format("%s/%cAxis%d", device.getDescriptor(), (positive) ? '+' : '-', axisCode); @@ -156,7 +156,9 @@ public class ControllerBindingDialog extends AlertDialog { @Override public boolean onGenericMotionEvent(@NonNull MotionEvent event) { - if (mType != ControllerBindingPreference.Type.AXIS && mType != ControllerBindingPreference.Type.BUTTON) { + if (mType != ControllerBindingPreference.Type.AXIS && + mType != ControllerBindingPreference.Type.HALF_AXIS && + mType != ControllerBindingPreference.Type.BUTTON) { return false; } diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingPreference.java b/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingPreference.java index 842943bc1..a498c5c43 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingPreference.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/ControllerBindingPreference.java @@ -18,6 +18,7 @@ public class ControllerBindingPreference extends Preference { public enum Type { BUTTON, AXIS, + HALF_AXIS, VIBRATION } @@ -149,10 +150,10 @@ public class ControllerBindingPreference extends Preference { updateValue(); } - public void initAxis(int controllerIndex, String axisName) { + public void initAxis(int controllerIndex, String axisName, int axisType) { mBindingName = axisName; mDisplayName = axisName; - mType = Type.AXIS; + mType = (axisType == AndroidHostInterface.CONTROLLER_AXIS_TYPE_HALF) ? Type.HALF_AXIS : Type.AXIS; mVisualType = VisualType.AXIS; setKey(String.format("Controller%d/Axis%s", controllerIndex, axisName)); updateValue(); diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/ControllerSettingsActivity.java b/android/app/src/main/java/com/github/stenzek/duckstation/ControllerSettingsActivity.java index 771e9e5d7..7cca19c21 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/ControllerSettingsActivity.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/ControllerSettingsActivity.java @@ -297,12 +297,12 @@ public class ControllerSettingsActivity extends AppCompatActivity { private void createPreferences(String controllerType) { final PreferenceScreen ps = getPreferenceScreen(); final SharedPreferences sp = getPreferenceManager().getSharedPreferences(); - final String[] controllerButtons = AndroidHostInterface.getControllerButtonNames(controllerType); - final String[] axisButtons = AndroidHostInterface.getControllerAxisNames(controllerType); + final String[] buttonNames = AndroidHostInterface.getControllerButtonNames(controllerType); + final String[] axisNames = AndroidHostInterface.getControllerAxisNames(controllerType); final int vibrationMotors = AndroidHostInterface.getControllerVibrationMotorCount(controllerType); - if (controllerButtons != null) { - for (String buttonName : controllerButtons) { + if (buttonNames != null) { + for (String buttonName : buttonNames) { final ControllerBindingPreference cbp = new ControllerBindingPreference(getContext(), null); cbp.initButton(controllerIndex, buttonName); mButtonsCategory.addPreference(cbp); @@ -310,10 +310,11 @@ public class ControllerSettingsActivity extends AppCompatActivity { } } - if (axisButtons != null) { - for (String axisName : axisButtons) { + if (axisNames != null) { + for (String axisName : axisNames) { + final int axisType = AndroidHostInterface.getControllerAxisType(controllerType, axisName); final ControllerBindingPreference cbp = new ControllerBindingPreference(getContext(), null); - cbp.initAxis(controllerIndex, axisName); + cbp.initAxis(controllerIndex, axisName, axisType); mAxisCategory.addPreference(cbp); activity.mPreferences.add(cbp); }