1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Reduce memory allocation in Windows Input Provider

This commit is contained in:
Diogo Trindade 2023-08-22 12:41:06 +01:00
parent 5ccd1a9761
commit 11392f6043

View File

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Timers; using System.Timers;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
using Artemis.UI.Windows.Utilities; using Artemis.UI.Windows.Utilities;
using HidSharp;
using Linearstar.Windows.RawInput; using Linearstar.Windows.RawInput;
using Linearstar.Windows.RawInput.Native; using Linearstar.Windows.RawInput.Native;
using Serilog; using Serilog;
@ -22,6 +24,7 @@ public class WindowsInputProvider : InputProvider
private readonly IInputService _inputService; private readonly IInputService _inputService;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Timer _taskManagerTimer; private readonly Timer _taskManagerTimer;
private readonly Dictionary<RawInputDeviceHandle, string> _handleToDevicePath;
private int _lastProcessId; private int _lastProcessId;
delegate nint WndProc(nint hWnd, uint msg, nint wParam, nint lParam); delegate nint WndProc(nint hWnd, uint msg, nint wParam, nint lParam);
@ -40,6 +43,7 @@ public class WindowsInputProvider : InputProvider
_taskManagerTimer = new Timer(500); _taskManagerTimer = new Timer(500);
_taskManagerTimer.Elapsed += TaskManagerTimerOnElapsed; _taskManagerTimer.Elapsed += TaskManagerTimerOnElapsed;
_taskManagerTimer.Start(); _taskManagerTimer.Start();
_handleToDevicePath = new Dictionary<RawInputDeviceHandle, string>();
_hWnd = User32.CreateWindowEx(0, "STATIC", "", 0x80000000, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); _hWnd = User32.CreateWindowEx(0, "STATIC", "", 0x80000000, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
_hWndProcHook = User32.GetWindowLongPtr(_hWnd, GWL_WNDPROC); _hWndProcHook = User32.GetWindowLongPtr(_hWnd, GWL_WNDPROC);
@ -106,6 +110,20 @@ public class WindowsInputProvider : InputProvider
if (active?.ProcessName == "Taskmgr" || active?.ProcessName == "Idle") if (active?.ProcessName == "Taskmgr" || active?.ProcessName == "Idle")
_inputService.ReleaseAll(); _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 #region Keyboard
@ -126,7 +144,7 @@ public class WindowsInputProvider : InputProvider
if (key == KeyboardKey.None) if (key == KeyboardKey.None)
return; 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 // Let the core know there is an identifier so it can store new identifications if applicable
if (identifier != null) if (identifier != null)
@ -164,7 +182,7 @@ public class WindowsInputProvider : InputProvider
private int _previousMouseX; private int _previousMouseX;
private int _previousMouseY; private int _previousMouseY;
private void HandleMouseData(RawInputData data, RawInputMouseData mouseData) private void HandleMouseData(RawInputData data, RawInputMouseData mouseData)
{ {
// Only submit mouse movement 25 times per second but increment the delta // Only submit mouse movement 25 times per second but increment the delta
@ -176,7 +194,7 @@ public class WindowsInputProvider : InputProvider
} }
ArtemisDevice? device = null; ArtemisDevice? device = null;
string? identifier = data.Device?.DevicePath; string? identifier = GetDeviceIdentifier(in data);
if (identifier != null) if (identifier != null)
try try
{ {