forked from Clones/Controlify
🐛 Fix rumble not working on joysticks + some driver refactoring
This commit is contained in:
@ -4,6 +4,8 @@ import dev.isxander.controlify.Controlify;
|
||||
import dev.isxander.controlify.bindings.ControllerBindings;
|
||||
import dev.isxander.controlify.controller.AbstractController;
|
||||
import dev.isxander.controlify.controller.BatteryLevel;
|
||||
import dev.isxander.controlify.driver.gamepad.BasicGamepadInputDriver;
|
||||
import dev.isxander.controlify.driver.gamepad.GamepadDrivers;
|
||||
import dev.isxander.controlify.hid.ControllerHIDService;
|
||||
import dev.isxander.controlify.debug.DebugProperties;
|
||||
import dev.isxander.controlify.driver.*;
|
||||
|
@ -112,7 +112,7 @@ public class SingleJoystickController extends AbstractController<JoystickState,
|
||||
|
||||
// the duration doesn't matter because we are not updating the joystick state,
|
||||
// so there is never any SDL check to stop the rumble after the desired time.
|
||||
if (SDL_JoystickRumbleTriggers(ptrJoystick, (short)(strongMagnitude * 65535.0F), (short)(weakMagnitude * 65535.0F), 1) != 0) {
|
||||
if (SDL_JoystickRumble(ptrJoystick, (short)(strongMagnitude * 65535.0F), (short)(weakMagnitude * 65535.0F), 1) != 0) {
|
||||
Log.LOGGER.error("Could not rumble controller " + name() + ": " + SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package dev.isxander.controlify.driver;
|
||||
|
||||
import dev.isxander.controlify.controller.gamepad.GamepadState;
|
||||
import dev.isxander.controlify.driver.gamepad.BasicGamepadInputDriver;
|
||||
import dev.isxander.controlify.hid.HIDDevice;
|
||||
import dev.isxander.controlify.utils.Log;
|
||||
|
||||
@ -157,9 +158,9 @@ public class SteamDeckDriver implements GyroDriver, BasicGamepadInputDriver {
|
||||
// .6 - X
|
||||
// .7 - A
|
||||
// .12 - Select
|
||||
// .13 - STEAM
|
||||
// .14 - Start
|
||||
// .15 - L5
|
||||
// .13 - STEAM
|
||||
// .14 - Start
|
||||
// .15 - L5
|
||||
// .16 - R5
|
||||
// .17 - L trackpad click
|
||||
// .18 - R trackpad click
|
||||
@ -170,11 +171,11 @@ public class SteamDeckDriver implements GyroDriver, BasicGamepadInputDriver {
|
||||
int buttons1BitMap,
|
||||
|
||||
// Buttons 2:
|
||||
// .9 - L4
|
||||
// .9 - L4
|
||||
// .10 - R4
|
||||
// .14 - L3 touch
|
||||
// .15 - R3 touch
|
||||
// .18 - (...)
|
||||
// .18 - (...)
|
||||
int buttons2BitMap,
|
||||
|
||||
short leftTrackpadX,
|
||||
|
@ -1,6 +1,7 @@
|
||||
package dev.isxander.controlify.driver;
|
||||
package dev.isxander.controlify.driver.gamepad;
|
||||
|
||||
import dev.isxander.controlify.controller.gamepad.GamepadState;
|
||||
import dev.isxander.controlify.driver.Driver;
|
||||
|
||||
public interface BasicGamepadInputDriver extends Driver {
|
||||
|
@ -1,6 +1,8 @@
|
||||
package dev.isxander.controlify.driver;
|
||||
package dev.isxander.controlify.driver.gamepad;
|
||||
|
||||
import dev.isxander.controlify.controller.gamepad.GamepadState;
|
||||
import dev.isxander.controlify.driver.GUIDProvider;
|
||||
import dev.isxander.controlify.driver.NameProviderDriver;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.glfw.GLFWGamepadState;
|
||||
|
@ -1,7 +1,8 @@
|
||||
package dev.isxander.controlify.driver;
|
||||
package dev.isxander.controlify.driver.gamepad;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import dev.isxander.controlify.debug.DebugProperties;
|
||||
import dev.isxander.controlify.driver.*;
|
||||
import dev.isxander.controlify.hid.HIDDevice;
|
||||
import dev.isxander.controlify.utils.Log;
|
||||
|
@ -1,10 +1,10 @@
|
||||
package dev.isxander.controlify.driver;
|
||||
package dev.isxander.controlify.driver.gamepad;
|
||||
|
||||
import com.sun.jna.Memory;
|
||||
import com.sun.jna.Pointer;
|
||||
import dev.isxander.controlify.controller.BatteryLevel;
|
||||
import dev.isxander.controlify.controller.gamepad.GamepadState;
|
||||
import dev.isxander.controlify.debug.DebugProperties;
|
||||
import dev.isxander.controlify.driver.*;
|
||||
import dev.isxander.controlify.utils.Log;
|
||||
import io.github.libsdl4j.api.gamecontroller.SDL_GameController;
|
||||
import net.minecraft.util.Mth;
|
@ -164,7 +164,7 @@ public class ControllerHIDService {
|
||||
Log.LOGGER.info("Using SDL to identify controller type.");
|
||||
return Optional.of(new ControllerHIDInfo(
|
||||
ControllerType.getTypeForHID(new HIDIdentifier(vid, pid)),
|
||||
Optional.of(new HIDDevice.IDOnly(vid, pid, path))
|
||||
Optional.of(new HIDDevice.SDLHidApi(vid, pid, path))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
package dev.isxander.controlify.hid;
|
||||
|
||||
public sealed interface HIDDevice permits HIDDevice.Hid4Java, HIDDevice.IDOnly {
|
||||
import com.sun.jna.Memory;
|
||||
import io.github.libsdl4j.api.hidapi.SDL_hid_device;
|
||||
import io.github.libsdl4j.api.hidapi.SdlHidApi;
|
||||
import io.github.libsdl4j.jna.size_t;
|
||||
|
||||
public sealed interface HIDDevice permits HIDDevice.Hid4Java, HIDDevice.IDOnly, HIDDevice.SDLHidApi {
|
||||
int vendorID();
|
||||
|
||||
int productID();
|
||||
@ -88,4 +93,69 @@ public sealed interface HIDDevice permits HIDDevice.Hid4Java, HIDDevice.IDOnly {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
final class SDLHidApi implements HIDDevice {
|
||||
private final int vendorId;
|
||||
private final int productId;
|
||||
private final String path;
|
||||
|
||||
private SDL_hid_device device;
|
||||
|
||||
public SDLHidApi(int vendorId, int productId, String path) {
|
||||
this.vendorId = vendorId;
|
||||
this.productId = productId;
|
||||
this.path = path;
|
||||
this.device = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int vendorID() {
|
||||
return vendorId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int productID() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String path() {
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsCommunication() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() {
|
||||
device = SdlHidApi.SDL_hid_open_path(path, 0);
|
||||
SdlHidApi.SDL_hid_set_nonblocking(device, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
SdlHidApi.SDL_hid_close(device);
|
||||
device = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer) {
|
||||
try (Memory memory = new Memory(buffer.length)) {
|
||||
int ret = SdlHidApi.SDL_hid_read(device, memory, new size_t(buffer.length));
|
||||
memory.read(0, buffer, 0, buffer.length);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int write(byte[] buffer, int packetLength, byte reportId) {
|
||||
try (Memory memory = new Memory(buffer.length + 1)) {
|
||||
memory.setByte(buffer.length, reportId);
|
||||
memory.write(1, buffer, 0, buffer.length);
|
||||
return SdlHidApi.SDL_hid_write(device, memory, new size_t(buffer.length));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user