1 package com.neuronrobotics.sdk.addons.gamepad;
43 import java.io.FileInputStream;
44 import java.io.IOException;
45 import java.lang.reflect.Method;
46 import java.security.AccessController;
47 import java.security.PrivilegedAction;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.Iterator;
51 import java.util.Properties;
52 import java.util.StringTokenizer;
53 import java.util.logging.Logger;
55 import net.java.games.input.Controller;
56 import net.java.games.util.plugins.*;
64 class DefaultControllerEnvironment
extends ControllerEnvironment {
65 static String libPath;
67 private static Logger log = Logger.getLogger(DefaultControllerEnvironment.class.getName());
76 static void loadLibrary(
final String lib_name) {
77 AccessController.doPrivileged(
78 new PrivilegedAction() {
79 public final Object run() {
80 String lib_path = System.getProperty(
"net.java.games.input.librarypath");
82 System.load(lib_path + File.separator + System.mapLibraryName(lib_name));
84 System.loadLibrary(lib_name);
90 static String getPrivilegedProperty(
final String property) {
91 return (String)AccessController.doPrivileged(
new PrivilegedAction() {
93 return System.getProperty(property);
99 static String getPrivilegedProperty(
final String property,
final String default_value) {
100 return (String)AccessController.doPrivileged(
new PrivilegedAction() {
101 public Object run() {
102 return System.getProperty(property, default_value);
110 private ArrayList controllers;
112 private Collection loadedPlugins =
new ArrayList();
117 public DefaultControllerEnvironment() {
124 public Controller[] getControllers() {
125 loadedPlugins.clear();
127 controllers =
new ArrayList();
128 AccessController.doPrivileged(
new PrivilegedAction() {
129 public Object run() {
135 String pluginClasses = getPrivilegedProperty(
"jinput.plugins",
"") +
" " + getPrivilegedProperty(
"net.java.games.input.plugins",
"");
136 if(!getPrivilegedProperty(
"jinput.useDefaultPlugin",
"true").toLowerCase().trim().equals(
"false") && !getPrivilegedProperty(
"net.java.games.input.useDefaultPlugin",
"true").toLowerCase().trim().equals(
"false")) {
137 String osName = getPrivilegedProperty(
"os.name",
"").trim();
138 if(osName.equals(
"Linux")) {
139 pluginClasses = pluginClasses +
" net.java.games.input.LinuxEnvironmentPlugin";
140 }
else if(osName.equals(
"Mac OS X")) {
141 pluginClasses = pluginClasses +
" net.java.games.input.OSXEnvironmentPlugin";
142 }
else if(osName.equals(
"Windows XP") || osName.equals(
"Windows Vista") || osName.equals(
"Windows 7") || osName.equals(
"Windows 8") || osName.equals(
"Windows 8.1") || osName.equals(
"Windows 10")) {
143 pluginClasses = pluginClasses +
" net.java.games.input.DirectAndRawInputEnvironmentPlugin";
144 }
else if(osName.equals(
"Windows 98") || osName.equals(
"Windows 2000")) {
145 pluginClasses = pluginClasses +
" net.java.games.input.DirectInputEnvironmentPlugin";
146 }
else if (osName.startsWith(
"Windows")) {
147 log.warning(
"Found unknown Windows version: " + osName);
148 log.warning(
"Attempting to use default windows plug-in.");
149 pluginClasses = pluginClasses +
" net.java.games.input.DirectAndRawInputEnvironmentPlugin";
151 log.warning(
"Trying to use default plugin, OS name " + osName +
" not recognised");
155 StringTokenizer pluginClassTok =
new StringTokenizer(pluginClasses,
" \t\n\r\f,;:");
156 while(pluginClassTok.hasMoreTokens()) {
157 String className = pluginClassTok.nextToken();
159 if(!loadedPlugins.contains(className)) {
160 log.fine(
"Loading: " + className);
162 Class ceClass = Class.forName(className);
163 net.java.games.input.ControllerEnvironment ce = (net.java.games.input.ControllerEnvironment) ceClass.newInstance();
164 if(ce.isSupported()) {
165 addControllers(ce.getControllers());
166 loadedPlugins.add(ce.getClass().getName());
168 logln(ceClass.getName() +
" is not supported");
171 }
catch (Throwable e) {
176 Controller[] ret =
new Controller[controllers.size()];
177 Iterator it = controllers.iterator();
179 while (it.hasNext()) {
180 ret[i] = (Controller)it.next();
187 private void scanControllers() {
188 String pluginPathName = getPrivilegedProperty(
"jinput.controllerPluginPath");
189 if(pluginPathName ==
null) {
190 pluginPathName =
"controller";
193 scanControllersAt(getPrivilegedProperty(
"java.home") +
194 File.separator +
"lib"+File.separator + pluginPathName);
195 scanControllersAt(getPrivilegedProperty(
"user.dir")+
196 File.separator + pluginPathName);
199 private void scanControllersAt(String path) {
200 File file =
new File(path);
201 if (!file.exists()) {
205 Plugins plugins =
new Plugins(file);
207 for(
int i=0;i<envClasses.length;i++){
210 envClasses[i].getName()
211 +
" loaded by "+envClasses[i].getClassLoader());
213 envClasses[i].newInstance();
214 if(ce.isSupported()) {
215 addControllers(ce.getControllers());
216 loadedPlugins.add(ce.getClass().getName());
218 logln(envClasses[i].getName() +
" is not supported");
220 }
catch (Throwable e) {
224 }
catch (Exception e) {
232 private void addControllers(Controller[] c) {
233 for (
int i = 0; i < c.length; i++) {
234 controllers.add(c[i]);
238 public boolean isSupported() {