From 7c71b578bc0ffee7d926c26fc6a2c4f7c6f36616 Mon Sep 17 00:00:00 2001 From: Karim Mreisi Date: Wed, 15 Feb 2023 08:26:38 +0100 Subject: [PATCH] game menu: show input device game menu options --- app/src/main/java/com/limelight/Game.java | 17 ++----- app/src/main/java/com/limelight/GameMenu.java | 40 +++++++++------- .../binding/input/ControllerHandler.java | 46 ++++++++----------- .../binding/input/GameInputDevice.java | 16 +++++++ .../java/com/limelight/ui/GameGestures.java | 4 +- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 66 insertions(+), 59 deletions(-) create mode 100644 app/src/main/java/com/limelight/binding/input/GameInputDevice.java diff --git a/app/src/main/java/com/limelight/Game.java b/app/src/main/java/com/limelight/Game.java index 936dd4652..5ba3350e9 100644 --- a/app/src/main/java/com/limelight/Game.java +++ b/app/src/main/java/com/limelight/Game.java @@ -4,6 +4,7 @@ import com.limelight.binding.PlatformBinding; import com.limelight.binding.audio.AndroidAudioRenderer; import com.limelight.binding.input.ControllerHandler; +import com.limelight.binding.input.GameInputDevice; import com.limelight.binding.input.KeyboardTranslator; import com.limelight.binding.input.capture.InputCaptureManager; import com.limelight.binding.input.capture.InputCaptureProvider; @@ -1417,15 +1418,6 @@ public void toggleKeyboard() { inputManager.toggleSoftInput(0, 0); } - public boolean isMouseEmulationActive() { - return controllerHandler.isMouseEmulationEnabled(); - } - - public void toggleMouseEmulation() { - controllerHandler.toggleMouseEmulation(); - } - - // Returns true if the event was consumed // NB: View is only present if called from a view callback private boolean handleMotionEvent(View view, MotionEvent event) { @@ -2269,10 +2261,9 @@ public void onUsbPermissionPromptCompleted() { updatePipAutoEnter(); } - @Override - public void showGameMenu() { - new GameMenu(this, conn); + public void showGameMenu(GameInputDevice device) { + new GameMenu(this, conn, device); } @Override @@ -2286,7 +2277,7 @@ public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { // Intercept back key event before android handles it // Always handle the request, the user has to select "Disconnect" within the game menu to actually disconnect if (keyCode == keyEvent.KEYCODE_BACK) { - showGameMenu(); + showGameMenu(null); return true; } case KeyEvent.ACTION_UP: diff --git a/app/src/main/java/com/limelight/GameMenu.java b/app/src/main/java/com/limelight/GameMenu.java index 0803ac24c..e2da27643 100644 --- a/app/src/main/java/com/limelight/GameMenu.java +++ b/app/src/main/java/com/limelight/GameMenu.java @@ -4,10 +4,14 @@ import android.os.Handler; import android.widget.ArrayAdapter; +import com.limelight.binding.input.GameInputDevice; import com.limelight.binding.input.KeyboardTranslator; import com.limelight.nvstream.NvConnection; import com.limelight.nvstream.input.KeyboardPacket; +import java.util.ArrayList; +import java.util.List; + /** * Provide options for ongoing Game Stream. *

@@ -17,11 +21,11 @@ public class GameMenu { private static final long KEY_UP_DELAY = 25; - private static class MenuOption { + public static class MenuOption { private final String label; private final Runnable runnable; - MenuOption(String label, Runnable runnable) { + public MenuOption(String label, Runnable runnable) { this.label = label; this.runnable = runnable; } @@ -29,10 +33,12 @@ private static class MenuOption { private final Game game; private final NvConnection conn; + private final GameInputDevice device; - public GameMenu(Game game, NvConnection conn) { + public GameMenu(Game game, NvConnection conn, GameInputDevice device) { this.game = game; this.conn = conn; + this.device = device; showMenu(); } @@ -79,10 +85,6 @@ private void sendKeys(short[] keys) { }), KEY_UP_DELAY); } - private boolean isMouseEmulationActive() { - return game.isMouseEmulationActive(); - } - private void showMenuDialog(String title, MenuOption[] options) { AlertDialog.Builder builder = new AlertDialog.Builder(game); builder.setTitle(title); @@ -132,15 +134,19 @@ private void showSpecialKeysMenu() { } private void showMenu() { - showMenuDialog("Game Menu", new MenuOption[]{ - new MenuOption(getString(R.string.game_menu_send_keys), () -> showSpecialKeysMenu()), - new MenuOption(getString(isMouseEmulationActive() ? - R.string.game_menu_toggle_mouse_off : R.string.game_menu_toggle_mouse_on), - () -> game.toggleMouseEmulation()), - new MenuOption(getString(R.string.game_menu_toggle_keyboard), - () -> game.toggleKeyboard()), - new MenuOption(getString(R.string.game_menu_disconnect), () -> game.onBackPressed()), - new MenuOption(getString(R.string.game_menu_cancel), null), - }); + List options = new ArrayList<>(); + + options.add(new MenuOption(getString(R.string.game_menu_toggle_keyboard), + () -> game.toggleKeyboard())); + + if (device != null) { + options.addAll(device.getGameMenuOptions()); + } + + options.add(new MenuOption(getString(R.string.game_menu_send_keys), () -> showSpecialKeysMenu())); + options.add(new MenuOption(getString(R.string.game_menu_disconnect), () -> game.onBackPressed())); + options.add(new MenuOption(getString(R.string.game_menu_cancel), null)); + + showMenuDialog("Game Menu", options.toArray(new MenuOption[options.size()])); } } diff --git a/app/src/main/java/com/limelight/binding/input/ControllerHandler.java b/app/src/main/java/com/limelight/binding/input/ControllerHandler.java index ce75502c3..bccd4885f 100644 --- a/app/src/main/java/com/limelight/binding/input/ControllerHandler.java +++ b/app/src/main/java/com/limelight/binding/input/ControllerHandler.java @@ -22,7 +22,9 @@ import android.view.MotionEvent; import android.widget.Toast; +import com.limelight.GameMenu; import com.limelight.LimeLog; +import com.limelight.R; import com.limelight.binding.input.driver.AbstractController; import com.limelight.binding.input.driver.UsbDriverListener; import com.limelight.binding.input.driver.UsbDriverService; @@ -36,6 +38,8 @@ import org.cgutman.shieldcontrollerextensions.SceManager; import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; public class ControllerHandler implements InputManager.InputDeviceListener, UsbDriverListener { @@ -1511,7 +1515,7 @@ public boolean handleButtonUp(KeyEvent event) { if ((context.inputMap & ControllerPacket.PLAY_FLAG) != 0 && event.getEventTime() - context.startDownTime > ControllerHandler.START_DOWN_TIME_MOUSE_MODE_MS && prefConfig.mouseEmulation) { - gestures.showGameMenu(); + gestures.showGameMenu(context); } context.inputMap &= ~ControllerPacket.PLAY_FLAG; break; @@ -1858,7 +1862,7 @@ public void deviceAdded(AbstractController controller) { usbDeviceContexts.put(controller.getControllerId(), context); } - class GenericControllerContext { + class GenericControllerContext implements GameInputDevice { public int id; public boolean external; @@ -1901,9 +1905,20 @@ public void run() { } }; - public void toggleMouseEmulation(boolean enable) { + @Override + public List getGameMenuOptions() { + List options = new ArrayList<>(); + options.add(new GameMenu.MenuOption(activityContext.getString(mouseEmulationActive ? + R.string.game_menu_toggle_mouse_off : R.string.game_menu_toggle_mouse_on), + () -> toggleMouseEmulation())); + + return options; + } + + public void toggleMouseEmulation() { handler.removeCallbacks(mouseEmulationRunnable); - mouseEmulationActive = enable; + mouseEmulationActive = !mouseEmulationActive; + Toast.makeText(activityContext, "Mouse emulation is: " + (mouseEmulationActive ? "ON" : "OFF"), Toast.LENGTH_SHORT).show(); if (mouseEmulationActive) { handler.postDelayed(mouseEmulationRunnable, mouseEmulationReportPeriod); @@ -1985,27 +2000,4 @@ public void destroy() { // Nothing for now } } - - public void toggleMouseEmulation() { - - boolean enable = !defaultContext.mouseEmulationActive; - defaultContext.mouseEmulationActive = enable; - defaultContext.toggleMouseEmulation(enable); - - for (int i = 0; i < inputDeviceContexts.size(); i++) { - InputDeviceContext deviceContext = inputDeviceContexts.valueAt(i); - deviceContext.toggleMouseEmulation(enable); - } - - for (int i = 0; i < usbDeviceContexts.size(); i++) { - UsbDeviceContext deviceContext = usbDeviceContexts.valueAt(i); - deviceContext.toggleMouseEmulation(enable); - } - - Toast.makeText(activityContext, "Mouse emulation is: " + (enable ? "ON" : "OFF"), Toast.LENGTH_SHORT).show(); - } - - public boolean isMouseEmulationEnabled() { - return defaultContext.mouseEmulationActive; - } } diff --git a/app/src/main/java/com/limelight/binding/input/GameInputDevice.java b/app/src/main/java/com/limelight/binding/input/GameInputDevice.java new file mode 100644 index 000000000..96df3ada0 --- /dev/null +++ b/app/src/main/java/com/limelight/binding/input/GameInputDevice.java @@ -0,0 +1,16 @@ +package com.limelight.binding.input; + +import com.limelight.GameMenu; + +import java.util.List; + +/** + * Generic Input Device + */ +public interface GameInputDevice { + + /** + * @return list of device specific game menu options, e.g. configure a controller's mouse mode + */ + List getGameMenuOptions(); +} diff --git a/app/src/main/java/com/limelight/ui/GameGestures.java b/app/src/main/java/com/limelight/ui/GameGestures.java index 43ab1a2d0..a4197b29b 100644 --- a/app/src/main/java/com/limelight/ui/GameGestures.java +++ b/app/src/main/java/com/limelight/ui/GameGestures.java @@ -1,7 +1,9 @@ package com.limelight.ui; +import com.limelight.binding.input.GameInputDevice; + public interface GameGestures { void toggleKeyboard(); - void showGameMenu(); + void showGameMenu(GameInputDevice device); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cf342b421..39f91a132 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -132,9 +132,9 @@ App ID: + Toggle On-screen Keyboard Enable Controller Mouse Emulation Disable Controller Mouse Emulation - Toggle On-screen Keyboard Disconnect Cancel Send special Key(s)