Repository navigation
Expand file tree
/
Copy pathIdentityStackProtocol.cs
More file actions
127 lines (121 loc) · 6.53 KB
/
Copy pathIdentityStackProtocol.cs
File metadata and controls
127 lines (121 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Buffers.Binary;
using System.Runtime.InteropServices;
using System.Text;
// Small, bounded snapshots; sent only when stacks change or Explorer reloads.
internal static class IdentityStackProtocol
{
const uint WmCopyData = 0x004A;
const uint Magic = 0x57425331; // WBS1
const uint Flags = 0x0001 | 0x0002; // SMTO_BLOCK | SMTO_ABORTIFHUNG
static IntPtr lastWindow;
static byte[]? lastPayload;
[StructLayout(LayoutKind.Sequential)]
struct CopyData { public IntPtr DwData; public int CbData; public IntPtr LpData; }
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SendMessageTimeout(IntPtr window, uint message, IntPtr wParam,
ref CopyData data, uint flags, uint timeoutMilliseconds, out IntPtr result);
public static bool Send(IntPtr tapWindow, AppStackConfig config, Settings settings)
{
if (tapWindow == IntPtr.Zero || config.Groups.Count > 4) return false;
using var buffer = new MemoryStream(8192);
Span<byte> header = stackalloc byte[8];
BinaryPrimitives.WriteUInt32LittleEndian(header[..4], 3);
BinaryPrimitives.WriteUInt32LittleEndian(header[4..], 4);
buffer.Write(header);
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
Span<byte> length = stackalloc byte[2];
Span<byte> appearance = stackalloc byte[12];
for (int slot = 0; slot < 4; slot++)
{
var members = slot < config.Groups.Count ? config.Groups[slot]?.Members ?? [] : [];
// One unusable group (hand-edited, or a disabled group sharing an app with another)
// is sent as empty instead of blocking every other folder.
var ids = members.Select(member => member?.AppId?.Trim() ?? "").ToList();
if (ids.Count is 1 or > 12 || ids.Any(id => id.Length is 0 or > 256 || id.Contains('\0')) ||
ids.Distinct(StringComparer.OrdinalIgnoreCase).Count() != ids.Count || ids.Any(seen.Contains))
{
members = [];
ids = [];
}
foreach (var id in ids) seen.Add(id);
BinaryPrimitives.WriteUInt16LittleEndian(length, (ushort)members.Count);
buffer.Write(length);
var group = slot < config.Groups.Count ? config.Groups[slot] : null;
int color = group == null ? 0 : StackFolderPalette.For(settings, group).Folder.ToArgb();
appearance.Clear();
BinaryPrimitives.WriteInt32LittleEndian(appearance[..4], color);
for (int glyph = 0; glyph < 4; glyph++)
{
char initial = glyph < members.Count ? (members[glyph]?.Name ?? "").Trim().FirstOrDefault() : ' ';
if (glyph == 3 && members.Count > 4) initial = '+';
if (char.IsSurrogate(initial) || char.IsControl(initial) || initial == '\0') initial = '?';
BinaryPrimitives.WriteUInt16LittleEndian(appearance.Slice(4 + glyph * 2, 2), char.ToUpperInvariant(initial));
}
buffer.Write(appearance);
buffer.WriteByte(group?.OpenOnClick == true ? (byte)1 : (byte)0);
foreach (var id in ids)
{
BinaryPrimitives.WriteUInt16LittleEndian(length, (ushort)id.Length);
buffer.Write(length);
buffer.Write(Encoding.Unicode.GetBytes(id));
}
}
byte[] payload = buffer.ToArray();
if (payload.Length > 16384) return false;
if (tapWindow == lastWindow && lastPayload is not null && payload.AsSpan().SequenceEqual(lastPayload)) return true;
IntPtr dataBytes = IntPtr.Zero;
try
{
dataBytes = Marshal.AllocHGlobal(payload.Length);
Marshal.Copy(payload, 0, dataBytes, payload.Length);
var data = new CopyData { DwData = (IntPtr)Magic, CbData = payload.Length, LpData = dataBytes };
bool okay = SendMessageTimeout(tapWindow, WmCopyData, IntPtr.Zero, ref data, Flags, 500, out var result) != IntPtr.Zero &&
result != IntPtr.Zero;
if (okay) { lastWindow = tapWindow; lastPayload = payload; }
return okay;
}
finally { if (dataBytes != IntPtr.Zero) Marshal.FreeHGlobal(dataBytes); }
}
public static void Invalidate() { lastWindow = IntPtr.Zero; lastPayload = null; }
// Explorer sends this after a completed same-taskbar drag. Parse before
// scheduling any settings mutation, so Explorer's UI thread never waits for it.
public static bool TryReadDrop(IntPtr lParam, out IntPtr taskbar, out System.Drawing.Point? startPoint,
out System.Drawing.Point dropPoint,
out string source, out string target)
{
taskbar = IntPtr.Zero;
startPoint = null;
dropPoint = default;
source = target = "";
if (lParam == IntPtr.Zero) return false;
var data = Marshal.PtrToStructure<CopyData>(lParam);
if (data.DwData != (IntPtr)0x57424431 || data.CbData is < 24 or > 2048 || data.LpData == IntPtr.Zero) return false;
byte[] bytes = new byte[data.CbData];
Marshal.Copy(data.LpData, bytes, 0, bytes.Length);
uint version = BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(0, 4));
if (version is not (2 or 3) || (version == 3 && bytes.Length < 32)) return false;
taskbar = (IntPtr)unchecked((long)BinaryPrimitives.ReadUInt64LittleEndian(bytes.AsSpan(4, 8)));
if (taskbar == IntPtr.Zero) return false;
int offset = 12;
if (version == 3)
{
startPoint = new System.Drawing.Point(BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(offset, 4)),
BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(offset + 4, 4)));
offset += 8;
}
dropPoint = new System.Drawing.Point(BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(offset, 4)),
BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(offset + 4, 4)));
offset += 8;
bool ReadId(bool allowEmpty, out string id)
{
id = "";
if (bytes.Length - offset < 2) return false;
int chars = BinaryPrimitives.ReadUInt16LittleEndian(bytes.AsSpan(offset, 2)); offset += 2;
int size = chars * 2;
if (chars > 256 || (!allowEmpty && chars == 0) || bytes.Length - offset < size) return false;
id = Encoding.Unicode.GetString(bytes.AsSpan(offset, size)); offset += size;
return !id.Contains('\0');
}
return ReadId(false, out source) && ReadId(true, out target) && offset == bytes.Length;
}
}