From 891b4642b7d000935ed36273e04178f97f104f99 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 20 Mar 2021 02:52:59 +1000 Subject: [PATCH] Android: Allow back button on controllers to be bound --- .../duckstation/ControllerBindingDialog.java | 6 +- .../duckstation/EmulationSurfaceView.java | 56 ++++++++++++------- 2 files changed, 39 insertions(+), 23 deletions(-) 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 53f345cd7..237bff93f 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 @@ -72,8 +72,8 @@ public class ControllerBindingDialog extends AlertDialog { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if (!EmulationSurfaceView.isBindableDevice(event.getDevice()) || !EmulationSurfaceView.isBindableKeyCode(event.getKeyCode())) { - return super.onKeyUp(keyCode, event); + if (!EmulationSurfaceView.isBindableDevice(event.getDevice()) || !EmulationSurfaceView.isBindableKeyEvent(event)) { + return super.onKeyDown(keyCode, event); } if (mType == ControllerBindingPreference.Type.BUTTON) @@ -81,7 +81,7 @@ public class ControllerBindingDialog extends AlertDialog { else if (mType == ControllerBindingPreference.Type.VIBRATION) mCurrentBinding = event.getDevice().getDescriptor(); else - return super.onKeyUp(keyCode, event); + return super.onKeyDown(keyCode, event); updateMessage(); updateBinding(); 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 7a4b3a578..8bfd693d6 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 @@ -35,46 +35,62 @@ public class EmulationSurfaceView extends SurfaceView { ((sources & InputDevice.SOURCE_CLASS_BUTTON) == InputDevice.SOURCE_CLASS_BUTTON); } + public static boolean isGamepadDevice(InputDevice inputDevice) { + final int sources = (inputDevice != null) ? inputDevice.getSources() : 0; + return ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD); + } + public static boolean isJoystickMotionEvent(MotionEvent event) { final int source = event.getSource(); return ((source & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK); } - public static boolean isGamepadDevice(InputDevice inputDevice) { - final int sources = inputDevice.getSources(); - return ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD); - } - - public static boolean isBindableKeyCode(int keyCode) { - switch (keyCode) { + public static boolean isBindableKeyEvent(KeyEvent event) { + switch (event.getKeyCode()) { case KeyEvent.KEYCODE_BACK: case KeyEvent.KEYCODE_HOME: - case KeyEvent.KEYCODE_MENU: case KeyEvent.KEYCODE_POWER: - case KeyEvent.KEYCODE_CAMERA: - case KeyEvent.KEYCODE_CALL: - case KeyEvent.KEYCODE_ENDCALL: - case KeyEvent.KEYCODE_VOICE_ASSIST: - return false; + // We're okay if we get these from a gamepad. + return isGamepadDevice(event.getDevice()); default: return true; } } - private static boolean isExternalKeyCode(int keyCode) { + private static boolean isSystemKeyCode(int keyCode) { switch (keyCode) { - case KeyEvent.KEYCODE_BACK: - case KeyEvent.KEYCODE_HOME: case KeyEvent.KEYCODE_MENU: + case KeyEvent.KEYCODE_SOFT_RIGHT: + case KeyEvent.KEYCODE_HOME: + case KeyEvent.KEYCODE_BACK: + case KeyEvent.KEYCODE_CALL: + case KeyEvent.KEYCODE_ENDCALL: case KeyEvent.KEYCODE_VOLUME_UP: case KeyEvent.KEYCODE_VOLUME_DOWN: case KeyEvent.KEYCODE_VOLUME_MUTE: + case KeyEvent.KEYCODE_MUTE: case KeyEvent.KEYCODE_POWER: + case KeyEvent.KEYCODE_HEADSETHOOK: + case KeyEvent.KEYCODE_MEDIA_PLAY: + case KeyEvent.KEYCODE_MEDIA_PAUSE: + case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: + case KeyEvent.KEYCODE_MEDIA_STOP: + case KeyEvent.KEYCODE_MEDIA_NEXT: + case KeyEvent.KEYCODE_MEDIA_PREVIOUS: + case KeyEvent.KEYCODE_MEDIA_REWIND: + case KeyEvent.KEYCODE_MEDIA_RECORD: + case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: case KeyEvent.KEYCODE_CAMERA: - case KeyEvent.KEYCODE_CALL: - case KeyEvent.KEYCODE_ENDCALL: - case KeyEvent.KEYCODE_VOICE_ASSIST: + case KeyEvent.KEYCODE_FOCUS: + case KeyEvent.KEYCODE_SEARCH: + case KeyEvent.KEYCODE_BRIGHTNESS_DOWN: + case KeyEvent.KEYCODE_BRIGHTNESS_UP: + case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: + case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP: + case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN: + case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT: + case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT: return true; default: @@ -208,7 +224,7 @@ public class EmulationSurfaceView extends SurfaceView { hi.handleControllerButtonEvent(controllerIndex, keyCode, pressed); // We don't want to eat external button events unless it's actually bound. - if (isExternalKeyCode(keyCode)) + if (isSystemKeyCode(keyCode)) return (controllerIndex >= 0 && hi.hasControllerButtonBinding(controllerIndex, keyCode)); else return true;