Add support for multiple controllers

This commit is contained in:
Cameron Gutman
2015-02-19 18:49:53 -05:00
parent 545c3397db
commit ff344cfc30
3 changed files with 55 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
package com.limelight.input;
import com.limelight.LimeLog;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.ControllerPacket;
import com.limelight.nvstream.input.KeyboardPacket;
@@ -20,11 +21,16 @@ public class EvdevHandler extends EvdevReader {
/* GFE's prefix for every key code */
public static final short KEY_PREFIX = (short) 0x80;
/* Global controller ID state */
private static int assignedControllerIds = 0;
private static Object controllerIdLock = new Object();
/* Gamepad state */
private short buttonFlags;
private byte leftTrigger, rightTrigger;
private short leftStickX, leftStickY, rightStickX, rightStickY;
private boolean gamepadSynced;
private short controllerId = -1;
private short mouseDeltaX, mouseDeltaY;
private byte mouseScroll;
@@ -41,9 +47,6 @@ public class EvdevHandler extends EvdevReader {
this.conn = conn;
this.mapping = mapping;
// We want limelight-common to scale the axis values to match Xinput values
ControllerPacket.enableAxisScaling = true;
absLX = new EvdevAbsolute(device, mapping.abs_x, mapping.reverse_x);
absLY = new EvdevAbsolute(device, mapping.abs_y, !mapping.reverse_y);
absRX = new EvdevAbsolute(device, mapping.abs_rx, mapping.reverse_rx);
@@ -57,6 +60,30 @@ public class EvdevHandler extends EvdevReader {
gamepadSynced = true;
}
private void assignNewControllerId() {
synchronized (controllerIdLock) {
for (short i = 0; i < 4; i++) {
if ((assignedControllerIds & (1 << i)) == 0) {
// Assign this unused value to this input device
assignedControllerIds |= (1 << i);
controllerId = i;
LimeLog.info("Assigning controller ID "+i);
return;
}
}
}
// All IDs have been assigned so use controller 0 for the rest
controllerId = 0;
}
private void releaseControllerId() {
LimeLog.info("Releasing controller ID "+controllerId);
synchronized (controllerIdLock) {
assignedControllerIds &= ~(1 << controllerId);
}
}
@Override
protected void parseEvent(ByteBuffer buffer) {
if (buffer.limit()==EvdevConstants.MAX_STRUCT_SIZE_BYTES) {
@@ -73,7 +100,16 @@ public class EvdevHandler extends EvdevReader {
if (type==EvdevConstants.EV_SYN) {
if (!gamepadSynced) {
conn.sendControllerInput(buttonFlags, leftTrigger, rightTrigger, leftStickX, leftStickY, rightStickX, rightStickY);
// Assign a controller ID if one hasn't been assigned yet.
// Note that we're only assigning IDs to things that actually send
// some form of controller input to avoid falsely reserving IDs for
// devices that aren't actually controllers or are inactive controllers.
if (controllerId < 0) {
assignNewControllerId();
}
conn.sendControllerInput(controllerId, buttonFlags, leftTrigger, rightTrigger,
leftStickX, leftStickY, rightStickX, rightStickY);
gamepadSynced = true;
}
if (mouseDeltaX != 0 || mouseDeltaY != 0) {
@@ -222,6 +258,14 @@ public class EvdevHandler extends EvdevReader {
gamepadSynced &= !gamepadModified;
}
@Override
protected void deviceRemoved() {
// Release this device's controller ID (if it has one)
if (controllerId >= 0) {
releaseControllerId();
}
}
private short accountForDeadzone(short value) {
return Math.abs(value) > mapping.abs_deadzone?value:0;
}

View File

@@ -43,6 +43,7 @@ public abstract class EvdevReader implements Runnable {
}
protected abstract void parseEvent(ByteBuffer buffer);
protected abstract void deviceRemoved();
@Override
public void run() {
@@ -54,6 +55,7 @@ public abstract class EvdevReader implements Runnable {
}
} catch (IOException e) {
LimeLog.warning("Input device removed");
deviceRemoved();
}
}

View File

@@ -75,6 +75,11 @@ public class GamepadMapper extends EvdevReader {
notify();
}
}
@Override
protected void deviceRemoved() {
// Nothing for us to do
}
public synchronized void readKey(String key, String name) throws InterruptedException {
System.out.println(name);