From 083eee58ee24d73d43dcaba32b17df010f7e5482 Mon Sep 17 00:00:00 2001 From: endolf Date: Sun, 10 Jun 2007 15:03:27 +0000 Subject: [PATCH] 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. --- build.xml | 17 +++ .../games/input/ControllerEnvironment.java | 10 ++ .../input/DefaultControllerEnvironment.java | 80 +++++++------ .../games/input/OSXEnvironmentPlugin.java | 67 ++++++++++- .../games/input/AWTEnvironmentPlugin.java | 7 +- .../games/input/LinuxEnvironmentPlugin.java | 95 ++++++++++++---- .../java/games/input/LinuxEventDevice.java | 2 +- .../games/input/LinuxForceFeedbackEffect.java | 2 +- .../java/net/java/games/input/LinuxPOV.java | 2 +- .../input/DirectInputEnvironmentPlugin.java | 93 +++++++++++++--- .../net/java/games/input/IDirectInput.java | 2 +- .../java/games/input/IDirectInputDevice.java | 2 +- .../java/games/input/IDirectInputEffect.java | 2 +- .../input/RawInputEnvironmentPlugin.java | 67 +++++++++-- plugins/windows/src/native/build.xml | 6 +- .../net/java/games/input/WinTabContext.java | 16 --- .../net/java/games/input/WinTabDevice.java | 34 +++--- .../games/input/WinTabEnvironmentPlugin.java | 105 +++++++++++++----- 18 files changed, 455 insertions(+), 154 deletions(-) diff --git a/build.xml b/build.xml index 317cbf7..9cb1087 100644 --- a/build.xml +++ b/build.xml @@ -83,6 +83,23 @@ + + + + + + + + + + + + + + + + + diff --git a/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java b/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java index c7a4a42..db6f1dc 100644 --- a/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java +++ b/coreAPI/src/java/net/java/games/input/ControllerEnvironment.java @@ -38,6 +38,9 @@ *****************************************************************************/ package net.java.games.input; +import java.io.File; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Iterator; @@ -105,6 +108,13 @@ public abstract class ControllerEnvironment { controllerListeners.add(l); } + /** + * Returns the isSupported status of this environment. + * What makes an environment supported or not is up to the + * particular plugin, but may include OS or available hardware. + */ + public abstract boolean isSupported(); + /** * Removes a listener for controller state change events. */ diff --git a/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java b/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java index 544562a..098a1cf 100644 --- a/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java +++ b/coreAPI/src/java/net/java/games/input/DefaultControllerEnvironment.java @@ -41,6 +41,7 @@ package net.java.games.input; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.lang.reflect.Method; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; @@ -57,18 +58,8 @@ import net.java.games.util.plugins.*; * @author Michael Martak */ class DefaultControllerEnvironment extends ControllerEnvironment { - /** - * Location of the LIB directory. - */ - static String libPath; - - /** - * List of all controllers in this environment - */ - private ArrayList controllers; - - private Collection loadedPlugins = new ArrayList(); - + static String libPath; + /** * Static utility method for loading native libraries. * It will try to load from either the path given by @@ -90,28 +81,36 @@ class DefaultControllerEnvironment extends ControllerEnvironment { }); } - /** - * Public no-arg constructor. - */ - public DefaultControllerEnvironment() { - } - - private static String getPrivilegedProperty(final String property) { - return (String)AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return System.getProperty(property); - } - }); - } - - private static String getPrivilegedProperty(final String property, final String default_value) { + 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); } }); } - + + /** + * List of all controllers in this environment + */ + private ArrayList controllers; + + private Collection loadedPlugins = new ArrayList(); + + /** + * Public no-arg constructor. + */ + public DefaultControllerEnvironment() { + } + /** * Returns a list of all controllers available to this environment, * or an empty array if there are no controllers in this environment. @@ -147,15 +146,20 @@ class DefaultControllerEnvironment extends ControllerEnvironment { System.out.println("Trying to use default plugin, OS name " + osName +" not recognised"); } } - ArrayList pluginClassList = new ArrayList(); + StringTokenizer pluginClassTok = new StringTokenizer(pluginClasses, " \t\n\r\f,;:"); while(pluginClassTok.hasMoreTokens()) { String className = pluginClassTok.nextToken(); try { if(!loadedPlugins.contains(className)) { - Class ceClass = Class.forName(className); + Class ceClass = Class.forName(className); ControllerEnvironment ce = (ControllerEnvironment) ceClass.newInstance(); - addControllers(ce.getControllers()); + if(ce.isSupported()) { + addControllers(ce.getControllers()); + loadedPlugins.add(ce.getClass().getName()); + } else { + logln(ceClass.getName() + " is not supported"); + } } } catch (Throwable e) { e.printStackTrace(); @@ -199,9 +203,13 @@ class DefaultControllerEnvironment extends ControllerEnvironment { envClasses[i].getName() +" loaded by "+envClasses[i].getClassLoader()); ControllerEnvironment ce = (ControllerEnvironment) - envClasses[i].newInstance(); - addControllers(ce.getControllers()); - loadedPlugins.add(ce.getClass().getName()); + envClasses[i].newInstance(); + if(ce.isSupported()) { + addControllers(ce.getControllers()); + loadedPlugins.add(ce.getClass().getName()); + } else { + logln(envClasses[i].getName() + " is not supported"); + } } catch (Throwable e) { e.printStackTrace(); } @@ -219,4 +227,8 @@ class DefaultControllerEnvironment extends ControllerEnvironment { controllers.add(c[i]); } } + + public boolean isSupported() { + return true; + } } diff --git a/plugins/OSX/src/java/net/java/games/input/OSXEnvironmentPlugin.java b/plugins/OSX/src/java/net/java/games/input/OSXEnvironmentPlugin.java index 66813b7..9ba03a1 100755 --- a/plugins/OSX/src/java/net/java/games/input/OSXEnvironmentPlugin.java +++ b/plugins/OSX/src/java/net/java/games/input/OSXEnvironmentPlugin.java @@ -38,6 +38,7 @@ *****************************************************************************/ package net.java.games.input; +import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; @@ -55,8 +56,54 @@ import java.security.PrivilegedAction; * @version 1.0 */ public final class OSXEnvironmentPlugin 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-osx"); + String osName = getPrivilegedProperty("os.name", "").trim(); + if(osName.equals("Mac OS X")) { + // Could check isMacOSXEqualsOrBetterThan in here too. + supported = true; + loadLibrary("jinput-osx"); + } } private final static boolean isMacOSXEqualsOrBetterThan(int major_required, int minor_required) { @@ -70,7 +117,7 @@ public final class OSXEnvironmentPlugin extends ControllerEnvironment implements major = Integer.parseInt(major_str); minor = Integer.parseInt(minor_str); } catch (Exception e) { - ControllerEnvironment.logln("Exception occurred while trying to determine OS version: " + e); + logln("Exception occurred while trying to determine OS version: " + e); // Best guess, no return false; } @@ -80,13 +127,21 @@ public final class OSXEnvironmentPlugin extends ControllerEnvironment implements private final Controller[] controllers; public OSXEnvironmentPlugin() { - this.controllers = enumerateControllers(); + if(isSupported()) { + this.controllers = enumerateControllers(); + } else { + this.controllers = new Controller[0]; + } } public final Controller[] getControllers() { return controllers; } + public boolean isSupported() { + return supported; + } + private final static void addElements(OSXHIDQueue queue, List elements, List components, boolean map_mouse_buttons) throws IOException { Iterator it = elements.iterator(); while (it.hasNext()) { @@ -202,19 +257,19 @@ public final class OSXEnvironmentPlugin extends ControllerEnvironment implements createControllersFromDevice(device, controllers); device_used = old_size != controllers.size(); } catch (IOException e) { - ControllerEnvironment.logln("Failed to create controllers from device: " + device.getProductName()); + logln("Failed to create controllers from device: " + device.getProductName()); } if (!device_used) device.release(); } catch (IOException e) { - ControllerEnvironment.logln("Failed to enumerate device: " + e.getMessage()); + logln("Failed to enumerate device: " + e.getMessage()); } } } finally { it.close(); } } catch (IOException e) { - ControllerEnvironment.log("Failed to enumerate devices: " + e.getMessage()); + log("Failed to enumerate devices: " + e.getMessage()); return new Controller[]{}; } Controller[] controllers_array = new Controller[controllers.size()]; diff --git a/plugins/awt/src/net/java/games/input/AWTEnvironmentPlugin.java b/plugins/awt/src/net/java/games/input/AWTEnvironmentPlugin.java index 96e5986..b6b062a 100644 --- a/plugins/awt/src/net/java/games/input/AWTEnvironmentPlugin.java +++ b/plugins/awt/src/net/java/games/input/AWTEnvironmentPlugin.java @@ -34,7 +34,8 @@ import net.java.games.util.plugins.Plugin; * @author elias */ public class AWTEnvironmentPlugin extends ControllerEnvironment implements Plugin { - private final Controller[] controllers; + + private final Controller[] controllers; public AWTEnvironmentPlugin() { this.controllers = new Controller[]{new AWTKeyboard(), new AWTMouse()}; @@ -43,4 +44,8 @@ public class AWTEnvironmentPlugin extends ControllerEnvironment implements Plugi public Controller[] getControllers() { return controllers; } + + public boolean isSupported() { + return true; + } } diff --git a/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java b/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java index 513c85d..1ad937a 100644 --- a/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java +++ b/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java @@ -41,21 +41,64 @@ import java.security.PrivilegedAction; 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"); + 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 { - try { - DefaultControllerEnvironment.loadLibrary(LIBNAME); - } catch (UnsatisfiedLinkError e) { + String osName = getPrivilegedProperty("os.name", "").trim(); + if(osName.equals("Linux")) { + supported = true; try { - DefaultControllerEnvironment.loadLibrary(LIBNAME + POSTFIX64BIT); - } catch (UnsatisfiedLinkError e2) { - ControllerEnvironment.logln("Failed to load 64 bit library: " + e2.getMessage()); - // throw original error - throw e; + loadLibrary(LIBNAME); + } catch (UnsatisfiedLinkError e) { + try { + loadLibrary(LIBNAME + POSTFIX64BIT); + } catch (UnsatisfiedLinkError e2) { + logln("Failed to load 64 bit library: " + e2.getMessage()); + // throw original error + throw e; + } } } } @@ -65,15 +108,19 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen } public LinuxEnvironmentPlugin() { - this.controllers = enumerateControllers(); - ControllerEnvironment.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; - } - }); + 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, @@ -120,7 +167,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen povs[3][1] = event_component; break; default: - ControllerEnvironment.logln("Unknown POV instance: " + native_code); + logln("Unknown POV instance: " + native_code); break; } } else if (identifier != null) { @@ -305,7 +352,7 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen } else device.close(); } catch (IOException e) { - ControllerEnvironment.logln("Failed to open device (" + event_file + "): " + e.getMessage()); + logln("Failed to open device (" + event_file + "): " + e.getMessage()); } } } @@ -357,11 +404,11 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen } else device.close(); } catch (IOException e) { - ControllerEnvironment.logln("Failed to create Controller: " + e.getMessage()); + logln("Failed to create Controller: " + e.getMessage()); device.close(); } } catch (IOException e) { - ControllerEnvironment.logln("Failed to open device (" + event_file + "): " + e.getMessage()); + logln("Failed to open device (" + event_file + "): " + e.getMessage()); } } } @@ -373,9 +420,13 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen LinuxDevice device = (LinuxDevice)devices.get(i); device.close(); } catch (IOException e) { - ControllerEnvironment.logln("Failed to close device: " + e.getMessage()); + logln("Failed to close device: " + e.getMessage()); } } } } + + public boolean isSupported() { + return supported; + } } diff --git a/plugins/linux/src/java/net/java/games/input/LinuxEventDevice.java b/plugins/linux/src/java/net/java/games/input/LinuxEventDevice.java index 08cb8f2..a28c4c7 100644 --- a/plugins/linux/src/java/net/java/games/input/LinuxEventDevice.java +++ b/plugins/linux/src/java/net/java/games/input/LinuxEventDevice.java @@ -190,7 +190,7 @@ final class LinuxEventDevice implements LinuxDevice { rumblers.add(new LinuxRumbleFF(this)); } } catch (IOException e) { - ControllerEnvironment.logln("Failed to enumerate rumblers: " + e.getMessage()); + LinuxEnvironmentPlugin.logln("Failed to enumerate rumblers: " + e.getMessage()); } return (Rumbler[])rumblers.toArray(new Rumbler[]{}); } diff --git a/plugins/linux/src/java/net/java/games/input/LinuxForceFeedbackEffect.java b/plugins/linux/src/java/net/java/games/input/LinuxForceFeedbackEffect.java index 528cbff..299e029 100644 --- a/plugins/linux/src/java/net/java/games/input/LinuxForceFeedbackEffect.java +++ b/plugins/linux/src/java/net/java/games/input/LinuxForceFeedbackEffect.java @@ -56,7 +56,7 @@ abstract class LinuxForceFeedbackEffect implements Rumbler { write_task.write(0); } } catch (IOException e) { - ControllerEnvironment.logln("Failed to rumble: " + e); + LinuxEnvironmentPlugin.logln("Failed to rumble: " + e); } } diff --git a/plugins/linux/src/java/net/java/games/input/LinuxPOV.java b/plugins/linux/src/java/net/java/games/input/LinuxPOV.java index 7e62501..8cc4e74 100644 --- a/plugins/linux/src/java/net/java/games/input/LinuxPOV.java +++ b/plugins/linux/src/java/net/java/games/input/LinuxPOV.java @@ -88,7 +88,7 @@ final class LinuxPOV extends LinuxComponent { else if (last_x == 1 && last_y == 1) return Component.POV.DOWN_RIGHT; else { - ControllerEnvironment.logln("Unknown values x = " + last_x + " | y = " + last_y); + LinuxEnvironmentPlugin.logln("Unknown values x = " + last_x + " | y = " + last_y); return Component.POV.OFF; } } diff --git a/plugins/windows/src/java/net/java/games/input/DirectInputEnvironmentPlugin.java b/plugins/windows/src/java/net/java/games/input/DirectInputEnvironmentPlugin.java index 3e1636b..be452bc 100644 --- a/plugins/windows/src/java/net/java/games/input/DirectInputEnvironmentPlugin.java +++ b/plugins/windows/src/java/net/java/games/input/DirectInputEnvironmentPlugin.java @@ -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 diff --git a/plugins/windows/src/java/net/java/games/input/IDirectInput.java b/plugins/windows/src/java/net/java/games/input/IDirectInput.java index dea99d5..a7393d5 100644 --- a/plugins/windows/src/java/net/java/games/input/IDirectInput.java +++ b/plugins/windows/src/java/net/java/games/input/IDirectInput.java @@ -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); } } diff --git a/plugins/windows/src/java/net/java/games/input/IDirectInputDevice.java b/plugins/windows/src/java/net/java/games/input/IDirectInputDevice.java index 86eea1c..40d8b8d 100644 --- a/plugins/windows/src/java/net/java/games/input/IDirectInputDevice.java +++ b/plugins/windows/src/java/net/java/games/input/IDirectInputDevice.java @@ -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 diff --git a/plugins/windows/src/java/net/java/games/input/IDirectInputEffect.java b/plugins/windows/src/java/net/java/games/input/IDirectInputEffect.java index 5cb372f..2d4f55c 100644 --- a/plugins/windows/src/java/net/java/games/input/IDirectInputEffect.java +++ b/plugins/windows/src/java/net/java/games/input/IDirectInputEffect.java @@ -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()); } } diff --git a/plugins/windows/src/java/net/java/games/input/RawInputEnvironmentPlugin.java b/plugins/windows/src/java/net/java/games/input/RawInputEnvironmentPlugin.java index d8185e2..767d461 100644 --- a/plugins/windows/src/java/net/java/games/input/RawInputEnvironmentPlugin.java +++ b/plugins/windows/src/java/net/java/games/input/RawInputEnvironmentPlugin.java @@ -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 diff --git a/plugins/windows/src/native/build.xml b/plugins/windows/src/native/build.xml index 42a6ed1..f250b24 100644 --- a/plugins/windows/src/native/build.xml +++ b/plugins/windows/src/native/build.xml @@ -3,10 +3,12 @@ - + + + @@ -28,7 +30,7 @@ - + diff --git a/plugins/wintab/src/java/net/java/games/input/WinTabContext.java b/plugins/wintab/src/java/net/java/games/input/WinTabContext.java index 185f4c2..6bbb95a 100644 --- a/plugins/wintab/src/java/net/java/games/input/WinTabContext.java +++ b/plugins/wintab/src/java/net/java/games/input/WinTabContext.java @@ -21,12 +21,10 @@ public class WinTabContext { } public synchronized void open() { - ControllerEnvironment.logln("Opening context"); this.hCTX = nOpen(window.getHwnd()); List devices = new ArrayList(); int numSupportedDevices = nGetNumberOfSupportedDevices(); - ControllerEnvironment.logln(numSupportedDevices + " devices maximum supported"); for(int i=0;i