Implement the isSupported method and make plugins work again. There was an issue where accessing the static methods in DefaultControllerEnvironment and ControllerEnvironment would cause ploblems when the plugins were loaded using the PluginLoader class loader.

This commit is contained in:
endolf 2007-06-10 15:03:27 +00:00
parent d6368e8dc5
commit 083eee58ee
18 changed files with 455 additions and 154 deletions

View file

@ -42,6 +42,7 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import net.java.games.util.plugins.Plugin;
@ -52,8 +53,53 @@ import net.java.games.util.plugins.Plugin;
* @version 1.0
*/
public final class DirectInputEnvironmentPlugin extends ControllerEnvironment implements Plugin {
private static boolean supported = false;
/**
* 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");
if (lib_path != null)
System.load(lib_path + File.separator + System.mapLibraryName(lib_name));
else
System.loadLibrary(lib_name);
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 {
DefaultControllerEnvironment.loadLibrary("jinput-dx8");
String osName = getPrivilegedProperty("os.name", "").trim();
if(osName.startsWith("Windows")) {
supported = true;
loadLibrary("jinput-dx8");
}
}
private final Controller[] controllers;
@ -61,29 +107,36 @@ public final class DirectInputEnvironmentPlugin extends ControllerEnvironment im
private final DummyWindow window;
/** Creates new DirectInputEnvironment */
public DirectInputEnvironmentPlugin() {
public DirectInputEnvironmentPlugin() {
DummyWindow window = null;
Controller[] controllers = new Controller[]{};
try {
window = new DummyWindow();
if(isSupported()) {
try {
controllers = enumControllers(window);
window = new DummyWindow();
try {
controllers = enumControllers(window);
} catch (IOException e) {
window.destroy();
throw e;
}
} catch (IOException e) {
window.destroy();
throw e;
logln("Failed to enumerate devices: " + e.getMessage());
}
} catch (IOException e) {
ControllerEnvironment.logln("Failed to enumerate devices: " + e.getMessage());
this.window = window;
this.controllers = controllers;
AccessController.doPrivileged(
new PrivilegedAction() {
public final Object run() {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
return null;
}
});
} else {
// These are final fields, so can't set them, then over ride
// them if we are supported.
this.window = null;
this.controllers = controllers;
}
this.window = window;
this.controllers = controllers;
AccessController.doPrivileged(
new PrivilegedAction() {
public final Object run() {
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
return null;
}
});
}
public final Controller[] getControllers() {
@ -185,4 +238,8 @@ public final class DirectInputEnvironmentPlugin extends ControllerEnvironment im
*/
}
}
public boolean isSupported() {
return supported;
}
} // class DirectInputEnvironment

View file

@ -82,7 +82,7 @@ final class IDirectInput {
IDirectInputDevice device = new IDirectInputDevice(window, address, instance_guid, product_guid, dev_type, dev_subtype, instance_name, product_name);
devices.add(device);
} catch (IOException e) {
DefaultControllerEnvironment.logln("Failed to initialize device " + product_name + " because of: " + e);
DirectInputEnvironmentPlugin.logln("Failed to initialize device " + product_name + " because of: " + e);
}
}

View file

@ -228,7 +228,7 @@ final class IDirectInputDevice {
enumEffects();
createRumblers();
} catch (IOException e) {
ControllerEnvironment.logln("Failed to create rumblers: " + e.getMessage());
DirectInputEnvironmentPlugin.logln("Failed to create rumblers: " + e.getMessage());
}
/* Some DirectInput lamer-designer made the device state
* axis mode be per-device not per-axis, so I'll just

View file

@ -64,7 +64,7 @@ final class IDirectInputEffect implements Rumbler {
} else
stop();
} catch (IOException e) {
ControllerEnvironment.logln("Failed to set rumbler gain: " + e.getMessage());
DirectInputEnvironmentPlugin.logln("Failed to set rumbler gain: " + e.getMessage());
}
}

View file

@ -42,6 +42,7 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import net.java.games.util.plugins.Plugin;
@ -52,21 +53,68 @@ import net.java.games.util.plugins.Plugin;
* @version 1.0
*/
public final class RawInputEnvironmentPlugin extends ControllerEnvironment implements Plugin {
private static boolean supported = false;
/**
* 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");
if (lib_path != null)
System.load(lib_path + File.separator + System.mapLibraryName(lib_name));
else
System.loadLibrary(lib_name);
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 {
DefaultControllerEnvironment.loadLibrary("jinput-raw");
String osName = getPrivilegedProperty("os.name", "").trim();
if(osName.startsWith("Windows")) {
supported = true;
loadLibrary("jinput-raw");
}
}
private final Controller[] controllers;
private final Controller[] controllers;
/** Creates new DirectInputEnvironment */
public RawInputEnvironmentPlugin() {
RawInputEventQueue queue;
Controller[] controllers = new Controller[]{};
try {
queue = new RawInputEventQueue();
controllers = enumControllers(queue);
} catch (IOException e) {
ControllerEnvironment.logln("Failed to enumerate devices: " + e.getMessage());
if(isSupported()) {
try {
queue = new RawInputEventQueue();
controllers = enumControllers(queue);
} catch (IOException e) {
logln("Failed to enumerate devices: " + e.getMessage());
}
}
this.controllers = controllers;
}
@ -122,6 +170,10 @@ public final class RawInputEnvironmentPlugin extends ControllerEnvironment imple
return controllers_array;
}
public boolean isSupported() {
return supported;
}
/*
* The raw input API, while being able to access
* multiple mice and keyboards, is a bit raw (hah)
@ -151,4 +203,5 @@ public final class RawInputEnvironmentPlugin extends ControllerEnvironment imple
private final static native byte[] getKeyboardClassGUID();
private final static native byte[] getMouseClassGUID();
} // class DirectInputEnvironment

View file

@ -3,10 +3,12 @@
<project name="JInput dx8 port, Native code" basedir="." default="compile">
<property environment="env"/>
<property name="dxhome" location="${env.DXSDK_DIR}"/>
<property name="sdkhome" location="${env.MSSDk}"/>
<property name="sdkhome" location="c:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2"/>
<target name="compile_dir">
<echo message="${compiledir}"/>
<echo message="sdkhome: ${sdkhome}"/>
<echo message="dxhome: ${dxhome}"/>
<apply dir="${compiledir}" failonerror="true" executable="cl" dest="${compiledir}" skipemptyfilesets="true">
<arg line="/Ox /Wp64 /W2 /nologo /c"/>
<arg value="/I${sdkhome}\include"/>
@ -28,7 +30,7 @@
<srcfile/>
<arg line="/Fe${dllname} /link"/>
<arg value="/LIBPATH:${java.home}\lib"/>
<arg value="/LIBPATH:${dxhome}\lib\x86"/>
<arg value="/LIBPATH:${dxhome}\lib"/>
<arg value="/LIBPATH:${sdkhome}\lib"/>
<arg line="/DLL ${libs}"/>
<fileset dir="${commonhome}/src/native" includes="*.obj"/>