summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorendolf <[email protected]>2007-08-12 20:09:14 +0000
committerendolf <[email protected]>2007-08-12 20:09:14 +0000
commita46e691452656fa28a840390fcf9b9a6280f5ebd (patch)
tree4e5ace09ec4e39b97af40fc59c3c6c4388d52081
parentb1cb9d95371294aa6074fac10e2c5a1876403171 (diff)
Make the joystick plugin on linux respect POV Hats
git-svn-id: file:///home/sven/projects/JOGL/git-svn/svn-server-sync/jinput/trunk@200 e343933a-64c8-49c5-92b1-88f2ce3e89e8
-rw-r--r--plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java29
-rw-r--r--plugins/linux/src/java/net/java/games/input/LinuxJoystickAxis.java9
-rw-r--r--plugins/linux/src/java/net/java/games/input/LinuxJoystickDevice.java34
-rw-r--r--plugins/linux/src/java/net/java/games/input/LinuxJoystickPOV.java55
4 files changed, 124 insertions, 3 deletions
diff --git a/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java b/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java
index ebfc0f0..2b93099 100644
--- a/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java
+++ b/plugins/linux/src/java/net/java/games/input/LinuxEnvironmentPlugin.java
@@ -314,6 +314,8 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
List components = new ArrayList();
byte[] axisMap = device.getAxisMap();
char[] buttonMap = device.getButtonMap();
+ LinuxJoystickAxis[] hatBits = new LinuxJoystickAxis[6];
+
for (int i = 0; i < device.getNumButtons(); i++) {
Component.Identifier.Button button_id = (Component.Identifier.Button)LinuxNativeTypesMap.getButtonID(buttonMap[i]);
if (button_id != null) {
@@ -326,8 +328,33 @@ public final class LinuxEnvironmentPlugin extends ControllerEnvironment implemen
Component.Identifier.Axis axis_id;
axis_id = (Component.Identifier.Axis) LinuxNativeTypesMap.getAbsAxisID(axisMap[i]);
LinuxJoystickAxis axis = new LinuxJoystickAxis(axis_id);
+
device.registerAxis(i, axis);
- components.add(axis);
+
+ if(axisMap[i]==NativeDefinitions.ABS_HAT0X) {
+ hatBits[0] = axis;
+ } else if(axisMap[i]==NativeDefinitions.ABS_HAT0Y) {
+ hatBits[1] = axis;
+ axis = new LinuxJoystickPOV(Component.Identifier.Axis.POV, hatBits[0], hatBits[1]);
+ device.registerPOV((LinuxJoystickPOV)axis);
+ components.add(axis);
+ } else if(axisMap[i]==NativeDefinitions.ABS_HAT1X) {
+ hatBits[2] = axis;
+ } else if(axisMap[i]==NativeDefinitions.ABS_HAT1Y) {
+ hatBits[3] = axis;
+ axis = new LinuxJoystickPOV(Component.Identifier.Axis.POV, hatBits[2], hatBits[3]);
+ device.registerPOV((LinuxJoystickPOV)axis);
+ components.add(axis);
+ } else if(axisMap[i]==NativeDefinitions.ABS_HAT2X) {
+ hatBits[4] = axis;
+ } else if(axisMap[i]==NativeDefinitions.ABS_HAT2Y) {
+ hatBits[5] = axis;
+ axis = new LinuxJoystickPOV(Component.Identifier.Axis.POV, hatBits[4], hatBits[5]);
+ device.registerPOV((LinuxJoystickPOV)axis);
+ components.add(axis);
+ } else {
+ components.add(axis);
+ }
}
return new LinuxJoystickAbstractController(device, (Component[])components.toArray(new Component[]{}), new Controller[]{}, new Rumbler[]{});
diff --git a/plugins/linux/src/java/net/java/games/input/LinuxJoystickAxis.java b/plugins/linux/src/java/net/java/games/input/LinuxJoystickAxis.java
index 0b2621b..47adc99 100644
--- a/plugins/linux/src/java/net/java/games/input/LinuxJoystickAxis.java
+++ b/plugins/linux/src/java/net/java/games/input/LinuxJoystickAxis.java
@@ -46,9 +46,15 @@ import java.io.IOException;
*/
class LinuxJoystickAxis extends AbstractComponent {
private float value;
+ private boolean analog;
public LinuxJoystickAxis(Component.Identifier.Axis axis_id) {
+ this(axis_id, true);
+ }
+
+ public LinuxJoystickAxis(Component.Identifier.Axis axis_id, boolean analog) {
super(axis_id.getName(), axis_id);
+ this.analog = analog;
}
public final boolean isRelative() {
@@ -56,11 +62,12 @@ class LinuxJoystickAxis extends AbstractComponent {
}
public final boolean isAnalog() {
- return true;
+ return analog;
}
final void setValue(float value) {
this.value = value;
+ resetHasPolled();
}
protected final float poll() throws IOException {
diff --git a/plugins/linux/src/java/net/java/games/input/LinuxJoystickDevice.java b/plugins/linux/src/java/net/java/games/input/LinuxJoystickDevice.java
index bc37e7b..0dcd5da 100644
--- a/plugins/linux/src/java/net/java/games/input/LinuxJoystickDevice.java
+++ b/plugins/linux/src/java/net/java/games/input/LinuxJoystickDevice.java
@@ -48,6 +48,8 @@ final class LinuxJoystickDevice implements LinuxDevice {
private final Event event = new Event();
private final LinuxJoystickButton[] buttons;
private final LinuxJoystickAxis[] axes;
+ private final Map povXs = new HashMap();
+ private final Map povYs = new HashMap();
private final byte[] axisMap;
private final char[] buttonMap;
@@ -100,7 +102,17 @@ final class LinuxJoystickDevice implements LinuxDevice {
if (axis != null) {
float value = (float)joystick_event.getValue()/AXIS_MAX_VALUE;
axis.setValue(value);
- event.set(axis, value, joystick_event.getNanos());
+ if(povXs.containsKey(new Integer(index))) {
+ LinuxJoystickPOV pov = (LinuxJoystickPOV)(povXs.get(new Integer(index)));
+ pov.updateValue();
+ event.set(pov, pov.getPollData(), joystick_event.getNanos());
+ } else if(povYs.containsKey(new Integer(index))) {
+ LinuxJoystickPOV pov = (LinuxJoystickPOV)(povYs.get(new Integer(index)));
+ pov.updateValue();
+ event.set(pov, pov.getPollData(), joystick_event.getNanos());
+ } else {
+ event.set(axis, value, joystick_event.getNanos());
+ }
break;
}
}
@@ -121,6 +133,26 @@ final class LinuxJoystickDevice implements LinuxDevice {
public final void registerButton(int index, LinuxJoystickButton button) {
buttons[index] = button;
}
+
+ public final void registerPOV(LinuxJoystickPOV pov) {
+ // The x and y on a joystick device are not the same as on an event device
+ LinuxJoystickAxis xAxis = pov.getYAxis();
+ LinuxJoystickAxis yAxis = pov.getXAxis();
+ int xIndex;
+ int yIndex;
+ for(xIndex=0;xIndex<axes.length;xIndex++) {
+ if(axes[xIndex]==xAxis) {
+ break;
+ }
+ }
+ for(yIndex=0;yIndex<axes.length;yIndex++) {
+ if(axes[yIndex]==yAxis) {
+ break;
+ }
+ }
+ povXs.put(new Integer(xIndex),pov);
+ povYs.put(new Integer(yIndex),pov);
+ }
public final synchronized boolean getNextEvent(Event event) throws IOException {
return event_queue.getNextEvent(event);
diff --git a/plugins/linux/src/java/net/java/games/input/LinuxJoystickPOV.java b/plugins/linux/src/java/net/java/games/input/LinuxJoystickPOV.java
new file mode 100644
index 0000000..2ae5dc2
--- /dev/null
+++ b/plugins/linux/src/java/net/java/games/input/LinuxJoystickPOV.java
@@ -0,0 +1,55 @@
+package net.java.games.input;
+
+import java.io.IOException;
+
+public class LinuxJoystickPOV extends LinuxJoystickAxis {
+
+ private LinuxJoystickAxis hatX;
+ private LinuxJoystickAxis hatY;
+
+ LinuxJoystickPOV(Component.Identifier.Axis id, LinuxJoystickAxis hatX, LinuxJoystickAxis hatY) {
+ super(id, false);
+ this.hatX = hatX;
+ this.hatY = hatY;
+ }
+
+ protected LinuxJoystickAxis getXAxis() {
+ return hatX;
+ }
+
+ protected LinuxJoystickAxis getYAxis() {
+ return hatY;
+ }
+
+
+
+ protected void updateValue() {
+ LinuxEnvironmentPlugin.logln("Updating pov " + hatX.getName() + ", " + hatY.getName());
+ float last_x = hatX.getPollData();
+ float last_y = hatY.getPollData();
+
+ resetHasPolled();
+ if (last_x == -1 && last_y == -1)
+ setValue(Component.POV.UP_LEFT);
+ else if (last_x == -1 && last_y == 0)
+ setValue(Component.POV.LEFT);
+ else if (last_x == -1 && last_y == 1)
+ setValue(Component.POV.DOWN_LEFT);
+ else if (last_x == 0 && last_y == -1)
+ setValue(Component.POV.UP);
+ else if (last_x == 0 && last_y == 0)
+ setValue(Component.POV.OFF);
+ else if (last_x == 0 && last_y == 1)
+ setValue(Component.POV.DOWN);
+ else if (last_x == 1 && last_y == -1)
+ setValue(Component.POV.UP_RIGHT);
+ else if (last_x == 1 && last_y == 0)
+ setValue(Component.POV.RIGHT);
+ else if (last_x == 1 && last_y == 1)
+ setValue(Component.POV.DOWN_RIGHT);
+ else {
+ LinuxEnvironmentPlugin.logln("Unknown values x = " + last_x + " | y = " + last_y);
+ setValue(Component.POV.OFF);
+ }
+ }
+}