-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
929 additions
and
936 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,48 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Sandman | ||
namespace Sandman; | ||
|
||
public class NativeMethods | ||
{ | ||
public class NativeMethods | ||
public static TimeSpan GetTimeSinceLastActivity() | ||
{ | ||
public static TimeSpan GetTimeSinceLastActivity() | ||
LASTINPUTINFO lastInputInfo = LASTINPUTINFO.Create(); | ||
if (!GetLastInputInfo(ref lastInputInfo)) | ||
{ | ||
LASTINPUTINFO lastInputInfo = LASTINPUTINFO.Create(); | ||
if (!GetLastInputInfo(ref lastInputInfo)) | ||
{ | ||
return TimeSpan.Zero; | ||
} | ||
|
||
// Even though it says "ticks," it's really milliseconds. | ||
/// <see cref="https://docs.microsoft.com/en-us/dotnet/api/system.environment.tickcount"/> | ||
uint msecEnvTicks = (uint)Environment.TickCount; | ||
uint lastInputTick = lastInputInfo.dwTime; | ||
uint idleTime = msecEnvTicks - lastInputTick; | ||
return TimeSpan.FromMilliseconds(idleTime); | ||
return TimeSpan.Zero; | ||
} | ||
|
||
// Even though it says "ticks," it's really milliseconds. | ||
/// <see cref="https://docs.microsoft.com/en-us/dotnet/api/system.environment.tickcount"/> | ||
uint msecEnvTicks = (uint)Environment.TickCount; | ||
uint lastInputTick = lastInputInfo.dwTime; | ||
uint idleTime = msecEnvTicks - lastInputTick; | ||
return TimeSpan.FromMilliseconds(idleTime); | ||
} | ||
|
||
/// <see cref="https://www.pinvoke.net/default.aspx/Structures/LASTINPUTINFO.html"/> | ||
[StructLayout(LayoutKind.Sequential)] | ||
private struct LASTINPUTINFO | ||
{ | ||
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO)); | ||
|
||
[MarshalAs(UnmanagedType.U4)] | ||
public UInt32 cbSize; | ||
[MarshalAs(UnmanagedType.U4)] | ||
public UInt32 dwTime; // Even though it says "ticks," it's really milliseconds. | ||
|
||
public static LASTINPUTINFO Create() => new LASTINPUTINFO() | ||
{ | ||
cbSize = (uint)SizeOf, | ||
dwTime = 0, | ||
}; | ||
} | ||
|
||
/// <see cref="https://www.pinvoke.net/default.aspx/user32.GetLastInputInfo"/> | ||
/// <param name="plii"></param> | ||
/// <returns></returns> | ||
[DllImport("user32.dll")] | ||
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); | ||
/// <see cref="https://www.pinvoke.net/default.aspx/Structures/LASTINPUTINFO.html"/> | ||
[StructLayout(LayoutKind.Sequential)] | ||
private struct LASTINPUTINFO | ||
{ | ||
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO)); | ||
|
||
[MarshalAs(UnmanagedType.U4)] | ||
public UInt32 cbSize; | ||
[MarshalAs(UnmanagedType.U4)] | ||
public UInt32 dwTime; // Even though it says "ticks," it's really milliseconds. | ||
|
||
public static LASTINPUTINFO Create() => new LASTINPUTINFO() | ||
{ | ||
cbSize = (uint)SizeOf, | ||
dwTime = 0, | ||
}; | ||
} | ||
|
||
/// <see cref="https://www.pinvoke.net/default.aspx/user32.GetLastInputInfo"/> | ||
/// <param name="plii"></param> | ||
/// <returns></returns> | ||
[DllImport("user32.dll")] | ||
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); | ||
} |
Oops, something went wrong.