Rescan controllers

According this topic (http://www.java-gaming.org/index.php?topic=21694.0) and my own tests, the controller environnement could not rescan controllers.
I’m just getting the source and I wonder how many time it take to add this feature and if it is only java fix or I need to fix native part.

I need controller rescan so why not add it myself if you help me in explaining the main architecture :wink:

I made some patches to net.java.games.input.DirectInputEnvironmentPlugin and net.java.games.input.DefaultControllerEnvironment classes and it seems to work. (I am on Windows for now).
I need to improve cleaning device first and test again.

I will keep this post up to date if you are interested in :wink:

Hi

Definitely interested, it’s not something I’ve implemented myself as it’s not important to me, so to finally have someone who is bothered enough to actually implement it is great news.

Keep us updated on progress.

Endolf

I wonder why java API is frozen to 1.4 ? ???

O____o ?
What are you talking about?

The ant build script wants 1.4 java source :

<javac … source="1.4" target="1.4">

And yes, Sun Oracle has release Java 7 :wink:

Hi

This was originally done as there were embedded VMs that were running 1.4 that were being targeted by the sun game group, who did the original API work for JInput, Jogl and Joal. It’s not changed since then because nobody has been bothered enough by the restriction to go through the code base, plus the plugins and update anything that needed changing :). Until someone does, then the target version might as well stay at 1.4 :slight_smile:

Endolf

I’m interresting in rescan controllers too. I just have bumped into this problem :wink:
I’m doing a frontend for a acarde cabinet. So it start without keyboard or mouse, and time to time, they need to be connected/disconnected.

Yes, I succed in rescanning controller.

If I remember well, I only do it for Windows (my target platform) and in Java 6.
If you are interested in, may be I could make you a patch :wink:

Please do :), even if it’s only for 1 os, it would be good to have this submission.

Thanks

Endolf

It should nice ;D Thanks.

I paste you my patch here :

Index: coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java
===================================================================
--- coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java	(revision 247)
+++ coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java	(working copy)
@@ -169,6 +169,21 @@
 					e.printStackTrace();
 				}
 			}
+        } else {
+        	controllers = new ArrayList();
+        	for (Object ceName : loadedPlugins) {
+				try {
+					Class ceClass = Class.forName((String) ceName);
+					ControllerEnvironment ce = (ControllerEnvironment) ceClass.newInstance();
+					if (ce.isSupported()) {
+						addControllers(ce.getControllers());
+					} else {
+						logln(ceClass.getName() + " is not supported");
+					}
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+        	}
         }
         Controller[] ret = new Controller[controllers.size()];
         Iterator it = controllers.iterator();
Index: plugins/windows/src/java/net/java/games/input/DirectInputEnvironmentPlugin.java
===================================================================
--- plugins/windows/src/java/net/java/games/input/DirectInputEnvironmentPlugin.java	(revision 247)
+++ plugins/windows/src/java/net/java/games/input/DirectInputEnvironmentPlugin.java	(working copy)
@@ -111,28 +111,20 @@
 		}
 	}
 
-	private final Controller[] controllers;
+	private Controller[] controllers = new Controller[0];
 	private final List active_devices = new ArrayList();
 	private final DummyWindow window;
 
 	/** Creates new DirectInputEnvironment */
 	public DirectInputEnvironmentPlugin() {		
 		DummyWindow window = null;
-		Controller[] controllers = new Controller[]{};
 		if(isSupported()) {
 			try {
 				window = new DummyWindow();
-				try {
-					controllers = enumControllers(window);
-				} catch (IOException e) {
-					window.destroy();
-					throw e;
-				}
 			} catch (IOException e) {
 				logln("Failed to enumerate devices: " + e.getMessage());
 			}
 			this.window = window;
-			this.controllers = controllers;
 			AccessController.doPrivileged(
 					new PrivilegedAction() {
 						public final Object run() {
@@ -144,12 +136,23 @@
 			// These are final fields, so can't set them, then over ride 
 			// them if we are supported.
 			this.window = null;
-			this.controllers = controllers;
 		}
 	}
 
 	public final Controller[] getControllers() {
-		return controllers;
+		if (this.window != null) {
+			try {
+				try {
+					this.controllers = this.enumControllers(this.window);
+				} catch (IOException e) {
+					this.window.destroy();
+					throw e;
+				}
+			} catch (IOException e) {
+				logln("Failed to enumerate devices: " + e.getMessage());
+			}
+		}
+		return this.controllers;
 	}
 
 	private final Component[] createComponents(IDirectInputDevice device, boolean map_mouse_buttons) {

As you could see, I only upgrade DirectInputEnvironnementPlugin (and DefaultControllerEnvironnement ::))
May be you will need to change the implicit for-each for Java 1.4.

The patch works well for me. I suppose it could be applied to other platforms.