Controllers - should these work in the sandbox?

If they should, then some extra bits are required in org.lwjgl.input.Controllers :slight_smile:

Checked on PC only so far. Got to do something else so will check on Mac later


import java.security.AccessController;
import java.security.PrivilegedAction;

...

	public static void create() throws LWJGLException {
		if (created)
			return;
		
		try {
			net.java.games.input.Controller[] found = (net.java.games.input.Controller[])
                            AccessController.doPrivileged(new PrivilegedAction() {
                                public Object run() {
                                    String plugins = System.getProperty("jinput.plugins");			
                                    if (plugins == null) {
                                        if (System.getProperty("os.name").startsWith("Win"))
                                                System.setProperty("jinput.plugins",WINDOWS_PLUGIN);
                                        if (System.getProperty("os.name").startsWith("Lin"))
                                                System.setProperty("jinput.plugins",LINUX_PLUGIN);
                                        if (System.getProperty("os.name").startsWith("Mac"))
                                                System.setProperty("jinput.plugins",MACOS_PLUGIN);
                                    }
                                    return ControllerEnvironment.getDefaultEnvironment().getControllers();
                                }
                            });            

			ArrayList lollers = new ArrayList();
			for (int i=0;i<found.length;i++) {
				net.java.games.input.Controller c = found[i];
	
				if ((!c.getType().equals(net.java.games.input.Controller.Type.KEYBOARD)) &&
					(!c.getType().equals(net.java.games.input.Controller.Type.MOUSE))) {
					lollers.add(c);
				}
			}
				
			int length = lollers.size();
			
			for (int i=0;i<length;i++) {
				final net.java.games.input.Controller c = (net.java.games.input.Controller) lollers.get(i);
	
                                AccessController.doPrivileged(new PrivilegedAction() {
                                    public Object run() {
                                        createController(c);
                                        return null;
                                    }
                                });

			}
			
			created = true;
		} catch (Throwable e) {
			throw new LWJGLException("Failed to initialise controllers",e);
		}
	}


Nice one Alan, you’re dead right.

Kev

Another issue.

The PC returns values between -1 and +1 for each axis.

The Mac initially returns +1 for all axes, until you stir the sticks. Then for each axis an auto-calibration appears to take place. If the sticks are fully stired around the four quadrants, then the final range is 0 to 1 for each axis.

My current workaround is to check for a Mac and the double the value received from each axis, then subtract 1.

If you forget to stir the sticks, you will get maximum rate movement in all axes, until the sticks are stired. Then everything is alright.

Other notes:

  • There appears to be a memory leak. After lots of debug cycles Netbeans runs out of memory.
  • POV doesn’t work correctly on the Mac, so you can’t use it x-platform
  • The same controller is detected twice on the Mac

Re: memory link - are you calling clearEvents() on Controllers each loop?

Kev

Well no, but the prog does read all pending events every frame (so hopefully shouldn’t need to). However if I run the game a dozen times within session with the Netbeans IDE there are out of memory errors. While this has happened before the controllers stuff was in, it occurs more frequently now (or it could be my imagination). Maybe JInput does not clean up its plugins when classes are disposed & some native resource allocations are left hanging about. Your destroy method in Controllers is commented out, so you are probably aware of this. :slight_smile:

Alan

Hold on, are you talking about between process invocations? As in restarting the game all together?

Kev

I know it sounds odd. Netbeans 3.6 (I know - old) tends to fall over if apps repeatedly terminate abnormally. I also noticed this behaviour more frequently after adding controllers. I wondered if there was a memory leak somewhere.

I found this

So I could be wrong :wink: Maybe something just keeled over. I’ll do some more scientific testing.

Edit - Think I’m wrong :slight_smile: - I ran the app a good dozen times within Netbeans without hitting any problems. I think my earlier problems must have been purely down to app crashes during development destabilising Netbeans.