Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
Added a ReaderWriterLockSlim instance for VMultiHandler for thread sa…
Browse files Browse the repository at this point in the history
…fety

Not using thread local data so doing this to be on the safe side
when using multiple controllers
  • Loading branch information
Ryochan7 committed Jul 31, 2020
1 parent 4dd8c98 commit 97d0d3e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions DS4Windows/DS4Control/OutputKBM/VMultiHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using VMultiDllWrapper;

namespace DS4Windows.DS4Control
Expand All @@ -16,6 +17,9 @@ public class VMultiHandler : VirtualKBMBase
private HashSet<KeyboardModifier> modifiers = new HashSet<KeyboardModifier>();
private HashSet<KeyboardKey> pressedKeys = new HashSet<KeyboardKey>();

// Used to guard reports and attempt to keep methods thread safe
private ReaderWriterLockSlim eventLock = new ReaderWriterLockSlim();

public VMultiHandler()
{
vMulti = new VMulti();
Expand All @@ -35,6 +39,8 @@ public override bool Disconnect()

private void Release()
{
eventLock.EnterWriteLock();

mouseReport.ResetMousePos();
vMulti.updateMouse(mouseReport);

Expand All @@ -51,22 +57,29 @@ private void Release()
pressedKeys.Clear();

vMulti.updateKeyboard(keyReport);

eventLock.ExitWriteLock();
}

public override void MoveRelativeMouse(int x, int y)
{
//Console.WriteLine("RAW MOUSE {0} {1}", x, y);
eventLock.EnterWriteLock();

mouseReport.ResetMousePos();
mouseReport.MouseX = (byte)(x < -127 ? 127 : (x > 127) ? 127 : x);
mouseReport.MouseY = (byte)(y < -127 ? 127 : (y > 127) ? 127 : y);

vMulti.updateMouse(mouseReport);

eventLock.ExitWriteLock();
}

public override void PerformKeyPress(uint key)
{
//Console.WriteLine("PerformKeyPress {0}", key);
bool sync = false;
eventLock.EnterWriteLock();

if (key < MODIFIER_MASK)
{
Expand All @@ -93,6 +106,8 @@ public override void PerformKeyPress(uint key)
{
vMulti.updateKeyboard(keyReport);
}

eventLock.ExitWriteLock();
}

/// <summary>
Expand All @@ -103,6 +118,7 @@ public override void PerformKeyPressAlt(uint key)
{
//Console.WriteLine("PerformKeyPressAlt {0}", key);
bool sync = false;
eventLock.EnterWriteLock();

if (key < MODIFIER_MASK)
{
Expand All @@ -129,12 +145,15 @@ public override void PerformKeyPressAlt(uint key)
{
vMulti.updateKeyboard(keyReport);
}

eventLock.ExitWriteLock();
}

public override void PerformKeyRelease(uint key)
{
//Console.WriteLine("PerformKeyRelease {0}", key);
bool sync = false;
eventLock.EnterWriteLock();

if (key < MODIFIER_MASK)
{
Expand All @@ -161,6 +180,8 @@ public override void PerformKeyRelease(uint key)
{
vMulti.updateKeyboard(keyReport);
}

eventLock.ExitWriteLock();
}

/// <summary>
Expand All @@ -171,6 +192,7 @@ public override void PerformKeyReleaseAlt(uint key)
{
//Console.WriteLine("PerformKeyReleaseAlt {0}", key);
bool sync = false;
eventLock.EnterWriteLock();

if (key < MODIFIER_MASK)
{
Expand All @@ -197,12 +219,15 @@ public override void PerformKeyReleaseAlt(uint key)
{
vMulti.updateKeyboard(keyReport);
}

eventLock.ExitWriteLock();
}

public override void PerformMouseButtonEvent(uint mouseButton)
{
bool sync = false;
MouseButton temp = (MouseButton)mouseButton;
eventLock.EnterWriteLock();

mouseReport.ResetMousePos();

Expand All @@ -221,6 +246,8 @@ public override void PerformMouseButtonEvent(uint mouseButton)
{
vMulti.updateMouse(mouseReport);
}

eventLock.ExitWriteLock();
}

/// <summary>
Expand All @@ -232,6 +259,7 @@ public override void PerformMouseButtonEventAlt(uint mouseButton, int type)
{
bool sync = false;
MouseButton temp = (MouseButton)mouseButton;
eventLock.EnterWriteLock();

mouseReport.ResetMousePos();

Expand All @@ -250,6 +278,8 @@ public override void PerformMouseButtonEventAlt(uint mouseButton, int type)
{
vMulti.updateMouse(mouseReport);
}

eventLock.ExitWriteLock();
}

/// <summary>
Expand All @@ -259,9 +289,11 @@ public override void PerformMouseButtonEventAlt(uint mouseButton, int type)
/// <param name="horizontal"></param>
public override void PerformMouseWheelEvent(int vertical, int horizontal)
{
eventLock.EnterWriteLock();
mouseReport.ResetMousePos();
mouseReport.WheelPosition = (byte)vertical;
vMulti.updateMouse(mouseReport);
eventLock.ExitWriteLock();
}

public override string GetDisplayName()
Expand All @@ -277,6 +309,7 @@ public override string GetIdentifier()
public override void PerformMouseButtonPress(uint mouseButton)
{
bool sync = false;
eventLock.EnterWriteLock();

MouseButton tempButton = (MouseButton)mouseButton;
if (!mouseReport.HeldButtons.Contains(tempButton))
Expand All @@ -290,11 +323,14 @@ public override void PerformMouseButtonPress(uint mouseButton)
{
vMulti.updateMouse(mouseReport);
}

eventLock.ExitWriteLock();
}

public override void PerformMouseButtonRelease(uint mouseButton)
{
bool sync = false;
eventLock.EnterWriteLock();

MouseButton tempButton = (MouseButton)mouseButton;
if (mouseReport.HeldButtons.Contains(tempButton))
Expand All @@ -308,6 +344,8 @@ public override void PerformMouseButtonRelease(uint mouseButton)
{
vMulti.updateMouse(mouseReport);
}

eventLock.ExitWriteLock();
}
}
}

0 comments on commit 97d0d3e

Please sign in to comment.