From a27f220f79d0ec812d5d79a05cc5f8e58bd3f1ac Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 13 Jan 2021 18:55:11 +1000 Subject: [PATCH] Android: Support binding volume buttons --- .../src/cpp/android_controller_interface.h | 4 +-- .../duckstation/EmulationSurfaceView.java | 33 ++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/android/app/src/cpp/android_controller_interface.h b/android/app/src/cpp/android_controller_interface.h index a0888dfed..9374f6e39 100644 --- a/android/app/src/cpp/android_controller_interface.h +++ b/android/app/src/cpp/android_controller_interface.h @@ -44,8 +44,8 @@ private: enum : u32 { NUM_CONTROLLERS = 1, - NUM_AXISES = 10, - NUM_BUTTONS = 18 + NUM_AXISES = 12, + NUM_BUTTONS = 23 }; struct ControllerData diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java b/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java index 7be36c326..4da13d040 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java @@ -75,6 +75,10 @@ public class EmulationSurfaceView extends SurfaceView { KeyEvent.KEYCODE_BUTTON_R2, // 16 KeyEvent.KEYCODE_BUTTON_C, // 17 KeyEvent.KEYCODE_BUTTON_Z, // 18 + KeyEvent.KEYCODE_VOLUME_DOWN, // 19 + KeyEvent.KEYCODE_VOLUME_UP, // 20 + KeyEvent.KEYCODE_MENU, // 21 + KeyEvent.KEYCODE_POWER, // 22 }; private static final int[] axisCodes = new int[]{ MotionEvent.AXIS_X, // 0/LeftX @@ -87,6 +91,8 @@ public class EmulationSurfaceView extends SurfaceView { MotionEvent.AXIS_RY, // 7 MotionEvent.AXIS_HAT_X, // 8 MotionEvent.AXIS_HAT_Y, // 9 + MotionEvent.AXIS_GAS, // 10 + MotionEvent.AXIS_BRAKE, // 11 }; public static int getButtonIndexForKeyCode(int keyCode) { @@ -161,14 +167,17 @@ public class EmulationSurfaceView extends SurfaceView { private ArrayList mControllerKeyMapping; private ArrayList mControllerAxisMapping; - private boolean handleControllerKey(int deviceId, int keyCode, boolean pressed) { + private boolean handleControllerKey(int deviceId, int keyCode, int repeatCount, boolean pressed) { boolean result = false; for (ButtonMapping mapping : mControllerKeyMapping) { if (mapping.deviceId != deviceId || mapping.deviceAxisOrButton != keyCode) continue; - AndroidHostInterface.getInstance().handleControllerButtonEvent(0, mapping.buttonMapping, pressed); - Log.d("EmulationSurfaceView", String.format("handleControllerKey %d -> %d %d", keyCode, mapping.buttonMapping, pressed ? 1 : 0)); + if (repeatCount == 0) { + AndroidHostInterface.getInstance().handleControllerButtonEvent(0, mapping.buttonMapping, pressed); + Log.d("EmulationSurfaceView", String.format("handleControllerKey %d -> %d %d", keyCode, mapping.buttonMapping, pressed ? 1 : 0)); + } + result = true; } @@ -177,24 +186,26 @@ public class EmulationSurfaceView extends SurfaceView { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if (!isDPadOrButtonEvent(event) || isExternalKeyCode(keyCode)) + if (!isDPadOrButtonEvent(event)) return false; - if (event.getRepeatCount() == 0) - handleControllerKey(event.getDeviceId(), keyCode, true); + if (handleControllerKey(event.getDeviceId(), keyCode, event.getRepeatCount(), true)) + return true; - return true; + // eat non-external button events anyway + return !isExternalKeyCode(keyCode); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { - if (!isDPadOrButtonEvent(event) || isExternalKeyCode(keyCode)) + if (!isDPadOrButtonEvent(event)) return false; - if (event.getRepeatCount() == 0) - handleControllerKey(event.getDeviceId(), keyCode, false); + if (handleControllerKey(event.getDeviceId(), keyCode, 0, false)) + return true; - return true; + // eat non-external button events anyway + return !isExternalKeyCode(keyCode); } @Override