| 1 | using System.Runtime.InteropServices; |
| 2 | |
| 3 | namespace Cage.GuestClipboard; |
| 4 | |
| 5 | internal readonly record struct InputStroke(ushort Value, bool Up, bool Unicode = false, int MouseButton = 0, int WheelDelta = 0, bool Horizontal = false) |
| 6 | { |
| 7 | internal static InputStroke Key(ushort value, bool up, bool unicode = false) => new(value, up, unicode); |
| 8 | internal static InputStroke Button(int bit, bool up) => new(0, up, false, bit); |
| 9 | internal static InputStroke Wheel(int delta, bool horizontal = false) => new(0, false, false, 0, delta, horizontal); |
| 10 | internal InputStroke Press => this with { Up = false }; |
| 11 | } |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | internal sealed class NativeInputCoordinator(Func<InputStroke, bool> inject) |
| 17 | { |
| 18 | internal sealed class Lease |
| 19 | { |
| 20 | internal readonly Dictionary<InputStroke, Func<bool>> Pressed = []; |
| 21 | internal readonly Dictionary<uint, InputStroke[]> Symbols = []; |
| 22 | internal readonly HashSet<InputStroke> Releasing = []; |
| 23 | internal bool Closed, Resetting; |
| 24 | } |
| 25 | private readonly object sync = new(); |
| 26 | private readonly HashSet<Lease> leases = []; |
| 27 | private readonly Dictionary<InputStroke, HashSet<Lease>> owners = []; |
| 28 | internal Lease Open() { lock(sync) { var lease = new Lease(); leases.Add(lease); return lease; } } |
| 29 | internal bool KeyHeld(ushort key) { lock(sync) return owners.ContainsKey(InputStroke.Key(key, false)); } |
| 30 | internal int Buttons(Lease lease) { lock(sync) return lease.Pressed.Keys.Aggregate(0, (mask, key) => mask | key.MouseButton); } |
| 31 | internal bool Owns(Lease lease, InputStroke stroke) { lock(sync) return lease.Pressed.ContainsKey(stroke.Press); } |
| 32 | internal void Emit(Lease lease, InputStroke[] strokes, Action validate, Func<bool> releaseAllowed) |
| 33 | { |
| 34 | lock(sync) |
| 35 | { |
| 36 | if(!leases.Contains(lease) || lease.Closed || lease.Resetting) throw new InvalidDataException("native_input_lease_closed"); |
| 37 | validate(); |
| 38 | foreach(var stroke in strokes) |
| 39 | { |
| 40 | var key = stroke.Press; |
| 41 | if(stroke.WheelDelta == 0 && stroke.Up) |
| 42 | { |
| 43 | |
| 44 | |
| 45 | if(!lease.Pressed.ContainsKey(key)) continue; |
| 46 | if(owners[key].Count > 1) { Forget(lease, key); continue; } |
| 47 | } |
| 48 | if(!inject(stroke)) throw new InvalidDataException("native_input_failed"); |
| 49 | if(stroke.WheelDelta != 0) continue; |
| 50 | if(stroke.Up) Forget(lease, key); |
| 51 | else |
| 52 | { |
| 53 | if(!owners.TryGetValue(key, out var set)) owners[key] = set = []; |
| 54 | set.Add(lease); lease.Pressed[key] = releaseAllowed; lease.Releasing.Remove(key); |
| 55 | } |
| 56 | } |
| 57 | validate(); |
| 58 | } |
| 59 | } |
| 60 | internal void Close(Lease lease) { lock(sync) { lease.Closed = true; Release(lease); } } |
| 61 | internal void Reset(Lease lease) { lock(sync) { lease.Resetting = true; Release(lease); } } |
| 62 | internal void ReleaseInputs(Lease lease,InputStroke[] strokes){lock(sync){foreach(var stroke in strokes)if(lease.Pressed.ContainsKey(stroke.Press))lease.Releasing.Add(stroke.Press);Release(lease);}} |
| 63 | internal void Retry() { lock(sync) foreach(var lease in leases.Where(l => l.Closed || l.Resetting || l.Releasing.Count>0).ToArray()) Release(lease); } |
| 64 | internal void CloseAll() { lock(sync) foreach(var lease in leases.ToArray()) { lease.Closed = true; Release(lease); } } |
| 65 | private void Release(Lease lease) |
| 66 | { |
| 67 | foreach(var item in lease.Pressed.Where(item=>lease.Closed||lease.Resetting||lease.Releasing.Contains(item.Key)).ToArray()) |
| 68 | { |
| 69 | if(owners[item.Key].Count > 1) { Forget(lease, item.Key); continue; } |
| 70 | try { if(item.Value() && inject(item.Key with { Up = true })) Forget(lease, item.Key); } |
| 71 | catch { } |
| 72 | } |
| 73 | if(lease.Pressed.Count == 0) |
| 74 | { |
| 75 | lease.Symbols.Clear(); lease.Resetting = false; |
| 76 | if(lease.Closed) leases.Remove(lease); |
| 77 | } |
| 78 | } |
| 79 | private void Forget(Lease lease, InputStroke key) |
| 80 | { |
| 81 | lease.Pressed.Remove(key);lease.Releasing.Remove(key); |
| 82 | if(owners.TryGetValue(key, out var set) && set.Remove(lease) && set.Count == 0) owners.Remove(key); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | internal static class NativeGuestInput |
| 87 | { |
| 88 | private static readonly NativeInputCoordinator State = new(Inject); |
| 89 | private static readonly System.Threading.Timer Retry = new(_ => State.Retry(), null, 250, 250); |
| 90 | static NativeGuestInput() => AppDomain.CurrentDomain.ProcessExit += (_, _) => { Retry.Dispose(); State.CloseAll(); }; |
| 91 | internal static NativeInputCoordinator.Lease OpenLease() => State.Open(); |
| 92 | internal static void CloseLease(NativeInputCoordinator.Lease lease) => State.Close(lease); |
| 93 | internal static void ResetLease(NativeInputCoordinator.Lease lease) => State.Reset(lease); |
| 94 | internal static void ReleaseInputs(NativeInputCoordinator.Lease lease,InputStroke[] strokes)=>State.ReleaseInputs(lease,strokes); |
| 95 | internal static bool PressedKey(ushort key) => State.KeyHeld(key); |
| 96 | internal static int PressedButtons(NativeInputCoordinator.Lease lease) => State.Buttons(lease); |
| 97 | internal static bool Owns(NativeInputCoordinator.Lease lease, InputStroke stroke) => State.Owns(lease, stroke); |
| 98 | internal static void Emit(NativeInputCoordinator.Lease lease, InputStroke[] strokes, Action validateBeforeAfter, Func<bool> releaseAllowed) => State.Emit(lease, strokes, validateBeforeAfter, releaseAllowed); |
| 99 | private static bool Inject(InputStroke stroke) |
| 100 | { |
| 101 | var input = new Input(); |
| 102 | if(stroke.WheelDelta != 0) input.Data.Mouse = new Mouse { Flags = stroke.Horizontal ? 0x1000u : 0x800u, Data = unchecked((uint)stroke.WheelDelta) }; |
| 103 | else if(stroke.MouseButton != 0) input.Data.Mouse = new Mouse { Flags = (stroke.MouseButton, stroke.Up) switch { (1,false) => 2u, (1,true) => 4u, (2,false) => 32u, (2,true) => 64u, (4,false) => 8u, (4,true) => 16u, _ => throw new InvalidDataException("native_input_invalid") } }; |
| 104 | else { input.Type = 1; input.Data.Key = new Key { Virtual = stroke.Unicode ? (ushort)0 : stroke.Value, Scan = stroke.Unicode ? stroke.Value : (ushort)0, Flags = (stroke.Unicode ? 4u : 0) | (stroke.Up ? 2u : 0) }; } |
| 105 | return SendInput(1, [input], Marshal.SizeOf<Input>()) == 1; |
| 106 | } |
| 107 | [StructLayout(LayoutKind.Sequential)] private struct Mouse { internal int X,Y; internal uint Data,Flags,Time; internal UIntPtr Extra; } |
| 108 | [StructLayout(LayoutKind.Sequential)] private struct Key { internal ushort Virtual,Scan; internal uint Flags,Time; internal UIntPtr Extra; } |
| 109 | [StructLayout(LayoutKind.Explicit)] private struct Data { [FieldOffset(0)] internal Mouse Mouse; [FieldOffset(0)] internal Key Key; } |
| 110 | [StructLayout(LayoutKind.Sequential)] private struct Input { internal uint Type; internal Data Data; } |
| 111 | [DllImport("user32.dll")] private static extern uint SendInput(uint count, Input[] inputs, int size); |
| 112 | } |