Use range from evdev device for gamepad input

This commit is contained in:
Iwan Timmer
2014-01-28 01:34:45 +01:00
parent c1426e7f28
commit 41598aea73
7 changed files with 115 additions and 6 deletions

View File

@@ -0,0 +1,55 @@
package com.limelight.input;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* Class that handles absolute input ranges from evdev device
* @author Iwan Timmer
*/
public class EvdevAbsolute {
private final static int ABS_OFFSET = 0x40;
private final static int READ_ONLY = 2;
private final static char EVDEV_TYPE = 'E';
private int avg;
private int range;
public EvdevAbsolute(String filename, int axis) {
ByteBuffer buffer = ByteBuffer.allocate(6*4);
buffer.order(ByteOrder.nativeOrder());
byte[] data = buffer.array();
int request = getRequest(READ_ONLY, EVDEV_TYPE, ABS_OFFSET+axis, 6*4);
IO.ioctl(filename, data, request);
buffer.getInt(); //Skip current value
int min = buffer.getInt();
int max = buffer.getInt();
avg = (min+max)/2;
range = max-avg;
}
private int getRequest(int dir, int type, int nr, int size) {
return (dir << 30) | (size << 16) | (type << 8) | nr;
}
/**
* Convert input value to short range
* @param value received input
* @return input value as short
*/
public short getShort(int value) {
return (short) ((value-avg) * range / Short.MAX_VALUE);
}
/**
* Convert input value to byte range
* @param value received input
* @return input value as byte
*/
public byte getByte(int value) {
return (byte) ((value-avg) * range / Byte.MAX_VALUE);
}
}

View File

@@ -28,6 +28,8 @@ public class EvdevHandler implements Runnable {
private byte leftTrigger, rightTrigger;
private short leftStickX, leftStickY, rightStickX, rightStickY;
private EvdevAbsolute absLX, absLY, absRX, absRY, absLT, absRT;
private NvConnection conn;
private FileChannel deviceInput;
private ByteBuffer inputBuffer;
@@ -48,6 +50,13 @@ public class EvdevHandler implements Runnable {
inputBuffer = ByteBuffer.allocate(EvdevConstants.MAX_STRUCT_SIZE_BYTES);
inputBuffer.order(ByteOrder.nativeOrder());
absLX = new EvdevAbsolute(device, mapping.abs_x);
absLY = new EvdevAbsolute(device, mapping.abs_y);
absRX = new EvdevAbsolute(device, mapping.abs_rx);
absRY = new EvdevAbsolute(device, mapping.abs_ry);
absLT = new EvdevAbsolute(device, mapping.abs_rudder);
absRT = new EvdevAbsolute(device, mapping.abs_throttle);
translator = new KeyboardTranslator(conn);
}
@@ -140,17 +149,17 @@ public class EvdevHandler implements Runnable {
conn.sendMouseMove((short) 0, (short) value);
} else if (type==EvdevConstants.EV_ABS) {
if (code==mapping.abs_x)
leftStickX = (short) value;
leftStickX = absLX.getShort(value);
else if (code==mapping.abs_y)
leftStickY = (short) value;
leftStickY = absLY.getShort(value);
else if (code==mapping.abs_rx)
rightStickX = (short) value;
rightStickX = absRX.getShort(value);
else if (code==mapping.abs_ry)
rightStickY = (short) value;
rightStickY = absRY.getShort(value);
else if (code==mapping.abs_throttle)
leftTrigger = (byte) value;
leftTrigger = absLT.getByte(value);
else if (code==mapping.abs_rudder)
rightTrigger = (byte) value;
rightTrigger = absRT.getByte(value);
conn.sendControllerInput(buttonFlags, leftTrigger, rightTrigger, leftStickX, leftStickY, rightStickX, rightStickY);
}

View File

@@ -0,0 +1,14 @@
package com.limelight.input;
/**
* IO bindings
* @author Iwan Timmer
*/
public class IO {
static {
System.loadLibrary("nv_io");
}
public static native boolean ioctl(String filename, byte[] buffer, int request);
}