mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-04-17 05:50:07 +00:00
more gamepad stuff
- now have settings files - now read/write gamepad settings to file - now map button/axis for gamepads
This commit is contained in:
@@ -36,6 +36,8 @@ public class Limelight implements NvConnectionListener {
|
|||||||
PlatformBinding.getAudioRenderer(),
|
PlatformBinding.getAudioRenderer(),
|
||||||
PlatformBinding.getVideoDecoderRenderer());
|
PlatformBinding.getVideoDecoderRenderer());
|
||||||
|
|
||||||
|
ControllerListener.startSendingInput(conn);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startControllerListener() {
|
private static void startControllerListener() {
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import java.awt.Dimension;
|
|||||||
import java.awt.Toolkit;
|
import java.awt.Toolkit;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
import javax.swing.Box;
|
import javax.swing.Box;
|
||||||
import javax.swing.BoxLayout;
|
import javax.swing.BoxLayout;
|
||||||
@@ -20,10 +22,14 @@ import com.limelight.input.ControllerComponent;
|
|||||||
import com.limelight.input.Gamepad;
|
import com.limelight.input.Gamepad;
|
||||||
import com.limelight.input.GamepadHandler;
|
import com.limelight.input.GamepadHandler;
|
||||||
import com.limelight.input.GamepadSettings;
|
import com.limelight.input.GamepadSettings;
|
||||||
|
import com.limelight.settings.GamepadSettingsManager;
|
||||||
|
|
||||||
public class SettingsFrame extends JFrame {
|
public class SettingsFrame extends JFrame {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private boolean configChanged = false;
|
||||||
|
private GamepadSettings config;
|
||||||
|
|
||||||
public SettingsFrame() {
|
public SettingsFrame() {
|
||||||
super("Limelight Settings");
|
super("Limelight Settings");
|
||||||
this.setSize(800, 500);
|
this.setSize(800, 500);
|
||||||
@@ -50,11 +56,10 @@ public class SettingsFrame extends JFrame {
|
|||||||
componentBox.add(Box.createHorizontalStrut(10));
|
componentBox.add(Box.createHorizontalStrut(10));
|
||||||
componentBox.add(components[i].getLabel());
|
componentBox.add(components[i].getLabel());
|
||||||
componentBox.add(Box.createHorizontalGlue());
|
componentBox.add(Box.createHorizontalGlue());
|
||||||
componentBox.add(components[i].getTextField());
|
componentBox.add(components[i].getMapButton());
|
||||||
componentBox.add(Box.createHorizontalStrut(10));
|
componentBox.add(Box.createHorizontalStrut(10));
|
||||||
components[i].getTextField().setColumns(10);
|
components[i].getMapButton().setMaximumSize(new Dimension(50, 30));
|
||||||
components[i].getTextField().setMaximumSize(new Dimension(50, 30));
|
components[i].getMapButton().addActionListener(createListener());
|
||||||
components[i].getTextField().addActionListener(createListener());
|
|
||||||
if (i > components.length / 2) {
|
if (i > components.length / 2) {
|
||||||
rightColumn.add(componentBox);
|
rightColumn.add(componentBox);
|
||||||
if (i < components.length - 1) {
|
if (i < components.length - 1) {
|
||||||
@@ -80,6 +85,16 @@ public class SettingsFrame extends JFrame {
|
|||||||
|
|
||||||
c.add(mainPanel);
|
c.add(mainPanel);
|
||||||
|
|
||||||
|
this.addWindowListener(new WindowAdapter() {
|
||||||
|
@Override
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
super.windowClosing(e);
|
||||||
|
if (configChanged) {
|
||||||
|
updateConfigs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
|
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
@@ -92,7 +107,7 @@ public class SettingsFrame extends JFrame {
|
|||||||
//#allthejank
|
//#allthejank
|
||||||
ControllerComponent contComp = ControllerComponent.valueOf(((JTextField)e.getSource()).getName());
|
ControllerComponent contComp = ControllerComponent.valueOf(((JTextField)e.getSource()).getName());
|
||||||
|
|
||||||
contComp.getTextField().setText("Select Input");
|
contComp.getMapButton().setText("Select Input");
|
||||||
|
|
||||||
Gamepad listenPad = GamepadHandler.getGamepads().get(0);
|
Gamepad listenPad = GamepadHandler.getGamepads().get(0);
|
||||||
listenPad.poll();
|
listenPad.poll();
|
||||||
@@ -100,14 +115,21 @@ public class SettingsFrame extends JFrame {
|
|||||||
Event event = new Event();
|
Event event = new Event();
|
||||||
queue.getNextEvent(event);
|
queue.getNextEvent(event);
|
||||||
Component comp = event.getComponent();
|
Component comp = event.getComponent();
|
||||||
contComp.getTextField().setText(comp.getName());
|
contComp.getMapButton().setText(comp.getName());
|
||||||
|
|
||||||
GamepadSettings config = listenPad.getConfiguration();
|
config = listenPad.getConfiguration();
|
||||||
|
if (config == null) {
|
||||||
|
config = new GamepadSettings();
|
||||||
|
}
|
||||||
|
|
||||||
config.insertSetting(contComp, comp);
|
config.insertSetting(contComp, comp);
|
||||||
|
|
||||||
|
configChanged = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateConfigs() {
|
||||||
|
GamepadSettingsManager.writeSettings(config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package com.limelight.input;
|
package com.limelight.input;
|
||||||
|
|
||||||
import javax.swing.JLabel;
|
import java.io.Serializable;
|
||||||
import javax.swing.JTextField;
|
|
||||||
|
|
||||||
public enum ControllerComponent {
|
import javax.swing.JButton;
|
||||||
|
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),
|
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),
|
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 X", true), RS_X("Right Stick X", true), RS_Y("Left Stick Y", true),
|
||||||
@@ -12,14 +14,13 @@ public enum ControllerComponent {
|
|||||||
BTN_START("Start Button", false), BTN_BACK("Back Button", false), BTN_SPECIAL("Special Button", false);
|
BTN_START("Start Button", false), BTN_BACK("Back Button", false), BTN_SPECIAL("Special Button", false);
|
||||||
|
|
||||||
private JLabel label;
|
private JLabel label;
|
||||||
private JTextField textBox;
|
private JButton mapButton;
|
||||||
private boolean analog;
|
private boolean analog;
|
||||||
|
|
||||||
private ControllerComponent(String name, boolean analog) {
|
private ControllerComponent(String name, boolean analog) {
|
||||||
this.label = new JLabel(name);
|
this.label = new JLabel(name);
|
||||||
this.textBox = new JTextField();
|
this.mapButton = new JButton();
|
||||||
this.textBox.setEditable(false);
|
this.mapButton.setName(this.name());
|
||||||
this.textBox.setName(this.name());
|
|
||||||
this.analog = analog;
|
this.analog = analog;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,8 +28,8 @@ public enum ControllerComponent {
|
|||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JTextField getTextField() {
|
public JButton getMapButton() {
|
||||||
return textBox;
|
return mapButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAnalog() {
|
public boolean isAnalog() {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public class ControllerListener {
|
|||||||
*/
|
*/
|
||||||
public static boolean startUp() {
|
public static boolean startUp() {
|
||||||
if (listenerThread == null || !listenerThread.isAlive()) {
|
if (listenerThread == null || !listenerThread.isAlive()) {
|
||||||
|
System.out.println("Controller Listener thread starting up");
|
||||||
listenerThread = new Thread() {
|
listenerThread = new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -37,12 +38,13 @@ public class ControllerListener {
|
|||||||
construct = defEnv.getDeclaredConstructor();
|
construct = defEnv.getDeclaredConstructor();
|
||||||
construct.setAccessible(true);
|
construct.setAccessible(true);
|
||||||
|
|
||||||
|
ControllerEnvironment defaultEnv = null;
|
||||||
|
|
||||||
|
//TODO: allow "rescanning" to work again
|
||||||
|
defaultEnv = (ControllerEnvironment)construct.newInstance();
|
||||||
|
|
||||||
while(!isInterrupted()) {
|
while(!isInterrupted()) {
|
||||||
|
|
||||||
ControllerEnvironment defaultEnv = null;
|
|
||||||
|
|
||||||
defaultEnv = (ControllerEnvironment)construct.newInstance();
|
|
||||||
|
|
||||||
Controller[] ca = defaultEnv.getControllers();
|
Controller[] ca = defaultEnv.getControllers();
|
||||||
LinkedList<Controller> gamepads = new LinkedList<Controller>();
|
LinkedList<Controller> gamepads = new LinkedList<Controller>();
|
||||||
|
|
||||||
@@ -60,9 +62,7 @@ public class ControllerListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GamepadHandler.addGamepads(gamepads);
|
GamepadHandler.addGamepads(gamepads);
|
||||||
if (conn != null) {
|
GamepadHandler.setConnection(conn);
|
||||||
GamepadHandler.setConnection(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
@@ -83,12 +83,19 @@ public class ControllerListener {
|
|||||||
|
|
||||||
public static void stopListening() {
|
public static void stopListening() {
|
||||||
if (listenerThread != null && listenerThread.isAlive()) {
|
if (listenerThread != null && listenerThread.isAlive()) {
|
||||||
|
System.out.println("Stopping Controller Listener thread");
|
||||||
listenerThread.interrupt();
|
listenerThread.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void startSendingInput(NvConnection connection) {
|
public static void startSendingInput(NvConnection connection) {
|
||||||
|
System.out.println("Starting to send controller input");
|
||||||
conn = connection;
|
conn = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void stopSendingInput() {
|
||||||
|
System.out.println("Stopping sending controller input");
|
||||||
|
conn = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,11 +104,13 @@ public class Gamepad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleComponent(Component comp, float value) {
|
private void handleComponent(Component comp, float value) {
|
||||||
ControllerComponent contComp = config.getControllerComponent(comp);
|
if (config != null) {
|
||||||
if (contComp.isAnalog()) {
|
ControllerComponent contComp = config.getControllerComponent(comp);
|
||||||
handleAnalog(contComp, value);
|
if (contComp.isAnalog()) {
|
||||||
} else {
|
handleAnalog(contComp, value);
|
||||||
handleButtons(contComp, value);
|
} else {
|
||||||
|
handleButtons(contComp, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.limelight.nvstream.NvConnection;
|
import com.limelight.nvstream.NvConnection;
|
||||||
|
import com.limelight.settings.GamepadSettingsManager;
|
||||||
|
|
||||||
import net.java.games.input.Controller;
|
import net.java.games.input.Controller;
|
||||||
|
|
||||||
@@ -18,9 +19,11 @@ public class GamepadHandler {
|
|||||||
|
|
||||||
gamepads.clear();
|
gamepads.clear();
|
||||||
|
|
||||||
|
|
||||||
|
GamepadSettings settings = GamepadSettingsManager.getSettings();
|
||||||
for (Controller pad : pads) {
|
for (Controller pad : pads) {
|
||||||
|
|
||||||
gamepads.add(new Gamepad(pad, null)); //TODO: need to create/get the settings for this controller
|
gamepads.add(new Gamepad(pad, settings));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +37,7 @@ public class GamepadHandler {
|
|||||||
|
|
||||||
public static void startUp() {
|
public static void startUp() {
|
||||||
if (handler == null || !handler.isAlive()) {
|
if (handler == null || !handler.isAlive()) {
|
||||||
|
System.out.println("Gamepad Handler thread starting up");
|
||||||
handler = new Thread(new Runnable() {
|
handler = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -42,6 +46,7 @@ public class GamepadHandler {
|
|||||||
if (!gamepad.poll()) {
|
if (!gamepad.poll()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
gamepad.handleEvents(conn);
|
gamepad.handleEvents(conn);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -56,6 +61,7 @@ public class GamepadHandler {
|
|||||||
|
|
||||||
public static void stopHandler() {
|
public static void stopHandler() {
|
||||||
if (handler != null && handler.isAlive()) {
|
if (handler != null && handler.isAlive()) {
|
||||||
|
System.out.println("Stopping Gamepad Handler thread");
|
||||||
handler.interrupt();
|
handler.interrupt();
|
||||||
conn = null;
|
conn = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package com.limelight.input;
|
package com.limelight.input;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import net.java.games.input.Component;
|
import net.java.games.input.Component;
|
||||||
|
|
||||||
public class GamepadSettings {
|
public class GamepadSettings implements Serializable {
|
||||||
|
private static final long serialVersionUID = -185035113915743149L;
|
||||||
|
|
||||||
private HashMap<ControllerComponent, Component> mapping;
|
private HashMap<ControllerComponent, Component> mapping;
|
||||||
private HashMap<Component, ControllerComponent> inverseMapping;
|
private HashMap<Component, ControllerComponent> inverseMapping;
|
||||||
|
|
||||||
|
|||||||
83
src/com/limelight/settings/GamepadSettingsManager.java
Normal file
83
src/com/limelight/settings/GamepadSettingsManager.java
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package com.limelight.settings;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
|
import com.limelight.input.GamepadSettings;
|
||||||
|
|
||||||
|
public class GamepadSettingsManager {
|
||||||
|
private static GamepadSettings cachedSettings;
|
||||||
|
|
||||||
|
|
||||||
|
public static GamepadSettings getSettings() {
|
||||||
|
if (cachedSettings == null) {
|
||||||
|
File gamepadFile = SettingsManager.getInstance().getGamepadFile();
|
||||||
|
ObjectInputStream ois = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
ois = new ObjectInputStream(new FileInputStream(gamepadFile));
|
||||||
|
GamepadSettings savedSettings = (GamepadSettings)ois.readObject();
|
||||||
|
cachedSettings = savedSettings;
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.out.println("Saved file is not of the correct type. It might have been modified externally.");
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("Could not find gamepad settings file");
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Could not read gamepad settings file");
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (ois != null) {
|
||||||
|
try {
|
||||||
|
ois.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Could not close gamepad settings file");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cachedSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void writeSettings(GamepadSettings settings) {
|
||||||
|
cachedSettings = settings;
|
||||||
|
|
||||||
|
File gamepadFile = SettingsManager.getInstance().getGamepadFile();
|
||||||
|
ObjectOutputStream ous = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
ous = new ObjectOutputStream(new FileOutputStream(gamepadFile));
|
||||||
|
ous.writeObject(settings);
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("Could not find gamepad settings file");
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Could not write gamepad settings file");
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (ous != null) {
|
||||||
|
try {
|
||||||
|
ous.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Unable to close gamepad settings file");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
65
src/com/limelight/settings/SettingsManager.java
Normal file
65
src/com/limelight/settings/SettingsManager.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package com.limelight.settings;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class SettingsManager {
|
||||||
|
|
||||||
|
public static String SETTINGS_DIR = System.getProperty("user.home") + File.separator + "Limelight";
|
||||||
|
|
||||||
|
//directory to hold limelight settings
|
||||||
|
private File settingsDir;
|
||||||
|
|
||||||
|
private File settingsFile;
|
||||||
|
private File gamepadFile;
|
||||||
|
|
||||||
|
private static SettingsManager manager;
|
||||||
|
|
||||||
|
private SettingsManager() {
|
||||||
|
settingsFile = new File(SETTINGS_DIR + "settings");
|
||||||
|
gamepadFile = new File(SETTINGS_DIR + "gamepad");
|
||||||
|
settingsDir = new File(SETTINGS_DIR);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SettingsManager getInstance() {
|
||||||
|
if (manager == null) {
|
||||||
|
manager = new SettingsManager();
|
||||||
|
}
|
||||||
|
return manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getGamepadFile() {
|
||||||
|
if (!settingsDir.exists()) {
|
||||||
|
settingsFile.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gamepadFile.exists()) {
|
||||||
|
try {
|
||||||
|
gamepadFile.createNewFile();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Unable to create gamepad file");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return gamepadFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getSettingsFile() {
|
||||||
|
if (!settingsDir.exists()) {
|
||||||
|
settingsFile.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!settingsFile.exists()) {
|
||||||
|
try {
|
||||||
|
settingsFile.createNewFile();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Unable to create setting file");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return settingsFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user