From 5c894f5055dfdbf571afee3057e46b9912aedb9b Mon Sep 17 00:00:00 2001 From: Michael Speth Date: Fri, 26 Oct 2012 00:28:20 +1300 Subject: Applied the patch and seems to work after testing. --- .../java/games/input/ControllerEnvironment.java | 5 ++ .../games/input/DefaultControllerEnvironment.java | 65 +++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) (limited to 'coreAPI/src/java') diff --git a/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java b/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java index 17208f7..3a7c21e 100644 --- a/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java +++ b/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java @@ -100,6 +100,11 @@ public abstract class ControllerEnvironment { * or an empty array if there are no controllers in this environment. */ public abstract Controller[] getControllers(); + /** + * Rescans the devices and provides a list of new controllers. + * @return a list of all controllers available to this environment. + */ + public abstract Controller[] rescanControllers(); /** * Adds a listener for controller state change events. diff --git a/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java b/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java index 431a9ff..b193ef0 100644 --- a/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java +++ b/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java @@ -105,9 +105,10 @@ class DefaultControllerEnvironment extends ControllerEnvironment { /** * List of all controllers in this environment */ - private ArrayList controllers; + private ArrayList controllers; private Collection loadedPlugins = new ArrayList(); + private ArrayList environments = new ArrayList(); /** * Public no-arg constructor. @@ -122,7 +123,7 @@ class DefaultControllerEnvironment extends ControllerEnvironment { public Controller[] getControllers() { if (controllers == null) { // Controller list has not been scanned. - controllers = new ArrayList(); + controllers = new ArrayList(); AccessController.doPrivileged(new PrivilegedAction() { public Object run() { scanControllers(); @@ -159,6 +160,7 @@ class DefaultControllerEnvironment extends ControllerEnvironment { Class ceClass = Class.forName(className); ControllerEnvironment ce = (ControllerEnvironment) ceClass.newInstance(); if(ce.isSupported()) { + environments.add(ce); addControllers(ce.getControllers()); loadedPlugins.add(ce.getClass().getName()); } else { @@ -170,6 +172,65 @@ class DefaultControllerEnvironment extends ControllerEnvironment { } } } + if(!environments.isEmpty()){ + Controller[] newScanControllers = environments.get(0).getControllers(); + Controller[] ret = new Controller[newScanControllers.length]; + for(int i = 0; i < newScanControllers.length; i++){ + ret[i] = newScanControllers[i]; + } + return ret; + } + + Controller[] ret = new Controller[controllers.size()]; + Iterator it = controllers.iterator(); + int i = 0; + while (it.hasNext()) { + ret[i] = (Controller)it.next(); + i++; + } + return ret; + } + + /** + * Returns a list of all controllers available to this environment, + * or an empty array if there are no controllers in this environment. + */ + public Controller[] rescanControllers() { + if(!environments.isEmpty()){ + Controller[] newScanControllers = environments.get(0).rescanControllers(); + // need to add controllers that were connected + for(int i = 0; i < newScanControllers.length; i++){ + boolean controllerExist = false; + for(Controller controller:controllers){ + if(newScanControllers[i] == controller){ + controllerExist = true; + break; + } + } + if(!controllerExist){ + controllers.add(newScanControllers[i]); + } + } + ArrayList removeControllers = new ArrayList(); + // need to remove controllers that have disconnected + for(Controller controller:controllers){ + boolean controllerExist = false; + for(int i = 0; i < newScanControllers.length; i++){ + if(controller == newScanControllers[i]){ + controllerExist = true; + break; + } + } + if(!controllerExist){ + //controllers.remove(controller); + removeControllers.add(controller); + } + } + for(Controller controller: removeControllers){ + controllers.remove(controller); + } + } + Controller[] ret = new Controller[controllers.size()]; Iterator it = controllers.iterator(); int i = 0; -- cgit v1.2.3