Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Issue detecting input devices  (Read 1366 times)
0 Members and 1 Guest are viewing this topic.
Offline Roos42

Junior Newbie





« Posted 2017-09-06 07:27:59 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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.

1  
2  
3  
4  
5  
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
1  
DirectAndRawInputEnvironmentPlugin DARIEP = new DirectAndRawInputEnvironmentPlugin();


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

« JGO Spiffy Duke »


Medals: 974
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2017-09-06 08:22:08 »

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 Smiley

Offline Roos42

Junior Newbie





« Reply #2 - Posted 2017-09-06 08:30:51 »

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?   Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online princec

« JGO Spiffy Duke »


Medals: 974
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2017-09-06 10:30:14 »

Does DirectAndRawInputEnvironmentPlugin.addControllerListener work?

Cas Smiley

Offline Roos42

Junior Newbie





« Reply #4 - Posted 2017-09-06 10:35:27 »

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
1  
DirectAndRawInputEnvironmentPlugin DARIEP = new DirectAndRawInputEnvironmentPlugin();
it seems to be a 'frozen' list of the connected devices and is not updated as devices gets hot plugged.
Online princec

« JGO Spiffy Duke »


Medals: 974
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2017-09-06 10:46:34 »

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 Smiley

Offline Roos42

Junior Newbie





« Reply #6 - Posted 2017-09-06 11:00:10 »

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!

 Smiley
Offline Roos42

Junior Newbie





« Reply #7 - Posted 2017-09-06 11:25:52 »

Okay, after abit of fidling I have the following code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
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.
Online princec

« JGO Spiffy Duke »


Medals: 974
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #8 - Posted 2017-09-06 11:44:20 »

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

Cas Smiley

Offline Roos42

Junior Newbie





« Reply #9 - Posted 2017-09-07 06:22:32 »

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
1  
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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online princec

« JGO Spiffy Duke »


Medals: 974
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2017-09-07 09:14:21 »

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

Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1569 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!