Cant get data to poll

I wrote a small class to implement jinput for joysticks into my code, its picking up the joysticks but the poll data never updates. I have run jtest and it works just fine. Can someone point out whats going wrong before this lib makes me what to hurl an input device? ???


import net.java.games.input.Controller;
import net.java.games.input.ControllerListener;
import net.java.games.input.ControllerEvent;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Component;

public class Joystick implements ControllerListener
{
    private Controller    Joystick_Controller = null;

    private Component[]   Joystick_Parts;
    private float[]       Joystick_oldState;

    public Joystick()
    {
        // add this module to controller listeners
        ControllerEnvironment.getDefaultEnvironment()
                .addControllerListener(this);

        // find a joystick
        processfindJoystick();
        
    }

    public void processfindJoystick()
    {
        this.Joystick_Controller = null;

        // poll a list of present controllers.
        Controller[] tempList = ControllerEnvironment.getDefaultEnvironment()
                .getControllers();

        for(Controller currentController: tempList)
            if(checkisJoystick(currentController))
            {
                setJoystick(currentController);
                return;
            }

        System.out.println("No joystick found.");
    }

    public void setJoystick(Controller newJoy)
    {
                this.Joystick_Controller = newJoy;
                this.Joystick_Parts      = newJoy.getComponents();
                this.Joystick_oldState   = new float[Joystick_Parts.length];


                System.out.println("Active Joystick set to: "
                        + this.Joystick_Controller.getName());
                
                this.Joystick_Controller.poll();

                for(int currentState = 0; currentState <
                        this.Joystick_oldState.length; currentState++)
                    this.Joystick_oldState[currentState] =
                            this.Joystick_Parts[currentState].getPollData();
    }

    public boolean checkisJoystick(Controller thisController)
    {
        if(thisController.getType() == Controller.Type.GAMEPAD ||
           thisController.getType() == Controller.Type.STICK)
            return true;

        return false;
    }

    public void controllerAdded(ControllerEvent ev)
    {
       //  attach to new joystick if theres not one already
        if(Joystick_Controller == null)
          if(checkisJoystick(ev.getController()))
              this.setJoystick(ev.getController());
    }

    public void controllerRemoved(ControllerEvent ev)
    {
       // if we loose the joystick, try to find another one.
        if(ev.getController() == Joystick_Controller)
            this.processfindJoystick();
    }

    public void Poll()
    {
        if(this.Joystick_Controller != null)
            if(!this.Joystick_Controller.poll())
                this.processfindJoystick();
        else
            return;

        for(int currentState = 0; currentState <
            this.Joystick_oldState.length; currentState++)
            if(this.Joystick_oldState[currentState] !=
                 this.Joystick_Controller.getComponents()[currentState].getPollData())
                   System.out.println(this.Joystick_Parts[currentState]
                           .getName() + " " + 
                        this.Joystick_Controller.getComponents()[currentState]
                           .getPollData());
                           
    }
}



If you mean the list of input devices never updating, then look at this thread:
http://www.java-gaming.org/index.php/topic,23964.0.html

I think they mean that the data itself never updates?, and I can’t see anything obvious at this time (spent a whole 20 seconds looking), you’re doing the poll on the controller and then getting the poll data, so it should work. Have you tried running the tests that come with JInput?

Endolf

Yes. Ive run the test module and it works fine. Button presses are displayed normally. When I run the code provided, the only output I get is the notice that my joystick has been found. However button/axis presses do not generate output.

Ok, that means it’s not JInput. Hmmmm

Ive swapped controllers, checked my security permissions, and tried a few other things. I noticed the module gives some errors when loading but so does the test module. And the test module still works correctly.

My console output from the code is:

Are you sure the device you are interested in is seen by JInput as a gamepad or stick type device?, check with the ControllerTextTest to see the type of the device.

HTH

Endolf

I changed several things in the code, and now it works well. I’m not really sure why, but I think I was either calling pollData() to many times instead of storing it to a variable, or because the function calling the poll of this module had a Synchronized keyword on it. But I rewrote it this way and it works now.

Also: I never did get controller connection/disconnection listener to work. That may be because im on a linux build


import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

public class Joystick
{
    private Controller    Joystick_Controller = null;

    private float[]       Joystick_oldpoll  = new float[20];

    public Joystick()
    {
        // find a joystick
        processfindJoystick();
        
    }

    public void processfindJoystick()
    {
        this.Joystick_Controller = null;

        // poll a list of present controllers.
        Controller[] tempList = ControllerEnvironment.getDefaultEnvironment()
                .getControllers();

        for(Controller currentController: tempList)
            if(checkisJoystick(currentController))
            {
                setJoystick(currentController);
                return;
            }

        System.out.println("No joystick found.");
    }

    public void setJoystick(Controller newJoy)
    {
                this.Joystick_Controller = newJoy;

                System.out.println("Active Joystick set to: "
                        + this.Joystick_Controller.getName());               
    }

    public boolean checkisJoystick(Controller thisController)
    {
        if(thisController.getType() == Controller.Type.GAMEPAD ||
           thisController.getType() == Controller.Type.STICK)
            if(thisController.poll())
             return true;

        return false;
    }

    public void poll()
    {
        if(this.Joystick_Controller != null)
            if(!this.Joystick_Controller.poll())
                this.processfindJoystick();


        if(this.Joystick_Controller == null)
        {
            System.out.println("No Joystick");
            return;
        }

        float tempPollData = 0.0f;
            
        for(int currentComp = 0;
        currentComp < this.Joystick_Controller.getComponents().length;
        currentComp++)
        {
            tempPollData = this.Joystick_Controller
                    .getComponents()[currentComp].getPollData();

            if(tempPollData != this.Joystick_oldpoll[currentComp])
            {
                System.out.println(this.Joystick_Controller.getComponents()
                        [currentComp].getName());

                this.Joystick_oldpoll[currentComp] = tempPollData;

            }
        }                           
    }
}

Seems my problem wasnt cleared up completely by the code change, only I am now more confused than before. I have two joysticks, which I am not plugging in at the same time. Both joysticks, when run with jtest respond correctly and fully. However with my code above, only one joystick responds correctly.

The first one a standard USB gravis gamepad, works fine in my code and jtest.

The second one, an adapter hub for ps2 controllers, works fully with jtest.
However under my code, its detected, and only one component axis change is detected, only right after the program starts.
I add a few lines to dump a list of all the detected controllers, and I tried plugging the ps2 controller into player 2’s slot to see
if I had the wrong controller. Again jtest works fine with it attached to player 1’s port on the adapter hub.

The axis change that is detected doesnt seem to be related to the controller, because I get the same output with not controller plugged into the hub after starting the program. I am confused as to why jtest would find the output and not this code,

Does it work with ControllerReadTest or ControllerEventTest?

Endolf

It works with both tests, heres the console output from text test.

and does it get axis updates?, just trying to make sure this is not a JInput issue, but a usage issue.

Endolf

yes, axis and buttons in jtest seem to update fine

Not jtest, ControllerReadTest or ControllerEventTest, not just displaying the names etc, but actual values when the controller changes. I’m trying to figure out of JInput is not detecting them, or if it’s the usage of JInput.

Cheers

Endolf