Skip to content

Commit

Permalink
Merge pull request #7 from umjammer/0.14.7v
Browse files Browse the repository at this point in the history
0.14.7v
  • Loading branch information
umjammer committed Feb 1, 2024
2 parents edf1ac3 + d2925f5 commit 244cd92
Show file tree
Hide file tree
Showing 29 changed files with 1,222 additions and 1,373 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* ~~npe on close~~
* windows/linux not tested yet
* dig into the difference between static and instance method reference
* ~~spi attach/detach controller~~

## LESSON

Expand Down
5 changes: 2 additions & 3 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.6v</version>
<version>0.14.7v</version>

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

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


<profiles>
<profile>
<!-- mvn -P jnaerator jnaerator:generate -->
Expand Down
29 changes: 11 additions & 18 deletions src/main/java/org/hid4java/HidDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class HidDevice {

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

private final HidDeviceManager manager;
private Info info;
private NativeHidDevice nativeDevice;
private final Runnable afterWrite;
private final Info info;
private final NativeHidDevice nativeDevice;

private boolean isOpen;

Expand Down Expand Up @@ -149,21 +149,14 @@ public enum HidBusType {
}
}

private final boolean autoDataRead;
private final int dataReadInterval;

/**
* @param info The HID device info structure providing details
* @param deviceManager The HID device manager providing access to device enumeration for post IO scanning
* @param servicesSpecification The HID services specification providing configuration details
* @param info The HID device info structure providing details
* @param afterWrite The HID device afterWrite providing access to device enumeration for post IO scanning
* @since 0.1.0
*/
public HidDevice(Info info, org.hid4java.HidDeviceManager deviceManager, HidServicesSpecification servicesSpecification) throws IOException {

this.manager = deviceManager;
public HidDevice(Info info, NativeHidDevice nativeDevice, Runnable afterWrite) throws IOException {

this.dataReadInterval = servicesSpecification.getDataReadInterval();
this.autoDataRead = servicesSpecification.isAutoDataRead();
this.afterWrite = afterWrite;

this.info = info;

Expand All @@ -174,7 +167,7 @@ public HidDevice(Info info, org.hid4java.HidDeviceManager deviceManager, HidServ
this.info.vendorId = this.info.vendorId & 0xffff;
this.info.productId = this.info.productId & 0xffff;

nativeDevice = manager.open(info);
this.nativeDevice = nativeDevice;
logger.finest(getPath() + "(@" + hashCode() + "): " + nativeDevice);
}

Expand Down Expand Up @@ -348,7 +341,7 @@ public int getFeatureReport(byte[] data, int reportId) throws IOException {
* @since 0.1.0
*/
public int sendFeatureReport(byte[] data, int reportId) throws IOException {
return nativeDevice.sendFeatureReport(data, (byte)reportId);
return nativeDevice.sendFeatureReport(data, (byte) reportId);
}

/**
Expand Down Expand Up @@ -395,8 +388,8 @@ public int write(byte[] message, int packetLength, int reportId, boolean applyPa
}

int result = nativeDevice.write(message, packetLength, (byte) reportId);
// Update HID manager
manager.afterDeviceWrite();
// Update HID afterWrite
afterWrite.run();
return result;
}

Expand Down
22 changes: 8 additions & 14 deletions src/main/java/org/hid4java/HidDeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hid4java.event.HidServicesListenerList;
import org.hid4java.HidServicesSpecification.ScanMode;


/**
Expand All @@ -55,13 +55,6 @@ public class HidDeviceManager {

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

/**
* Enables use of the libusb variant of the hidapi native library when running on a Linux platform.
* <p>
* The default is hidraw which enables Bluetooth devices but requires udev rules.
*/
public static boolean useLibUsbVariant = false;

/**
* The native device provider.
*/
Expand All @@ -80,7 +73,7 @@ public class HidDeviceManager {
/**
* HID services listener list
*/
private final HidServicesListenerList listenerList;
private final HidServicesListenerSupport listenerList;

/**
* The device enumeration thread
Expand All @@ -97,7 +90,7 @@ public class HidDeviceManager {
* @param hidServicesSpecification Provides various parameters for configuring HID services
* @throws HidException If USB HID initialization fails
*/
HidDeviceManager(HidServicesListenerList listenerList, HidServicesSpecification hidServicesSpecification) throws HidException {
HidDeviceManager(HidServicesListenerSupport listenerList, HidServicesSpecification hidServicesSpecification) throws HidException {

this.listenerList = listenerList;
this.hidServicesSpecification = hidServicesSpecification;
Expand Down Expand Up @@ -130,7 +123,7 @@ public class HidDeviceManager {
*/
public void start() throws IOException {

nativeManager.open();
nativeManager.open(hidServicesSpecification);

// Check for previous start
if (this.isScanning()) {
Expand Down Expand Up @@ -243,8 +236,9 @@ public List<HidDevice> getAttachedHidDevices() throws IOException {
// Wrap in HidDevice
hidDeviceList.add(new HidDevice(
hidDeviceInfo,
this,
hidServicesSpecification));
open(hidDeviceInfo),
this::afterDeviceWrite
));
// Move to the next in the linked list
}
}
Expand All @@ -255,7 +249,7 @@ public List<HidDevice> getAttachedHidDevices() throws IOException {
/**
* Indicate that a device write has occurred which may require a change in scanning frequency
*/
public void afterDeviceWrite() {
private void afterDeviceWrite() {

if (ScanMode.SCAN_AT_FIXED_INTERVAL_WITH_PAUSE_AFTER_WRITE == hidServicesSpecification.getScanMode() && isScanning()) {
stopScanThread();
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/hid4java/HidServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hid4java.event.HidServicesListenerList;


/**
* JNA bridge class to provide the following to USB HID:
* <ul>
* <li>Access to the <code>signal11/hidapi</code> via JNA</li>
* <li>Access to the native api via JNA</li>
* </ul>
* Requires the hidapi to be present on the classpath or the system library search path.
*
* TODO merge into HidManager
*
* @since 0.0.1
*/
public class HidServices {
Expand All @@ -53,7 +53,7 @@ public class HidServices {
/**
* The HID services listeners for receiving attach/detach events etc
*/
private final HidServicesListenerList listeners = new HidServicesListenerList();
private final HidServicesListenerSupport listeners = new HidServicesListenerSupport();

/**
* The HID device manager handles scanning operations
Expand All @@ -72,15 +72,15 @@ public static void main(String[] args) {
/**
* Initialise with a default HID specification
*
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerSupport, HidServicesSpecification)}
*/
public HidServices() throws IOException {
this(new HidServicesSpecification());
}

/**
* @param hidServicesSpecification Provides various parameters for configuring HID services
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerList, HidServicesSpecification)}
* @throws HidException If something goes wrong (see {@link HidDeviceManager#HidDeviceManager(HidServicesListenerSupport, HidServicesSpecification)}
*/
public HidServices(HidServicesSpecification hidServicesSpecification) throws IOException {
hidDeviceManager = new HidDeviceManager(listeners, hidServicesSpecification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*/

package org.hid4java.event;
package org.hid4java;

import java.util.Arrays;

Expand All @@ -41,24 +41,12 @@
public class HidServicesEvent {

private final HidDevice hidDevice;
private final byte[] dataReceived;

/**
* @param device The HidDevice involved in the event
*/
public HidServicesEvent(HidDevice device) {
hidDevice = device;
dataReceived = null;
}

/**
* @param device The HidDevice involved in the event
* @param dataReceived The contents of all data read
* @since 0.8.0
*/
public HidServicesEvent(HidDevice device, byte[] dataReceived) {
hidDevice = device;
this.dataReceived = Arrays.copyOf(dataReceived, dataReceived.length);
}

/**
Expand All @@ -68,13 +56,6 @@ public HidDevice getHidDevice() {
return hidDevice;
}

/**
* @return The data received (might be multiple packets of data)
*/
public byte[] getDataReceived() {
return dataReceived;
}

@Override
public String toString() {
return "HidServicesEvent{" +
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/org/hid4java/HidServicesListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

import java.util.EventListener;

import org.hid4java.event.HidServicesEvent;


/**
* Interface to provide the following to API consumers:
Expand All @@ -53,18 +51,4 @@ public interface HidServicesListener extends EventListener {
* @param event The event
*/
void hidDeviceDetached(HidServicesEvent event);

/**
* A HID failure occurred (enumeration, data transfer etc)
*
* @param event The event
*/
void hidFailure(HidServicesEvent event);

/**
* A HID input data buffer was populated
*
* @param event The event
*/
void hidDataReceived(HidServicesEvent event);
}
94 changes: 94 additions & 0 deletions src/main/java/org/hid4java/HidServicesListenerSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2015 Gary Rowe
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package org.hid4java;

import java.util.ArrayList;
import java.util.List;


/**
* HID services listener list
*
* @since 0.0.1
*/
public class HidServicesListenerSupport {

/**
* The list with registered listeners
*/
private final List<HidServicesListener> listeners = new ArrayList<>();

/**
* @param listener The listener to add (same instance are not duplicated)
*/
public final void add(HidServicesListener listener) {
if (this.listeners.contains(listener)) {
return;
}
this.listeners.add(listener);
}

/**
* @param listener The listener to remove
*/
public final void remove(HidServicesListener listener) {
this.listeners.remove(listener);
}

/**
* Removes all listeners
*/
public final void clear() {
this.listeners.clear();
}

/**
* Fire the HID device attached event
*
* @param hidDevice The device that was attached
*/
public void fireHidDeviceAttached(HidDevice hidDevice) {
HidServicesEvent event = new HidServicesEvent(hidDevice);

for (HidServicesListener listener : listeners) {
listener.hidDeviceAttached(event);
}
}

/**
* Fire the HID device detached event
*
* @param hidDevice The device that was detached
*/
public void fireHidDeviceDetached(HidDevice hidDevice) {
HidServicesEvent event = new HidServicesEvent(hidDevice);

for (HidServicesListener listener : listeners) {
listener.hidDeviceDetached(event);
}
}
}

Loading

0 comments on commit 244cd92

Please sign in to comment.