From 7dc8f236db5df40f7535a08cc546ea3e57033910 Mon Sep 17 00:00:00 2001 From: Iwan Timmer Date: Tue, 28 Jan 2014 14:49:58 +0100 Subject: [PATCH] Possible to reverse directions in gamepad mapping --- mappings/default.conf | 9 +++++++++ src/com/limelight/input/EvdevAbsolute.java | 16 ++++++++++------ src/com/limelight/input/EvdevHandler.java | 16 ++++++++-------- src/com/limelight/input/GamepadMapping.java | 13 +++++++++++-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/mappings/default.conf b/mappings/default.conf index 854ff49..8af1640 100644 --- a/mappings/default.conf +++ b/mappings/default.conf @@ -10,6 +10,15 @@ abs_ry = 4 abs_throttle = 6 abs_rudder = 7 +reverse_x = false +reverse_y = false +reverse_rx = false +reverse_ry = false +reverse_dpad_y = false +reverse_dpad_x = false +reverse_throttle = false +reverse_rudder = false + btn_south = 304 btn_east = 305 btn_north = 307 diff --git a/src/com/limelight/input/EvdevAbsolute.java b/src/com/limelight/input/EvdevAbsolute.java index 4aea23f..5534b6e 100644 --- a/src/com/limelight/input/EvdevAbsolute.java +++ b/src/com/limelight/input/EvdevAbsolute.java @@ -18,7 +18,9 @@ public class EvdevAbsolute { private int avg; private int range; - public EvdevAbsolute(String filename, int axis) { + private boolean reverse; + + public EvdevAbsolute(String filename, int axis, boolean reverse) { ByteBuffer buffer = ByteBuffer.allocate(6*4); buffer.order(ByteOrder.nativeOrder()); byte[] data = buffer.array(); @@ -30,6 +32,8 @@ public class EvdevAbsolute { int max = buffer.getInt(); avg = (min+max)/2; range = max-avg; + + this.reverse = reverse; } private int getRequest(int dir, int type, int nr, int size) { @@ -42,7 +46,7 @@ public class EvdevAbsolute { * @return input value as short */ public short getShort(int value) { - return (short) ((value-avg) * range / Short.MAX_VALUE); + return (short) ((value-avg) * (reverse?-range:range) / Short.MAX_VALUE); } /** @@ -51,7 +55,7 @@ public class EvdevAbsolute { * @return input value as byte */ public byte getByte(int value) { - return (byte) ((value-avg) * range / Byte.MAX_VALUE); + return (byte) ((value-avg) * (reverse?-range:range) / Byte.MAX_VALUE); } /** @@ -59,11 +63,11 @@ public class EvdevAbsolute { * @param value received input * @return input value as direction */ - public byte getDirection(int value) { + public int getDirection(int value) { if (value>(avg+range/4)) - return UP; + return (reverse?DOWN:UP); else if (value<(avg-range/4)) - return DOWN; + return (reverse?UP:DOWN); else return NONE; } diff --git a/src/com/limelight/input/EvdevHandler.java b/src/com/limelight/input/EvdevHandler.java index 097e1a0..34f0e08 100644 --- a/src/com/limelight/input/EvdevHandler.java +++ b/src/com/limelight/input/EvdevHandler.java @@ -50,14 +50,14 @@ 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); - absDX = new EvdevAbsolute(device, mapping.abs_dpad_x); - absDY = new EvdevAbsolute(device, mapping.abs_dpad_y); + 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); + absRY = new EvdevAbsolute(device, mapping.abs_ry, mapping.reverse_ry); + absLT = new EvdevAbsolute(device, mapping.abs_rudder, mapping.reverse_rudder); + absRT = new EvdevAbsolute(device, mapping.abs_throttle, mapping.reverse_throttle); + absDX = new EvdevAbsolute(device, mapping.abs_dpad_x, mapping.reverse_dpad_x); + absDY = new EvdevAbsolute(device, mapping.abs_dpad_y, mapping.reverse_dpad_y); translator = new KeyboardTranslator(conn); } diff --git a/src/com/limelight/input/GamepadMapping.java b/src/com/limelight/input/GamepadMapping.java index e9b76c2..823d7b2 100644 --- a/src/com/limelight/input/GamepadMapping.java +++ b/src/com/limelight/input/GamepadMapping.java @@ -24,6 +24,11 @@ public class GamepadMapping { public short abs_throttle = EvdevConstants.ABS_THROTTLE; public short abs_rudder = EvdevConstants.ABS_RUDDER; + public boolean reverse_x, reverse_y; + public boolean reverse_rx, reverse_ry; + public boolean reverse_dpad_y, reverse_dpad_x; + public boolean reverse_throttle, reverse_rudder; + public short btn_south = EvdevConstants.BTN_SOUTH; public short btn_east = EvdevConstants.BTN_EAST; public short btn_north = EvdevConstants.BTN_NORTH; @@ -48,8 +53,12 @@ public class GamepadMapping { for (Map.Entry entry:props.entrySet()) { try { - Field field = this.getClass().getField(entry.getKey().toString()); - field.setShort(this, Short.parseShort(entry.getValue().toString())); + String key = entry.getKey().toString(); + Field field = this.getClass().getField(key); + if (key.startsWith("reverse_")) + field.setBoolean(this, Boolean.parseBoolean(entry.getValue().toString())); + else + field.setShort(this, Short.parseShort(entry.getValue().toString())); } catch (NoSuchFieldException e) { System.err.println("No mapping found named " + entry.getKey()); } catch (NumberFormatException e) {