Skip to content

Commit

Permalink
Fix guitar axis inputs not working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNathannator committed Jun 14, 2024
1 parent d406992 commit 8f039f2
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Program/PacketParsing/Backends/GameInputBackendDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal class GameInputBackendDevice : IDisposable, IBackendClient
private EventWaitHandle threadStop = new EventWaitHandle(false, EventResetMode.ManualReset);
private volatile bool ioError = false;

private byte[] lastReport = Array.Empty<byte>();
private int lastReportLength = 0;

private bool inputsEnabled = false;

public ushort VendorId { get; }
Expand Down Expand Up @@ -112,7 +115,9 @@ private unsafe void ReadThread()

private unsafe void ReadThreaded(DeviceMapper mapper)
{
ulong lastTimestamp = 0;
// We unfortunately can't rely on timestamp to determine state change,
// as guitar axis changes do not change the timestamp
// ulong lastTimestamp = 0;
while (!threadStop.WaitOne(0))
{
int hResult = gameInput.GetCurrentReading(GameInputKind.RawDeviceReport, device, out var reading);
Expand All @@ -128,11 +133,11 @@ private unsafe void ReadThreaded(DeviceMapper mapper)

using (reading)
{
// Ignore unchanged reports
ulong timestamp = reading.GetTimestamp();
if (lastTimestamp == timestamp)
continue;
lastTimestamp = timestamp;
// // Ignore unchanged reports
// ulong timestamp = reading.GetTimestamp();
// if (lastTimestamp == timestamp)
// continue;
// lastTimestamp = timestamp;

if (!HandleReading(reading, mapper))
break;
Expand All @@ -158,6 +163,16 @@ private unsafe bool HandleReading(LightIGameInputReading reading, DeviceMapper m
Debug.Assert(size == readSize);

var data = new ReadOnlySpan<byte>(buffer, (int)size);
// Compare with last report to determine if any inputs changed
// Necessary due to the timestamp not updating on guitar axis changes
if (data.SequenceEqual(lastReport.AsSpan(0, lastReportLength)))
return true;

if (lastReport.Length < data.Length)
lastReport = new byte[data.Length];
data.CopyTo(lastReport);
lastReportLength = data.Length;

var packet = new XboxPacket(data, directionIn: true)
{
Header = new ReadOnlySpan<byte>(&reportId, sizeof(byte))
Expand Down

0 comments on commit 8f039f2

Please sign in to comment.