Issue detecting input devices

Hello,
I have a strange issue when I attempt to detect input devices; specifically I want to detect a gamepad when it is connected. To do this I have the following code:


private boolean connectGamePad(){
		Controller[] ca;
		DirectAndRawInputEnvironmentPlugin DARIEP = new DirectAndRawInputEnvironmentPlugin();	//Rebuild list of connected devices
		ca = DARIEP.getControllers();	//Acquire device list
		
		for(int i = 0; i < ca.length; i ++){
			if(ca[i].getType().equals(Controller.Type.GAMEPAD))
			{	// Acquire the first instance of a gamepad.
				gamepad = ca[i];
				gamepadButtons = gamepad.getComponents();
				return true;
			}
		}
		return false;
	}

This function is invoked repeatetly untill it returns true, meaning it will attempt to find a gamepad untill one is connected. This works flawlessly for the first several minutes while my program is running, and will detect a gamepad if it is connected after the program is started.
However, after several minutes, JInput starts printing an error and will no longer detect a gamepad if this is connected to the PC while the program is executing. The error can be seen below.


sep. 06, 2017 9:22:13 AM net.java.games.input.ControllerEnvironment log
INFO: Failed to enumerate devices: Failed to create window (1158)

sep. 06, 2017 9:22:13 AM net.java.games.input.ControllerEnvironment log
INFO: Failed to enumerate devices: Failed to create window (1158)

As far as I can figure, the error is printed when I run the following line

DirectAndRawInputEnvironmentPlugin DARIEP = new DirectAndRawInputEnvironmentPlugin();

Any idea of what is causing this would be appreciated. Thanks in advance

DirectAndRawInputEnvironmentPlugin is likely creating native resources - the window mentioned in the error message. However DirectAndRawInputEnvironmentPlugin is not actually cleaned up; it’s left on the heap. After a few minutes you’ll have 1000000 of them on the heap, each one having created new native resources that haven’t been released.

Even when/if GCed I suspect it is unlikely that it is being correctly cleaned up as I bet this isn’t a use case that was expected.

What happens if you just create one instance of the DirectAndRawInputEnvironmentPlugin and reuse it every call?

Cas :slight_smile:

Thanks for your input, I had considered that actually, tried invoking the garbage collector after the function, but as expected it did little to help me. The reason I am recreating the instance is that without a new instance I can’t seem to detect devices being plugged in, as far as I gather, it maintains an instantanous list of connected devices.

If there is an alternative way to force JInput to update connected devices I guess this could solve it? :slight_smile:

Does DirectAndRawInputEnvironmentPlugin.addControllerListener work?

Cas :slight_smile:

Not sure if my understanding is correct, but does that not listen for changes to already connected devices, or does it actually fire and event in case the list of devices changes? My issues arises form the fact that when I invoke

DirectAndRawInputEnvironmentPlugin DARIEP = new DirectAndRawInputEnvironmentPlugin();

it seems to be a ‘frozen’ list of the connected devices and is not updated as devices gets hot plugged.

I was thinking that the docco here
http://static.javadoc.io/net.java.jinput/jinput/2.0.7/net/java/games/input/ControllerListener.html#controllerAdded-net.java.games.input.ControllerEvent-
implied that’s exactly what it was supposed to do. Perhaps it’s just a bug if it’s not working?

Cas :slight_smile:

Ah I see - seems like it should work then, will require abit of rethinking of my class by I will look into it and get back here with a results; Thanks!

:slight_smile:

Okay, after abit of fidling I have the following code


private void connectGamePad(){
		controllerEnvironment.addControllerListener(new ControllerListener(){
			@Override
			public void controllerAdded(ControllerEvent arg0) {
				System.out.println("ADDED");
				Controller[] ca = controllerEnvironment.getControllers();
				for(int i = 0; i < ca.length; i ++){
					if(ca[i].getType().equals(Controller.Type.GAMEPAD))
					{	// Acquire the first instance of a gamepad.
						gamepad = ca[i];
						gamepadButtons = gamepad.getComponents();
						gamePadState = true;
						return;
					}
				}
				gamePadState = false;
			}

			@Override
			public void controllerRemoved(ControllerEvent arg0) {
				System.out.println("REMOVED");
				Controller[] ca = controllerEnvironment.getControllers();
				for(int i = 0; i < ca.length; i ++){
					if(ca[i].getType().equals(Controller.Type.GAMEPAD))
					{	// Acquire the first instance of a gamepad.
						gamepad = ca[i];
						gamepadButtons = gamepad.getComponents();
						gamePadState = true;
						return;
					}
				}
				gamePadState = false;
			}
		});
		
		Controller[] ca = controllerEnvironment.getControllers();
		for(int i = 0; i < ca.length; i ++){
			if(ca[i].getType().equals(Controller.Type.GAMEPAD))
			{	// Acquire the first instance of a gamepad.
				gamepad = ca[i];
				gamepadButtons = gamepad.getComponents();
				gamePadState = true;
			}
		}
	}

After registering the Listener, I go through the currently connected devices to see if a gamepad is already connected, this part works. However, when plugging and unplugging the gamepad the events do not seem to fire. To ensure that the GC dont come around and clean my memory, I have changed controllerEnvironment to a global variable.

I suppose if the events never fire then that’s a JInput bug. Which could do with fixing… @Endolf!

Cas :slight_smile:

Well either that or im doing something wrong with registering the listeners, but can’t seem to fint it. Just out of curiosity, is there from the library side a difference to creating and instance of DirectAndRawInputEnvironmentPlugin as oposed to statically calling function in ControllerEnvironment?

Originally I worked with the library, finding the controllers with the following line

ControllerEnvironment.getDefaultEnvironment().getControllers()

I found that this list was not updated when I recalled it, so I changed to creating new instances of DirectAndRawInputEnvironmentPlugin which worked (ish).

Anyway, if this is an error in the library I will cross my fingers for a fix. It is still maintained?

Thanks for your help so far.

@Endolf of these parts used to maintain it didn’t he?

Cas :slight_smile: