JibbSmart / JoyShockLibrary

Read DualSense, DualShock 4, JoyCon, and Pro Controller input on PC -- compiled for Windows, but code should work on other platforms.
Other
240 stars 48 forks source link

Store Hiddevice path inside JSL_SETTINGS #57

Open Valkirie opened 1 year ago

Valkirie commented 1 year ago

C# implementation has to be:

[DllImport("JoyShockLibrary")]
public static extern JOY_SETTINGS JslGetControllerInfoAndSettings(int deviceId);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public unsafe struct JOY_SETTINGS
{
    public int gyroSpace;
    public int colour;
    public int playerNumber;
    public int controllerType;
    public int splitType;
    [MarshalAs(UnmanagedType.U1)]
    public bool isCalibrating;
    [MarshalAs(UnmanagedType.U1)]
    public bool autoCalibrationEnabled;
    [MarshalAs(UnmanagedType.U1)]
    public bool isConnected;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string path;
}
JibbSmart commented 1 year ago

Thanks for this!

Just because a long string adds a lot to the size of JOY_SETTINGS, I think it'd be better to have a dedicated function for getting the string. The caller would give it a char array and the length of the array, and the function would fill the array with the path up to the given length.

Would you be up for giving that a go?

Valkirie commented 1 year ago

I could implement a dedicated function I guess. The only "issue" is that I'm not sure we can guess the hid path size in advance. Bluetooth devices have a longer path than wired devices. I'll investigate.

JibbSmart commented 1 year ago

It doesn't need to know the size in advance, just have an array that's big enough. Just like how you pre-allocated 256 in the JOY_SETTINGS version, the caller could say "it's probably not more than 256 bytes" and use an array that big.

I think this is a common pattern for strings in C. Commonly, something like this would also return an int that's the length of the actual string