aboutsummaryrefslogtreecommitdiffstats
path: root/coreAPI/src/java/net
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2012-11-22 06:39:20 +0100
committerSven Gothel <[email protected]>2012-11-22 06:39:20 +0100
commit49416164a4729b1b7e234841ba9be48b9aa90830 (patch)
tree3ca433f39dd4886de493ccd7046d83809540c980 /coreAPI/src/java/net
parentd0737cc4a34a41ad9bdac7de379250cacbf55a1d (diff)
parent5c894f5055dfdbf571afee3057e46b9912aedb9b (diff)
Merge remote-tracking branch 'conzar/detect'HEADmaster
Diffstat (limited to 'coreAPI/src/java/net')
-rw-r--r--coreAPI/src/java/net/java/games/input/ControllerEnvironment.java5
-rw-r--r--coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java65
2 files changed, 68 insertions, 2 deletions
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<Controller> controllers;
private Collection loadedPlugins = new ArrayList();
+ private ArrayList<ControllerEnvironment> environments = new ArrayList<ControllerEnvironment>();
/**
* 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<Controller>();
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<Controller> removeControllers = new ArrayList<Controller>();
+ // 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;