1
0
forked from Clones/Controlify

controller hid identification + ps4 buttons

This commit is contained in:
isXander
2023-02-03 19:23:08 +00:00
parent 5bd390f2b1
commit c9b0870af3
54 changed files with 302 additions and 44 deletions

View File

@ -0,0 +1,68 @@
package dev.isxander.controlify.controller.hid;
import dev.isxander.controlify.Controlify;
import org.hid4java.*;
import org.hid4java.event.HidServicesEvent;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Set;
import java.util.function.Consumer;
public class ControllerHIDService implements HidServicesListener {
// https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/hid-usages#usage-page
private static final Set<Integer> CONTROLLER_USAGE_IDS = Set.of(
0x04, // Joystick
0x05, // Gamepad
0x08 // Multi-axis Controller
);
private final HidServicesSpecification specification;
private final Queue<Consumer<HidDevice>> deviceQueue;
public ControllerHIDService() {
this.deviceQueue = new ArrayDeque<>();
this.specification = new HidServicesSpecification();
specification.setAutoStart(false);
specification.setScanInterval(2000); // long interval, so we can guarantee this runs after GLFW hook
}
public void start() {
var services = HidManager.getHidServices(specification);
services.addHidServicesListener(this);
services.start();
}
public void awaitNextDevice(Consumer<HidDevice> consumer) {
deviceQueue.add(consumer);
}
@Override
public void hidDeviceAttached(HidServicesEvent event) {
var device = event.getHidDevice();
if (isController(device)) {
if (deviceQueue.peek() != null) {
deviceQueue.poll().accept(event.getHidDevice());
}
}
}
private boolean isController(HidDevice device) {
var isGenericDesktopControlOrGameControl = device.getUsagePage() == 0x1 || device.getUsagePage() == 0x5;
var isController = CONTROLLER_USAGE_IDS.contains(device.getUsage());
return isGenericDesktopControlOrGameControl && isController;
}
@Override
public void hidDeviceDetached(HidServicesEvent event) {
}
@Override
public void hidFailure(HidServicesEvent event) {
}
}