Skip to content

Commit

Permalink
Merge pull request #6 from umjammer/0.14.6v
Browse files Browse the repository at this point in the history
0.14.6v
  • Loading branch information
umjammer committed Jan 28, 2024
2 parents bcc4a20 + 7e897c2 commit edf1ac3
Show file tree
Hide file tree
Showing 16 changed files with 1,259 additions and 50 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

# hid4java

by jna directory instead of hidapi
🎮 hid api for java

- by jna directory instead of hidapi
- with [jinput](https://github.com/umjammer/jinput) spi

## Install

Expand Down
111 changes: 65 additions & 46 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

<groupId>org.hid4java</groupId>
<artifactId>hid4java</artifactId>
<version>0.14.5v</version>
<version>0.14.6v</version>

<name>hid4java</name>
<description>A cross-platform Java Native Access (JNA) wrapper for the libusb/hidapi library</description>
<description>A cross-platform hidapi like library (depends on jna but hidapi native library)</description>
<url>https://github.com/umjammer/hid4java</url>
<inceptionYear>2014</inceptionYear>
<developers>
Expand All @@ -34,22 +34,69 @@
<url>[email protected]:umjammer/hid4java.git</url>
</scm>

<distributionManagement>
<repository>
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<properties>
<jinput.groupId>com.github.umjammer.jinput</jinput.groupId> <!-- net.java.jinput / com.github.umjammer.jinput -->
<jinput.version>2.0.14v</jinput.version>
<jinput.version>2.0.15v</jinput.version>
</properties>


<profiles>
<profile>
<!-- mvn -P jnaerator jnaerator:generate -->
<id>jnaerator</id>
<build>
<plugins>
<plugin>
<groupId>com.nativelibs4java</groupId>
<artifactId>maven-jnaerator-plugin</artifactId>
<version>0.12</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<javaOutputDirectory>${project.build.testSourceDirectory}</javaOutputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<!-- mvn -P run antrun:run -Dfoo='bar' -->
<id>run</id>
<build>
<plugins>
<plugin>
<!-- GOOD: fork: OK, test jars: OK, user args, default args: OK -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<java classname="vavi.games.input.hid4java.spi.GamepadTest" fork="true">
<classpath>
<path refid="maven.compile.classpath"/>
<path refid="maven.test.classpath"/>
</classpath>
<jvmarg value="-Djava.util.logging.config.file=${project.build.testOutputDirectory}/logging.properties" />
</java>
</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -128,7 +175,6 @@
</archive>
</configuration>
</plugin>

</plugins>
</build>

Expand All @@ -152,6 +198,12 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.github.umjammer</groupId>
<artifactId>vavi-commons</artifactId>
<version>1.1.9</version>
</dependency>

<dependency>
<groupId>${jinput.groupId}</groupId>
<artifactId>osx-plugin</artifactId>
Expand All @@ -178,38 +230,5 @@
<artifactId>junit-platform-commons</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.umjammer</groupId>
<artifactId>vavi-commons</artifactId>
<version>1.1.9</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<!-- mvn -P jnaerator jnaerator:generate -->
<id>jnaerator</id>
<build>
<plugins>
<plugin>
<groupId>com.nativelibs4java</groupId>
<artifactId>maven-jnaerator-plugin</artifactId>
<version>0.12</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<javaOutputDirectory>${project.build.testSourceDirectory}</javaOutputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
6 changes: 4 additions & 2 deletions src/main/java/org/hid4java/InputReportEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@
*/
public class InputReportEvent extends EventObject {

/** the report Id number if used or zero */
/** the report id number if used or zero */
private final int reportId;

/** the report data */
/** the report data that contains first report id */
private final byte[] report;
private final int length;

/** @param report a report data that contains first report id */
public InputReportEvent(Object source, int reportId, byte[] report, int length) {
super(source);
this.reportId = reportId;
this.report = report;
this.length = length;
}

/** the report data that contains first report id */
public int getReportId() {
return reportId;
}
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/vavi/games/input/hid4java/spi/Hid4JavaComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2023 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/

package vavi.games.input.hid4java.spi;

import java.util.function.Function;

import net.java.games.input.AbstractComponent;
import net.java.games.input.WrappedComponent;
import net.java.games.input.usb.HidComponent;
import net.java.games.input.usb.parser.Field;
import net.java.games.input.usb.parser.HidParser.Feature;

import static net.java.games.input.usb.parser.HidParser.Feature.RELATIVE;


/**
* Hid4JavaComponent.
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 2023-11-06 nsano initial version <br>
*/
public class Hid4JavaComponent extends AbstractComponent implements HidComponent, WrappedComponent<Field> {

/** an input report definition */
private final Field field;

/** special data picker (e.g. for touch x, y) */
private Function<byte[], Integer> function;

/**
* Protected constructor
*
* @param name A name for the axis
* @param field an input report descriptor fragment.
*/
protected Hid4JavaComponent(String name, Identifier id, Field field) {
super(name, id);
this.field = field;
}

/**
* @param offset bits (must be excluded first one byte (8 bits) for report id)
* @param size bit length
*/
public Hid4JavaComponent(String name, Identifier id, int offset, int size) {
this(name, id, offset, size, null);
}

/**
* @param offset bits (must be excluded first one byte (8 bits) for report id)
* @param size bit length
*/
public Hid4JavaComponent(String name, Identifier id, int offset, int size, Function<byte[], Integer> function) {
super(name, id);
this.field = new Field(offset, size);
this.function = function;
}

@Override
public boolean isRelative() {
return field != null && Feature.containsIn(RELATIVE, field.getFeature());
}

@Override
public boolean isValueChanged(byte[] data) {
return getEventValue() != getValue(data);
}

@Override
public float getValue() {
return getEventValue();
}

/** by hid input report */
private int getValue(byte[] data) {
int value = field.getValue(data);
return function != null ? function.apply(data) : value;
}

@Override
public void setValue(byte[] data) {
setEventValue(field.getValue(data));
}

@Override
public Field getWrappedObject() {
return field;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2023 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/

package vavi.games.input.hid4java.spi;

import java.io.IOException;
import java.util.logging.Level;

import net.java.games.input.AbstractController;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.Rumbler;
import net.java.games.input.usb.HidController;
import org.hid4java.HidDevice;
import vavi.util.Debug;
import vavi.util.StringUtil;


/**
* Hid4JavaController.
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 2023-09-18 nsano initial version <br>
*/
public class Hid4JavaController extends AbstractController implements HidController {

/** */
private final HidDevice device;

/**
* Protected constructor for a controller containing the specified
* axes, child controllers, and rumblers
*
* @param device the controller
* @param components components for the controller
* @param children child controllers for the controller
* @param rumblers rumblers for the controller
*/
protected Hid4JavaController(HidDevice device, Component[] components, Controller[] children, Rumbler[] rumblers) {
super(device.getManufacturer() + "/" + device.getProduct(), components, children, rumblers);
this.device = device;
Debug.println("device: " + device + ", " + device.isOpen());
}

@Override
public int getVendorId() {
return device.getVendorId();
}

@Override
public int getProductId() {
return device.getProductId();
}

@Override
public void open() throws IOException {
super.open();

device.open();
device.addInputReportListener(event -> {
byte[] data = event.getReport();
fireOnInput(new Hid4JavaInputEvent(Hid4JavaController.this, getComponents(), data));
});
}

@Override
public void close() throws IOException {
device.close();
super.close();
}

@Override
public Type getType() {
return Type.GAMEPAD;
}

@Override
public void output(Report report) throws IOException {
((HidReport) report).setup(getRumblers());

int reportId = ((HidReport) report).getReportId();
byte[] data = ((HidReport) report).getData();
Debug.println(Level.FINER, "reportId: " + reportId + "\n" + StringUtil.getDump(data));
int r = device.write(data, data.length, reportId);
if (r == -1) {
throw new IOException("write returns -1");
}
}
}
Loading

0 comments on commit edf1ac3

Please sign in to comment.