forked from Clones/Controlify
1.1 changelog, bump version, update comparison
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package dev.isxander.controlify.controller.hid;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import dev.isxander.controlify.Controlify;
|
||||
import dev.isxander.controlify.controller.ControllerType;
|
||||
import org.hid4java.*;
|
||||
@ -11,7 +12,7 @@ public class ControllerHIDService {
|
||||
private final HidServicesSpecification specification;
|
||||
private HidServices services;
|
||||
|
||||
private final Queue<HIDIdentifierWithPath> unconsumedControllerHIDs;
|
||||
private final Queue<Pair<HidDevice, HIDIdentifier>> unconsumedControllerHIDs;
|
||||
private final Map<String, HidDevice> attachedDevices = new HashMap<>();
|
||||
private boolean disabled = false;
|
||||
// https://learn.microsoft.com/en-us/windows-hardware/drivers/hid/hid-usages#usage-page
|
||||
@ -41,19 +42,19 @@ public class ControllerHIDService {
|
||||
public ControllerHIDInfo fetchType() {
|
||||
doScanOnThisThread();
|
||||
|
||||
HIDIdentifierWithPath hid = unconsumedControllerHIDs.poll();
|
||||
Pair<HidDevice, HIDIdentifier> hid = unconsumedControllerHIDs.poll();
|
||||
if (hid == null) {
|
||||
Controlify.LOGGER.warn("No controller found via USB hardware scan! This prevents identifying controller type.");
|
||||
return new ControllerHIDInfo(ControllerType.UNKNOWN, Optional.empty());
|
||||
}
|
||||
|
||||
ControllerType type = ControllerType.getTypeMap().getOrDefault(hid.identifier(), ControllerType.UNKNOWN);
|
||||
ControllerType type = ControllerType.getTypeMap().getOrDefault(hid.getSecond(), ControllerType.UNKNOWN);
|
||||
if (type == ControllerType.UNKNOWN)
|
||||
Controlify.LOGGER.warn("Controller found via USB hardware scan, but it was not found in the controller identification database! (HID: {})", hid.identifier());
|
||||
Controlify.LOGGER.warn("Controller found via USB hardware scan, but it was not found in the controller identification database! (HID: {})", hid.getSecond());
|
||||
|
||||
unconsumedControllerHIDs.removeIf(h -> hid.path().equals(h.path()));
|
||||
unconsumedControllerHIDs.removeIf(h -> hid.getFirst().getPath().equals(h.getFirst().getPath()));
|
||||
|
||||
return new ControllerHIDInfo(type, Optional.of(hid.path()));
|
||||
return new ControllerHIDInfo(type, Optional.of(hid.getFirst()));
|
||||
}
|
||||
|
||||
public boolean isDisabled() {
|
||||
@ -75,7 +76,7 @@ public class ControllerHIDService {
|
||||
// add an unconsumed identifier that can be removed if not disconnected
|
||||
HIDIdentifier identifier = new HIDIdentifier(attachedDevice.getVendorId(), attachedDevice.getProductId());
|
||||
if (isController(attachedDevice))
|
||||
unconsumedControllerHIDs.add(new HIDIdentifierWithPath(attachedDevice.getPath(), identifier));
|
||||
unconsumedControllerHIDs.add(new Pair<>(attachedDevice, identifier));
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,7 +91,7 @@ public class ControllerHIDService {
|
||||
removeList.add(deviceId);
|
||||
|
||||
// remove device from unconsumed list
|
||||
unconsumedControllerHIDs.removeIf(device -> this.attachedDevices.get(deviceId).getPath().equals(device.path()));
|
||||
unconsumedControllerHIDs.removeIf(device -> this.attachedDevices.get(deviceId).getPath().equals(device.getFirst().getPath()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,16 +106,12 @@ public class ControllerHIDService {
|
||||
boolean isGenericDesktopControlOrGameControl = device.getUsagePage() == 0x1 || device.getUsagePage() == 0x5;
|
||||
boolean isSelfIdentifiedController = CONTROLLER_USAGE_IDS.contains(device.getUsage());
|
||||
|
||||
return ControllerType.getTypeMap().containsKey(new HIDIdentifier(device.getVendorId(), device.getProductId()))
|
||||
|| (isGenericDesktopControlOrGameControl && isSelfIdentifiedController);
|
||||
return isControllerType || (isGenericDesktopControlOrGameControl && isSelfIdentifiedController);
|
||||
}
|
||||
|
||||
public record ControllerHIDInfo(ControllerType type, Optional<String> path) {
|
||||
public record ControllerHIDInfo(ControllerType type, Optional<HidDevice> hidDevice) {
|
||||
public Optional<String> createControllerUID() {
|
||||
return path.map(p -> UUID.nameUUIDFromBytes(p.getBytes())).map(UUID::toString);
|
||||
return hidDevice.map(HidDevice::getPath).map(p -> UUID.nameUUIDFromBytes(p.getBytes())).map(UUID::toString);
|
||||
}
|
||||
}
|
||||
|
||||
private record HIDIdentifierWithPath(String path, HIDIdentifier identifier) {
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user