upload
This commit is contained in:
commit
1c7a8cf25f
579 files changed
+83196
No files matched your search
@@ -0,0 +1,192 @@
|
||||
using System;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public sealed class DarkBoostKeys : IDisposable
|
||||
{
|
||||
internal const int BindId = 4;
|
||||
|
||||
internal const int NextBindId = 5;
|
||||
|
||||
private readonly KeyBinder _binder;
|
||||
|
||||
private readonly InputRelay _relay;
|
||||
|
||||
private readonly IRawInputSource _source;
|
||||
|
||||
private readonly Func<bool> _offered;
|
||||
|
||||
private readonly Func<bool> _gameRunning;
|
||||
|
||||
private int _vk;
|
||||
|
||||
private uint _modifiers;
|
||||
|
||||
private bool _gameOnly;
|
||||
|
||||
private int _nextVk;
|
||||
|
||||
private uint _nextModifiers;
|
||||
|
||||
private bool _nextGameOnly;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
internal const int ZoomBindId = 6;
|
||||
|
||||
private int _zoomVk;
|
||||
|
||||
private uint _zoomModifiers;
|
||||
|
||||
private bool _zoomGameOnly = true;
|
||||
|
||||
public Func<bool> ZoomOffered { get; set; } = () => true;
|
||||
|
||||
public bool IsZoomBound => _binder.IsBound(6);
|
||||
|
||||
public bool Keyboard { get; private set; }
|
||||
|
||||
public bool Mouse { get; private set; }
|
||||
|
||||
public bool IsBound => _binder.IsBound(4);
|
||||
|
||||
public bool IsNextBound => _binder.IsBound(5);
|
||||
|
||||
public event Action<bool>? Zoom;
|
||||
|
||||
public event Action<string>? ZoomIgnored;
|
||||
|
||||
public event Action<bool>? Want;
|
||||
|
||||
public event Action? Next;
|
||||
|
||||
public DarkBoostKeys(Func<bool> offered, Func<bool> gameRunning, Action wake, Func<InputRelay, IRawInputSource> source)
|
||||
{
|
||||
_offered = offered;
|
||||
_gameRunning = gameRunning;
|
||||
_binder = new KeyBinder(gameRunning);
|
||||
_relay = new InputRelay(_binder.IsInteresting, wake);
|
||||
_source = source(_relay);
|
||||
_binder.Fired += (int id, bool on) =>
|
||||
{
|
||||
if (id == 4)
|
||||
{
|
||||
Want?.Invoke(on);
|
||||
}
|
||||
else if ((id == 5) & on)
|
||||
{
|
||||
Next?.Invoke();
|
||||
}
|
||||
else if (id == 6)
|
||||
{
|
||||
Zoom?.Invoke(on);
|
||||
}
|
||||
};
|
||||
_binder.Ignored += (int id, string why) =>
|
||||
{
|
||||
if (id == 6)
|
||||
{
|
||||
ZoomIgnored?.Invoke(why);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public bool BindZoom(uint modifiers, int vk, bool gameOnly = true)
|
||||
{
|
||||
bool flag = _binder.Register(6, modifiers, vk, KeyMode.Hold, gameOnly, () => ZoomOffered());
|
||||
if (flag)
|
||||
{
|
||||
_zoomVk = vk;
|
||||
_zoomModifiers = modifiers;
|
||||
_zoomGameOnly = gameOnly;
|
||||
}
|
||||
Settle();
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void UnbindZoom()
|
||||
{
|
||||
_binder.Unregister(6);
|
||||
Settle();
|
||||
}
|
||||
|
||||
public bool Bind(uint modifiers, int vk, bool hold, bool gameOnly)
|
||||
{
|
||||
bool flag = _binder.Register(4, modifiers, vk, hold ? KeyMode.Hold : KeyMode.Press, gameOnly, _offered);
|
||||
if (flag)
|
||||
{
|
||||
_vk = vk;
|
||||
_modifiers = modifiers;
|
||||
_gameOnly = gameOnly;
|
||||
}
|
||||
Settle();
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void Unbind()
|
||||
{
|
||||
_binder.Unregister(4);
|
||||
Settle();
|
||||
}
|
||||
|
||||
public bool BindNext(uint modifiers, int vk, bool gameOnly)
|
||||
{
|
||||
bool flag = _binder.Register(5, modifiers, vk, KeyMode.Press, gameOnly, _offered);
|
||||
if (flag)
|
||||
{
|
||||
_nextVk = vk;
|
||||
_nextModifiers = modifiers;
|
||||
_nextGameOnly = gameOnly;
|
||||
}
|
||||
Settle();
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void UnbindNext()
|
||||
{
|
||||
_binder.Unregister(5);
|
||||
Settle();
|
||||
}
|
||||
|
||||
public void Settle()
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
bool offered = _offered();
|
||||
bool gameRunning = _gameRunning();
|
||||
(bool, bool) tuple = RawInputPolicy.Wanted(offered, IsBound, _vk, _modifiers, _gameOnly, gameRunning);
|
||||
(bool, bool) tuple2 = RawInputPolicy.Wanted(offered, IsNextBound, _nextVk, _nextModifiers, _nextGameOnly, gameRunning);
|
||||
(bool, bool) tuple3 = RawInputPolicy.Wanted(ZoomOffered(), IsZoomBound, _zoomVk, _zoomModifiers, _zoomGameOnly, gameRunning);
|
||||
bool flag = tuple.Item1 || tuple2.Item1 || tuple3.Item1;
|
||||
bool flag2 = tuple.Item2 || tuple2.Item2 || tuple3.Item2;
|
||||
if (!flag && !flag2)
|
||||
{
|
||||
_binder.ReleaseAll();
|
||||
}
|
||||
if (flag != Keyboard || flag2 != Mouse)
|
||||
{
|
||||
_source.Apply(flag, flag2);
|
||||
Keyboard = flag;
|
||||
Mouse = flag2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Drain()
|
||||
{
|
||||
_relay.Drain(_binder);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
_disposed = true;
|
||||
_binder.ReleaseAll();
|
||||
_source.Apply(keyboard: false, mouse: false);
|
||||
bool keyboard = (Mouse = false);
|
||||
Keyboard = keyboard;
|
||||
_source.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public sealed class HotkeyCaptureGate
|
||||
{
|
||||
public static readonly HotkeyCaptureGate Shared = new HotkeyCaptureGate();
|
||||
|
||||
private readonly object _lock = new object();
|
||||
|
||||
private readonly HashSet<object> _listening = new HashSet<object>();
|
||||
|
||||
public bool IsCapturing
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _listening.Count > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public event Action? Began;
|
||||
|
||||
public event Action? Ended;
|
||||
|
||||
internal void Enter(object picker)
|
||||
{
|
||||
bool flag;
|
||||
lock (_lock)
|
||||
{
|
||||
flag = _listening.Count == 0;
|
||||
_listening.Add(picker);
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
Began?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
internal void Leave(object picker)
|
||||
{
|
||||
bool flag;
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_listening.Remove(picker))
|
||||
{
|
||||
return;
|
||||
}
|
||||
flag = _listening.Count == 0;
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
Ended?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public sealed class HotkeyCaptureRelease : IDisposable
|
||||
{
|
||||
private readonly HotkeyCaptureGate _gate;
|
||||
|
||||
private readonly Action _releaseAll;
|
||||
|
||||
private readonly Action _registerAll;
|
||||
|
||||
private bool _released;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
public bool IsReleased => _released;
|
||||
|
||||
public HotkeyCaptureRelease(HotkeyCaptureGate gate, Action releaseAll, Action registerAll)
|
||||
{
|
||||
_gate = gate ?? throw new ArgumentNullException("gate");
|
||||
_releaseAll = releaseAll ?? throw new ArgumentNullException("releaseAll");
|
||||
_registerAll = registerAll ?? throw new ArgumentNullException("registerAll");
|
||||
_gate.Began += OnBegan;
|
||||
_gate.Ended += OnEnded;
|
||||
}
|
||||
|
||||
private void OnBegan()
|
||||
{
|
||||
if (_disposed || _released)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_released = true;
|
||||
try
|
||||
{
|
||||
_releaseAll();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnded()
|
||||
{
|
||||
if (_disposed || !_released)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_released = false;
|
||||
try
|
||||
{
|
||||
_registerAll();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
_disposed = true;
|
||||
_gate.Began -= OnBegan;
|
||||
_gate.Ended -= OnEnded;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
internal static class HotkeyProbe
|
||||
{
|
||||
internal const int ErrorHotkeyAlreadyRegistered = 1409;
|
||||
|
||||
private const int ProbeId = 31317;
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool RegisterHotKey(nint hWnd, int id, uint fsModifiers, uint vk);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool UnregisterHotKey(nint hWnd, int id);
|
||||
|
||||
internal static int Real(nint hwnd, uint mask, uint vk)
|
||||
{
|
||||
if (hwnd == IntPtr.Zero)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return Run(() => RegisterHotKey(hwnd, 31317, mask, vk), () => Marshal.GetLastWin32Error(), () =>
|
||||
{
|
||||
UnregisterHotKey(hwnd, 31317);
|
||||
});
|
||||
}
|
||||
|
||||
internal static int Run(Func<bool> register, Func<int> lastError, Action unregister)
|
||||
{
|
||||
if (register())
|
||||
{
|
||||
unregister();
|
||||
return 0;
|
||||
}
|
||||
int num = lastError();
|
||||
if (num != 0)
|
||||
{
|
||||
return num;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public static class HotkeyStatus
|
||||
{
|
||||
private static readonly object Gate = new object();
|
||||
|
||||
private static readonly Dictionary<HotkeySlot, string> Refused = new Dictionary<HotkeySlot, string>();
|
||||
|
||||
public static void Record(HotkeySlot slot, string combo, bool bound)
|
||||
{
|
||||
lock (Gate)
|
||||
{
|
||||
if (bound)
|
||||
{
|
||||
Refused.Remove(slot);
|
||||
}
|
||||
else
|
||||
{
|
||||
Refused[slot] = combo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Forget(HotkeySlot slot)
|
||||
{
|
||||
lock (Gate)
|
||||
{
|
||||
Refused.Remove(slot);
|
||||
}
|
||||
}
|
||||
|
||||
public static string Line()
|
||||
{
|
||||
List<(HotkeySlot, string)> refused;
|
||||
lock (Gate)
|
||||
{
|
||||
refused = (from p in Refused
|
||||
orderby p.Key
|
||||
select (Key: p.Key, Value: p.Value)).ToList();
|
||||
}
|
||||
return Line(refused);
|
||||
}
|
||||
|
||||
public static string Line(IReadOnlyList<(HotkeySlot Slot, string Combo)> refused)
|
||||
{
|
||||
if (refused == null || refused.Count == 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
if (refused.Count == 1)
|
||||
{
|
||||
return $"Another app owns {refused[0].Combo}, so “{DisplayPageName(refused[0].Slot)}” does nothing. " + "Pick a different combination on the Display tab.";
|
||||
}
|
||||
List<string> list = refused.Select(((HotkeySlot Slot, string Combo) r) => r.Combo + " (“" + DisplayPageName(r.Slot) + "”)").ToList();
|
||||
string text = string.Join(", ", list.Take(list.Count - 1)) + " and " + list[list.Count - 1];
|
||||
return "Another app owns " + text + ", so they do nothing. Pick different combinations on the Display tab.";
|
||||
}
|
||||
|
||||
private static string DisplayPageName(HotkeySlot slot)
|
||||
{
|
||||
if (slot != HotkeySlot.QuickVibrance)
|
||||
{
|
||||
return HotkeyConflicts.Describe(slot);
|
||||
}
|
||||
return "Quick colour popup";
|
||||
}
|
||||
|
||||
internal static void ResetForTest()
|
||||
{
|
||||
lock (Gate)
|
||||
{
|
||||
Refused.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public interface IRawInputSource : IDisposable
|
||||
{
|
||||
void Apply(bool keyboard, bool mouse);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public readonly record struct InputEvent(int VirtualKey, bool Down);
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public sealed class InputRelay
|
||||
{
|
||||
public const int Capacity = 256;
|
||||
|
||||
private const int Mask = 255;
|
||||
|
||||
private readonly InputEvent[] _ring = new InputEvent[256];
|
||||
|
||||
private readonly Func<int, bool> _wanted;
|
||||
|
||||
private readonly Action _wake;
|
||||
|
||||
private int _head;
|
||||
|
||||
private int _tail;
|
||||
|
||||
private int _overflow;
|
||||
|
||||
public int Pending => Volatile.Read(in _head) - Volatile.Read(in _tail);
|
||||
|
||||
public InputRelay(Func<int, bool> wanted, Action wake)
|
||||
{
|
||||
_wanted = wanted;
|
||||
_wake = wake;
|
||||
}
|
||||
|
||||
public void OnRawEvent(int vk, bool down)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_wanted(vk))
|
||||
{
|
||||
return;
|
||||
}
|
||||
int head = _head;
|
||||
int num = Volatile.Read(in _tail);
|
||||
if (head - num >= 256)
|
||||
{
|
||||
Volatile.Write(ref _overflow, 1);
|
||||
return;
|
||||
}
|
||||
_ring[head & 0xFF] = new InputEvent(vk, down);
|
||||
Volatile.Write(ref _head, head + 1);
|
||||
if (head == num)
|
||||
{
|
||||
_wake();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void Drain(KeyBinder binder)
|
||||
{
|
||||
int num = _tail;
|
||||
while (num != Volatile.Read(in _head))
|
||||
{
|
||||
InputEvent e = _ring[num & 0xFF];
|
||||
_ring[num & 0xFF] = default;
|
||||
num++;
|
||||
Volatile.Write(ref _tail, num);
|
||||
binder.Handle(e);
|
||||
}
|
||||
if (Interlocked.Exchange(ref _overflow, 0) != 0)
|
||||
{
|
||||
binder.ReleaseAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public sealed class KeyBinder
|
||||
{
|
||||
private sealed class Binding
|
||||
{
|
||||
public int Id;
|
||||
|
||||
public uint Modifiers;
|
||||
|
||||
public int Vk;
|
||||
|
||||
public KeyMode Mode;
|
||||
|
||||
public bool GameOnly;
|
||||
|
||||
public Func<bool>? Allowed;
|
||||
|
||||
public bool Held;
|
||||
|
||||
public bool Engaged;
|
||||
}
|
||||
|
||||
public const int VK_LBUTTON = 1;
|
||||
|
||||
public const int VK_RBUTTON = 2;
|
||||
|
||||
public const int VK_MBUTTON = 4;
|
||||
|
||||
public const int VK_XBUTTON1 = 5;
|
||||
|
||||
public const int VK_XBUTTON2 = 6;
|
||||
|
||||
private readonly List<Binding> _bindings = new List<Binding>();
|
||||
|
||||
private readonly Func<bool> _gameRunning;
|
||||
|
||||
private int _modifierSides;
|
||||
|
||||
private volatile bool[] _interesting = Rebuilt(Array.Empty<Binding>());
|
||||
|
||||
public const string NoGameRunning = "no game running";
|
||||
|
||||
public const string NotAllowed = "not allowed";
|
||||
|
||||
public int Count => _bindings.Count;
|
||||
|
||||
public event Action<int, bool>? Fired;
|
||||
|
||||
public event Action<int, string>? Ignored;
|
||||
|
||||
public KeyBinder(Func<bool> gameRunning)
|
||||
{
|
||||
_gameRunning = gameRunning;
|
||||
}
|
||||
|
||||
public static bool IsBindable(int vk)
|
||||
{
|
||||
if (vk > 0 && vk < 256 && vk != 1 && vk != 2)
|
||||
{
|
||||
return ModifierSide(vk) < 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Register(int id, uint modifiers, int vk, KeyMode mode = KeyMode.Press, bool gameOnly = false, Func<bool>? allowed = null)
|
||||
{
|
||||
if (!IsBindable(vk))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
modifiers &= 0xF;
|
||||
foreach (Binding binding in _bindings)
|
||||
{
|
||||
if (binding.Id != id && binding.Vk == vk && binding.Modifiers == modifiers)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Unregister(id);
|
||||
_bindings.Add(new Binding
|
||||
{
|
||||
Id = id,
|
||||
Modifiers = modifiers,
|
||||
Vk = vk,
|
||||
Mode = mode,
|
||||
GameOnly = gameOnly,
|
||||
Allowed = allowed
|
||||
});
|
||||
_interesting = Rebuilt(_bindings);
|
||||
ForgetUnwatchedModifiers();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Unregister(int id)
|
||||
{
|
||||
for (int num = _bindings.Count - 1; num >= 0; num--)
|
||||
{
|
||||
Binding binding = _bindings[num];
|
||||
if (binding.Id == id)
|
||||
{
|
||||
_bindings.RemoveAt(num);
|
||||
if (binding.Engaged)
|
||||
{
|
||||
binding.Engaged = false;
|
||||
Fired?.Invoke(binding.Id, arg2: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
_interesting = Rebuilt(_bindings);
|
||||
ForgetUnwatchedModifiers();
|
||||
}
|
||||
|
||||
public bool IsBound(int id)
|
||||
{
|
||||
return _bindings.Exists((Binding b) => b.Id == id);
|
||||
}
|
||||
|
||||
public bool IsInteresting(int vk)
|
||||
{
|
||||
bool[] interesting = _interesting;
|
||||
if ((uint)vk < (uint)interesting.Length)
|
||||
{
|
||||
return interesting[vk];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Handle(InputEvent e)
|
||||
{
|
||||
int num = ModifierSide(e.VirtualKey);
|
||||
if (num >= 0)
|
||||
{
|
||||
if (e.Down)
|
||||
{
|
||||
_modifierSides |= 1 << num;
|
||||
}
|
||||
else
|
||||
{
|
||||
_modifierSides &= ~(1 << num);
|
||||
}
|
||||
}
|
||||
else if (e.Down)
|
||||
{
|
||||
OnDown(e.VirtualKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnUp(e.VirtualKey);
|
||||
}
|
||||
}
|
||||
|
||||
public void ReleaseAll()
|
||||
{
|
||||
_modifierSides = 0;
|
||||
foreach (Binding binding in _bindings)
|
||||
{
|
||||
binding.Held = false;
|
||||
if (binding.Engaged)
|
||||
{
|
||||
binding.Engaged = false;
|
||||
Fired?.Invoke(binding.Id, arg2: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDown(int vk)
|
||||
{
|
||||
uint num = HeldModifiers();
|
||||
Binding binding = null;
|
||||
int num2 = -1;
|
||||
foreach (Binding binding2 in _bindings)
|
||||
{
|
||||
if (binding2.Vk == vk && (binding2.Modifiers & ~num) == 0)
|
||||
{
|
||||
int num3 = PopCount(binding2.Modifiers);
|
||||
if (num3 > num2)
|
||||
{
|
||||
binding = binding2;
|
||||
num2 = num3;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (binding == null || binding.Held)
|
||||
{
|
||||
return;
|
||||
}
|
||||
binding.Held = true;
|
||||
if (binding.GameOnly && !_gameRunning())
|
||||
{
|
||||
Ignored?.Invoke(binding.Id, "no game running");
|
||||
return;
|
||||
}
|
||||
if (binding.Allowed != null && !binding.Allowed())
|
||||
{
|
||||
Ignored?.Invoke(binding.Id, "not allowed");
|
||||
return;
|
||||
}
|
||||
if (binding.Mode == KeyMode.Hold)
|
||||
{
|
||||
binding.Engaged = true;
|
||||
}
|
||||
Fired?.Invoke(binding.Id, arg2: true);
|
||||
}
|
||||
|
||||
private void OnUp(int vk)
|
||||
{
|
||||
foreach (Binding binding in _bindings)
|
||||
{
|
||||
if (binding.Vk == vk && binding.Held)
|
||||
{
|
||||
binding.Held = false;
|
||||
if (binding.Engaged)
|
||||
{
|
||||
binding.Engaged = false;
|
||||
Fired?.Invoke(binding.Id, arg2: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private uint HeldModifiers()
|
||||
{
|
||||
uint num = 0u;
|
||||
if ((_modifierSides & 3) != 0)
|
||||
{
|
||||
num |= 4;
|
||||
}
|
||||
if ((_modifierSides & 0xC) != 0)
|
||||
{
|
||||
num |= 2;
|
||||
}
|
||||
if ((_modifierSides & 0x30) != 0)
|
||||
{
|
||||
num |= 1;
|
||||
}
|
||||
if ((_modifierSides & 0xC0) != 0)
|
||||
{
|
||||
num |= 8;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
private static int ModifierSide(int vk)
|
||||
{
|
||||
switch (vk)
|
||||
{
|
||||
case 16:
|
||||
case 160:
|
||||
return 0;
|
||||
case 161:
|
||||
return 1;
|
||||
case 17:
|
||||
case 162:
|
||||
return 2;
|
||||
case 163:
|
||||
return 3;
|
||||
case 18:
|
||||
case 164:
|
||||
return 4;
|
||||
case 165:
|
||||
return 5;
|
||||
case 91:
|
||||
return 6;
|
||||
case 92:
|
||||
return 7;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private static int PopCount(uint v)
|
||||
{
|
||||
return BitOperations.PopCount(v);
|
||||
}
|
||||
|
||||
private static bool[] Rebuilt(IEnumerable<Binding> bindings)
|
||||
{
|
||||
bool[] array = new bool[256];
|
||||
uint num = 0u;
|
||||
foreach (Binding binding in bindings)
|
||||
{
|
||||
array[binding.Vk] = true;
|
||||
num |= binding.Modifiers;
|
||||
}
|
||||
if ((num & 4) != 0)
|
||||
{
|
||||
bool flag;
|
||||
array[160] = (flag = (array[161] = true));
|
||||
array[16] = flag;
|
||||
}
|
||||
if ((num & 2) != 0)
|
||||
{
|
||||
bool flag;
|
||||
array[162] = (flag = (array[163] = true));
|
||||
array[17] = flag;
|
||||
}
|
||||
if ((num & 1) != 0)
|
||||
{
|
||||
bool flag;
|
||||
array[164] = (flag = (array[165] = true));
|
||||
array[18] = flag;
|
||||
}
|
||||
if ((num & 8) != 0)
|
||||
{
|
||||
array[91] = (array[92] = true);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
private void ForgetUnwatchedModifiers()
|
||||
{
|
||||
bool[] interesting = _interesting;
|
||||
int num = 0;
|
||||
if (interesting[160])
|
||||
{
|
||||
num |= 3;
|
||||
}
|
||||
if (interesting[162])
|
||||
{
|
||||
num |= 0xC;
|
||||
}
|
||||
if (interesting[164])
|
||||
{
|
||||
num |= 0x30;
|
||||
}
|
||||
if (interesting[91])
|
||||
{
|
||||
num |= 0xC0;
|
||||
}
|
||||
_modifierSides &= num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public enum KeyMode
|
||||
{
|
||||
Press,
|
||||
Hold
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public static class RawInputPolicy
|
||||
{
|
||||
public static (bool Keyboard, bool Mouse) Wanted(bool offered, bool bound, int vk, uint modifiers, bool gameOnly, bool gameRunning)
|
||||
{
|
||||
if (!offered || !bound || (gameOnly && !gameRunning))
|
||||
{
|
||||
return (Keyboard: false, Mouse: false);
|
||||
}
|
||||
bool flag = IsMouseButton(vk);
|
||||
return (Keyboard: !flag || modifiers != 0, Mouse: flag);
|
||||
}
|
||||
|
||||
public static bool IsMouseButton(int vk)
|
||||
{
|
||||
if ((uint)(vk - 4) <= 2u)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace VibranceHud.Hotkeys;
|
||||
|
||||
public sealed class RawKeyListener : IRawInputSource, IDisposable
|
||||
{
|
||||
internal struct RAWINPUTDEVICE
|
||||
{
|
||||
public ushort UsagePage;
|
||||
|
||||
public ushort Usage;
|
||||
|
||||
public uint Flags;
|
||||
|
||||
public nint Target;
|
||||
}
|
||||
|
||||
private sealed class Sink : NativeWindow
|
||||
{
|
||||
private readonly RawKeyListener _owner;
|
||||
|
||||
public Sink(RawKeyListener owner)
|
||||
{
|
||||
_owner = owner;
|
||||
}
|
||||
|
||||
protected override void WndProc(ref Message m)
|
||||
{
|
||||
switch (m.Msg)
|
||||
{
|
||||
case 255:
|
||||
_owner.Read(m.LParam);
|
||||
break;
|
||||
case 32849:
|
||||
{
|
||||
int num = ((IntPtr)m.WParam).ToInt32();
|
||||
_owner.ChangeRegistration((num & 1) != 0, (num & 2) != 0, Handle);
|
||||
return;
|
||||
}
|
||||
case 32850:
|
||||
_owner.ChangeRegistration(keyboard: false, mouse: false, IntPtr.Zero);
|
||||
DestroyHandle();
|
||||
Application.ExitThread();
|
||||
return;
|
||||
}
|
||||
base.WndProc(ref m);
|
||||
}
|
||||
}
|
||||
|
||||
internal const uint RIDEV_REMOVE = 1u;
|
||||
|
||||
internal const uint RIDEV_INPUTSINK = 256u;
|
||||
|
||||
internal const ushort UsagePageGeneric = 1;
|
||||
|
||||
internal const ushort UsageMouse = 2;
|
||||
|
||||
internal const ushort UsageKeyboard = 6;
|
||||
|
||||
private const int WM_INPUT = 255;
|
||||
|
||||
private const int WM_APPLY = 32849;
|
||||
|
||||
private const int WM_STOP = 32850;
|
||||
|
||||
private const uint RID_INPUT = 268435459u;
|
||||
|
||||
private const int RIM_TYPEMOUSE = 0;
|
||||
|
||||
private const int RIM_TYPEKEYBOARD = 1;
|
||||
|
||||
private const ushort RI_KEY_BREAK = 1;
|
||||
|
||||
private const ushort RI_KEY_E0 = 2;
|
||||
|
||||
private const ushort ButtonFlagsWeRead = 1008;
|
||||
|
||||
internal static readonly int HeaderSize = 8 + 2 * IntPtr.Size;
|
||||
|
||||
private const int BufferSize = 64;
|
||||
|
||||
private readonly InputRelay _relay;
|
||||
|
||||
private readonly byte[] _buffer = new byte[64];
|
||||
|
||||
private GCHandle _pin;
|
||||
|
||||
private Thread? _thread;
|
||||
|
||||
private Sink? _sink;
|
||||
|
||||
private readonly ManualResetEventSlim _ready = new ManualResetEventSlim(initialState: false);
|
||||
|
||||
private bool _registeredKeyboard;
|
||||
|
||||
private bool _registeredMouse;
|
||||
|
||||
private readonly Func<RAWINPUTDEVICE[], bool>? _registrar;
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
private static extern bool RegisterRawInputDevices([In] RAWINPUTDEVICE[] devices, uint count, uint size);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern uint GetRawInputData(nint rawInput, uint command, nint data, ref uint size, uint headerSize);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool PostMessage(nint hWnd, int msg, nint wParam, nint lParam);
|
||||
|
||||
public RawKeyListener(InputRelay relay)
|
||||
: this(relay, null)
|
||||
{
|
||||
}
|
||||
|
||||
internal RawKeyListener(InputRelay relay, Func<RAWINPUTDEVICE[], bool>? registrar)
|
||||
{
|
||||
_relay = relay;
|
||||
_registrar = registrar;
|
||||
_pin = GCHandle.Alloc(_buffer, GCHandleType.Pinned);
|
||||
}
|
||||
|
||||
public void Apply(bool keyboard, bool mouse)
|
||||
{
|
||||
if (!keyboard && !mouse)
|
||||
{
|
||||
if (_thread != null)
|
||||
{
|
||||
Sink sink = _sink;
|
||||
if (sink != null)
|
||||
{
|
||||
PostMessage(sink.Handle, 32850, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
_thread.Join(2000);
|
||||
_thread = null;
|
||||
_sink = null;
|
||||
_ready.Reset();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (_thread == null)
|
||||
{
|
||||
_thread = new Thread(Run)
|
||||
{
|
||||
IsBackground = true,
|
||||
Name = "PlexusX Dark Boost keys"
|
||||
};
|
||||
_thread.Start();
|
||||
if (!_ready.Wait(2000))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
Sink sink2 = _sink;
|
||||
if (sink2 != null)
|
||||
{
|
||||
int value = (keyboard ? 1 : 0) | (mouse ? 2 : 0);
|
||||
PostMessage(sink2.Handle, 32849, new IntPtr(value), IntPtr.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Post(nint window, int message)
|
||||
{
|
||||
if (window != IntPtr.Zero)
|
||||
{
|
||||
PostMessage(window, message, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Apply(keyboard: false, mouse: false);
|
||||
if (_pin.IsAllocated)
|
||||
{
|
||||
_pin.Free();
|
||||
}
|
||||
_ready.Dispose();
|
||||
}
|
||||
|
||||
private void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
Sink sink = new Sink(this);
|
||||
sink.CreateHandle(new CreateParams
|
||||
{
|
||||
Parent = new IntPtr(-3)
|
||||
});
|
||||
_sink = sink;
|
||||
_ready.Set();
|
||||
Application.Run();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_ready.Set();
|
||||
}
|
||||
}
|
||||
|
||||
internal static RAWINPUTDEVICE Device(ushort usage, bool add, nint target)
|
||||
{
|
||||
if (!add)
|
||||
{
|
||||
return new RAWINPUTDEVICE
|
||||
{
|
||||
UsagePage = 1,
|
||||
Usage = usage,
|
||||
Flags = 1u,
|
||||
Target = IntPtr.Zero
|
||||
};
|
||||
}
|
||||
return new RAWINPUTDEVICE
|
||||
{
|
||||
UsagePage = 1,
|
||||
Usage = usage,
|
||||
Flags = 256u,
|
||||
Target = target
|
||||
};
|
||||
}
|
||||
|
||||
private void ChangeRegistration(bool keyboard, bool mouse, nint target)
|
||||
{
|
||||
RAWINPUTDEVICE[] array = new RAWINPUTDEVICE[2];
|
||||
int num = 0;
|
||||
if (keyboard != _registeredKeyboard)
|
||||
{
|
||||
array[num++] = Device(6, keyboard, target);
|
||||
}
|
||||
if (mouse != _registeredMouse)
|
||||
{
|
||||
array[num++] = Device(2, mouse, target);
|
||||
}
|
||||
switch (num)
|
||||
{
|
||||
case 0:
|
||||
return;
|
||||
case 1:
|
||||
Array.Resize(ref array, 1);
|
||||
break;
|
||||
}
|
||||
if ((_registrar != null) ? _registrar(array) : RegisterRawInputDevices(array, (uint)num, (uint)Marshal.SizeOf<RAWINPUTDEVICE>()))
|
||||
{
|
||||
_registeredKeyboard = keyboard;
|
||||
_registeredMouse = mouse;
|
||||
}
|
||||
}
|
||||
|
||||
private void Read(nint rawInput)
|
||||
{
|
||||
uint size = 64u;
|
||||
uint rawInputData = GetRawInputData(rawInput, 268435459u, _pin.AddrOfPinnedObject(), ref size, (uint)HeaderSize);
|
||||
if (rawInputData != 0 && rawInputData != uint.MaxValue && rawInputData <= 64)
|
||||
{
|
||||
Decode(new ReadOnlySpan<byte>(_buffer, 0, (int)rawInputData), _relay);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Decode(ReadOnlySpan<byte> raw, InputRelay relay)
|
||||
{
|
||||
if (raw.Length < HeaderSize + 8)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint num = BinaryPrimitives.ReadUInt32LittleEndian(raw);
|
||||
ReadOnlySpan<byte> source = raw.Slice(HeaderSize);
|
||||
switch (num)
|
||||
{
|
||||
case 0u:
|
||||
{
|
||||
ushort num5 = BinaryPrimitives.ReadUInt16LittleEndian(source.Slice(4));
|
||||
if ((num5 & 0x3F0) != 0)
|
||||
{
|
||||
if ((num5 & 0x10) != 0)
|
||||
{
|
||||
relay.OnRawEvent(4, down: true);
|
||||
}
|
||||
if ((num5 & 0x20) != 0)
|
||||
{
|
||||
relay.OnRawEvent(4, down: false);
|
||||
}
|
||||
if ((num5 & 0x40) != 0)
|
||||
{
|
||||
relay.OnRawEvent(5, down: true);
|
||||
}
|
||||
if ((num5 & 0x80) != 0)
|
||||
{
|
||||
relay.OnRawEvent(5, down: false);
|
||||
}
|
||||
if ((num5 & 0x100) != 0)
|
||||
{
|
||||
relay.OnRawEvent(6, down: true);
|
||||
}
|
||||
if ((num5 & 0x200) != 0)
|
||||
{
|
||||
relay.OnRawEvent(6, down: false);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1u:
|
||||
{
|
||||
ushort num2 = BinaryPrimitives.ReadUInt16LittleEndian(source);
|
||||
ushort num3 = BinaryPrimitives.ReadUInt16LittleEndian(source.Slice(2));
|
||||
int num4 = BinaryPrimitives.ReadUInt16LittleEndian(source.Slice(6));
|
||||
if (num4 != 0 && num4 < 255)
|
||||
{
|
||||
bool flag = (num3 & 2) != 0;
|
||||
relay.OnRawEvent(num4 switch
|
||||
{
|
||||
16 => (num2 == 54) ? 161 : 160,
|
||||
17 => flag ? 163 : 162,
|
||||
18 => flag ? 165 : 164,
|
||||
_ => num4,
|
||||
}, (num3 & 1) == 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user