Skip to content

Commit

Permalink
game menu: show input device game menu options
Browse files Browse the repository at this point in the history
  • Loading branch information
Karim Mreisi committed Feb 15, 2023
1 parent c9fe96a commit 7c71b57
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 59 deletions.
17 changes: 4 additions & 13 deletions app/src/main/java/com/limelight/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
40 changes: 23 additions & 17 deletions app/src/main/java/com/limelight/GameMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand All @@ -17,22 +21,24 @@ 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;
}
}

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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<MenuOption> 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()]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -1901,9 +1905,20 @@ public void run() {
}
};

public void toggleMouseEmulation(boolean enable) {
@Override
public List<GameMenu.MenuOption> getGameMenuOptions() {
List<GameMenu.MenuOption> 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);
Expand Down Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/limelight/binding/input/GameInputDevice.java
Original file line number Diff line number Diff line change
@@ -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<GameMenu.MenuOption> getGameMenuOptions();
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/limelight/ui/GameGestures.java
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@
<string name="applist_details_id">App ID:</string>

<!-- In Game menu -->
<string name="game_menu_toggle_keyboard">Toggle On-screen Keyboard</string>
<string name="game_menu_toggle_mouse_on">Enable Controller Mouse Emulation</string>
<string name="game_menu_toggle_mouse_off">Disable Controller Mouse Emulation</string>
<string name="game_menu_toggle_keyboard">Toggle On-screen Keyboard</string>
<string name="game_menu_disconnect">Disconnect</string>
<string name="game_menu_cancel">Cancel</string>
<string name="game_menu_send_keys">Send special Key(s)</string>
Expand Down

0 comments on commit 7c71b57

Please sign in to comment.