diff --git a/src/Kirei.Application/System/Input/IInputActionMapper.cs b/src/Kirei.Application/System/InputProcessing/IInputActionMapper.cs similarity index 76% rename from src/Kirei.Application/System/Input/IInputActionMapper.cs rename to src/Kirei.Application/System/InputProcessing/IInputActionMapper.cs index 6d171b2..7c7625f 100644 --- a/src/Kirei.Application/System/Input/IInputActionMapper.cs +++ b/src/Kirei.Application/System/InputProcessing/IInputActionMapper.cs @@ -1,6 +1,6 @@ using System; -namespace Kirei.Application.System.Input +namespace Kirei.Application.System.InputProcessing { public interface IInputActionMapper { diff --git a/src/Kirei.Application/System/Input/IInputListener.cs b/src/Kirei.Application/System/InputProcessing/IInputListener.cs similarity index 71% rename from src/Kirei.Application/System/Input/IInputListener.cs rename to src/Kirei.Application/System/InputProcessing/IInputListener.cs index a9f75ed..592c9a5 100644 --- a/src/Kirei.Application/System/Input/IInputListener.cs +++ b/src/Kirei.Application/System/InputProcessing/IInputListener.cs @@ -1,6 +1,6 @@ using System; -namespace Kirei.Application.System.Input +namespace Kirei.Application.System.InputProcessing { public interface IInputListener { diff --git a/src/Kirei.Infrastructure/Kirei.Infrastructure.csproj b/src/Kirei.Infrastructure/Kirei.Infrastructure.csproj index 42f680d..1d6f511 100644 --- a/src/Kirei.Infrastructure/Kirei.Infrastructure.csproj +++ b/src/Kirei.Infrastructure/Kirei.Infrastructure.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Kirei.Infrastructure/Native/User32.Wrappers.cs b/src/Kirei.Infrastructure/Native/User32.Wrappers.cs index f69b84e..9cadc70 100644 --- a/src/Kirei.Infrastructure/Native/User32.Wrappers.cs +++ b/src/Kirei.Infrastructure/Native/User32.Wrappers.cs @@ -9,15 +9,6 @@ namespace Kirei.Infrastructure.Native { internal static partial class User32 { - internal struct LASTINPUTINFO - { - public uint cbSize; - public uint dwTime; - } - - [DllImport("user32.dll")] - private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); - [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int GetClassName( IntPtr hWnd, @@ -39,16 +30,6 @@ SetWindowPosFlags uFlags private static extern bool GetWindowRect( IntPtr hwnd, ref RECT rectangle); - - internal static long GetUserIdleTime() - { - var lastInputStruct = new LASTINPUTINFO(); - lastInputStruct.cbSize = (uint)Marshal.SizeOf(lastInputStruct); - - GetLastInputInfo(ref lastInputStruct); - - return ((uint)Environment.TickCount - lastInputStruct.dwTime); - } internal static string GetClassName(IntPtr hWnd) { diff --git a/src/Kirei.Infrastructure/System/Input/InputListener.cs b/src/Kirei.Infrastructure/System/Input/InputListener.cs deleted file mode 100644 index 6aac851..0000000 --- a/src/Kirei.Infrastructure/System/Input/InputListener.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Kirei.Application.System.Input; -using Kirei.Infrastructure.Configuration; -using Kirei.Infrastructure.Native; - -using System.Threading; - -namespace Kirei.Infrastructure.System.Input -{ - public class InputListener : - IInputListener - { - private IInputActionMapper _inputActionMapper; - private bool hasIconsBeenHidden = false; - - public void Listen(IInputActionMapper inputActionMapper) - { - _inputActionMapper = inputActionMapper; - - while (true) - { - var lastInputInMilliseconds = User32.GetUserIdleTime(); - var inactiveAfterMs = ConfigurationProvider.Configuration.Application.InactiveAfterMs; - - if (lastInputInMilliseconds >= inactiveAfterMs && !hasIconsBeenHidden) - { - _inputActionMapper.HandleInput(); - hasIconsBeenHidden = true; - } - else if (lastInputInMilliseconds < inactiveAfterMs && hasIconsBeenHidden) - { - _inputActionMapper.HandleInput(); - hasIconsBeenHidden = false; - } - - Thread.Sleep(ConfigurationProvider.Configuration.Application.InputPollingRate); - } - } - } -} diff --git a/src/Kirei.Infrastructure/System/Input/InputActionMapper.cs b/src/Kirei.Infrastructure/System/InputProcessing/InputActionMapper.cs similarity index 86% rename from src/Kirei.Infrastructure/System/Input/InputActionMapper.cs rename to src/Kirei.Infrastructure/System/InputProcessing/InputActionMapper.cs index c1afe19..6ef98b4 100644 --- a/src/Kirei.Infrastructure/System/Input/InputActionMapper.cs +++ b/src/Kirei.Infrastructure/System/InputProcessing/InputActionMapper.cs @@ -1,9 +1,9 @@ -using Kirei.Application.System.Input; +using Kirei.Application.System.InputProcessing; using System; using System.Collections.Generic; -namespace Kirei.Infrastructure.System.Input +namespace Kirei.Infrastructure.System.InputProcessing { public class InputActionMapper : IInputActionMapper { diff --git a/src/Kirei.Infrastructure/System/InputProcessing/InputListener.cs b/src/Kirei.Infrastructure/System/InputProcessing/InputListener.cs new file mode 100644 index 0000000..26a803c --- /dev/null +++ b/src/Kirei.Infrastructure/System/InputProcessing/InputListener.cs @@ -0,0 +1,57 @@ +using Kirei.Application.System.InputProcessing; +using Kirei.Infrastructure.Configuration; + +using NeatInput; +using NeatInput.Domain.Processing; + +using System; +using System.Threading; + +namespace Kirei.Infrastructure.System.InputProcessing +{ + public class InputListener : + IInputListener + { + private InputProvider _inputProvider; + private IInputActionMapper _inputActionMapper; + + private DateTime lastInputReceivedAt; + private bool hasIconsBeenHidden = false; + + public void Listen(IInputActionMapper inputActionMapper) + { + _inputProvider = new InputProvider(); + _inputActionMapper = inputActionMapper; + + lastInputReceivedAt = DateTime.Now; + + _inputProvider.InputReceived += OnInputReceived; + + var inactiveAfterMs = ConfigurationProvider.Configuration.Application.InactiveAfterMs; + var inputPollingRateMs = ConfigurationProvider.Configuration.Application.InputPollingRate; + + while (true) + { + var lastInputInMilliseconds = DateTime.Now.Subtract(lastInputReceivedAt).TotalMilliseconds; + + if (lastInputInMilliseconds >= inactiveAfterMs && !hasIconsBeenHidden) + { + _inputActionMapper.HandleInput(); + hasIconsBeenHidden = true; + } + else if (lastInputInMilliseconds < inactiveAfterMs && hasIconsBeenHidden) + { + _inputActionMapper.HandleInput(); + hasIconsBeenHidden = false; + } + + Thread.Sleep(inputPollingRateMs); + } + } + + private void OnInputReceived(Input input) + { + lastInputReceivedAt = DateTime.Now; + } + } +} diff --git a/src/Kirei/App.cs b/src/Kirei/App.cs index 13af09a..1bda856 100644 --- a/src/Kirei/App.cs +++ b/src/Kirei/App.cs @@ -1,7 +1,7 @@ using Kirei.Application; using Kirei.Application.System; using Kirei.Application.System.Desktop; -using Kirei.Application.System.Input; +using Kirei.Application.System.InputProcessing; using Kirei.Infrastructure.Configuration; namespace Kirei diff --git a/src/Kirei/Program.cs b/src/Kirei/Program.cs index d773972..9e25cf7 100644 --- a/src/Kirei/Program.cs +++ b/src/Kirei/Program.cs @@ -1,11 +1,11 @@ using Kirei.Application; using Kirei.Application.System; using Kirei.Application.System.Desktop; -using Kirei.Application.System.Input; +using Kirei.Application.System.InputProcessing; using Kirei.Infrastructure; using Kirei.Infrastructure.System; using Kirei.Infrastructure.System.Desktop; -using Kirei.Infrastructure.System.Input; +using Kirei.Infrastructure.System.InputProcessing; using Microsoft.Extensions.DependencyInjection;