Skip to content

Instantly share code, notes, and snippets.

@Vzor-
Last active June 6, 2024 19:56
Show Gist options
  • Save Vzor-/6bb25a103489463b007b2aec6b803ad3 to your computer and use it in GitHub Desktop.
Save Vzor-/6bb25a103489463b007b2aec6b803ad3 to your computer and use it in GitHub Desktop.
package org.hid4java.jna;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class DarwinHidApiExecutionWrapper implements DarwinHidApiLibrary {
public static DarwinHidApiExecutionWrapper INSTANCE = new DarwinHidApiExecutionWrapper();
private static final DarwinHidApiLibrary wrappedInstance = DarwinHidApiLibrary.INSTANCE;
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
@Override
public void hid_init() {
try {
executor.submit(wrappedInstance::hid_init).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public void hid_exit() {
try {
executor.submit(wrappedInstance::hid_exit).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public Pointer hid_open(short vendor_id, short product_id, WString serial_number) {
try {
return executor.submit(() -> wrappedInstance.hid_open(vendor_id, product_id, serial_number)).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public void hid_close(Pointer device) {
wrappedInstance.hid_close(device);
}
@Override
public Pointer hid_error(Pointer device) {
return wrappedInstance.hid_error(device);
}
@Override
public int hid_read(Pointer device, WideStringBuffer.ByReference bytes, int length) {
return wrappedInstance.hid_read(device, bytes, length);
}
@Override
public int hid_read_timeout(Pointer device, WideStringBuffer.ByReference bytes, int length, int timeout) {
return wrappedInstance.hid_read_timeout(device, bytes, length, timeout);
}
@Override
public int hid_write(Pointer device, WideStringBuffer.ByReference data, int len) {
return wrappedInstance.hid_write(device, data, len);
}
@Override
public int hid_get_feature_report(Pointer device, WideStringBuffer.ByReference data, int length) {
return wrappedInstance.hid_get_feature_report(device, data, length);
}
@Override
public int hid_send_feature_report(Pointer device, WideStringBuffer.ByReference data, int length) {
return wrappedInstance.hid_send_feature_report(device, data, length);
}
@Override
public int hid_get_indexed_string(Pointer device, int idx, WideStringBuffer.ByReference string, int len) {
return wrappedInstance.hid_get_indexed_string(device, idx, string, len);
}
@Override
public int hid_get_report_descriptor(Pointer device, byte[] buffer, int size) {
return wrappedInstance.hid_get_report_descriptor(device, buffer, size);
}
@Override
public int hid_get_manufacturer_string(Pointer device, WideStringBuffer.ByReference str, int len) {
return wrappedInstance.hid_get_manufacturer_string(device, str, len);
}
@Override
public int hid_get_product_string(Pointer device, WideStringBuffer.ByReference str, int len) {
return wrappedInstance.hid_get_product_string(device, str, len);
}
@Override
public int hid_get_serial_number_string(Pointer device, WideStringBuffer.ByReference str, int len) {
return wrappedInstance.hid_get_serial_number_string(device, str, len);
}
@Override
public int hid_set_nonblocking(Pointer device, int nonblock) {
return wrappedInstance.hid_set_nonblocking(device, nonblock);
}
@Override
public HidDeviceInfoStructure hid_enumerate(short vendor_id, short product_id) {
try {
return executor.submit(() -> wrappedInstance.hid_enumerate(vendor_id, product_id)).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public void hid_free_enumeration(Pointer devs) {
wrappedInstance.hid_free_enumeration(devs);
}
@Override
public Pointer hid_open_path(String path) {
try {
return executor.submit(() -> wrappedInstance.hid_open_path(path)).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public String hid_version_str() {
return wrappedInstance.hid_version_str();
}
@Override
public void hid_darwin_set_open_exclusive(int openExclusive) {
wrappedInstance.hid_darwin_set_open_exclusive(openExclusive);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment