BowlerKernel
ControllerEnvironment.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.gamepad;
2 import java.io.File;
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 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.logging.Logger;
44 
45 import org.apache.commons.lang3.SystemUtils;
46 
47 import net.java.games.input.Controller;
48 import net.java.games.input.ControllerEvent;
49 import net.java.games.input.ControllerListener;
50 import us.ihmc.jinput.JInputLibraryLoader;
51 import us.ihmc.tools.nativelibraries.NativeLibraryLoader;
52 
77 public abstract class ControllerEnvironment {
78  static {
79  JInputLibraryLoader.loadLibraries();
80 
81  libfix();
82 
83  }
84 
85  protected static void libfix() {
86  String absolutePathToDirectory = "";
87  if (SystemUtils.IS_OS_WINDOWS) {
88  absolutePathToDirectory = NativeLibraryLoader.extractLibraries("", "jinput-raw", "jinput-raw_64",
89  "jinput-dx8_64", "jinput-dx8", "jinput-wintab");
90  } else if (SystemUtils.IS_OS_LINUX) {
91  absolutePathToDirectory = NativeLibraryLoader.extractLibraries("", "jinput-linux64", "jinput-linux");
92  } else if (SystemUtils.IS_OS_MAC) {
93  absolutePathToDirectory = new File(NativeLibraryLoader.extractLibraryAbsolute("", "libjinput-osx.jnilib"))
94  .getParent();
95  }
96  File test = new File(absolutePathToDirectory);
97  if (!test.isDirectory())
98  absolutePathToDirectory = test.getParentFile().getAbsolutePath();
99  // System.out.println("Setting net.java.games.input.librarypath :
100  // "+absolutePathToDirectory);
101  System.setProperty("net.java.games.input.librarypath", absolutePathToDirectory);
102  }
103 
104  static void logln(String msg) {
105  log(msg + "\n");
106  }
107 
108  static void log(String msg) {
109  Logger.getLogger(ControllerEnvironment.class.getName()).info(msg);
110  }
111 
116  new DefaultControllerEnvironment();
117 
121  protected final ArrayList controllerListeners = new ArrayList();
122 
126  protected ControllerEnvironment() {
127  }
128 
133  public abstract Controller[] getControllers();
134 
138  public void addControllerListener(ControllerListener l) {
139  assert l != null;
140  controllerListeners.add(l);
141  }
142 
148  public abstract boolean isSupported();
149 
153  public void removeControllerListener(ControllerListener l) {
154  assert l != null;
155  controllerListeners.remove(l);
156  }
157 
162  protected void fireControllerAdded(Controller c) {
163  ControllerEvent ev = new ControllerEvent(c);
164  Iterator it = controllerListeners.iterator();
165  while (it.hasNext()) {
166  ((ControllerListener)it.next()).controllerAdded(ev);
167  }
168  }
169 
174  protected void fireControllerRemoved(Controller c) {
175  ControllerEvent ev = new ControllerEvent(c);
176  Iterator it = controllerListeners.iterator();
177  while (it.hasNext()) {
178  ((ControllerListener)it.next()).controllerRemoved(ev);
179  }
180  }
181 
187  return defaultEnvironment;
188  }
189 } // ControllerEnvironment