mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-04-24 00:56:42 +00:00
more controller stuff
-new controller listener to be a singleton thread that listens for controller input even when not streaming, then sends input when streaming.\ -main frame will stop controller listener when closing -gamepads should now use the gamepad config settings (WIP)
This commit is contained in:
@@ -1,19 +1,13 @@
|
|||||||
package com.limelight;
|
package com.limelight;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
|
|
||||||
import net.java.games.input.Controller;
|
|
||||||
import net.java.games.input.ControllerEnvironment;
|
|
||||||
|
|
||||||
import com.limelight.binding.PlatformBinding;
|
import com.limelight.binding.PlatformBinding;
|
||||||
import com.limelight.gui.MainFrame;
|
import com.limelight.gui.MainFrame;
|
||||||
import com.limelight.gui.StreamFrame;
|
import com.limelight.gui.StreamFrame;
|
||||||
import com.limelight.input.GamepadHandler;
|
import com.limelight.input.ControllerListener;
|
||||||
import com.limelight.nvstream.NvConnection;
|
import com.limelight.nvstream.NvConnection;
|
||||||
import com.limelight.nvstream.NvConnectionListener;
|
import com.limelight.nvstream.NvConnectionListener;
|
||||||
import com.limelight.nvstream.StreamConfiguration;
|
import com.limelight.nvstream.StreamConfiguration;
|
||||||
@@ -27,7 +21,6 @@ public class Limelight implements NvConnectionListener {
|
|||||||
private NvConnection conn;
|
private NvConnection conn;
|
||||||
private boolean connectionFailed;
|
private boolean connectionFailed;
|
||||||
private static JFrame limeFrame;
|
private static JFrame limeFrame;
|
||||||
private Thread controllerListenerThread;
|
|
||||||
private StreamConfiguration streamConfig = new StreamConfiguration(1280, 720, 30);
|
private StreamConfiguration streamConfig = new StreamConfiguration(1280, 720, 30);
|
||||||
|
|
||||||
public Limelight(String host) {
|
public Limelight(String host) {
|
||||||
@@ -45,69 +38,15 @@ public class Limelight implements NvConnectionListener {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startControllerListener() {
|
private static void startControllerListener() {
|
||||||
controllerListenerThread = new Thread() {
|
ControllerListener.startUp();
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This is really janky, but it is currently the only way to rescan for controllers.
|
|
||||||
* The DefaultControllerEnvironment class caches the results of scanning and if a controller is
|
|
||||||
* unplugged or plugged in, it will not detect it. Since DefaultControllerEnvironment is package-protected
|
|
||||||
* we have to use reflections in order to manually instantiate a new instance to ensure there is no caching.
|
|
||||||
* Supposedly Aaron is going to fix JInput and we will have the ability to rescan soon!
|
|
||||||
*/
|
|
||||||
try {
|
|
||||||
//#allthejank
|
|
||||||
Constructor<? extends ControllerEnvironment> construct = null;
|
|
||||||
|
|
||||||
Class<? extends ControllerEnvironment> defEnv = ControllerEnvironment.getDefaultEnvironment().getClass();
|
|
||||||
construct = defEnv.getDeclaredConstructor();
|
|
||||||
construct.setAccessible(true);
|
|
||||||
|
|
||||||
while(!isInterrupted()) {
|
|
||||||
|
|
||||||
ControllerEnvironment defaultEnv = null;
|
|
||||||
|
|
||||||
defaultEnv = (ControllerEnvironment)construct.newInstance();
|
|
||||||
|
|
||||||
Controller[] ca = defaultEnv.getControllers();
|
|
||||||
LinkedList<Controller> gamepads = new LinkedList<Controller>();
|
|
||||||
|
|
||||||
/*
|
|
||||||
* iterates through the controllers and adds gamepads and ps3 controller to the list
|
|
||||||
* NOTE: JInput does not consider a PS3 controller to be a gamepad (it thinks it's a "STICK") so we must use the
|
|
||||||
* name of it.
|
|
||||||
*/
|
|
||||||
for(int i = 0; i < ca.length; i++){
|
|
||||||
if (ca[i].getType() == Controller.Type.GAMEPAD) {
|
|
||||||
gamepads.add(ca[i]);
|
|
||||||
} else if (ca[i].getName().contains("PLAYSTATION")) {
|
|
||||||
gamepads.add(ca[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GamepadHandler.addGamepads(gamepads, conn);
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
controllerListenerThread.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createFrame() {
|
private static void createFrame() {
|
||||||
MainFrame main = new MainFrame();
|
MainFrame main = new MainFrame();
|
||||||
main.build();
|
main.build();
|
||||||
limeFrame = main.getLimeFrame();
|
limeFrame = main.getLimeFrame();
|
||||||
|
startControllerListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createInstance(String host, boolean fullscreen) {
|
public static void createInstance(String host, boolean fullscreen) {
|
||||||
@@ -156,7 +95,7 @@ public class Limelight implements NvConnectionListener {
|
|||||||
@Override
|
@Override
|
||||||
public void connectionStarted() {
|
public void connectionStarted() {
|
||||||
streamFrame.hideSpinner();
|
streamFrame.hideSpinner();
|
||||||
startControllerListener();
|
ControllerListener.startSendingInput(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -168,14 +107,6 @@ public class Limelight implements NvConnectionListener {
|
|||||||
// Kill the connection to the target
|
// Kill the connection to the target
|
||||||
conn.stop();
|
conn.stop();
|
||||||
|
|
||||||
// Kill the controller rescanning thread
|
|
||||||
if (controllerListenerThread != null) {
|
|
||||||
controllerListenerThread.interrupt();
|
|
||||||
try {
|
|
||||||
controllerListenerThread.join();
|
|
||||||
} catch (InterruptedException e1) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Spin off a new thread to update the UI since
|
// Spin off a new thread to update the UI since
|
||||||
// this thread has been interrupted and will terminate
|
// this thread has been interrupted and will terminate
|
||||||
// shortly
|
// shortly
|
||||||
|
|||||||
@@ -6,6 +6,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 java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
@@ -24,6 +26,7 @@ import org.xmlpull.v1.XmlPullParserException;
|
|||||||
|
|
||||||
import com.limelight.Limelight;
|
import com.limelight.Limelight;
|
||||||
import com.limelight.binding.PlatformBinding;
|
import com.limelight.binding.PlatformBinding;
|
||||||
|
import com.limelight.input.ControllerListener;
|
||||||
import com.limelight.nvstream.NvConnection;
|
import com.limelight.nvstream.NvConnection;
|
||||||
import com.limelight.nvstream.http.NvHTTP;
|
import com.limelight.nvstream.http.NvHTTP;
|
||||||
|
|
||||||
@@ -41,7 +44,13 @@ public class MainFrame {
|
|||||||
public void build() {
|
public void build() {
|
||||||
limeFrame = new JFrame("Limelight");
|
limeFrame = new JFrame("Limelight");
|
||||||
limeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
limeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
limeFrame.addWindowListener(new WindowAdapter() {
|
||||||
|
@Override
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
super.windowClosing(e);
|
||||||
|
ControllerListener.stopListening();
|
||||||
|
}
|
||||||
|
});
|
||||||
Container mainPane = limeFrame.getContentPane();
|
Container mainPane = limeFrame.getContentPane();
|
||||||
|
|
||||||
mainPane.setLayout(new BorderLayout());
|
mainPane.setLayout(new BorderLayout());
|
||||||
@@ -85,12 +94,25 @@ public class MainFrame {
|
|||||||
Box contentBox = Box.createVerticalBox();
|
Box contentBox = Box.createVerticalBox();
|
||||||
contentBox.add(Box.createVerticalStrut(20));
|
contentBox.add(Box.createVerticalStrut(20));
|
||||||
contentBox.add(hostBox);
|
contentBox.add(hostBox);
|
||||||
|
JButton settings = new JButton("Settings");
|
||||||
|
settings.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
new SettingsFrame().build();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
contentBox.add(settings);
|
||||||
contentBox.add(Box.createVerticalStrut(5));
|
contentBox.add(Box.createVerticalStrut(5));
|
||||||
contentBox.add(fullscreen);
|
contentBox.add(fullscreen);
|
||||||
contentBox.add(Box.createVerticalStrut(5));
|
contentBox.add(Box.createVerticalStrut(5));
|
||||||
contentBox.add(streamBox);
|
contentBox.add(streamBox);
|
||||||
contentBox.add(Box.createVerticalStrut(10));
|
contentBox.add(Box.createVerticalStrut(10));
|
||||||
contentBox.add(pairBox);
|
contentBox.add(pairBox);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
contentBox.add(Box.createVerticalGlue());
|
contentBox.add(Box.createVerticalGlue());
|
||||||
|
|
||||||
centerPane.add(contentBox);
|
centerPane.add(contentBox);
|
||||||
|
|||||||
@@ -4,21 +4,23 @@ import javax.swing.JLabel;
|
|||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
public enum ControllerComponent {
|
public enum ControllerComponent {
|
||||||
BTN_A("Button 1 (A)"), BTN_X("Button 2 (X)"), BTN_Y("Button 3 (Y)"), BTN_B("Button 4 (B)"),
|
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"), DPAD_DOWN("D-pad Down"), DPAD_LEFT("D-pad Left"), DPAD_RIGHT("D-pad Right"),
|
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"), LS_Y("Left Stick X"), RS_X("Right Stick X"), RS_Y("Left Stick Y"),
|
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_THUMB("Left Stick Button"), RS_THUMB("Right Stick Button"),
|
LS_THUMB("Left Stick Button", false), RS_THUMB("Right Stick Button", false),
|
||||||
LT("Left Trigger"), RT("Right Trigger"), LB("Left Bumper"), RB("Right Bumper"),
|
LT("Left Trigger", true), RT("Right Trigger", true), LB("Left Bumper", false), RB("Right Bumper", false),
|
||||||
BTN_START("Start Button"), BTN_BACK("Back Button"), BTN_SPECIAL("Special Button");
|
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 JTextField textBox;
|
||||||
|
private boolean analog;
|
||||||
|
|
||||||
private ControllerComponent(String name) {
|
private ControllerComponent(String name, boolean analog) {
|
||||||
this.label = new JLabel(name);
|
this.label = new JLabel(name);
|
||||||
this.textBox = new JTextField();
|
this.textBox = new JTextField();
|
||||||
this.textBox.setEditable(false);
|
this.textBox.setEditable(false);
|
||||||
this.textBox.setName(this.name());
|
this.textBox.setName(this.name());
|
||||||
|
this.analog = analog;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JLabel getLabel() {
|
public JLabel getLabel() {
|
||||||
@@ -28,4 +30,8 @@ public enum ControllerComponent {
|
|||||||
public JTextField getTextField() {
|
public JTextField getTextField() {
|
||||||
return textBox;
|
return textBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAnalog() {
|
||||||
|
return analog;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
94
src/com/limelight/input/ControllerListener.java
Normal file
94
src/com/limelight/input/ControllerListener.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package com.limelight.input;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import com.limelight.nvstream.NvConnection;
|
||||||
|
|
||||||
|
import net.java.games.input.Controller;
|
||||||
|
import net.java.games.input.ControllerEnvironment;
|
||||||
|
|
||||||
|
public class ControllerListener {
|
||||||
|
private static Thread listenerThread;
|
||||||
|
private static NvConnection conn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* starts a thread to listen to controllers
|
||||||
|
* @return true if it started a thread, false if the thread is already running.
|
||||||
|
*/
|
||||||
|
public static boolean startUp() {
|
||||||
|
if (listenerThread == null || !listenerThread.isAlive()) {
|
||||||
|
listenerThread = new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is really janky, but it is currently the only way to rescan for controllers.
|
||||||
|
* The DefaultControllerEnvironment class caches the results of scanning and if a controller is
|
||||||
|
* unplugged or plugged in, it will not detect it. Since DefaultControllerEnvironment is package-protected
|
||||||
|
* we have to use reflections in order to manually instantiate a new instance to ensure there is no caching.
|
||||||
|
* Supposedly Aaron is going to fix JInput and we will have the ability to rescan soon!
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
//#allthejank
|
||||||
|
Constructor<? extends ControllerEnvironment> construct = null;
|
||||||
|
|
||||||
|
Class<? extends ControllerEnvironment> defEnv = ControllerEnvironment.getDefaultEnvironment().getClass();
|
||||||
|
construct = defEnv.getDeclaredConstructor();
|
||||||
|
construct.setAccessible(true);
|
||||||
|
|
||||||
|
while(!isInterrupted()) {
|
||||||
|
|
||||||
|
ControllerEnvironment defaultEnv = null;
|
||||||
|
|
||||||
|
defaultEnv = (ControllerEnvironment)construct.newInstance();
|
||||||
|
|
||||||
|
Controller[] ca = defaultEnv.getControllers();
|
||||||
|
LinkedList<Controller> gamepads = new LinkedList<Controller>();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* iterates through the controllers and adds gamepads and ps3 controller to the list
|
||||||
|
* NOTE: JInput does not consider a PS3 controller to be a gamepad (it thinks it's a "STICK") so we must use the
|
||||||
|
* name of it.
|
||||||
|
*/
|
||||||
|
for(int i = 0; i < ca.length; i++){
|
||||||
|
if (ca[i].getType() == Controller.Type.GAMEPAD) {
|
||||||
|
gamepads.add(ca[i]);
|
||||||
|
} else if (ca[i].getName().contains("PLAYSTATION")) {
|
||||||
|
gamepads.add(ca[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GamepadHandler.addGamepads(gamepads);
|
||||||
|
if (conn != null) {
|
||||||
|
GamepadHandler.setConnection(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
listenerThread.start();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void stopListening() {
|
||||||
|
if (listenerThread != null && listenerThread.isAlive()) {
|
||||||
|
listenerThread.interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void startSendingInput(NvConnection connection) {
|
||||||
|
conn = connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,54 +1,36 @@
|
|||||||
package com.limelight.input;
|
package com.limelight.input;
|
||||||
|
|
||||||
import com.limelight.nvstream.NvConnection;
|
import com.limelight.nvstream.NvConnection;
|
||||||
|
import com.limelight.nvstream.input.ControllerPacket;
|
||||||
|
|
||||||
import net.java.games.input.Component;
|
import net.java.games.input.Component;
|
||||||
import net.java.games.input.Controller;
|
import net.java.games.input.Controller;
|
||||||
import net.java.games.input.Event;
|
import net.java.games.input.Event;
|
||||||
import net.java.games.input.EventQueue;
|
import net.java.games.input.EventQueue;
|
||||||
|
|
||||||
public abstract class Gamepad {
|
public class Gamepad {
|
||||||
protected Controller pad;
|
private Controller pad;
|
||||||
private NvConnection conn;
|
private GamepadSettings config;
|
||||||
|
|
||||||
protected short inputMap = 0x0000;
|
private short inputMap = 0x0000;
|
||||||
protected byte leftTrigger = 0x00;
|
private byte leftTrigger = 0x00;
|
||||||
protected byte rightTrigger = 0x00;
|
private byte rightTrigger = 0x00;
|
||||||
protected short rightStickX = 0x0000;
|
private short rightStickX = 0x0000;
|
||||||
protected short rightStickY = 0x0000;
|
private short rightStickY = 0x0000;
|
||||||
protected short leftStickX = 0x0000;
|
private short leftStickX = 0x0000;
|
||||||
protected short leftStickY = 0x0000;
|
private short leftStickY = 0x0000;
|
||||||
|
|
||||||
protected GamepadSettings configuration;
|
public Gamepad(Controller pad, GamepadSettings settings) {
|
||||||
|
this.config = settings;
|
||||||
public enum ControllerType { XBOX, PS3 };
|
|
||||||
|
|
||||||
|
|
||||||
public static Gamepad createInstance(NvConnection conn, Controller pad, ControllerType type) {
|
|
||||||
switch (type) {
|
|
||||||
case XBOX:
|
|
||||||
return new XBox360Controller(conn, pad);
|
|
||||||
case PS3:
|
|
||||||
return new PS3Controller(conn, pad);
|
|
||||||
default:
|
|
||||||
return new XBox360Controller(conn, pad);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Gamepad(NvConnection conn, Controller pad) {
|
|
||||||
this.conn = conn;
|
|
||||||
this.pad = pad;
|
this.pad = pad;
|
||||||
|
|
||||||
configuration = new GamepadSettings();
|
|
||||||
|
|
||||||
for (Component comp : pad.getComponents()) {
|
for (Component comp : pad.getComponents()) {
|
||||||
initValue(comp);
|
initValue(comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public GamepadSettings getConfiguration() {
|
public GamepadSettings getConfiguration() {
|
||||||
return configuration;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initValue(Component comp) {
|
private void initValue(Component comp) {
|
||||||
@@ -59,16 +41,18 @@ public abstract class Gamepad {
|
|||||||
return pad.poll();
|
return pad.poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendControllerPacket() {
|
private void sendControllerPacket(NvConnection conn) {
|
||||||
conn.sendControllerInput(inputMap, leftTrigger, rightTrigger,
|
if (conn != null) {
|
||||||
leftStickX, leftStickY, rightStickX, rightStickY);
|
conn.sendControllerInput(inputMap, leftTrigger, rightTrigger,
|
||||||
|
leftStickX, leftStickY, rightStickX, rightStickY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventQueue getEvents() {
|
public EventQueue getEvents() {
|
||||||
return pad.getEventQueue();
|
return pad.getEventQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleEvents() {
|
public void handleEvents(NvConnection conn) {
|
||||||
EventQueue queue = pad.getEventQueue();
|
EventQueue queue = pad.getEventQueue();
|
||||||
Event event = new Event();
|
Event event = new Event();
|
||||||
|
|
||||||
@@ -79,7 +63,7 @@ public abstract class Gamepad {
|
|||||||
|
|
||||||
handleEvent(event);
|
handleEvent(event);
|
||||||
|
|
||||||
sendControllerPacket();
|
sendControllerPacket(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -120,14 +104,15 @@ public abstract class Gamepad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleComponent(Component comp, float value) {
|
private void handleComponent(Component comp, float value) {
|
||||||
if (comp.isAnalog()) {
|
ControllerComponent contComp = config.getControllerComponent(comp);
|
||||||
handleAnalog(comp, value);
|
if (contComp.isAnalog()) {
|
||||||
|
handleAnalog(contComp, value);
|
||||||
} else {
|
} else {
|
||||||
handleButtons(comp, value);
|
handleButtons(contComp, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void toggle(short button, boolean press) {
|
private void toggle(short button, boolean press) {
|
||||||
if (press) {
|
if (press) {
|
||||||
inputMap |= button;
|
inputMap |= button;
|
||||||
} else {
|
} else {
|
||||||
@@ -135,6 +120,90 @@ public abstract class Gamepad {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void handleAnalog(Component comp, float value);
|
private void handleAnalog(ControllerComponent contComp, float value) {
|
||||||
protected abstract void handleButtons(Component comp, float value);
|
|
||||||
|
|
||||||
|
switch (contComp) {
|
||||||
|
case LS_X:
|
||||||
|
leftStickX = (short)Math.round(value * 0x7FFF);
|
||||||
|
break;
|
||||||
|
case LS_Y:
|
||||||
|
leftStickY = (short)Math.round(value * 0x7FFF);
|
||||||
|
break;
|
||||||
|
case RS_X:
|
||||||
|
leftStickX = (short)Math.round(value * 0x7FFF);
|
||||||
|
break;
|
||||||
|
case RS_Y:
|
||||||
|
rightStickY = (short)Math.round(value * 0x7FFF);
|
||||||
|
break;
|
||||||
|
case LT:
|
||||||
|
leftTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
|
||||||
|
break;
|
||||||
|
case RT:
|
||||||
|
rightTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("A mapping error has occured. Ignoring: " + contComp.name());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleButtons(ControllerComponent contComp, float value) {
|
||||||
|
boolean press = false;
|
||||||
|
|
||||||
|
if (value > 0.5F) {
|
||||||
|
press = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (contComp) {
|
||||||
|
case BTN_A:
|
||||||
|
toggle(ControllerPacket.A_FLAG, press);
|
||||||
|
break;
|
||||||
|
case BTN_X:
|
||||||
|
toggle(ControllerPacket.X_FLAG, press);
|
||||||
|
break;
|
||||||
|
case BTN_Y:
|
||||||
|
toggle(ControllerPacket.Y_FLAG, press);
|
||||||
|
break;
|
||||||
|
case BTN_B:
|
||||||
|
toggle(ControllerPacket.B_FLAG, press);
|
||||||
|
break;
|
||||||
|
case DPAD_UP:
|
||||||
|
toggle(ControllerPacket.UP_FLAG, press);
|
||||||
|
break;
|
||||||
|
case DPAD_DOWN:
|
||||||
|
toggle(ControllerPacket.DOWN_FLAG, press);
|
||||||
|
break;
|
||||||
|
case DPAD_LEFT:
|
||||||
|
toggle(ControllerPacket.LEFT_FLAG, press);
|
||||||
|
break;
|
||||||
|
case DPAD_RIGHT:
|
||||||
|
toggle(ControllerPacket.RIGHT_FLAG, press);
|
||||||
|
break;
|
||||||
|
case LS_THUMB:
|
||||||
|
toggle(ControllerPacket.LS_CLK_FLAG, press);
|
||||||
|
break;
|
||||||
|
case RS_THUMB:
|
||||||
|
toggle(ControllerPacket.RS_CLK_FLAG, press);
|
||||||
|
break;
|
||||||
|
case LB:
|
||||||
|
toggle(ControllerPacket.LB_FLAG, press);
|
||||||
|
break;
|
||||||
|
case RB:
|
||||||
|
toggle(ControllerPacket.RB_FLAG, press);
|
||||||
|
break;
|
||||||
|
case BTN_START:
|
||||||
|
toggle(ControllerPacket.PLAY_FLAG, press);
|
||||||
|
break;
|
||||||
|
case BTN_BACK:
|
||||||
|
toggle(ControllerPacket.BACK_FLAG, press);
|
||||||
|
break;
|
||||||
|
case BTN_SPECIAL:
|
||||||
|
toggle(ControllerPacket.SPECIAL_BUTTON_FLAG, press);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("A mapping error has occured. Ignoring: " + contComp.name());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,62 +5,60 @@ import java.util.Collections;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.swing.event.ListSelectionEvent;
|
|
||||||
|
|
||||||
import com.limelight.input.Gamepad.ControllerType;
|
|
||||||
import com.limelight.nvstream.NvConnection;
|
import com.limelight.nvstream.NvConnection;
|
||||||
|
|
||||||
import net.java.games.input.Controller;
|
import net.java.games.input.Controller;
|
||||||
|
|
||||||
public class GamepadHandler {
|
public class GamepadHandler {
|
||||||
private static LinkedList<Gamepad> gamepads = new LinkedList<Gamepad>();
|
private static LinkedList<Gamepad> gamepads = new LinkedList<Gamepad>();
|
||||||
private static GamepadHandler singleton;
|
private static NvConnection conn;
|
||||||
|
private static Thread handler;
|
||||||
|
|
||||||
public static void addGamepads(List<Controller> pads, NvConnection conn) {
|
public static void addGamepads(List<Controller> pads) {
|
||||||
if (singleton == null) {
|
|
||||||
singleton = new GamepadHandler();
|
|
||||||
singleton.startUp();
|
|
||||||
}
|
|
||||||
|
|
||||||
gamepads.clear();
|
gamepads.clear();
|
||||||
|
|
||||||
for (Controller pad : pads) {
|
for (Controller pad : pads) {
|
||||||
|
|
||||||
gamepads.add(Gamepad.createInstance(conn, pad, getType(pad)));
|
gamepads.add(new Gamepad(pad, null)); //TODO: need to create/get the settings for this controller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ControllerType getType(Controller pad) {
|
public static void setConnection(NvConnection connection) {
|
||||||
if (pad.getType() == Controller.Type.GAMEPAD) {
|
conn = connection;
|
||||||
return ControllerType.XBOX;
|
|
||||||
}
|
|
||||||
if (pad.getName().contains("PLAYSTATION")) {
|
|
||||||
return ControllerType.PS3;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Gamepad> getGamepads() {
|
public static List<Gamepad> getGamepads() {
|
||||||
return Collections.unmodifiableList(gamepads);
|
return Collections.unmodifiableList(gamepads);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startUp() {
|
public static void startUp() {
|
||||||
new Thread(new Runnable() {
|
if (handler == null || !handler.isAlive()) {
|
||||||
@Override
|
handler = new Thread(new Runnable() {
|
||||||
public void run() {
|
@Override
|
||||||
while (true) {
|
public void run() {
|
||||||
for (Gamepad gamepad : gamepads) {
|
while (true) {
|
||||||
if (!gamepad.poll()) {
|
for (Gamepad gamepad : gamepads) {
|
||||||
break;
|
if (!gamepad.poll()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
gamepad.handleEvents(conn);
|
||||||
}
|
}
|
||||||
gamepad.handleEvents();
|
try {
|
||||||
|
Thread.sleep(20);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
Thread.sleep(20);
|
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}).start();
|
handler.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void stopHandler() {
|
||||||
|
if (handler != null && handler.isAlive()) {
|
||||||
|
handler.interrupt();
|
||||||
|
conn = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,18 @@ import net.java.games.input.Component;
|
|||||||
|
|
||||||
public class GamepadSettings {
|
public class GamepadSettings {
|
||||||
private HashMap<ControllerComponent, Component> mapping;
|
private HashMap<ControllerComponent, Component> mapping;
|
||||||
|
private HashMap<Component, ControllerComponent> inverseMapping;
|
||||||
|
|
||||||
public void insertSetting(ControllerComponent contComp, Component comp) {
|
public void insertSetting(ControllerComponent contComp, Component comp) {
|
||||||
mapping.put(contComp, comp);
|
mapping.put(contComp, comp);
|
||||||
|
inverseMapping.put(comp, contComp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Component getComponent(ControllerComponent contComp) {
|
public Component getComponent(ControllerComponent contComp) {
|
||||||
return mapping.get(contComp);
|
return mapping.get(contComp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ControllerComponent getControllerComponent(Component comp) {
|
||||||
|
return inverseMapping.get(comp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
package com.limelight.input;
|
|
||||||
|
|
||||||
import com.limelight.nvstream.NvConnection;
|
|
||||||
import com.limelight.nvstream.input.ControllerPacket;
|
|
||||||
|
|
||||||
import net.java.games.input.Component;
|
|
||||||
import net.java.games.input.Controller;
|
|
||||||
|
|
||||||
public class PS3Controller extends Gamepad {
|
|
||||||
|
|
||||||
public PS3Controller(NvConnection conn, Controller pad) {
|
|
||||||
super(conn, pad);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void handleAnalog(Component comp, float value) {
|
|
||||||
Component.Identifier id = comp.getIdentifier();
|
|
||||||
|
|
||||||
if (id == Component.Identifier.Axis.Z) {
|
|
||||||
rightStickX = (short)Math.round(value * 0x7FFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.RZ) {
|
|
||||||
rightStickY = (short)Math.round(-value * 0x7FFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.X) {
|
|
||||||
leftStickX = (short)Math.round(value * 0x7FFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.Y) {
|
|
||||||
leftStickY = (short)Math.round(-value * 0x7FFF);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void handleButtons(Component comp, float value) {
|
|
||||||
Component.Identifier id = comp.getIdentifier();
|
|
||||||
boolean press = value > 0.5F;
|
|
||||||
|
|
||||||
if (id == Component.Identifier.Button._7) {
|
|
||||||
toggle(ControllerPacket.LEFT_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._5) {
|
|
||||||
toggle(ControllerPacket.RIGHT_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._4) {
|
|
||||||
toggle(ControllerPacket.UP_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._6) {
|
|
||||||
toggle(ControllerPacket.DOWN_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._14) {
|
|
||||||
toggle(ControllerPacket.A_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._15) {
|
|
||||||
toggle(ControllerPacket.X_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._12) {
|
|
||||||
toggle(ControllerPacket.Y_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._13) {
|
|
||||||
toggle(ControllerPacket.B_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._0) {
|
|
||||||
toggle(ControllerPacket.BACK_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._3) {
|
|
||||||
toggle(ControllerPacket.PLAY_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._2) {
|
|
||||||
toggle(ControllerPacket.RS_CLK_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._1) {
|
|
||||||
toggle(ControllerPacket.LS_CLK_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._10) {
|
|
||||||
toggle(ControllerPacket.LB_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._11) {
|
|
||||||
toggle(ControllerPacket.RB_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._16) {
|
|
||||||
toggle(ControllerPacket.SPECIAL_BUTTON_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._8) {
|
|
||||||
leftTrigger = (byte)Math.round((press ? 1 : 0) * 0xFF);
|
|
||||||
} else if (id == Component.Identifier.Button._9) {
|
|
||||||
rightTrigger = (byte)Math.round((press ? 1 : 0) * 0xFF);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
package com.limelight.input;
|
|
||||||
|
|
||||||
import net.java.games.input.Component;
|
|
||||||
import net.java.games.input.Controller;
|
|
||||||
|
|
||||||
import com.limelight.nvstream.NvConnection;
|
|
||||||
import com.limelight.nvstream.input.ControllerPacket;
|
|
||||||
|
|
||||||
public class XBox360Controller extends Gamepad {
|
|
||||||
|
|
||||||
public XBox360Controller(NvConnection conn, Controller pad) {
|
|
||||||
super(conn, pad);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void handleButtons(Component comp, float value) {
|
|
||||||
Component.Identifier id = comp.getIdentifier();
|
|
||||||
boolean press = value > 0.5F;
|
|
||||||
|
|
||||||
if (id == Component.Identifier.Button._13) {
|
|
||||||
toggle(ControllerPacket.LEFT_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._14) {
|
|
||||||
toggle(ControllerPacket.RIGHT_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._11) {
|
|
||||||
toggle(ControllerPacket.UP_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._12) {
|
|
||||||
toggle(ControllerPacket.DOWN_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._0) {
|
|
||||||
toggle(ControllerPacket.A_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._2) {
|
|
||||||
toggle(ControllerPacket.X_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._3) {
|
|
||||||
toggle(ControllerPacket.Y_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._1) {
|
|
||||||
toggle(ControllerPacket.B_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._9) {
|
|
||||||
toggle(ControllerPacket.BACK_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._8) {
|
|
||||||
toggle(ControllerPacket.PLAY_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._7) {
|
|
||||||
toggle(ControllerPacket.RS_CLK_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._6) {
|
|
||||||
toggle(ControllerPacket.LS_CLK_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._4) {
|
|
||||||
toggle(ControllerPacket.LB_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._5) {
|
|
||||||
toggle(ControllerPacket.RB_FLAG, press);
|
|
||||||
} else if (id == Component.Identifier.Button._10) {
|
|
||||||
toggle(ControllerPacket.SPECIAL_BUTTON_FLAG, press);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void handleAnalog(Component comp, float value) {
|
|
||||||
Component.Identifier id = comp.getIdentifier();
|
|
||||||
|
|
||||||
if (id == Component.Identifier.Axis.RX) {
|
|
||||||
rightStickX = (short)Math.round(value * 0x7FFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.RY) {
|
|
||||||
rightStickY = (short)Math.round(-value * 0x7FFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.X) {
|
|
||||||
leftStickX = (short)Math.round(value * 0x7FFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.Y) {
|
|
||||||
leftStickY = (short)Math.round(-value * 0x7FFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.Z) {
|
|
||||||
leftTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
|
|
||||||
} else if (id == Component.Identifier.Axis.RZ) {
|
|
||||||
rightTrigger = (byte)Math.round((value + 1) / 2 * 0xFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user