Skip to content

Commit

Permalink
Prevent Octavi Exception on Connect (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMoebiuz authored Feb 25, 2024
1 parent 821e24a commit a716951
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions MobiFlight/Joysticks/Octavi/Octavi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,35 @@ public Octavi(SharpDX.DirectInput.Joystick joystick, JoystickDefinition definiti

}

/// <summary>
/// Method is not called by regular Joystick Manager
/// This method is called implicitly when Update() is called
/// </summary>
public void Connect()
{
if (Device == null)
// Prevent reentry and parallel execution by multiple threads
lock (this)
{
Device = DeviceList.Local.GetHidDeviceOrNull(vendorID: VendorId, productID: ProductId);
if (Device == null) return;
}
if (Device == null)
{
Device = DeviceList.Local.GetHidDeviceOrNull(vendorID: VendorId, productID: ProductId);
if (Device == null) return;
}

Stream = Device.Open();
Stream.ReadTimeout = System.Threading.Timeout.Infinite;
reportDescriptor = Device.GetReportDescriptor();
inputReceiver = reportDescriptor.CreateHidDeviceInputReceiver();
inputReceiver.Received += InputReceiver_Received;
inputReceiver.Start(Stream);
if (Stream == null)
{
Stream = Device.Open();
Stream.ReadTimeout = System.Threading.Timeout.Infinite;
reportDescriptor = Device.GetReportDescriptor();
}

if (inputReceiver == null)
{
inputReceiver = reportDescriptor.CreateHidDeviceInputReceiver();
inputReceiver.Received += InputReceiver_Received;
inputReceiver.Start(Stream);
}
}
}

private void InputReceiver_Received(object sender, System.EventArgs e)
Expand Down Expand Up @@ -80,6 +95,8 @@ protected void TriggerButtonPress(int i, MobiFlightButton.InputEvent inputEvent)

public override void Update()
{
// Octavi is not a DirectInput device
// so we have to connect it here.
if (Stream == null || inputReceiver == null)
{
Connect();
Expand All @@ -99,10 +116,17 @@ protected override void EnumerateDevices()

public override void Shutdown()
{
Stream.Close();
inputReceiver.Received -= InputReceiver_Received;
Stream = null;
inputReceiver = null;
if (Stream != null)
{
Stream.Close();
Stream = null;
}

if (inputReceiver != null)
{
inputReceiver.Received -= InputReceiver_Received;
inputReceiver = null;
}
}
}
}

0 comments on commit a716951

Please sign in to comment.