Controller input / Handling when controllers are added or removed

Started by TLH, August 07, 2012, 14:22:22

Previous topic - Next topic

TLH

Greetings chaps!

I have controller input working just fine, including the ability to choose between connected controllers and to re-map/save settings per-controller. The list of connected controllers is correctly detected at launch, but I've made two different (though probably somewhat dependent) attempts to detect controllers being added/removed and either way it acts as if no change was made.

I'm using net.java.games.input.ControllerEnvironment, and the symptoms are identical for each of the following objects:
  • ControllerEnvironment.getDefaultEnvironment()
  • WinTabEnvironmentPlugin.getDefaultEnvironment()
  • RawInputEnvironmentPlugin.getDefaultEnvironment()
  • LWJGLEnvironmentPlugin.getDefaultEnvironment()
Method 1: Calling ControllerEnvironment.getControllers()
This is returning a useful Controller[] which I then filter to pick out the relevant types (e.g. Controller.Type.STICK) and save them in my own list, but the returned array is always the same regardless of adding/removing devices, acting as if it is defined once when the program starts then never updated.

Method 2: Adding a ControllerListener with ControllerEnvironment.addControllerListener(ControllerListener)
ControllerListener is an interface with the methods:
  • void controllerAdded(ControllerEvent)
  • void controllerRemoved(ControllerEvent)
Neither of which ever fire when connecting or disconnecting USB controllers.

I am able to 'detect' the currently-selected controller being removed simply by checking for Controller.poll() returning false then handle the removal smoothly, which I'll probably leave in as a sort of fail-safe, though the general problem stands.

Does one need to call some sort of global refresh before ControllerEnvironment objects will register a change?
If there's no obvious omission or cause then I'll make a self-contained example :)

Thanks in advance for any assistance!

Klemss

Controller[] rawsCon;

DirectAndRawInputEnvironmentPlugin directEnv = new DirectAndRawInputEnvironmentPlugin();
if (directEnv.isSupported()) {
	rawsCon = directEnv.getControllers();
} else {
	rawsCon = ControllerEnvironment.getDefaultEnvironment().getControllers();
}


I use this code personally. It doesn't work on linux though (hence the need to check if the feature is supported).
But it's more of a JInput question I think, you would have more luck on the JInput forum.

There are no event listener yet, you have to do it yourself (doing it for my game was a pain in the ass actually, but more because it's a multiplayer game).

TLH

Ah! Thanks for those tips; I think I've pieced everything together into something acceptable now.

The main difference was the act of re-instantiating DirectAndRawInputEnvironmentPlugin to get a proper refresh (which you are doing, but I was not) as per this thread I found:
Quotein the latest build you can create an instance of DirectAndRawInputEnvironmentPlugin every time you want to know what is plugged in. then use the getControllers() method and so on.

To cover platforms that don't support DirectAndRawInputEnvironmentPlugin, I think I'll instead make it fetch the controller list only once and hide the refresh option, since the only possible change that could occur when refreshing from the default environment is to re-add 'ghost' controllers that were removed from the app's list via a poll failure.