-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace WinForms with raw WinAPI calls (#274)
- Loading branch information
Showing
53 changed files
with
660 additions
and
462 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using LightBulb.PlatformInterop.Internal; | ||
|
||
namespace LightBulb.PlatformInterop; | ||
|
||
public partial class GlobalHotKey : NativeResource<int> | ||
{ | ||
private readonly object _lock = new(); | ||
private readonly IDisposable _wndProcRegistration; | ||
|
||
private DateTimeOffset _lastTriggerTimestamp = DateTimeOffset.MinValue; | ||
|
||
public GlobalHotKey(int id, Action callback) | ||
: base(id) | ||
{ | ||
_wndProcRegistration = WndProcSponge | ||
.Default | ||
.Listen( | ||
0x312, | ||
m => | ||
{ | ||
// Filter out other hotkey events | ||
if (m.WParam != Handle) | ||
return; | ||
|
||
// Throttle triggers | ||
lock (_lock) | ||
{ | ||
if ( | ||
(DateTimeOffset.Now - _lastTriggerTimestamp).Duration().TotalSeconds | ||
< 0.05 | ||
) | ||
return; | ||
|
||
_lastTriggerTimestamp = DateTimeOffset.Now; | ||
} | ||
|
||
callback(); | ||
} | ||
); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
_wndProcRegistration.Dispose(); | ||
|
||
if (!NativeMethods.UnregisterHotKey(WndProcSponge.Default.Handle, Handle)) | ||
Debug.WriteLine($"Failed to dispose global hotkey #{Handle}."); | ||
} | ||
} | ||
|
||
public partial class GlobalHotKey | ||
{ | ||
private static int _lastHotKeyHandle; | ||
|
||
public static GlobalHotKey? TryRegister(int virtualKey, int modifiers, Action callback) | ||
{ | ||
var handle = Interlocked.Increment(ref _lastHotKeyHandle); | ||
|
||
if ( | ||
!NativeMethods.RegisterHotKey( | ||
WndProcSponge.Default.Handle, | ||
handle, | ||
modifiers, | ||
virtualKey | ||
) | ||
) | ||
{ | ||
Debug.WriteLine( | ||
$"Failed to register global hotkey (key: {virtualKey}, mods: {modifiers})." | ||
); | ||
|
||
return null; | ||
} | ||
|
||
return new GlobalHotKey(handle, callback); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace LightBulb.PlatformInterop.Internal; | ||
|
||
internal delegate bool EnumMonitorsProc( | ||
nint hMonitor, | ||
nint hdcMonitor, | ||
Rect lprcMonitor, | ||
nint dwData | ||
); |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace LightBulb.PlatformInterop.Internal; | ||
|
||
internal delegate bool EnumWindowsProc(nint hWnd, nint lParam); |
2 changes: 1 addition & 1 deletion
2
LightBulb.WindowsApi/Types/GammaRamp.cs → ...ulb.PlatformInterop/Internal/GammaRamp.cs
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LightBulb.PlatformInterop.Internal; | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal readonly record struct MonitorInfoEx | ||
{ | ||
public MonitorInfoEx() => Size = Marshal.SizeOf(this); | ||
|
||
public int Size { get; } | ||
public Rect Monitor { get; init; } | ||
public Rect WorkArea { get; init; } | ||
public uint Flags { get; init; } | ||
|
||
[field: MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] | ||
public string? DeviceName { get; init; } | ||
} |
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
11 changes: 3 additions & 8 deletions
11
...Bulb.WindowsApi/NativeMethods.Kernel32.cs → ...nterop/Internal/NativeMethods.Kernel32.cs
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
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
2 changes: 1 addition & 1 deletion
2
...WindowsApi/Types/PowerBroadcastSetting.cs → ...Interop/Internal/PowerBroadcastSetting.cs
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,7 +1,7 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LightBulb.WindowsApi.Types; | ||
namespace LightBulb.PlatformInterop.Internal; | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal readonly record struct PowerBroadcastSetting(Guid PowerSettingId); |
Oops, something went wrong.