From 11392f60437c3abbaca0cbf280838c07caf17635 Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Tue, 22 Aug 2023 12:41:06 +0100 Subject: [PATCH 1/2] Reduce memory allocation in Windows Input Provider --- .../Providers/Input/WindowsInputProvider.cs | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs b/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs index 45c5624d3..648b7daf6 100644 --- a/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs +++ b/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Timers; using Artemis.Core; using Artemis.Core.Services; using Artemis.UI.Windows.Utilities; +using HidSharp; using Linearstar.Windows.RawInput; using Linearstar.Windows.RawInput.Native; using Serilog; @@ -22,6 +24,7 @@ public class WindowsInputProvider : InputProvider private readonly IInputService _inputService; private readonly ILogger _logger; private readonly Timer _taskManagerTimer; + private readonly Dictionary _handleToDevicePath; private int _lastProcessId; delegate nint WndProc(nint hWnd, uint msg, nint wParam, nint lParam); @@ -40,6 +43,7 @@ public class WindowsInputProvider : InputProvider _taskManagerTimer = new Timer(500); _taskManagerTimer.Elapsed += TaskManagerTimerOnElapsed; _taskManagerTimer.Start(); + _handleToDevicePath = new Dictionary(); _hWnd = User32.CreateWindowEx(0, "STATIC", "", 0x80000000, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); _hWndProcHook = User32.GetWindowLongPtr(_hWnd, GWL_WNDPROC); @@ -106,6 +110,20 @@ public class WindowsInputProvider : InputProvider if (active?.ProcessName == "Taskmgr" || active?.ProcessName == "Idle") _inputService.ReleaseAll(); } + + private string? GetDeviceIdentifier(in RawInputData data) + { + if (_handleToDevicePath.TryGetValue(data.Header.DeviceHandle, out string? identifier)) + return identifier; + + string? newIdentifier = data.Device?.DevicePath; + if (newIdentifier == null) + return null; + + _handleToDevicePath.Add(data.Header.DeviceHandle, newIdentifier); + + return newIdentifier; + } #region Keyboard @@ -126,7 +144,7 @@ public class WindowsInputProvider : InputProvider if (key == KeyboardKey.None) return; - string? identifier = data.Device?.DevicePath; + string? identifier = GetDeviceIdentifier(in data); // Let the core know there is an identifier so it can store new identifications if applicable if (identifier != null) @@ -164,7 +182,7 @@ public class WindowsInputProvider : InputProvider private int _previousMouseX; private int _previousMouseY; - + private void HandleMouseData(RawInputData data, RawInputMouseData mouseData) { // Only submit mouse movement 25 times per second but increment the delta @@ -176,7 +194,7 @@ public class WindowsInputProvider : InputProvider } ArtemisDevice? device = null; - string? identifier = data.Device?.DevicePath; + string? identifier = GetDeviceIdentifier(in data); if (identifier != null) try { From 51e1ecea68cf76ed5c84baa98c7e4996f114cad0 Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Fri, 1 Sep 2023 08:55:05 +0100 Subject: [PATCH 2/2] Remove weird import not sure how that got there haha --- src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs b/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs index 648b7daf6..04a701854 100644 --- a/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs +++ b/src/Artemis.UI.Windows/Providers/Input/WindowsInputProvider.cs @@ -6,7 +6,6 @@ using System.Timers; using Artemis.Core; using Artemis.Core.Services; using Artemis.UI.Windows.Utilities; -using HidSharp; using Linearstar.Windows.RawInput; using Linearstar.Windows.RawInput.Native; using Serilog;