mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-04-11 10:26:12 +00:00
more gamepad changes
- now have a menu bar to access settings - now handle user input to assign mappings - fixed some typos - now properly shutdown controller threads on exit - renamed GamepadSettings to GamepadMapping - created a lock to ensure we do not add/remove gamepads from the list while we are handling their events - fixed settings directory booch
This commit is contained in:
@@ -8,7 +8,7 @@ import javax.swing.JLabel;
|
||||
public enum ControllerComponent implements Serializable {
|
||||
BTN_A("Button 1 (A)", false), BTN_X("Button 2 (X)", false), BTN_Y("Button 3 (Y)", false), BTN_B("Button 4 (B)", false),
|
||||
DPAD_UP("D-pad Up", false), DPAD_DOWN("D-pad Down", false), DPAD_LEFT("D-pad Left", false), DPAD_RIGHT("D-pad Right", false),
|
||||
LS_X("Left Stick X", true), LS_Y("Left Stick X", true), RS_X("Right Stick X", true), RS_Y("Left Stick Y", true),
|
||||
LS_X("Left Stick X", true), LS_Y("Left Stick Y", true), RS_X("Right Stick X", true), RS_Y("Right Stick Y", true),
|
||||
LS_THUMB("Left Stick Button", false), RS_THUMB("Right Stick Button", false),
|
||||
LT("Left Trigger", true), RT("Right Trigger", true), LB("Left Bumper", false), RB("Right Bumper", false),
|
||||
BTN_START("Start Button", false), BTN_BACK("Back Button", false), BTN_SPECIAL("Special Button", false);
|
||||
|
||||
@@ -86,11 +86,17 @@ public class ControllerListener {
|
||||
System.out.println("Stopping Controller Listener thread");
|
||||
listenerThread.interrupt();
|
||||
}
|
||||
if (GamepadHandler.isRunning()) {
|
||||
GamepadHandler.stopHandler();
|
||||
}
|
||||
}
|
||||
|
||||
public static void startSendingInput(NvConnection connection) {
|
||||
System.out.println("Starting to send controller input");
|
||||
conn = connection;
|
||||
if (!GamepadHandler.isRunning()) {
|
||||
GamepadHandler.startUp();
|
||||
}
|
||||
}
|
||||
|
||||
public static void stopSendingInput() {
|
||||
|
||||
@@ -10,7 +10,7 @@ import net.java.games.input.EventQueue;
|
||||
|
||||
public class Gamepad {
|
||||
private Controller pad;
|
||||
private GamepadSettings config;
|
||||
private GamepadMapping config;
|
||||
|
||||
private short inputMap = 0x0000;
|
||||
private byte leftTrigger = 0x00;
|
||||
@@ -20,7 +20,7 @@ public class Gamepad {
|
||||
private short leftStickX = 0x0000;
|
||||
private short leftStickY = 0x0000;
|
||||
|
||||
public Gamepad(Controller pad, GamepadSettings settings) {
|
||||
public Gamepad(Controller pad, GamepadMapping settings) {
|
||||
this.config = settings;
|
||||
this.pad = pad;
|
||||
|
||||
@@ -29,7 +29,7 @@ public class Gamepad {
|
||||
}
|
||||
}
|
||||
|
||||
public GamepadSettings getConfiguration() {
|
||||
public GamepadMapping getConfiguration() {
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -104,8 +104,8 @@ public class Gamepad {
|
||||
}
|
||||
|
||||
private void handleComponent(Component comp, float value) {
|
||||
if (config != null) {
|
||||
ControllerComponent contComp = config.getControllerComponent(comp);
|
||||
ControllerComponent contComp = config.getControllerComponent(comp);
|
||||
if (contComp != null) {
|
||||
if (contComp.isAnalog()) {
|
||||
handleAnalog(contComp, value);
|
||||
} else {
|
||||
|
||||
@@ -4,6 +4,8 @@ package com.limelight.input;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import com.limelight.nvstream.NvConnection;
|
||||
import com.limelight.settings.GamepadSettingsManager;
|
||||
@@ -12,19 +14,23 @@ import net.java.games.input.Controller;
|
||||
|
||||
public class GamepadHandler {
|
||||
private static LinkedList<Gamepad> gamepads = new LinkedList<Gamepad>();
|
||||
private static Lock gamepadLock = new ReentrantLock();
|
||||
private static NvConnection conn;
|
||||
private static Thread handler;
|
||||
|
||||
private static boolean run = true;
|
||||
|
||||
public static void addGamepads(List<Controller> pads) {
|
||||
|
||||
gamepads.clear();
|
||||
|
||||
LinkedList<Gamepad> newPadList = new LinkedList<Gamepad>();
|
||||
|
||||
GamepadSettings settings = GamepadSettingsManager.getSettings();
|
||||
GamepadMapping settings = GamepadSettingsManager.getSettings();
|
||||
for (Controller pad : pads) {
|
||||
|
||||
gamepads.add(new Gamepad(pad, settings));
|
||||
newPadList.add(new Gamepad(pad, settings));
|
||||
}
|
||||
|
||||
gamepadLock.lock();
|
||||
gamepads = newPadList;
|
||||
gamepadLock.unlock();
|
||||
}
|
||||
|
||||
public static void setConnection(NvConnection connection) {
|
||||
@@ -37,21 +43,29 @@ public class GamepadHandler {
|
||||
|
||||
public static void startUp() {
|
||||
if (handler == null || !handler.isAlive()) {
|
||||
run = true;
|
||||
System.out.println("Gamepad Handler thread starting up");
|
||||
handler = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
while (run) {
|
||||
try {
|
||||
gamepadLock.lockInterruptibly();
|
||||
} catch (InterruptedException e1) {
|
||||
run = false;
|
||||
}
|
||||
for (Gamepad gamepad : gamepads) {
|
||||
if (!gamepad.poll()) {
|
||||
break;
|
||||
}
|
||||
|
||||
gamepad.handleEvents(conn);
|
||||
}
|
||||
gamepadLock.unlock();
|
||||
try {
|
||||
Thread.sleep(20);
|
||||
} catch (InterruptedException e) {}
|
||||
} catch (InterruptedException e) {
|
||||
run = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -67,4 +81,8 @@ public class GamepadHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isRunning() {
|
||||
return (handler != null && handler.isAlive());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
40
src/com/limelight/input/GamepadMapping.java
Normal file
40
src/com/limelight/input/GamepadMapping.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.limelight.input;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import net.java.games.input.Component;
|
||||
|
||||
public class GamepadMapping implements Serializable {
|
||||
private static final long serialVersionUID = -185035113915743149L;
|
||||
|
||||
private HashMap<String, ControllerComponent> mapping;
|
||||
|
||||
public GamepadMapping() {
|
||||
mapping = new HashMap<String, ControllerComponent>();
|
||||
}
|
||||
|
||||
public void insertMapping(ControllerComponent contComp, Component comp) {
|
||||
mapping.put(comp.getIdentifier().getName(), contComp);
|
||||
}
|
||||
|
||||
public ControllerComponent getControllerComponent(Component comp) {
|
||||
return mapping.get(comp.getIdentifier().getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the mapping for the specified component.</br>
|
||||
* NOTE: Use sparingly takes O(N) time.
|
||||
* @param contComp the component to get a mapping for
|
||||
* @return a mapping or an empty string if there is none
|
||||
*/
|
||||
public String getMapping(ControllerComponent contComp) {
|
||||
for (Entry<String, ControllerComponent> entry : mapping.entrySet()) {
|
||||
if (entry.getValue().equals(contComp)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.limelight.input;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.java.games.input.Component;
|
||||
|
||||
public class GamepadSettings implements Serializable {
|
||||
private static final long serialVersionUID = -185035113915743149L;
|
||||
|
||||
private HashMap<ControllerComponent, Component> mapping;
|
||||
private HashMap<Component, ControllerComponent> inverseMapping;
|
||||
|
||||
public void insertSetting(ControllerComponent contComp, Component comp) {
|
||||
mapping.put(contComp, comp);
|
||||
inverseMapping.put(comp, contComp);
|
||||
}
|
||||
|
||||
public Component getComponent(ControllerComponent contComp) {
|
||||
return mapping.get(contComp);
|
||||
}
|
||||
|
||||
public ControllerComponent getControllerComponent(Component comp) {
|
||||
return inverseMapping.get(comp);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user