Controller connection state not properly recognized

Hi all.

I’m developing a program to control a robot with a gamepad as a graduation task. It works well under a controlled enviroment, but I want to make the program be aware whether the controller is connected or not. The code is not running so well, and I’m not too good at debugging with NetBeans, so I want to know if you can help me with this. The only conclusion I could gather is that he keeps doing what he was doing before after I connected/disconnected the controller, but he will know if the controller is connected or not at the beginning of execution.


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Tela extends javax.swing.JFrame {
    public Tela() {
        // initialization
        addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                pollTimer.stop();
                System.exit(0);
            }
        });
        startPolling();
    }
    private void startPolling(){
        ActionListener pollPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent e){
                if(controle.checa() == 1){ // returns the number of Controller.Type.STICK or Controller.Type.GAMEPAD controllers connected
                    // interface update
                }else if(controle.checa() == 0){
                    // prints that there are no controllers
                }else{
                    // prints that there are too much controllers
                }
            }
        };
        pollTimer = new Timer(DELAY, pollPerformer);
        pollTimer.start();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> // NetBeans generated code for interface elements
    // </editor-fold>                        
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tela().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify // NetBeans generated code for interface elements
    // End of variables declaration
    private Controle controle = new Controle(); // class wrapped around JInput
    private Timer pollTimer;
    private static final int DELAY = 20;
}

While I’ve only been here shortly, and not in depth, I think that JInput not detecting if a controller was unplugged or plugged was a recurring problem. So far as I can tell (someone can correct me if I’m wrong) this thread has a solution with the last post also being a solution which no one has expanded on though.

Hi

Yup, it’s a feature that people make a lot of noise about until I ask for a volunteer to implement it properly. As pointed out, there is a quick and dirty way to reset the controllers on windows.

Endolf

Yes thanks a lot that works! I’m using it like that:

private Controller cs[],ctrl;
cs = new DirectAndRawInputEnvironmentPlugin().getControllers();
// for() loop to assign the desired controller to ctrl

The problem is that it’s too damn slow. I’ll see what I can do to speed it up, either using another timer for the enviroment check or catching the “Failed to poll device” exception. By the way, is that an exception I can catch? Who throws it?

That doesn’t work for me…
I’m on linux and if I use

new DirectAndRawInputEnvironmentPlugin().getControllers();

I only get an empty Controller[]…sad
No hot plug for me :smiley:

You need

new LinuxEnvironmentPlugin.getControllers()

Be warned though, you might end up with issues as the controller files are already open. You could loop through all your old controller objects, cast them to LinuxDevice and then call close on it so they can be reopened. At your own risk of course :slight_smile:

Endolf

thanks a lot endolf :wink: