Merge branch 'master' of github.com:limelight-stream/limelight-pc

This commit is contained in:
Cameron Gutman
2013-12-30 12:13:10 -06:00
2 changed files with 45 additions and 48 deletions

View File

@@ -136,6 +136,7 @@ public class MainFrame {
JMenu optionsMenu = new JMenu("Options"); JMenu optionsMenu = new JMenu("Options");
JMenuItem gamepadSettings = new JMenuItem("Gamepad Settings"); JMenuItem gamepadSettings = new JMenuItem("Gamepad Settings");
JMenuItem generalSettings = new JMenuItem("Preferences"); JMenuItem generalSettings = new JMenuItem("Preferences");
JMenuItem scanForGamepads = new JMenuItem("Scan for Gamepads");
gamepadSettings.addActionListener(new ActionListener() { gamepadSettings.addActionListener(new ActionListener() {
@Override @Override
@@ -151,9 +152,17 @@ public class MainFrame {
} }
}); });
scanForGamepads.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GamepadListener.rescanControllers();
}
});
optionsMenu.add(gamepadSettings); optionsMenu.add(gamepadSettings);
optionsMenu.add(generalSettings); optionsMenu.add(generalSettings);
optionsMenu.add(scanForGamepads);
menuBar.add(optionsMenu); menuBar.add(optionsMenu);
return menuBar; return menuBar;

View File

@@ -1,9 +1,7 @@
package com.limelight.input.gamepad; package com.limelight.input.gamepad;
import java.lang.reflect.Field; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -20,8 +18,7 @@ import net.java.games.input.ControllerEnvironment;
public class GamepadListener { public class GamepadListener {
private static Thread listenerThread; private static Thread listenerThread;
private static NvConnection conn; private static NvConnection conn;
private static Field controllers; private static ControllerEnvironment defaultEnv;
private static Method scan;
/** /**
* starts a thread to listen to controllers * starts a thread to listen to controllers
@@ -36,31 +33,12 @@ public class GamepadListener {
listenerThread = new Thread() { listenerThread = new Thread() {
@Override @Override
public void run() { 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 get access to the private field caching the controllers
* and the method to scan for controllers
*/
try { try {
//#allthejank
ControllerEnvironment defaultEnv = ControllerEnvironment.getDefaultEnvironment(); defaultEnv = ControllerEnvironment.getDefaultEnvironment();
//must be called so the controllers array list will not be null
defaultEnv.getControllers();
controllers = defaultEnv.getClass().getDeclaredField("controllers");
controllers.setAccessible(true);
scan = defaultEnv.getClass().getDeclaredMethod("scanControllers", (Class[])null);
scan.setAccessible(true);
while(!isInterrupted()) { while(!isInterrupted()) {
rescanControllers(defaultEnv);
Controller[] ca = defaultEnv.getControllers(); Controller[] ca = defaultEnv.getControllers();
LinkedList<Controller> gamepads = new LinkedList<Controller>(); LinkedList<Controller> gamepads = new LinkedList<Controller>();
@@ -96,29 +74,39 @@ public class GamepadListener {
} }
return false; return false;
} }
/* /**
* Janky method that clears the controller environment's list of controllers and tells it to scan for new ones. * This is really janky, but it is currently the only way to rescan for controllers.
* #allthejank * 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 create a new instance to scan for controllers
*/ */
@SuppressWarnings("rawtypes") public static void rescanControllers() {
private static void rescanControllers(ControllerEnvironment defaultEnv) { try {
try { System.out.println("Rescanning for controllers...");
((ArrayList)controllers.get(defaultEnv)).clear(); Constructor<? extends ControllerEnvironment> envConst = ControllerEnvironment.getDefaultEnvironment().getClass().getDeclaredConstructor((Class[])null);
scan.invoke(defaultEnv, (Object[])null); envConst.setAccessible(true);
} catch (IllegalArgumentException e) { defaultEnv = (ControllerEnvironment) envConst.newInstance((Object[])null);
// TODO Auto-generated catch block } catch (SecurityException e) {
e.printStackTrace(); // TODO Auto-generated catch block
} catch (SecurityException e) { e.printStackTrace();
// TODO Auto-generated catch block } catch (NoSuchMethodException e) {
e.printStackTrace(); // TODO Auto-generated catch block
} catch (IllegalAccessException e) { e.printStackTrace();
// TODO Auto-generated catch block } catch (IllegalArgumentException e) {
e.printStackTrace(); // TODO Auto-generated catch block
} catch (InvocationTargetException e) { e.printStackTrace();
// TODO Auto-generated catch block } catch (InstantiationException e) {
e.printStackTrace(); // TODO Auto-generated catch block
} e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
/** /**