diff --git a/DS4Windows/DS4Control/OutputKBM/VMultiHandler.cs b/DS4Windows/DS4Control/OutputKBM/VMultiHandler.cs index e7937a38b5..1c250e49c5 100644 --- a/DS4Windows/DS4Control/OutputKBM/VMultiHandler.cs +++ b/DS4Windows/DS4Control/OutputKBM/VMultiHandler.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using VMultiDllWrapper; namespace DS4Windows.DS4Control @@ -16,6 +17,9 @@ public class VMultiHandler : VirtualKBMBase private HashSet modifiers = new HashSet(); private HashSet pressedKeys = new HashSet(); + // Used to guard reports and attempt to keep methods thread safe + private ReaderWriterLockSlim eventLock = new ReaderWriterLockSlim(); + public VMultiHandler() { vMulti = new VMulti(); @@ -35,6 +39,8 @@ public override bool Disconnect() private void Release() { + eventLock.EnterWriteLock(); + mouseReport.ResetMousePos(); vMulti.updateMouse(mouseReport); @@ -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) { @@ -93,6 +106,8 @@ public override void PerformKeyPress(uint key) { vMulti.updateKeyboard(keyReport); } + + eventLock.ExitWriteLock(); } /// @@ -103,6 +118,7 @@ public override void PerformKeyPressAlt(uint key) { //Console.WriteLine("PerformKeyPressAlt {0}", key); bool sync = false; + eventLock.EnterWriteLock(); if (key < MODIFIER_MASK) { @@ -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) { @@ -161,6 +180,8 @@ public override void PerformKeyRelease(uint key) { vMulti.updateKeyboard(keyReport); } + + eventLock.ExitWriteLock(); } /// @@ -171,6 +192,7 @@ public override void PerformKeyReleaseAlt(uint key) { //Console.WriteLine("PerformKeyReleaseAlt {0}", key); bool sync = false; + eventLock.EnterWriteLock(); if (key < MODIFIER_MASK) { @@ -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(); @@ -221,6 +246,8 @@ public override void PerformMouseButtonEvent(uint mouseButton) { vMulti.updateMouse(mouseReport); } + + eventLock.ExitWriteLock(); } /// @@ -232,6 +259,7 @@ public override void PerformMouseButtonEventAlt(uint mouseButton, int type) { bool sync = false; MouseButton temp = (MouseButton)mouseButton; + eventLock.EnterWriteLock(); mouseReport.ResetMousePos(); @@ -250,6 +278,8 @@ public override void PerformMouseButtonEventAlt(uint mouseButton, int type) { vMulti.updateMouse(mouseReport); } + + eventLock.ExitWriteLock(); } /// @@ -259,9 +289,11 @@ public override void PerformMouseButtonEventAlt(uint mouseButton, int type) /// 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() @@ -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)) @@ -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)) @@ -308,6 +344,8 @@ public override void PerformMouseButtonRelease(uint mouseButton) { vMulti.updateMouse(mouseReport); } + + eventLock.ExitWriteLock(); } } }