Java Code Examples for net.java.games.input.ControllerEnvironment

The following are top voted examples for showing how to use net.java.games.input.ControllerEnvironment. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: java-natives   File: JInputTest.java   View source code 6 votes vote down vote up
@Test
public void nativesTest() {
  new NativeLibrary("jinput") //
      .register(Platform.Windows_x86, "jinput-dx8.dll") //
      .register(Platform.Windows_x86, "jinput-raw.dll") //
      .register(Platform.Windows_x64, "jinput-dx8_64.dll") //
      .register(Platform.Windows_x64, "jinput-raw_64.dll") //
      .register(Platform.Linux_x86, "libjinput-linux.so") //
      .register(Platform.Linux_x64, "libjinput-linux64.so") //
      .register(Platform.MacOS, "libjinput-osx.jnilib") //
      .require(true) //
      .deleteOnExit(true)//
      .extractTo(new File("target", "jinput-natives")) //
      .load(Loaders.JAVA_LIBRARY_PATH__LOADER);

  final Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
  assertTrue(controllers.length > 0);
  for (final Controller controller : controllers) {
    System.out.println(controller.getName());
  }
}
 
Example 2
Project: G2Dj   File: GamepadHandler.java   View source code 6 votes vote down vote up
public GamepadHandler()
{
    ArrayList<Controller> controllers = new ArrayList<Controller>(Arrays.asList(ControllerEnvironment.getDefaultEnvironment().getControllers()));
    Controller current;
    
    for (int i=0;i<controllers.size();i++)
    {
        current = controllers.get(i);
        
        if (current.getType() == Controller.Type.GAMEPAD || current.getType() == Controller.Type.STICK)
            m_GamePads.add(new Gamepad(current));
        else
            controllers.remove(i--);
        
    }
    
    //m_JInputControllerHandles = controllers.toArray(new net.java.games.input.Controller[controllers.size()]);
    
}
 
Example 3
Project: Yass   File: YassInput.java   View source code 6 votes vote down vote up
/**
 * Description of the Method
 *
 * @param index Description of the Parameter
 * @return Description of the Return Value
 */
public static int getButtonController(int index) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();

    int controllerCount = 0;
    for (int d = 0; d < cs.length; d++) {
        Controller.Type type = cs[d].getType();
        if ((type == Controller.Type.GAMEPAD) || (type == Controller.Type.STICK) || cs[d].getName().toLowerCase().contains("wiimote") || cs[d].getName().toLowerCase().contains("shuttle")) {
            if (controllerCount == index) {
                return d;
            }
            controllerCount++;
        }
    }
    return -1;
}
 
Example 4
Project: Yass   File: YassInput.java   View source code 6 votes vote down vote up
/**
 * Gets the button attribute of the YassInput class
 *
 * @param d Description of the Parameter
 * @param i Description of the Parameter
 * @return The button value
 */
public static int getButton(int d, int i) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();

    int buttonCount = 0;
    Component[] comps = cs[d].getComponents();
    for (int c = 0; c < comps.length; c++) {
        if (!(comps[c].getIdentifier() instanceof Component.Identifier.Axis)) {
            if (buttonCount == i) {
                return c;
            }
            buttonCount++;
        }
    }
    return -1;
}
 
Example 5
Project: Yass   File: YassInput.java   View source code 6 votes vote down vote up
/**
 * Description of the Method
 */
public void pollAllButtons() {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    for (Controller cc : cs) {
        Controller.Type type = cc.getType();
        //if (type == Controller.Type.MOUSE || type == Controller.Type.KEYBOARD) {
            // continue;
        //} else
        if (type == Controller.Type.GAMEPAD || type == Controller.Type.STICK) {
            cc.poll();
        } else if (type == Controller.Type.UNKNOWN) {
            cc.poll();
        } else if (cc.getName().toLowerCase().contains("wiimote")) {
            cc.poll();
        }
    }
}
 
Example 6
Project: gamepad-control   File: ControllerUtils.java   View source code 6 votes vote down vote up
public static Controller findGamepad() throws InterruptedException {
    Controller instance;
    do {
        Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
        instance = Stream.of(controllers)
                .filter(controller -> controller.getType() == GAMEPAD)
                .findAny()
                .orElseGet(() -> {
                    LOG.error("No controllers of type GAMEPAD found! Will retry in 5 sec");
                    return null;
                });
        if (instance == null) {
            Thread.sleep(5000);
        }
    } while (instance == null);
    LOG.info("Found a controller of type GAMEPAD: {}", instance);
    return instance;
}
 
Example 7
Project: nullpomino   File: JInputManager.java   View source code 6 votes vote down vote up
/**
 * Init
 */
public static void initKeyboard() {
	controllerEnvironment = ControllerEnvironment.getDefaultEnvironment();
	controllers = controllerEnvironment.getControllers();

	for(int i = 0; i < controllers.length; i++) {
		Controller c = controllers[i];

		if((c.getType() == Controller.Type.KEYBOARD) && (c instanceof Keyboard)) {
			if(NullpoMinoSlick.useJInputKeyboard) {
				log.debug("initKeyboard: Keyboard found");
			}
			keyboard = (Keyboard)c;
		}
	}

	if((keyboard == null) && (NullpoMinoSlick.useJInputKeyboard)) {
		log.error("initKeyboard: Keyboard NOT FOUND");

		// Linux
		if(System.getProperty("os.name").startsWith("Linux")) {
			log.error("If you can use sudo, try the following command and start NullpoMino again:");
			log.error("sudo chmod go+r /dev/input/*");
		}
	}
}
 
Example 8
Project: UAVBCI   File: JoystickHandler.java   View source code 6 votes vote down vote up
public JoystickHandler()
{
	connectBuffer();
	
	Controller[] ca = ControllerEnvironment.getDefaultEnvironment()
			.getControllers();
	
	// find the joystick
	for (int i = 0; i < ca.length; i++)
	{
		
		if (ca[i].getType() ==Controller.Type.STICK )
		{
			joystick = ca[i];
			x_axis = joystick.getComponent(Component.Identifier.Axis.X);
			landButton = joystick.getComponent(Component.Identifier.Button.TRIGGER);
		}
	}		
}
 
Example 9
Project: JAwesomeEngine   File: JInputReader.java   View source code 6 votes vote down vote up
public void updateControllers() {
	mousecontrollers.clear();
	keyboardcontrollers.clear();
	gamepadcontrollers.clear();

	Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
	for (Controller c : ca) {
		Type t = c.getType();
		if (t == Type.MOUSE)
			mousecontrollers.add((Mouse) c);
		if (t == Type.KEYBOARD)
			keyboardcontrollers.add((Keyboard) c);
		if (t == Type.GAMEPAD)
			gamepadcontrollers.add(c);
	}
}
 
Example 10
Project: litiengine   File: GamepadManager.java   View source code 5 votes vote down vote up
private void updateGamepads() {
  this.handleHotPluggedControllers = true;
  try {
    this.hackTheShitOutOfJInputBecauseItSucksHard();
    // update plugged in gamepads
    for (int i = 0; i < ControllerEnvironment.getDefaultEnvironment().getControllers().length; i++) {
      final Controller controller = ControllerEnvironment.getDefaultEnvironment().getControllers()[i];
      final Type type = controller.getType();
      if (!type.equals(Type.GAMEPAD)) {
        continue;
      }

      final IGamepad existing = Input.getGamepad(i);
      if (existing != null && existing.getName().equals(controller.getName())) {
        // already added
        continue;
      }

      // add new gamepads
      final IGamepad newGamepad = new Gamepad(i, controller);
      Input.gamepads().add(newGamepad);
      for (final Consumer<IGamepad> cons : this.gamepadAddedConsumer) {
        cons.accept(newGamepad);
      }
    }
  } catch (IllegalStateException e) {
    this.loop.terminate();
  } finally {
    this.handleHotPluggedControllers = false;
  }
}
 
Example 11
Project: Yass   File: YassInput.java   View source code 5 votes vote down vote up
/**
 * Description of the Method
 */
public static void init() {
    try {
        // JInput bug: list is never refreshed
        ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
        ce.getControllers();
    } catch (java.lang.UnsatisfiedLinkError e) {
        System.out.println("No jinput-raw in java.library.path");
    }
}
 
Example 12
Project: Yass   File: YassInput.java   View source code 5 votes vote down vote up
/**
 * Gets the controllerIndex attribute of the YassInput object
 *
 * @param s Description of the Parameter
 * @return The controllerIndex value
 */
public int getControllerIndex(String s) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    for (int d = 0; d < cs.length; d++) {
        if (cs[d].getName().equals(s)) {
            return d;
        }
    }
    return -1;
}
 
Example 13
Project: Yass   File: YassInput.java   View source code 5 votes vote down vote up
/**
 * Gets the controller attribute of the YassInput object
 *
 * @param s Description of the Parameter
 * @return The controller value
 */
public Controller getController(String s) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    for (Controller c : cs) {
        if (c.getName().equals(s)) {
            return c;
        }
    }
    return null;
}
 
Example 14
Project: Yass   File: YassInput.java   View source code 5 votes vote down vote up
/**
 * Gets the componentIndex attribute of the YassInput object
 *
 * @param d Description of the Parameter
 * @param s Description of the Parameter
 * @return The componentIndex value
 */
public int getComponentIndex(int d, String s) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    Component[] comps = cs[d].getComponents();
    for (int c = 0; c < comps.length; c++) {
        if (comps[c].getName().equals(s)) {
            return c;
        }
    }
    return -1;
}
 
Example 15
Project: Yass   File: YassInput.java   View source code 5 votes vote down vote up
/**
 * Gets the value attribute of the YassInput object
 *
 * @param d Description of the Parameter
 * @param c Description of the Parameter
 * @return The value value
 */
public int getButtonValue(int d, int c) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    Component[] comps = cs[d].getComponents();
    float val = comps[c].getPollData();
    if (comps[c].getIdentifier() instanceof Component.Identifier.Axis) {
        return 0;
    }
    if (val < comps[c].getDeadZone()) {
        return 0;
    }
    return (int) val;
}
 
Example 16
Project: snobot-2017   File: JoystickDiscoverer.java   View source code 5 votes vote down vote up
public static Map<String, ControllerConfiguration> rediscoverJoysticks()
{
    Controller[] availableControllers = ControllerEnvironment.getDefaultEnvironment().getControllers();

    Map<String, ControllerConfiguration> controllerConfig = new LinkedHashMap<>();
    Map<String, Integer> deviceCounter = new LinkedHashMap<>();

    controllerConfig.clear();
    for (Controller controller : availableControllers)
    {
        if (sTYPES_TO_IGNORE.contains(controller.getType()))
        {
            continue;
        }

        Class<? extends IMockJoystick> specializationClass = getDefaultSpecialization(controller);

        int deviceCtr = deviceCounter.getOrDefault(controller.getName(), 0);
        deviceCounter.put(controller.getName(), ++deviceCtr);

        String deviceName = controller.getName() + " " + deviceCtr;

        ControllerConfiguration configuration = new ControllerConfiguration(controller, specializationClass);
        controllerConfig.put(deviceName, configuration);
    }

    return sort(controllerConfig);
}
 
Example 17
Project: qupath   File: AdvancedControllerActionFactory.java   View source code 5 votes vote down vote up
/**
	 * Get controllers from JInput that would be suitable
	 * 
	 * @return
	 */
	private static List<Controller> getCompatibleControllers() {
		ControllerEnvironment controllerEnvironment = ControllerEnvironment.getDefaultEnvironment(); 
//		controllerEnvironment.addControllerListener(new ControllerListener() {
//			public void controllerRemoved(ControllerEvent ev) {
//				System.err.println("ADDED: " + ev);
//			}
//
//		    /**
//		     * Invoked when a controller has been added.
//		     */
//		    public void controllerAdded(ControllerEvent ev) {
//				System.err.println("REMOVED: " + ev);
//		    }
//		});
		Controller[] controllers = controllerEnvironment.getControllers();
		List<Controller> advancedControllers = new ArrayList<>();
		for (Controller controller : controllers) {
			// For now, we only support 'Space Navigator' controllers... being more permissive can cause strange things to happen
			// (in particular, things go very badly wrong with VirtualBox)
			if (controller.getType() == Type.STICK && controller.getName().toLowerCase().equals("spacenavigator")) {
//			if (controller.getType() == Type.STICK || !controller.getName().toLowerCase().contains("virtualbox")) {
				logger.info("Registering controller: " + controller.getName() + ", " + controller.getType() ); 
				advancedControllers.add(controller);
			} else
				logger.trace("Skipping controller: " + controller.getName() + ", " + controller.getType() ); 
		}
		return advancedControllers;
	}
 
Example 18
Project: project-bianca   File: Joystick.java   View source code 5 votes vote down vote up
public HashMap<String, Integer> getControllers() {
	log.info(String.format("%s getting controllers", getName()));
	controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
	info(String.format("found %d controllers", controllers.length));
	controllerNames.clear();
	for (int i = 0; i < controllers.length; i++) {
		log.info(String.format("Found input device: %d %s", i, controllers[i].getName()));
		controllerNames.put(String.format("%d - %s", i, controllers[i].getName()), i);
	}
	return controllerNames;
}
 
Example 19
Project: Nes-emulator   File: ControllerImpl.java   View source code 5 votes vote down vote up
/**
 * This method detects the available joysticks / gamepads on the computer
 * and return them in a list.
 *
 * @return List of available joysticks / gamepads connected to the computer
 */
private static Controller[] getAvailablePadControllers() {
    List<Controller> gameControllers = new ArrayList<Controller>();
    // Get a list of the controllers JInput knows about and can interact
    // with
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
    // Check the useable controllers (gamepads or joysticks with at least 2
    // axis and 2 buttons)
    for (Controller controller : controllers) {
        if ((controller.getType() == Controller.Type.GAMEPAD) || (controller.getType() == Controller.Type.STICK)) {
            int nbOfAxis = 0;
            // Get this controllers components (buttons and axis)
            Component[] components = controller.getComponents();
            // Check the availability of X/Y axis and at least 2 buttons
            // (for A and B, because select and start can use the keyboard)
            for (Component component : components) {
                if ((component.getIdentifier() == Component.Identifier.Axis.X)
                        || (component.getIdentifier() == Component.Identifier.Axis.Y)) {
                    nbOfAxis++;
                }
            }
            if ((nbOfAxis >= 2) && (getButtons(controller).length >= 2)) {
                // Valid game controller
                gameControllers.add(controller);
            }
        }
    }
    return gameControllers.toArray(new Controller[0]);
}
 
Example 20
Project: ROV   File: PS3Controller.java   View source code 5 votes vote down vote up
public PS3Controller()
{
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
    for(Controller eachController : controllers) {
        if(eachController.getName().equals("PLAYSTATION(R)3 Controller")) {
            mainController = eachController;
            components = mainController.getComponents();
        }
    }
}
 
Example 21
Project: Noble   File: ListenerThread.java   View source code 5 votes vote down vote up
public void test() {
	Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();

    for (Controller controller : controllers) {
        
        if(controller.getType() == Controller.Type.STICK) {
        	System.out.println("c: " + controller.getName() + "; " + controller.getPortNumber());
        }
    }
}
 
Example 22
Project: J-ARDrone2   File: JoystickController.java   View source code 5 votes vote down vote up
private Controller findController() {
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
    for(int i = 0; i < controllers.length; i++){
        Controller controller = controllers[i];
        if (controller.getType() == Controller.Type.STICK) {
            return controller;
        }
    }
    return null;
}
 
Example 23
Project: UAVBCI   File: KeyBoardHandler.java   View source code 5 votes vote down vote up
public KeyBoardHandler()
	{
		//connectBuffer();
		
		Controller[] ca = ControllerEnvironment.getDefaultEnvironment()
				.getControllers();
		
		// find the joystick
		for (int i = 0; i < ca.length; i++)
		{
			System.out.println(ca[i].getType().toString());
			
			if (ca[i].getType() ==Controller.Type.KEYBOARD )
			{
				keyboard = (Keyboard) ca[i];
				//x_axis = keyboard.getComponent(Component.Identifier.Butt);
				
			}
		}	
		//print components
//		for (Component c : keyboard.getComponents())
//		{
//			System.out.println(c);
//			if (c.toString().equals("W"))
//			{
//				System.out.println("Yay");
//			}
//		
//		}
	}
 
Example 24
Project: JDriverStation   File: JoystickManager.java   View source code 5 votes vote down vote up
@SuppressWarnings("CallToThreadRun")
public boolean rescan() {
    synchronized (joysticks) {
        joysticks.clear();
        joystickIds.clear();
        joystickPositions.clear();
        joysticks.stream().forEach(this::fireJoystickRemoved);
    }
    controllerEnvironment.removeControllerListener(controllerListener);

    final Collection<Thread> shutdownHooks = ControllerEnvironmentHack.getShutdownHooks();
    if (shutdownHooks != null) {
        final Class linuxEnvClass = ControllerEnvironmentHack.getLinuxEnvironmentShutdownHookClass();
        if (linuxEnvClass != null) {
            Thread linuxEnvHook = null;
            for (Thread hook : shutdownHooks) {
                if (linuxEnvClass.isAssignableFrom(hook.getClass())) {
                    linuxEnvHook = hook;
                }
            }
            if (linuxEnvHook != null) {
                linuxEnvHook.run();
                Runtime.getRuntime().removeShutdownHook(linuxEnvHook);
            }
        }
    }

    final ControllerEnvironment ce = ControllerEnvironmentHack.newDefaultControllerEnvironment();
    if (ce == null) {
        return false;
    }

    ce.addControllerListener(controllerListener);
    ControllerEnvironmentHack.setDefaultEnvironment(ce);

    controllerEnvironment = ce;
    initializeJoysticks();

    return true;
}
 
Example 25
Project: JDriverStation   File: JoystickManager.java   View source code 5 votes vote down vote up
public static void setDefaultEnvironment(ControllerEnvironment ce) {
    try {
        defaultEnvironment.set(null, ce);
    } catch (IllegalArgumentException | NullPointerException | IllegalAccessException ex) {
        logger.log(Level.WARNING, "Unable to reinitialize ControllerEnvironment:", ex);
    }
}
 
Example 26
Project: JDriverStation   File: JoystickManager.java   View source code 5 votes vote down vote up
public static ControllerEnvironment newDefaultControllerEnvironment() {
    try {
        return defaultControllerEnvironmentConstructor.newInstance();
    } catch (InstantiationException | NullPointerException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        logger.log(Level.WARNING, "Unable to create new DefaultControllerEnvironment:", ex);
    }
    return null;
}
 
Example 27
Project: myrobotlab   File: Joystick.java   View source code 5 votes vote down vote up
public Map<String, Integer> getControllers() {
  log.info(String.format("%s getting controllers", getName()));
  hardwareControllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
  info(String.format("found %d controllers", hardwareControllers.length));
  controllerNames.clear();
  for (int i = 0; i < hardwareControllers.length; i++) {
    log.info(String.format("Found input device: %d %s", i, hardwareControllers[i].getName()));
    controllerNames.put(String.format("%d - %s", i, hardwareControllers[i].getName()), i);
  }
  return controllerNames;
}
 
Example 28
Project: halfnes   File: ControllerImpl.java   View source code 5 votes vote down vote up
/**
 * This method detects the available joysticks / gamepads on the computer
 * and return them in a list.
 *
 * @return List of available joysticks / gamepads connected to the computer
 */
private static Controller[] getAvailablePadControllers() {
    List<Controller> gameControllers = new ArrayList<>();
    // Get a list of the controllers JInput knows about and can interact
    // with
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
    // Check the useable controllers (gamepads or joysticks with at least 2
    // axis and 2 buttons)
    for (Controller controller : controllers) {
        if ((controller.getType() == Controller.Type.GAMEPAD) || (controller.getType() == Controller.Type.STICK)) {
            int nbOfAxis = 0;
            // Get this controllers components (buttons and axis)
            Component[] components = controller.getComponents();
            // Check the availability of X/Y axis and at least 2 buttons
            // (for A and B, because select and start can use the keyboard)
            for (Component component : components) {
                if ((component.getIdentifier() == Component.Identifier.Axis.X)
                        || (component.getIdentifier() == Component.Identifier.Axis.Y)) {
                    nbOfAxis++;
                }
            }
            if ((nbOfAxis >= 2) && (getButtons(controller).length >= 2)) {
                // Valid game controller
                gameControllers.add(controller);
            }
        }
    }
    return gameControllers.toArray(new Controller[0]);
}
 
Example 29
Project: halfnes   File: JInputTest.java   View source code 5 votes vote down vote up
@Test
public void testJInput() {
    ControllerEnvironment controllerEnvironment = ControllerEnvironment.getDefaultEnvironment();
    Controller[] controllers = controllerEnvironment.getControllers();
    System.out.println(String.format("%s controllers found.", controllers.length));
    Arrays.asList(controllers).forEach(controller -> {
        System.out.println(String.format("  %s (%s)", controller, controller.getType()));
    });
}
 
Example 30
Project: AI-project   File: JoystickFrame.java   View source code 5 votes vote down vote up
private void initialize() {
    
    System.out.println("initialize Joystick");
    System.out.println("read button map");
    this.buttonMap = new ButtonMap();
    XStream xstream = new XStream(new DomDriver());
    try {
        FileInputStream fin = new FileInputStream("buttonMap.sav");
        try (ObjectInputStream ois = new ObjectInputStream(fin)) {
            String xml = (String) ois.readObject();
            buttonMap = (ButtonMap) xstream.fromXML(xml);
        }
    } catch (IOException | ClassNotFoundException e) {
        System.out.println(e.getMessage());
    }
    jComboBox1.removeAllItems();
    ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
    int j = 0;
    for (int i = 0; i < ca.length; i++) {
        jComboBox1.insertItemAt(ca[i], i);
        if (buttonMap != null && ca[i].getName().equals(buttonMap.getContoller())) {
            controllerFound = true;
            j = i;
        }
    }
    controllerSelected = ca[j];
    jComboBox1.setSelectedIndex(j);
}
 
Example 31
Project: terminal-recall   File: GamepadInputDeviceServiceFactory.java   View source code 5 votes vote down vote up
public GamepadInputDeviceService(){
    try{final JVM jvm = new JVM();
    if(operatingSystem.contains("win")){
	jvm.loadFromJarToFile("/jinput-dx8_64.dll");
	jvm.loadFromJarToFile("/jinput-dx8.dll");
	jvm.loadFromJarToFile("/jinput-raw_64.dll");
	jvm.loadFromJarToFile("/jinput-raw.dll");
	jvm.loadFromJarToFile("/jinput-wintab.dll");
    } else if(operatingSystem.contains("linux")){
	jvm.loadFromJarToFile("/libjinput-linux64.so");
	jvm.loadFromJarToFile("/libjinput-linux.so");
    } else if(operatingSystem.contains("mac")){
	jvm.loadFromJarToFile("/libjinput-osx.jnilib");
    } else
	System.err.println("Warning: Couldn't determine OS; jInput will likely fail to load.");
    System.setProperty("net.java.games.input.librarypath", new File("DeleteMe").getAbsolutePath());
    }catch(Exception e){e.printStackTrace();}
    this.controllerEnvironment = ControllerEnvironment.getDefaultEnvironment();
    //Filter out the keyboards in favor of AWT's event-based tracking
    for(Controller controller:controllerEnvironment.getControllers()){
	final Type type = controller.getType();
	if(     type != Controller.Type.KEYBOARD && 
		type != Controller.Type.MOUSE    &&
		type != Controller.Type.TRACKBALL)
	    inputDevices.add(new GamepadInputDevice(controller));
    }//end for(controller types)
}
 
Example 32
Project: java-natives   File: JInputTest.java   View source code 4 votes vote down vote up
@Test
public void missingNativesTest() {
  Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
  assertTrue(controllers.length == 0);
}
 
Example 33
Project: Yass   File: YassInput.java   View source code 4 votes vote down vote up
/**
 * Gets the controllerCount attribute of the YassInput object
 *
 * @return The controllerCount value
 */
public int getControllerCount() {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    return cs.length;
}
 
Example 34
Project: JDriverStation   File: JoystickManager.java   View source code 4 votes vote down vote up
public JoystickManager() {
    controllerEnvironment = ControllerEnvironment.getDefaultEnvironment();
    initializeJoysticks();

    controllerListener = new ControllerListener() {

        @Override
        public void controllerRemoved(final ControllerEvent ev) {

            final Controller c = ev.getController();
            final Controller.Type t = c.getType();
            if (t.equals(Controller.Type.STICK) || t.equals(Controller.Type.GAMEPAD)) {
                synchronized (joysticks) {
                    joysticks.remove(ev.getController());
                    joystickIds.remove(c);
                }
                SwingUtilities.invokeLater(() -> fireJoystickRemoved(c));
            }

        }

        @Override
        public void controllerAdded(final ControllerEvent ev) {
            SwingUtilities.invokeLater(() -> {
                final Controller c = ev.getController();
                final Controller.Type t = c.getType();
                if (t.equals(Controller.Type.STICK) || t.equals(Controller.Type.GAMEPAD)) {
                    synchronized (joysticks) {
                        joystickIds.put(c, joystickIds.size());
                        joysticks.add(ev.getController());
                    }
                    SwingUtilities.invokeLater(() -> fireJoystickAdded(c));
                }
            });
        }
    };
    controllerEnvironment.addControllerListener(controllerListener);

    getCommunicationManager().setJoystickUpdater(robotJoysticks -> {
        synchronized (joysticks) {
            for (int j = 0; j < Math.min(joysticks.size(), robotJoysticks.size()); j++) {
                final Controller dsJoystick = joysticks.get(j);
                final Joystick robotJoystick = robotJoysticks.get(j);

                if (dsJoystick.poll()) {

                    final Component[] components = dsJoystick.getComponents();

                    int axisIndex = 1;
                    int buttonIndex = 1;
                    for (Component c : components) {
                        if (c.isAnalog() && axisIndex <= Joystick.NUM_AXES) {
                            robotJoystick.setAxis(axisIndex++, c.getPollData());
                        } else if (buttonIndex <= Joystick.NUM_BUTTONS) {
                            robotJoystick.setButton(buttonIndex++, c.getPollData() == 1.0f);
                        }
                    }
                } else {
                    joysticks.remove(j);
                    SwingUtilities.invokeLater(() -> fireJoystickRemoved(dsJoystick));
                }
            }
        }
    });
}
 
Example 35
Project: Yass   File: YassInput.java   View source code 3 votes vote down vote up
/**
 * Description of the Method
 *
 * @param d Description of the Parameter
 * @param c Description of the Parameter
 * @return Description of the Return Value
 */
public String toString(int d, int c) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    Component[] comps = cs[d].getComponents();
    return d + ":" + cs[d].getType() + " - " + comps[c].getName();
}
 
Example 36
Project: Yass   File: YassInput.java   View source code 2 votes vote down vote up
/**
 * Gets the controller attribute of the YassInput class
 *
 * @param i Description of the Parameter
 * @return The controller value
 */
private Controller getController(int i) {
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();
    return cs[i];
}