Skip to content

Commit

Permalink
Merge pull request #12 from umjammer/0.14.11v
Browse files Browse the repository at this point in the history
0.14.11v
  • Loading branch information
umjammer committed Apr 5, 2024
2 parents aee9980 + b129b59 commit 286b8c4
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 51 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.hid4java</groupId>
<artifactId>hid4java</artifactId>
<version>0.14.10v</version>
<version>0.14.11v</version>

<name>hid4java</name>
<description>A cross-platform hidapi like library (depends on jna but hidapi native library)</description>
Expand Down Expand Up @@ -36,7 +36,7 @@

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

<profiles>
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/hid4java/HidDeviceEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@


/**
* HidDeviceEvent.
* HidDeviceEvent. Assuming reuse.
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 2023-10-08 nsano initial version <br>
*/
public class HidDeviceEvent extends EventObject {

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

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

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

/** @param report a report data that contains first report id */
public HidDeviceEvent set(int reportId, byte[] report, int length) {
this.reportId = reportId;
this.report = report;
this.length = length;
return this;
}

/** the report data that contains first report id */
Expand Down
52 changes: 21 additions & 31 deletions src/main/java/org/hid4java/HidDevices.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@
package org.hid4java;

import java.io.IOException;
import java.net.URL;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -58,6 +56,24 @@ public class HidDevices {

private static final Logger logger = Logger.getLogger(HidDevices.class.getName());

static {
try {
try (InputStream is = HidDevices.class.getResourceAsStream("/META-INF/maven/org.hid4java/hid4java/pom.properties")) {
if (is != null) {
Properties props = new Properties();
props.load(is);
version = props.getProperty("version", "undefined in pom.properties");
} else {
version = System.getProperty("vavi.test.version", "undefined");
}
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

private static final String version;

/**
* The native device provider.
*/
Expand Down Expand Up @@ -92,7 +108,7 @@ public class HidDevices {
* @param args Nothing required
*/
public static void main(String[] args) {
System.out.println("Version: " + getVersion());
System.out.println("Version: " + version);
}

/**
Expand Down Expand Up @@ -320,32 +336,6 @@ public HidDevice getHidDevice(int vendorId, int productId, String serialNumber)
return null;
}

/**
* TODO check
* @return The current library version from the manifest or 0.0.x if an error occurs
*/
public static String getVersion() {

Class<HidDevices> clazz = HidDevices.class;
String className = clazz.getSimpleName() + ".class";
String classPath = clazz.getResource(className).toString();
if (!classPath.startsWith("jar")) {
// Class not from JAR
return "0.0.1";
}
String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +
"/META-INF/MANIFEST.MF";
Manifest manifest;
try {
manifest = new Manifest(new URL(manifestPath).openStream());
} catch (IOException e) {
return "0.0.2";
}
Attributes attr = manifest.getMainAttributes();
String value = attr.getValue("Implementation-Version");
return Objects.requireNonNullElse(value, "0.0.3");
}

/**
* @return True if the scan thread is running, false otherwise.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/hid4java/linux/LinuxHidDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public class LinuxHidDevice implements NativeHidDevice {
/** for input event */
private final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();

/** for reuse */
private final HidDeviceEvent hidDeviceEvent = new HidDeviceEvent(this);

LinuxHidDevice(HidSpecification specification) {
this.deviceHandle = -1;
this.deviceInfo = null;
Expand All @@ -87,7 +90,7 @@ public void open() {
ses.schedule(() -> {
try {
int r = read(inputBuffer, inputBuffer.length);
fireOnInputReport(new HidDeviceEvent(this, inputBuffer[0], inputBuffer, r));
fireOnInputReport(hidDeviceEvent.set(inputBuffer[0], inputBuffer, r));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/hid4java/macos/MacosHidDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public class MacosHidDevice implements NativeHidDevice {

Consumer<String> closer;

/** for reuse */
private final HidDeviceEvent hidDeviceEvent = new HidDeviceEvent(this);

/**
* Initialise the HID API library. Should always be called before using any other API calls.
*/
Expand Down Expand Up @@ -139,18 +142,18 @@ private static void onSignalCallback(Pointer context) {
private static void onReportCallback(Pointer context, int /* IOReturn */ result, Pointer sender, int /* IOHIDReportType */ report_type, int report_id, Pointer report, CFIndex reportLength) {
MacosHidDevice dev = (MacosHidDevice) UserObjectContext.get(context);
if (dev == null) {
logger.fine("here4.1: dev is null: " + UserObjectContext.objectIDMaster);
//logger.fine("here4.1: dev is null: " + UserObjectContext.objectIDMaster);
return;
}
logger.finest("here4.2: report_callback: dev: " + dev.deviceInfo.product);
//logger.finest("here4.2: report_callback: dev: " + dev.deviceInfo.product);

// Make a new Input Report object
int length = reportLength.intValue();
report.read(0, dev.inputData, 0, length);

// Signal a waiting thread that there is data.
dev.fireOnInputReport(new HidDeviceEvent(dev, report_id, dev.inputData, length));
logger.finest("here4.3: report: " + length + ", " + Thread.currentThread());
dev.fireOnInputReport(dev.hidDeviceEvent.set(report_id, dev.inputData, length));
//logger.finest("here4.3: report: " + length + ", " + Thread.currentThread());
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/hid4java/windows/WindowsHidDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class WindowsHidDevice implements NativeHidDevice {
/** for reuse */
private final byte[] inputBuffer = new byte[64];

/** for reuse */
private final HidDeviceEvent hidDeviceEvent = new HidDeviceEvent(this);

WindowsHidDevice(HidSpecification specification) {
this.deviceHandle = INVALID_HANDLE_VALUE;
this.blocking = true;
Expand All @@ -103,7 +106,7 @@ public void open() {
ses.schedule(() -> {
try {
int r = read(inputBuffer, inputBuffer.length);
fireOnInputReport(new HidDeviceEvent(this, inputBuffer[0], inputBuffer, r));
fireOnInputReport(hidDeviceEvent.set(inputBuffer[0], inputBuffer, r));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package vavi.games.input.hid4java.spi;

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

import net.java.games.input.AbstractController;
Expand Down Expand Up @@ -59,10 +60,13 @@ public int getProductId() {
public void open() throws IOException {
super.open();

Hid4JavaComponent[] hid4JavaComponents = Arrays.stream(getComponents()).map(Hid4JavaComponent.class::cast).toArray(Hid4JavaComponent[]::new);
Hid4JavaInputEvent hid4JavaInputEvent = new Hid4JavaInputEvent(this);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import net.java.games.input.Component;
import net.java.games.input.Event;
import net.java.games.input.InputEvent;
import net.java.games.input.usb.HidComponent;
import net.java.games.input.usb.HidInputEvent;


/**
* Hid4JavaInputEvent.
* Hid4JavaInputEvent. Assuming reuse.
* <p>
* <h4>system property</h4>
* <li>"net.java.games.input.InputEvent.fillAll" ... determine to fill all events (true) or events which value is changed (false)</li>
Expand All @@ -32,31 +33,37 @@ public class Hid4JavaInputEvent extends InputEvent implements HidInputEvent {
private final Deque<Hid4JavaComponent> deque = new LinkedList<>();

/** the time when got an event */
private final long time;
private long time;

/** source */
private final byte[] data;
private byte[] data;

@Override
public byte[] getData() {
return data;
}

/** */
public Hid4JavaInputEvent(Object source, Component[] components, byte[] data) {
public Hid4JavaInputEvent(Object source) {
super(source);
}

/** */
public Hid4JavaInputEvent set(Hid4JavaComponent[] components, byte[] data) {
this.data = data;
this.time = System.nanoTime();

boolean fillAll = Boolean.parseBoolean(System.getProperty("net.java.games.input.InputEvent.fillAll", "false"));

deque.clear();
for (Hid4JavaComponent component : Arrays.stream(components).map(Hid4JavaComponent.class::cast).toArray(Hid4JavaComponent[]::new)) {
for (Hid4JavaComponent component : components) {
if (fillAll || component.isValueChanged(data)) {
component.setValue(data);
deque.offer(component);
}
}

return this;
}

@Override
Expand Down

0 comments on commit 286b8c4

Please sign in to comment.