Permalink
Browse files

version 1.0

  • Loading branch information...
1 parent 7684659 commit 0542de4222b8f2f541f33ed519f7a32fe6441fb2 @dumbledore committed Jan 5, 2011
View
@@ -0,0 +1,47 @@
+*FlypadJoystick* is a *Java* wrapper for [PPJoy](http://ppjoy.blogspot.com/). In other words, it enables you to
+control PPJoy's virtual joystick directly from *Java*.
+
+Here goes a simple example.
+
+ try {
+ /*
+ * Try to create a new joystick.
+ */
+ Joystick joystick = new Joystick();
+
+ try {
+ /*
+ * Set joystick values
+ */
+
+ /*
+ * Set analog values for Axis X/Y/Z,
+ * Rotation X/Y/Z, Slider, Dial. Overall 8 axes.
+ *
+ * Here we set the Z Axis to maximum.
+ */
+ joystick.analog[Joystick.ANALOG_AXIS_Z] = Joystick.ANALOG_MAX;
+
+ /*
+ * Set digital values for the buttons. Overall 16 buttons.
+ *
+ * Here we turn on the 13-th button
+ */
+ joystick.digital[12] = Joystick.DIGITAL_ON;
+
+ /*
+ * Send the data to the joystick. Keep in mind,
+ * that the send method may throw a JoystickException
+ */
+ joystick.send();
+ } finally {
+ joystick.close();
+ }
+ } catch (JoystickException e) {
+ e.printStackTrace();
+ }
+
+
+FlypadJoystick is licensed under the Apache 2.0 license.
+FlypadJoystick relies on [JNA](https://jna.dev.java.net) for native access.
+Note that one needs [PPJoy](http://ppjoy.blogspot.com) installed before using FlypadJoystick.
@@ -58,7 +58,6 @@ javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
-main.class=com.sun.jna.Main
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
platform.active=default_platform
View
@@ -1,81 +0,0 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
-package com.sun.jna;
-
-import com.sun.jna.examples.win32.Kernel32;
-import com.sun.jna.examples.win32.W32API.HANDLE;
-import com.sun.jna.examples.win32.W32API.WORD;
-import org.flypad.joystick.IOCTL;
-import org.flypad.joystick.JoystickState;
-
-/**
- *
- * @author albus
- */
-public class Main {
-
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
-// String devName= "\\\\.\\PPJoyIOCTL1";
- String devName= "\\\\.\\PPJoyIOCTL1";
-
- System.out.println("Opening handle...");
-
- HANDLE h = Kernel32.INSTANCE.CreateFile(
- devName,
- Kernel32.GENERIC_WRITE,
- Kernel32.FILE_SHARE_WRITE,
- null,
- Kernel32.OPEN_EXISTING,
- 0,
- null);
-
- System.out.println("Handle opened!");
- System.out.println(h);
-
- JoystickState j = new JoystickState();
- j.setAlignType(Structure.ALIGN_NONE);
- j.initialize();
- j.ensureAllocated();
-
- final int size =
- Native.getNativeSize(JoystickState.class, j
- );
- System.out.println("struct size: " + size);
-
- WORD w = new WORD();
-
- System.out.println("RC: " + IOCTL.INSTANCE.GetLastError());
-
- System.out.println("Sending to joystick...");
- int res = IOCTL.INSTANCE.DeviceIoControl(
- h,
- 0x220000,
- j,
- size,
- Pointer.NULL,
- 0,
- j.getPointer(),//w,
- null//j.getPointer()
- );
-
- System.out.println("RES: " + res);
- System.out.println("RC: " + IOCTL.INSTANCE.GetLastError());
-
- try {
- System.out.println("sleeping...");
- Thread.sleep(5000);
- }
- catch (Exception e) {}
-
- System.out.println("Closing handle...");
- Kernel32.INSTANCE.CloseHandle(h);
- System.out.println("RC: " + IOCTL.INSTANCE.GetLastError());
- System.out.println("Handle closed!");
- }
-}
@@ -0,0 +1,129 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.flypad.joystick;
+
+import org.flypad.joystick.jna.IOCTL;
+import org.flypad.joystick.jna.JoystickState;
+import com.sun.jna.Native;
+import com.sun.jna.Pointer;
+import com.sun.jna.examples.win32.Kernel32;
+import com.sun.jna.examples.win32.W32API.HANDLE;
+
+/**
+ *
+ * @author albus
+ */
+public class Joystick implements JoystickConstants {
+ public static final String DEFAULT_NAME = "\\\\.\\PPJoyIOCTL1";
+
+ /**
+ * Analog values (axes)
+ */
+ public final int[] analog = new int[NUM_ANALOG];
+
+ /**
+ * Digital values (buttons)
+ */
+ public final byte[] digital = new byte[NUM_DIGITAL];
+
+
+ private HANDLE handle = null;
+ private final JoystickState state = new JoystickState();
+ private final int size =
+ Native.getNativeSize(JoystickState.class, state);
+
+ public Joystick() throws JoystickException {
+ this(DEFAULT_NAME);
+ }
+
+ public Joystick(final String name) throws JoystickException {
+ if (name == null) {
+ throw new NullPointerException();
+ }
+ initializeHandle(name);
+ resetButtons();
+ }
+
+ private void initializeHandle(final String name) throws JoystickException {
+ HANDLE h = Kernel32.INSTANCE.CreateFile(
+ name,
+ Kernel32.GENERIC_WRITE,
+ Kernel32.FILE_SHARE_WRITE,
+ null,
+ Kernel32.OPEN_EXISTING,
+ 0,
+ null);
+
+ final int rc = IOCTL.INSTANCE.GetLastError();
+
+ if (rc != 0) {
+ throw new JoystickException(
+ "Cannot create handle. Error code: " + rc);
+ }
+
+ handle = h;
+ }
+
+ public final void resetButtons() {
+ for (int i = 0; i < analog.length; i++) {
+ analog[i] = ANALOG_DEFAULT;
+ }
+
+ for (int i = 0; i < digital.length; i++) {
+ digital[i] = DIGITAL_OFF;
+ }
+ }
+
+ public final synchronized void close() throws JoystickException {
+ if (handle == null) {
+ throw new JoystickException("Joystick already closed");
+ }
+
+ Kernel32.INSTANCE.CloseHandle(handle);
+
+ final int rc = IOCTL.INSTANCE.GetLastError();
+
+ if (rc != 0) {
+ throw new JoystickException(
+ "Cannot close handle. Error code: " + rc);
+ }
+ }
+
+ protected final void finalize() throws Throwable {
+ try {
+ close();
+ } catch (Exception e) {}
+
+ super.finalize();
+ }
+
+ public final void send() throws JoystickException {
+ /*
+ * Copy the data to the joystick state
+ */
+ System.arraycopy(analog, 0, state.analog, 0, NUM_ANALOG);
+ System.arraycopy(digital, 0, state.digital, 0, NUM_DIGITAL);
+ state.write();
+
+ IOCTL.INSTANCE.DeviceIoControl(
+ handle,
+ 0x220000,
+ state,
+ size,
+ Pointer.NULL,
+ 0,
+ state.getPointer(),
+ null
+ );
+
+ final int rc = IOCTL.INSTANCE.GetLastError();
+
+ if (rc != 0) {
+ throw new JoystickException(
+ "DeviceIoControl error. Error code: " + rc);
+ }
+ }
+}
@@ -0,0 +1,32 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.flypad.joystick;
+
+/**
+ *
+ * @author albus
+ */
+public interface JoystickConstants {
+ public static final int ANALOG_MIN = 1;
+ public static final int ANALOG_MAX = 32767;
+ public static final int ANALOG_MID = 16384;
+ public static final int ANALOG_DEFAULT = ANALOG_MID;
+
+ public static final int ANALOG_AXIS_X = 0;
+ public static final int ANALOG_AXIS_Y = 1;
+ public static final int ANALOG_AXIS_Z = 2;
+ public static final int ANALOG_ROTATION_X = 5;
+ public static final int ANALOG_ROTATION_Y = 6;
+ public static final int ANALOG_ROTATION_Z = 3;
+ public static final int ANALOG_SLIDER = 4;
+ public static final int ANALOG_DIAL = 7;
+
+ public static final byte DIGITAL_OFF = 0;
+ public static final byte DIGITAL_ON = 1;
+
+ public static final byte NUM_ANALOG = 8;
+ public static final byte NUM_DIGITAL = 16;
+}
@@ -0,0 +1,22 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.flypad.joystick;
+
+import java.io.IOException;
+
+/**
+ *
+ * @author albus
+ */
+public class JoystickException extends IOException {
+ public JoystickException() {
+ super();
+ }
+
+ public JoystickException(final String message) {
+ super(message);
+ }
+}
@@ -1,42 +0,0 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
-package org.flypad.joystick;
-
-import com.sun.jna.Structure;
-
-/**
- *
- * @author albus
- */
-public class JoystickState extends Structure {
- /* Signature to identify packet to PPJoy IOCTL */
- public int signature = 0x53544143;
- /* Num of analog values we pass */
- public byte numAnalog = 8;
- /* Analog values */
- public int[] analog;
- /* Num of digital values we pass */
- public byte numDigital = 16;
- /* Digital values */
- public byte[] digital;
-
- public JoystickState() {
- analog = new int[8];
- digital = new byte[16];
- allocateMemory();
- }
-
- public void initialize() {
- for (int i = 0; i < analog.length; i++) {
- analog[i] = 32767; //1
- }
-
- for (int i = 0; i < digital.length; i++) {
- digital[i] = 1;
- }
- super.write();
- }
-}
@@ -3,7 +3,7 @@
* and open the template in the editor.
*/
-package org.flypad.joystick;
+package org.flypad.joystick.jna;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
Oops, something went wrong.

0 comments on commit 0542de4

Please sign in to comment.