BowlerKernel
DefaultControllerEnvironment.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.gamepad;
2 
3 /*
4  * %W% %E%
5  *
6  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
7  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
8  */
9 /*****************************************************************************
10  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * - Redistribution of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * - Redistribution in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materails provided with the distribution.
20  *
21  * Neither the name Sun Microsystems, Inc. or the names of the contributors
22  * may be used to endorse or promote products derived from this software
23  * without specific prior written permission.
24  *
25  * This software is provided "AS IS," without a warranty of any kind.
26  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
27  * ANY IMPLIED WARRANT OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
28  * NON-INFRINGEMEN, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND
29  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS
30  * A RESULT OF USING, MODIFYING OR DESTRIBUTING THIS SOFTWARE OR ITS
31  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
32  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
33  * INCIDENTAL OR PUNITIVE DAMAGES. HOWEVER CAUSED AND REGARDLESS OF THE THEORY
34  * OF LIABILITY, ARISING OUT OF THE USE OF OUR INABILITY TO USE THIS SOFTWARE,
35  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36  *
37  * You acknowledge that this software is not designed or intended for us in
38  * the design, construction, operation or maintenance of any nuclear facility
39  *
40  *****************************************************************************/
41 
42 import java.io.File;
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;
54 
55 import net.java.games.input.Controller;
56 import net.java.games.util.plugins.*;
57 
64 class DefaultControllerEnvironment extends ControllerEnvironment {
65  static String libPath;
66 
67  private static Logger log = Logger.getLogger(DefaultControllerEnvironment.class.getName());
68 
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");
81  if (lib_path != null)
82  System.load(lib_path + File.separator + System.mapLibraryName(lib_name));
83  else
84  System.loadLibrary(lib_name);
85  return null;
86  }
87  });
88  }
89 
90  static String getPrivilegedProperty(final String property) {
91  return (String)AccessController.doPrivileged(new PrivilegedAction() {
92  public Object run() {
93  return System.getProperty(property);
94  }
95  });
96  }
97 
98 
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);
103  }
104  });
105  }
106 
110  private ArrayList controllers;
111 
112  private Collection loadedPlugins = new ArrayList();
113 
117  public DefaultControllerEnvironment() {
118  }
119 
124  public Controller[] getControllers() {
125  loadedPlugins.clear();
126  // Controller list has not been scanned.
127  controllers = new ArrayList();
128  AccessController.doPrivileged(new PrivilegedAction() {
129  public Object run() {
130  scanControllers();
131  return null;
132  }
133  });
134  //Check the properties for specified controller classes
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";
150  } else {
151  log.warning("Trying to use default plugin, OS name " + osName +" not recognised");
152  }
153  }
154 
155  StringTokenizer pluginClassTok = new StringTokenizer(pluginClasses, " \t\n\r\f,;:");
156  while(pluginClassTok.hasMoreTokens()) {
157  String className = pluginClassTok.nextToken();
158  try {
159  if(!loadedPlugins.contains(className)) {
160  log.fine("Loading: " + className);
161  libfix();
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());
167  } else {
168  logln(ceClass.getName() + " is not supported");
169  }
170  }
171  } catch (Throwable e) {
172  e.printStackTrace();
173  }
174  }
175 
176  Controller[] ret = new Controller[controllers.size()];
177  Iterator it = controllers.iterator();
178  int i = 0;
179  while (it.hasNext()) {
180  ret[i] = (Controller)it.next();
181  i++;
182  }
183  return ret;
184  }
185 
186  /* This is jeff's new plugin code using Jeff's Plugin manager */
187  private void scanControllers() {
188  String pluginPathName = getPrivilegedProperty("jinput.controllerPluginPath");
189  if(pluginPathName == null) {
190  pluginPathName = "controller";
191  }
192 
193  scanControllersAt(getPrivilegedProperty("java.home") +
194  File.separator + "lib"+File.separator + pluginPathName);
195  scanControllersAt(getPrivilegedProperty("user.dir")+
196  File.separator + pluginPathName);
197  }
198 
199  private void scanControllersAt(String path) {
200  File file = new File(path);
201  if (!file.exists()) {
202  return;
203  }
204  try {
205  Plugins plugins = new Plugins(file);
206  Class[] envClasses = plugins.getExtends(ControllerEnvironment.class);
207  for(int i=0;i<envClasses.length;i++){
208  try {
209  ControllerEnvironment.logln("ControllerEnvironment "+
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());
217  } else {
218  logln(envClasses[i].getName() + " is not supported");
219  }
220  } catch (Throwable e) {
221  e.printStackTrace();
222  }
223  }
224  } catch (Exception e) {
225  e.printStackTrace();
226  }
227  }
228 
232  private void addControllers(Controller[] c) {
233  for (int i = 0; i < c.length; i++) {
234  controllers.add(c[i]);
235  }
236  }
237 
238  public boolean isSupported() {
239  return true;
240  }
241 }