net.java.games.input.LinuxEnvironmentPlugin Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2003 Jeremy Booth ([email protected])
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution.
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/
package net.java.games.input;
import net.java.games.util.plugins.Plugin;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
import java.io.FilenameFilter;
import java.security.AccessController;
import java.security.PrivilegedAction;
/** Environment plugin for linux
* @author elias
* @author Jeremy Booth ([email protected])
*/
public final class LinuxEnvironmentPlugin extends ControllerEnvironment implements Plugin {
private final static String LIBNAME = "jinput-linux";
private final static String POSTFIX64BIT = "64";
private static boolean supported = false;
private final Controller[] controllers;
private final List devices = new ArrayList();
private final static LinuxDeviceThread device_thread = new LinuxDeviceThread();
/**
* Static utility method for loading native libraries.
* It will try to load from either the path given by
* the net.java.games.input.librarypath property
* or through System.loadLibrary().
*
*/
static void loadLibrary(final String lib_name) {
AccessController.doPrivileged(
new PrivilegedAction() {
public final Object run() {
String lib_path = System.getProperty("net.java.games.input.librarypath");
try {
if (lib_path != null)
System.load(lib_path + File.separator + System.mapLibraryName(lib_name));
else
System.loadLibrary(lib_name);
} catch (UnsatisfiedLinkError e) {
logln("Failed to load library: " + e.getMessage());
e.printStackTrace();
supported = false;
}
return null;
}
});
}
static String getPrivilegedProperty(final String property) {
return (String)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(property);
}
});
}
static String getPrivilegedProperty(final String property, final String default_value) {
return (String)AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(property, default_value);
}
});
}
static {
String osName = getPrivilegedProperty("os.name", "").trim();
if(osName.equals("Linux")) {
supported = true;
if("i386".equals(getPrivilegedProperty("os.arch"))) {
loadLibrary(LIBNAME);
} else {
loadLibrary(LIBNAME + POSTFIX64BIT);
}
}
}
public final static Object execute(LinuxDeviceTask task) throws IOException {
return device_thread.execute(task);
}
public LinuxEnvironmentPlugin() {
if(isSupported()) {
this.controllers = enumerateControllers();
logln("Linux plugin claims to have found " + controllers.length + " controllers");
AccessController.doPrivileged(
new PrivilegedAction() {
public final Object run() {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
return null;
}
});
} else {
controllers = new Controller[0];
}
}
/** Returns a list of all controllers available to this environment,
* or an empty array if there are no controllers in this environment.
* @return Returns a list of all controllers available to this environment,
* or an empty array if there are no controllers in this environment.
*/
public final Controller[] getControllers() {
return controllers;
}
private final static Component[] createComponents(List event_components, LinuxEventDevice device) {
LinuxEventComponent[][] povs = new LinuxEventComponent[4][2];
List components = new ArrayList();
for (int i = 0; i < event_components.size(); i++) {
LinuxEventComponent event_component = (LinuxEventComponent)event_components.get(i);
Component.Identifier identifier = event_component.getIdentifier();
if (identifier == Component.Identifier.Axis.POV) {
int native_code = event_component.getDescriptor().getCode();
switch (native_code) {
case NativeDefinitions.ABS_HAT0X:
povs[0][0] = event_component;
break;
case NativeDefinitions.ABS_HAT0Y:
povs[0][1] = event_component;
break;
case NativeDefinitions.ABS_HAT1X:
povs[1][0] = event_component;
break;
case NativeDefinitions.ABS_HAT1Y:
povs[1][1] = event_component;
break;
case NativeDefinitions.ABS_HAT2X:
povs[2][0] = event_component;
break;
case NativeDefinitions.ABS_HAT2Y:
povs[2][1] = event_component;
break;
case NativeDefinitions.ABS_HAT3X:
povs[3][0] = event_component;
break;
case NativeDefinitions.ABS_HAT3Y:
povs[3][1] = event_component;
break;
default:
logln("Unknown POV instance: " + native_code);
break;
}
} else if (identifier != null) {
LinuxComponent component = new LinuxComponent(event_component);
components.add(component);
device.registerComponent(event_component.getDescriptor(), component);
}
}
for (int i = 0; i < povs.length; i++) {
LinuxEventComponent x = povs[i][0];
LinuxEventComponent y = povs[i][1];
if (x != null && y != null) {
LinuxComponent controller_component = new LinuxPOV(x, y);
components.add(controller_component);
device.registerComponent(x.getDescriptor(), controller_component);
device.registerComponent(y.getDescriptor(), controller_component);
}
}
Component[] components_array = new Component[components.size()];
components.toArray(components_array);
return components_array;
}
private final static Mouse createMouseFromDevice(LinuxEventDevice device, Component[] components) throws IOException {
Mouse mouse = new LinuxMouse(device, components, new Controller[]{}, device.getRumblers());
if (mouse.getX() != null && mouse.getY() != null && mouse.getPrimaryButton() != null)
return mouse;
else
return null;
}
private final static Keyboard createKeyboardFromDevice(LinuxEventDevice device, Component[] components) throws IOException {
Keyboard keyboard = new LinuxKeyboard(device, components, new Controller[]{}, device.getRumblers());
return keyboard;
}
private final static Controller createJoystickFromDevice(LinuxEventDevice device, Component[] components, Controller.Type type) throws IOException {
Controller joystick = new LinuxAbstractController(device, components, new Controller[]{}, device.getRumblers(), type);
return joystick;
}
private final static Controller createControllerFromDevice(LinuxEventDevice device) throws IOException {
List event_components = device.getComponents();
Component[] components = createComponents(event_components, device);
Controller.Type type = device.getType();
if (type == Controller.Type.MOUSE) {
return createMouseFromDevice(device, components);
} else if (type == Controller.Type.KEYBOARD) {
return createKeyboardFromDevice(device, components);
} else if (type == Controller.Type.STICK || type == Controller.Type.GAMEPAD) {
return createJoystickFromDevice(device, components, type);
} else
return null;
}
private final Controller[] enumerateControllers() {
List controllers = new ArrayList();
List eventControllers = new ArrayList();
List jsControllers = new ArrayList();
enumerateEventControllers(eventControllers);
enumerateJoystickControllers(jsControllers);
for(int i=0;i