Top Description Fields Constructors Methods
com.studiohartman.jamepad

public Class ControllerManager

extends Object
Class Inheritance
Imports
com.badlogic.gdx.utils.SharedLibraryLoader, java.io.ByteArrayOutputStream, .File, .FileInputStream, .IOException, .InputStream, java.nio.file.Files, .Path, .StandardCopyOption

This class handles initializing the native library, connecting to controllers, and managing the list of controllers. Generally, after creating a ControllerManager object and calling initSDLGamepad() on it, you would access the states of the attached gamepads by calling getState(). For some applications (but probably very few), getState may have a performance impact. In this case, it may make sense to use the getControllerIndex() method to access the objects used internally by ControllerManager.
Author
William Hartman

Field Summary

Modifier and TypeField and Description
private final Configuration
private ControllerIndex[]
private boolean
private String

Constructor Summary

AccessConstructor and Description
public
ControllerManager()

Default constructor.

public
ControllerManager(Configuration configuration)

Constructor.

public
ControllerManager(Configuration configuration, String
The path to a file containing SDL controller mappings.
mappingsPath
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public void
addMappingsFromFile(String
The path to the file containing controller mappings.
path
)

This method adds mappings held in the specified file.

public boolean

Returns:

Whether or not vibration was successfully started
doVibration
(int
The index of the controller that will be vibrated
index
,
float
The intensity of the left rumble motor (0-1)
leftMagnitude
,
float
The intensity of the rught rumble motor (0-1)
rightMagnitude
,
int duration_ms)

Starts vibrating the controller at this given index.

public ControllerIndex

Returns:

The internal ControllerIndex object for the passed index.
getControllerIndex
(int
the index of the ControllerIndex that will be returned
index
)

Returns a the ControllerIndex object with the passed index (0 for p1, 1 for p2, etc.).

public native String

Returns:

last error message logged by the native lib. Use this for debugging purposes.
getLastNativeError
()

public int

Returns:

the number of connected controllers.
getNumControllers
()

Return the number of controllers that are actually connected.

public ControllerState

Returns:

The state of the controller at the passed index.
getState
(int
The index of the controller to be checked
index
)

Return the state of a controller at the passed index.

public void
initSDLGamepad()

Initialize the ControllerIndex library.

private native boolean
nativeAddMappingsFromBuffer(byte[] buffer, int length)

private native boolean
private native void
private native boolean
private native int
private native boolean
nativeInitSDLGamepad(boolean disableRawInput)

public void
quitSDLGamepad()

This method quits all the native stuff.

public boolean

Returns:

True if the controller list was refreshed, false otherwise
update
()

Refresh the connected controllers in the controller list if something has been connected or unplugged.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configurationback to summary
private final Configuration configuration
controllersback to summary
private ControllerIndex[] controllers
isInitializedback to summary
private boolean isInitialized
mappingsPathback to summary
private String mappingsPath

Constructor Detail

ControllerManagerback to summary
public ControllerManager()

Default constructor. Makes a manager for 4 controllers with the built in mappings from here: https://github.com/gabomdq/SDL_GameControllerDB

ControllerManagerback to summary
public ControllerManager(Configuration configuration)

Constructor. Uses built-in mappings from here: https://github.com/gabomdq/SDL_GameControllerDB

Parameters
configuration:Configuration

see and its fields

ControllerManagerback to summary
public ControllerManager(Configuration configuration, String mappingsPath)

Constructor.

Parameters
configuration:Configuration

see and its fields

mappingsPath:String

The path to a file containing SDL controller mappings.

Method Detail

addMappingsFromFileback to summary
public void addMappingsFromFile(String path) throws IOException, IllegalStateException

This method adds mappings held in the specified file. The file is copied to the temp folder so that it can be read by the native code (if running from a .jar for instance)

Parameters
path:String

The path to the file containing controller mappings.

Exceptions
IOException:
if the file cannot be read, copied to a temp folder, or deleted.
IllegalStateException:
if the mappings cannot be applied to SDL
doVibrationback to summary
public boolean doVibration(int index, float leftMagnitude, float rightMagnitude, int duration_ms) throws IllegalStateException

Starts vibrating the controller at this given index. If this fails for one reason or another (e.g. the controller at that index doesn't support haptics, or if there is no controller at that index), this method will return false. Each call to this function cancels any previous rumble effect, and calling it with 0 intensity stops any rumbling.

Parameters
index:int

The index of the controller that will be vibrated

leftMagnitude:float

The intensity of the left rumble motor (0-1)

rightMagnitude:float

The intensity of the rught rumble motor (0-1)

Returns:boolean

Whether or not vibration was successfully started

Exceptions
IllegalStateException:
if Jamepad was not initialized
getControllerIndexback to summary
public ControllerIndex getControllerIndex(int index)

Returns a the ControllerIndex object with the passed index (0 for p1, 1 for p2, etc.). You should only use this method if you're worried about the object allocations from getState(). If you decide to do things this way, your code will be a good bit more verbose and you'll need to deal with potential exceptions. It is generally safe to store objects returned from this method. They will only change internally if you call quitSDLGamepad() followed by a call to initSDLGamepad(). Calling update() will run through all the controllers to check for newly plugged in or unplugged controllers. You could do this from your code, but keep that in mind.

Parameters
index:int

the index of the ControllerIndex that will be returned

Returns:ControllerIndex

The internal ControllerIndex object for the passed index.

Exceptions
IllegalStateException:
if Jamepad was not initialized
getLastNativeErrorback to summary
public native String getLastNativeError()
Returns:String

last error message logged by the native lib. Use this for debugging purposes.

getNumControllersback to summary
public int getNumControllers()

Return the number of controllers that are actually connected. This may disagree with the ControllerIndex objects held in here if something has been plugged in or unplugged since update() was last called.

Returns:int

the number of connected controllers.

Exceptions
IllegalStateException:
if Jamepad was not initialized
getStateback to summary
public ControllerState getState(int index) throws IllegalStateException

Return the state of a controller at the passed index. This is probably the way most people should use this library. It's simpler and less verbose, and controller connections and disconnections are automatically handled. Also, no exceptions are thrown here (unless Jamepad isn't initialized), so you don't need to have a million try/catches or anything. The returned state is immutable. This means an object is allocated every time you call this (unless the controller is disconnected). This shouldn't be a big deal (even for games) if your GC is tuned well, but if this is a problem for you, you can go directly through the internal ControllerIndex objects using getControllerIndex(). update() is called each time this method is called. Buttons are also queried, so values returned from isButtonJustPressed() in ControllerIndex may not be what you expect. Calling this method will have side effects if you are using the ControllerIndex objects yourself. This should be fine unless you are mixing and matching this method with ControllerIndex objects, which you probably shouldn't do anyway.

Parameters
index:int

The index of the controller to be checked

Returns:ControllerState

The state of the controller at the passed index.

Exceptions
IllegalStateException:
if Jamepad was not initialized
initSDLGamepadback to summary
public void initSDLGamepad() throws IllegalStateException

Initialize the ControllerIndex library. This loads the native library and initializes SDL in the native code.

Exceptions
IllegalStateException:
If the native code fails to initialize or if SDL is already initialized
nativeAddMappingsFromBufferback to summary
private native boolean nativeAddMappingsFromBuffer(byte[] buffer, int length)
nativeAddMappingsFromFileback to summary
private native boolean nativeAddMappingsFromFile(String path)
nativeCloseSDLGamepadback to summary
private native void nativeCloseSDLGamepad()
nativeControllerConnectedOrDisconnectedback to summary
private native boolean nativeControllerConnectedOrDisconnected()
nativeGetNumRollersback to summary
private native int nativeGetNumRollers()
nativeInitSDLGamepadback to summary
private native boolean nativeInitSDLGamepad(boolean disableRawInput)
quitSDLGamepadback to summary
public void quitSDLGamepad()

This method quits all the native stuff. Call it when you're done with Jamepad.

updateback to summary
public boolean update()

Refresh the connected controllers in the controller list if something has been connected or unplugged. If there hasn't been a change in whether controller are connected or not, nothing will happen.

Returns:boolean

True if the controller list was refreshed, false otherwise

Exceptions
IllegalStateException:
if Jamepad was not initialized
verifyInitializedback to summary
private boolean verifyInitialized() throws IllegalStateException