dsh-world 0.7.5 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +541 -446
- package/CONTRIBUTING.md +174 -163
- package/INSTALL.md +314 -314
- package/NOTES.md +98 -5
- package/README.md +332 -258
- package/client/index.js +1126 -986
- package/lib/compaction.d.ts +7 -1
- package/lib/compaction.js +8 -3
- package/lib/console/routes.d.ts +11 -3
- package/lib/console/routes.js +12 -7
- package/lib/console/snapshot.d.ts +2 -0
- package/lib/console/snapshot.js +1 -0
- package/lib/ecosystem/computer/browser/browser.d.ts +34 -0
- package/lib/ecosystem/computer/browser/browser.js +101 -0
- package/lib/ecosystem/computer/browser/cdp.d.ts +22 -0
- package/lib/ecosystem/computer/browser/cdp.js +91 -0
- package/lib/ecosystem/computer/browser/edge.d.ts +15 -0
- package/lib/ecosystem/computer/browser/edge.js +80 -0
- package/lib/ecosystem/computer/browser/page.d.ts +41 -0
- package/lib/ecosystem/computer/browser/page.js +140 -0
- package/lib/ecosystem/computer/browser/pageLib.d.ts +7 -0
- package/lib/ecosystem/computer/browser/pageLib.js +106 -0
- package/lib/ecosystem/computer/desktop/desktop.d.ts +91 -0
- package/lib/ecosystem/computer/desktop/desktop.js +322 -0
- package/lib/ecosystem/computer/desktop/driver.d.ts +34 -0
- package/lib/ecosystem/computer/desktop/driver.js +197 -0
- package/lib/ecosystem/computer/desktop/processes.d.ts +5 -0
- package/lib/ecosystem/computer/desktop/processes.js +15 -0
- package/lib/ecosystem/computer/desktop/recording.d.ts +77 -0
- package/lib/ecosystem/computer/desktop/recording.js +187 -0
- package/lib/ecosystem/computer/desktop/screen.d.ts +33 -0
- package/lib/ecosystem/computer/desktop/screen.js +182 -0
- package/lib/ecosystem/computer/desktop/turns.d.ts +58 -0
- package/lib/ecosystem/computer/desktop/turns.js +94 -0
- package/lib/ecosystem/computer/desktop/watching.d.ts +25 -0
- package/lib/ecosystem/computer/desktop/watching.js +81 -0
- package/lib/ecosystem/computer/inbox/inbox.d.ts +32 -0
- package/lib/ecosystem/computer/inbox/inbox.js +142 -0
- package/lib/ecosystem/computer/index.d.ts +82 -0
- package/lib/ecosystem/computer/index.js +133 -0
- package/lib/ecosystem/computer/procedures/alarm.d.ts +13 -0
- package/lib/ecosystem/computer/procedures/alarm.js +46 -0
- package/lib/ecosystem/computer/procedures/archive.d.ts +32 -0
- package/lib/ecosystem/computer/procedures/archive.js +68 -0
- package/lib/ecosystem/computer/procedures/catalog.d.ts +7 -0
- package/lib/ecosystem/computer/procedures/catalog.js +75 -0
- package/lib/ecosystem/computer/procedures/format.d.ts +167 -0
- package/lib/ecosystem/computer/procedures/format.js +332 -0
- package/lib/ecosystem/computer/procedures/library.d.ts +53 -0
- package/lib/ecosystem/computer/procedures/library.js +178 -0
- package/lib/ecosystem/computer/procedures/run.d.ts +80 -0
- package/lib/ecosystem/computer/procedures/run.js +245 -0
- package/lib/ecosystem/computer/tools.d.ts +21 -0
- package/lib/ecosystem/computer/tools.js +197 -0
- package/lib/ecosystem/qq-native/images.d.ts +8 -1
- package/lib/ecosystem/qq-native/images.js +34 -15
- package/lib/ecosystem/qq-native/index.js +5 -5
- package/lib/ecosystem/shared/turn.d.ts +3 -0
- package/lib/ecosystem/shared/turn.js +13 -3
- package/lib/ecosystem/wechat/home.d.ts +1 -0
- package/lib/ecosystem/wechat/home.js +9 -3
- package/lib/ecosystem/wechat/images.js +3 -5
- package/lib/ecosystem/wechat/index.d.ts +6 -0
- package/lib/ecosystem/wechat/index.js +3 -1
- package/lib/ecosystem/wechat/presence.d.ts +5 -1
- package/lib/ecosystem/wechat/presence.js +22 -13
- package/lib/ecosystem/wechat/reading.d.ts +1 -1
- package/lib/ecosystem/wechat/reading.js +2 -2
- package/lib/elsewhere.d.ts +34 -0
- package/lib/elsewhere.js +40 -0
- package/lib/history.d.ts +5 -0
- package/lib/history.js +7 -1
- package/lib/hostContract.d.ts +6 -0
- package/lib/images.d.ts +30 -0
- package/lib/images.js +101 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +98 -6
- package/lib/life/photoTools.js +4 -15
- package/lib/life/runtime.d.ts +12 -2
- package/lib/life/runtime.js +93 -12
- package/lib/life/toolRegistry.d.ts +9 -3
- package/lib/life/toolRegistry.js +46 -27
- package/lib/life/turnCost.d.ts +16 -0
- package/lib/life/turnCost.js +44 -0
- package/lib/life/types.d.ts +17 -0
- package/lib/memoryTools.d.ts +4 -2
- package/lib/memoryTools.js +25 -5
- package/lib/mood.d.ts +5 -1
- package/lib/mood.js +25 -8
- package/lib/promptSection.d.ts +3 -0
- package/lib/promptSection.js +29 -42
- package/lib/runtime.js +44 -17
- package/lib/sessionNews.d.ts +17 -0
- package/lib/sessionNews.js +55 -0
- package/lib/store.d.ts +6 -0
- package/lib/store.js +17 -3
- package/lib/types.d.ts +4 -0
- package/lib/worldAdd.d.ts +9 -0
- package/lib/worldAdd.js +39 -0
- package/lib/worldPrompt.d.ts +3 -2
- package/lib/worldPrompt.js +3 -2
- package/lib/worldRoutes.d.ts +5 -2
- package/lib/worldRoutes.js +29 -4
- package/lib/worldSwitch.d.ts +8 -2
- package/lib/worldSwitch.js +11 -5
- package/package.json +132 -129
- package/src/ecosystem/README.md +2 -1
- package/src/ecosystem/computer/NOTICE.md +27 -0
- package/src/ecosystem/computer/README.md +244 -0
- package/src/ecosystem/computer/desktop/win32.ps1 +858 -0
- package/src/ecosystem/wechat/windows/README.md +4 -2
- package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
- package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
- package/templates/world-prompt-memory.md +10 -10
- package/templates/world-prompt.md +65 -65
|
@@ -0,0 +1,858 @@
|
|
|
1
|
+
# src/ecosystem/computer/desktop/win32.ps1
|
|
2
|
+
# Her hands on the partner's computer, from Windows itself: no driver to install. One request per stdin line,
|
|
3
|
+
# {"id":1,"op":"windows","args":{}}, one reply per stdout line, {"id":1,"data":...} or {"id":1,"error":"..."}.
|
|
4
|
+
# Input is posted to the window, never sent through the partner's mouse and keyboard, so the pointer and the
|
|
5
|
+
# foreground stay theirs. Stdin closing is the only way this process ends.
|
|
6
|
+
# With -Pipe it is foreground mode's helper instead: started as administrator, it talks over that pipe (see below).
|
|
7
|
+
param([string]$Pipe = '', [string]$Key = '')
|
|
8
|
+
$ErrorActionPreference = 'Stop'
|
|
9
|
+
|
|
10
|
+
Add-Type -ReferencedAssemblies System.Drawing, UIAutomationClient, UIAutomationTypes, WindowsBase -TypeDefinition @'
|
|
11
|
+
using System;
|
|
12
|
+
using System.Collections.Generic;
|
|
13
|
+
using System.Diagnostics;
|
|
14
|
+
using System.Drawing;
|
|
15
|
+
using System.Drawing.Drawing2D;
|
|
16
|
+
using System.Drawing.Imaging;
|
|
17
|
+
using System.Runtime.InteropServices;
|
|
18
|
+
using System.Text;
|
|
19
|
+
using System.Threading;
|
|
20
|
+
using System.Windows.Automation;
|
|
21
|
+
|
|
22
|
+
public class DeskWindow { public long hwnd { get; set; } public long owner { get; set; } public uint pid { get; set; } public string exe { get; set; } public string title { get; set; } public bool minimized { get; set; } public int width { get; set; } public int height { get; set; } }
|
|
23
|
+
public class DeskShot { public int width { get; set; } public int height { get; set; } public int clientWidth { get; set; } public int clientHeight { get; set; } public string print { get; set; } public int left { get; set; } public int top { get; set; } }
|
|
24
|
+
|
|
25
|
+
public static class Desk {
|
|
26
|
+
[StructLayout(LayoutKind.Sequential)] struct RECT { public int L, T, R, B; }
|
|
27
|
+
[StructLayout(LayoutKind.Sequential)] struct POINT { public int X, Y; }
|
|
28
|
+
[StructLayout(LayoutKind.Sequential)] struct GUITHREADINFO { public int cbSize, flags; public IntPtr hwndActive, hwndFocus, hwndCapture, hwndMenuOwner, hwndMoveSize, hwndCaret; public RECT rcCaret; }
|
|
29
|
+
delegate bool EnumProc(IntPtr h, IntPtr l);
|
|
30
|
+
[DllImport("user32.dll")] static extern bool SetProcessDpiAwarenessContext(IntPtr c);
|
|
31
|
+
[DllImport("user32.dll")] static extern IntPtr SetThreadDpiAwarenessContext(IntPtr c);
|
|
32
|
+
[DllImport("user32.dll")] static extern bool EnumWindows(EnumProc cb, IntPtr l);
|
|
33
|
+
[DllImport("user32.dll")] static extern bool IsWindow(IntPtr h);
|
|
34
|
+
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
|
|
35
|
+
[DllImport("user32.dll")] static extern bool IsIconic(IntPtr h);
|
|
36
|
+
[DllImport("user32.dll")] static extern IntPtr GetWindow(IntPtr h, uint cmd);
|
|
37
|
+
[DllImport("user32.dll")] static extern int GetWindowLong(IntPtr h, int index);
|
|
38
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetWindowText(IntPtr h, StringBuilder s, int n);
|
|
39
|
+
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
|
|
40
|
+
[DllImport("dwmapi.dll")] static extern int DwmGetWindowAttribute(IntPtr h, int attribute, out int value, int size);
|
|
41
|
+
[DllImport("user32.dll")] static extern bool GetClientRect(IntPtr h, out RECT r);
|
|
42
|
+
[DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr h, out RECT r);
|
|
43
|
+
[DllImport("user32.dll")] static extern bool PrintWindow(IntPtr h, IntPtr hdc, uint flags);
|
|
44
|
+
// The wide version: through PostMessageA a Chinese WM_CHAR is read as ANSI bytes and arrives garbled.
|
|
45
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern bool PostMessageW(IntPtr h, uint msg, IntPtr w, IntPtr l);
|
|
46
|
+
static bool PostMessage(IntPtr h, uint msg, IntPtr w, IntPtr l) { return PostMessageW(h, msg, w, l); }
|
|
47
|
+
[DllImport("user32.dll")] static extern bool ClientToScreen(IntPtr h, ref POINT p);
|
|
48
|
+
[DllImport("user32.dll")] static extern bool ScreenToClient(IntPtr h, ref POINT p);
|
|
49
|
+
[DllImport("user32.dll")] static extern IntPtr ChildWindowFromPointEx(IntPtr h, POINT p, uint flags);
|
|
50
|
+
[DllImport("user32.dll")] static extern bool GetGUIThreadInfo(uint thread, ref GUITHREADINFO info);
|
|
51
|
+
[DllImport("user32.dll")] static extern uint MapVirtualKey(uint code, uint type);
|
|
52
|
+
[DllImport("user32.dll")] static extern bool GetKeyboardState(byte[] state);
|
|
53
|
+
[DllImport("user32.dll")] static extern bool SetKeyboardState(byte[] state);
|
|
54
|
+
[DllImport("user32.dll")] static extern bool AttachThreadInput(uint a, uint b, bool attach);
|
|
55
|
+
[DllImport("kernel32.dll")] static extern uint GetCurrentThreadId();
|
|
56
|
+
[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
|
|
57
|
+
[DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr h);
|
|
58
|
+
[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr h, int command);
|
|
59
|
+
[DllImport("user32.dll")] static extern bool GetCursorPos(out POINT p);
|
|
60
|
+
[DllImport("user32.dll")] static extern void keybd_event(byte vk, byte scan, uint flags, UIntPtr extra);
|
|
61
|
+
[DllImport("user32.dll")] static extern int GetSystemMetrics(int index);
|
|
62
|
+
[DllImport("user32.dll")] static extern uint SendInput(uint count, INPUT[] inputs, int size);
|
|
63
|
+
[DllImport("kernel32.dll")] static extern IntPtr OpenProcess(uint access, bool inherit, uint pid);
|
|
64
|
+
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h);
|
|
65
|
+
[DllImport("advapi32.dll")] static extern bool OpenProcessToken(IntPtr process, uint access, out IntPtr token);
|
|
66
|
+
[StructLayout(LayoutKind.Sequential)] struct KEYBDINPUT { public ushort vk, scan; public uint flags, time; public IntPtr extra; }
|
|
67
|
+
[StructLayout(LayoutKind.Sequential)] struct MOUSEINPUT { public int dx, dy; public uint data, flags, time; public IntPtr extra; }
|
|
68
|
+
[StructLayout(LayoutKind.Explicit)] struct INPUTUNION { [FieldOffset(0)] public MOUSEINPUT mouse; [FieldOffset(0)] public KEYBDINPUT key; }
|
|
69
|
+
[StructLayout(LayoutKind.Sequential)] struct INPUT { public uint type; public INPUTUNION u; }
|
|
70
|
+
|
|
71
|
+
// Per-monitor aware, so window sizes, screenshots and posted coordinates are all real pixels on a scaled screen.
|
|
72
|
+
static readonly IntPtr PER_MONITOR = new IntPtr(-4);
|
|
73
|
+
public static void Aware() { SetProcessDpiAwarenessContext(PER_MONITOR); SetThreadDpiAwarenessContext(PER_MONITOR); }
|
|
74
|
+
|
|
75
|
+
public static long Foreground() { return GetForegroundWindow().ToInt64(); }
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Top-level windows a person could look at: visible, not a tool window, not cloaked, titled. Owned ones count: a
|
|
79
|
+
* program's dialogs and panels (WeChat's Moments composer) are owned by its main window.
|
|
80
|
+
*/
|
|
81
|
+
public static List<DeskWindow> Windows() {
|
|
82
|
+
Aware();
|
|
83
|
+
var list = new List<DeskWindow>();
|
|
84
|
+
EnumWindows((h, l) => {
|
|
85
|
+
if (!IsWindowVisible(h) || (GetWindowLong(h, -20) & 0x80) != 0) return true;
|
|
86
|
+
int cloaked;
|
|
87
|
+
if (DwmGetWindowAttribute(h, 14, out cloaked, 4) == 0 && cloaked != 0) return true;
|
|
88
|
+
var title = new StringBuilder(256);
|
|
89
|
+
GetWindowText(h, title, 256);
|
|
90
|
+
if (title.Length == 0) return true;
|
|
91
|
+
uint pid;
|
|
92
|
+
GetWindowThreadProcessId(h, out pid);
|
|
93
|
+
string exe = "";
|
|
94
|
+
try { exe = Process.GetProcessById((int)pid).ProcessName; } catch (Exception) { }
|
|
95
|
+
RECT r;
|
|
96
|
+
GetWindowRect(h, out r);
|
|
97
|
+
list.Add(new DeskWindow { hwnd = h.ToInt64(), owner = GetWindow(h, 4).ToInt64(), pid = pid, exe = exe, title = title.ToString(), minimized = IsIconic(h), width = r.R - r.L, height = r.B - r.T });
|
|
98
|
+
return true;
|
|
99
|
+
}, IntPtr.Zero);
|
|
100
|
+
return list;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static IntPtr Live(long hwnd) {
|
|
104
|
+
var h = new IntPtr(hwnd);
|
|
105
|
+
if (!IsWindow(h)) throw new Exception("window " + hwnd + " is gone");
|
|
106
|
+
return h;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The client area as the window itself draws it, covered or not; a part of it when w > 0. The picture is shrunk to
|
|
111
|
+
* fit `edge` and enlarged at most `grow` times, so a small part can be looked at closely.
|
|
112
|
+
*/
|
|
113
|
+
public static DeskShot Shot(long hwnd, string file, int edge, double grow, int x, int y, int w, int h, int markX, int markY) {
|
|
114
|
+
Aware();
|
|
115
|
+
var window = Live(hwnd);
|
|
116
|
+
RECT c;
|
|
117
|
+
GetClientRect(window, out c);
|
|
118
|
+
if (c.R <= 0 || c.B <= 0) throw new Exception("the window has no client area");
|
|
119
|
+
using (var full = new Bitmap(c.R, c.B, PixelFormat.Format32bppArgb)) {
|
|
120
|
+
using (var g = Graphics.FromImage(full)) {
|
|
121
|
+
var dc = g.GetHdc();
|
|
122
|
+
// PW_CLIENTONLY | PW_RENDERFULLCONTENT: without the second flag GPU-drawn windows come out black.
|
|
123
|
+
bool drawn;
|
|
124
|
+
try { drawn = PrintWindow(window, dc, 3); } finally { g.ReleaseHdc(dc); }
|
|
125
|
+
if (!drawn) {
|
|
126
|
+
uint pid; GetWindowThreadProcessId(window, out pid);
|
|
127
|
+
throw new Exception(Elevated(pid) ? "ELEVATED: the window belongs to a program running as administrator" : "PrintWindow failed");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return Render(full, file, edge, grow, x, y, w, h, markX, markY, 0, 0);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** The whole screen (every monitor), as foreground mode sees it; positions on it are screen positions from left, top. */
|
|
134
|
+
public static DeskShot Screen(string file, int edge, double grow, int x, int y, int w, int h, int markX, int markY) {
|
|
135
|
+
Aware();
|
|
136
|
+
int left = GetSystemMetrics(76), top = GetSystemMetrics(77), width = GetSystemMetrics(78), height = GetSystemMetrics(79);
|
|
137
|
+
using (var full = new Bitmap(width, height, PixelFormat.Format32bppArgb)) {
|
|
138
|
+
using (var g = Graphics.FromImage(full)) g.CopyFromScreen(left, top, 0, 0, full.Size);
|
|
139
|
+
return Render(full, file, edge, grow, x, y, w, h, markX, markY, left, top);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** A process the helper cannot read the token of runs above it: as administrator, or protected. */
|
|
143
|
+
static bool Elevated(uint pid) {
|
|
144
|
+
var process = OpenProcess(0x1000, false, pid);
|
|
145
|
+
if (process == IntPtr.Zero) return true;
|
|
146
|
+
try { IntPtr token; if (!OpenProcessToken(process, 0x8, out token)) return true; CloseHandle(token); return false; }
|
|
147
|
+
finally { CloseHandle(process); }
|
|
148
|
+
}
|
|
149
|
+
/** A part of `full` (all of it when w is 0), fitted to `edge` and enlarged at most `grow` times, with the last click marked. */
|
|
150
|
+
static DeskShot Render(Bitmap full, string file, int edge, double grow, int x, int y, int w, int h, int markX, int markY, int left, int top) {
|
|
151
|
+
var whole = new Rectangle(0, 0, full.Width, full.Height);
|
|
152
|
+
var part = w > 0 ? Rectangle.Intersect(new Rectangle(x, y, w, h), whole) : whole;
|
|
153
|
+
if (part.Width <= 0 || part.Height <= 0) throw new Exception("the region is outside the picture");
|
|
154
|
+
double k = Math.Min(grow, (double)edge / Math.Max(part.Width, part.Height));
|
|
155
|
+
int ow = Math.Max(1, (int)Math.Round(part.Width * k)), oh = Math.Max(1, (int)Math.Round(part.Height * k));
|
|
156
|
+
using (var picture = new Bitmap(ow, oh)) {
|
|
157
|
+
using (var g = Graphics.FromImage(picture)) {
|
|
158
|
+
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
159
|
+
g.DrawImage(full, new Rectangle(0, 0, ow, oh), part, GraphicsUnit.Pixel);
|
|
160
|
+
// Where the last click landed, so a miss of a few pixels is seen and corrected rather than repeated.
|
|
161
|
+
if (markX >= 0 && markY >= 0) {
|
|
162
|
+
float cx = (float)((markX - part.X) * k), cy = (float)((markY - part.Y) * k);
|
|
163
|
+
using (var pen = new Pen(Color.Red, 3)) {
|
|
164
|
+
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
165
|
+
g.DrawEllipse(pen, cx - 14, cy - 14, 28, 28);
|
|
166
|
+
g.DrawLine(pen, cx - 22, cy, cx - 6, cy); g.DrawLine(pen, cx + 6, cy, cx + 22, cy);
|
|
167
|
+
g.DrawLine(pen, cx, cy - 22, cx, cy - 6); g.DrawLine(pen, cx, cy + 6, cx, cy + 22);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// A recording keeps hundreds of frames, which as PNG would run to hundreds of megabytes.
|
|
172
|
+
picture.Save(file, file.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ? ImageFormat.Jpeg : ImageFormat.Png);
|
|
173
|
+
return new DeskShot { width = ow, height = oh, clientWidth = full.Width, clientHeight = full.Height, print = Print(picture), left = left, top = top };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The picture shrunk to 16 by 16 greys, as hex: two pictures of the same window compare by it, so "did anything
|
|
179
|
+
* move" is answered without looking at either. File sizes do not answer it: a paused video still differs by a spinner.
|
|
180
|
+
*/
|
|
181
|
+
static string Print(Bitmap picture) {
|
|
182
|
+
var text = new StringBuilder(512);
|
|
183
|
+
using (var small = new Bitmap(16, 16)) {
|
|
184
|
+
using (var g = Graphics.FromImage(small)) { g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawImage(picture, new Rectangle(0, 0, 16, 16)); }
|
|
185
|
+
for (int y = 0; y < 16; y++) for (int x = 0; x < 16; x++) { var c = small.GetPixel(x, y); text.Append(((c.R * 30 + c.G * 59 + c.B * 11) / 100).ToString("x2")); }
|
|
186
|
+
}
|
|
187
|
+
return text.ToString();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Frames of a recording side by side, each with its time in the corner, as one picture: a whole stretch of video
|
|
192
|
+
* at the cost of a single image. Every tile takes the first frame's shape.
|
|
193
|
+
*/
|
|
194
|
+
public static DeskShot Sheet(string[] files, string[] labels, string file, int columns, int tile) {
|
|
195
|
+
if (files.Length == 0 || labels.Length != files.Length || columns < 1) throw new Exception("no frames to lay out");
|
|
196
|
+
int th, gap = 4;
|
|
197
|
+
using (var first = Image.FromFile(files[0])) th = Math.Max(1, (int)Math.Round((double)tile * first.Height / first.Width));
|
|
198
|
+
int rows = (files.Length + columns - 1) / columns;
|
|
199
|
+
int width = columns * tile + (columns + 1) * gap, height = rows * th + (rows + 1) * gap;
|
|
200
|
+
using (var sheet = new Bitmap(width, height)) {
|
|
201
|
+
using (var g = Graphics.FromImage(sheet))
|
|
202
|
+
using (var font = new Font("Segoe UI", 15, FontStyle.Bold, GraphicsUnit.Pixel)) {
|
|
203
|
+
g.Clear(Color.Black);
|
|
204
|
+
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
205
|
+
for (int i = 0; i < files.Length; i++) {
|
|
206
|
+
int x = gap + (i % columns) * (tile + gap), y = gap + (i / columns) * (th + gap);
|
|
207
|
+
using (var frame = Image.FromFile(files[i])) g.DrawImage(frame, new Rectangle(x, y, tile, th));
|
|
208
|
+
var size = g.MeasureString(labels[i], font);
|
|
209
|
+
g.FillRectangle(Brushes.Black, x, y, size.Width + 6, size.Height + 2);
|
|
210
|
+
g.DrawString(labels[i], font, Brushes.Yellow, x + 3, y + 1);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
sheet.Save(file, ImageFormat.Jpeg);
|
|
214
|
+
}
|
|
215
|
+
return new DeskShot { width = width, height = height, clientWidth = width, clientHeight = height };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The innermost child window at a client point of `top`, with the point moved into its client coordinates. */
|
|
219
|
+
static IntPtr Under(IntPtr top, ref POINT point) {
|
|
220
|
+
var at = top;
|
|
221
|
+
for (int depth = 0; depth < 16; depth++) {
|
|
222
|
+
var child = ChildWindowFromPointEx(at, point, 1 | 2 | 4);
|
|
223
|
+
if (child == IntPtr.Zero || child == at) break;
|
|
224
|
+
ClientToScreen(at, ref point);
|
|
225
|
+
ScreenToClient(child, ref point);
|
|
226
|
+
at = child;
|
|
227
|
+
}
|
|
228
|
+
return at;
|
|
229
|
+
}
|
|
230
|
+
static IntPtr Pack(int low, int high) { return new IntPtr(((high & 0xFFFF) << 16) | (low & 0xFFFF)); }
|
|
231
|
+
|
|
232
|
+
public static void Click(long hwnd, int x, int y, bool right, int count, int hold) {
|
|
233
|
+
Aware();
|
|
234
|
+
var point = new POINT { X = x, Y = y };
|
|
235
|
+
var target = Under(Live(hwnd), ref point);
|
|
236
|
+
var at = Pack(point.X, point.Y);
|
|
237
|
+
uint down = right ? 0x204u : 0x201u, up = right ? 0x205u : 0x202u, twice = right ? 0x206u : 0x203u;
|
|
238
|
+
var button = new IntPtr(right ? 2 : 1);
|
|
239
|
+
PostMessage(target, 0x200, IntPtr.Zero, at);
|
|
240
|
+
// A long press is one press, held: the button stays down until the time is up.
|
|
241
|
+
if (hold > 0) { PostMessage(target, down, button, at); Thread.Sleep(hold); PostMessage(target, up, IntPtr.Zero, at); return; }
|
|
242
|
+
for (int i = 0; i < count; i++) { PostMessage(target, i == 1 ? twice : down, button, at); PostMessage(target, up, IntPtr.Zero, at); }
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Steps a drag takes between its two ends: enough that programs see the pointer travel, not jump. */
|
|
246
|
+
const int DragSteps = 20;
|
|
247
|
+
/**
|
|
248
|
+
* Presses at one point, holds there as long as asked (a long press that starts the drag), moves to the other and lets
|
|
249
|
+
* go; the button stays down throughout, as each move says. Both ends are in the window's own pixels.
|
|
250
|
+
*/
|
|
251
|
+
public static void Drag(long hwnd, int x, int y, int x2, int y2, bool right, int hold) {
|
|
252
|
+
Aware();
|
|
253
|
+
var point = new POINT { X = x, Y = y };
|
|
254
|
+
var target = Under(Live(hwnd), ref point);
|
|
255
|
+
int dx = x2 - x, dy = y2 - y;
|
|
256
|
+
uint down = right ? 0x204u : 0x201u, up = right ? 0x205u : 0x202u;
|
|
257
|
+
var button = new IntPtr(right ? 2 : 1);
|
|
258
|
+
PostMessage(target, 0x200, IntPtr.Zero, Pack(point.X, point.Y));
|
|
259
|
+
PostMessage(target, down, button, Pack(point.X, point.Y));
|
|
260
|
+
Thread.Sleep(hold > 0 ? hold : 80);
|
|
261
|
+
for (int i = 1; i <= DragSteps; i++) { PostMessage(target, 0x200, button, Pack(point.X + dx * i / DragSteps, point.Y + dy * i / DragSteps)); Thread.Sleep(20); }
|
|
262
|
+
PostMessage(target, up, IntPtr.Zero, Pack(point.X + dx, point.Y + dy));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
public static void Scroll(long hwnd, int x, int y, int notchesUp, int notchesRight) {
|
|
266
|
+
Aware();
|
|
267
|
+
var top = Live(hwnd);
|
|
268
|
+
var point = new POINT { X = x, Y = y };
|
|
269
|
+
var target = Under(top, ref point);
|
|
270
|
+
var screen = new POINT { X = x, Y = y };
|
|
271
|
+
ClientToScreen(top, ref screen);
|
|
272
|
+
var at = Pack(screen.X, screen.Y);
|
|
273
|
+
if (notchesUp != 0) PostMessage(target, 0x20A, Pack(0, notchesUp * 120), at);
|
|
274
|
+
if (notchesRight != 0) PostMessage(target, 0x20E, Pack(0, notchesRight * 120), at);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Where that window's own thread keeps the keyboard focus, which it keeps while in the background. */
|
|
278
|
+
static IntPtr Focused(IntPtr top, out uint thread) {
|
|
279
|
+
uint pid;
|
|
280
|
+
thread = GetWindowThreadProcessId(top, out pid);
|
|
281
|
+
var info = new GUITHREADINFO();
|
|
282
|
+
info.cbSize = Marshal.SizeOf(info);
|
|
283
|
+
return GetGUIThreadInfo(thread, ref info) && info.hwndFocus != IntPtr.Zero ? info.hwndFocus : top;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
public static void Type(long hwnd, string text) {
|
|
287
|
+
uint thread;
|
|
288
|
+
var to = Focused(Live(hwnd), out thread);
|
|
289
|
+
foreach (char c in text) PostMessage(to, 0x102, new IntPtr(c), new IntPtr(1));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
static int Left(int modifier) { return modifier == 0x11 ? 0xA2 : modifier == 0x10 ? 0xA0 : 0xA4; }
|
|
293
|
+
static bool Extended(int vk) { return (vk >= 0x21 && vk <= 0x28) || vk == 0x2D || vk == 0x2E; }
|
|
294
|
+
static IntPtr KeyBits(int vk, bool up) {
|
|
295
|
+
long bits = 1 | ((long)MapVirtualKey((uint)vk, 0) << 16);
|
|
296
|
+
if (Extended(vk)) bits |= 1L << 24;
|
|
297
|
+
if (up) bits |= 0xC0000000L;
|
|
298
|
+
return new IntPtr(bits);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Posted key messages do not change the keyboard state a program reads for Ctrl, Shift and Alt, so for a
|
|
303
|
+
* combination that window's input state is shared for a moment and the modifiers set in it, then put back.
|
|
304
|
+
*/
|
|
305
|
+
public static void Key(long hwnd, int vk, int[] modifiers, int hold) {
|
|
306
|
+
uint thread;
|
|
307
|
+
var to = Focused(Live(hwnd), out thread);
|
|
308
|
+
uint me = GetCurrentThreadId();
|
|
309
|
+
bool shared = modifiers.Length > 0 && AttachThreadInput(me, thread, true);
|
|
310
|
+
var saved = new byte[256];
|
|
311
|
+
bool alt = Array.IndexOf(modifiers, 0x12) >= 0;
|
|
312
|
+
uint down = alt ? 0x104u : 0x100u, up = alt ? 0x105u : 0x101u;
|
|
313
|
+
try {
|
|
314
|
+
if (shared) {
|
|
315
|
+
GetKeyboardState(saved);
|
|
316
|
+
var state = (byte[])saved.Clone();
|
|
317
|
+
// Qt and others ask for the left key (VK_LCONTROL), not only the generic one.
|
|
318
|
+
foreach (var m in modifiers) { state[m] |= 0x80; state[Left(m)] |= 0x80; }
|
|
319
|
+
SetKeyboardState(state);
|
|
320
|
+
}
|
|
321
|
+
foreach (var m in modifiers) PostMessage(to, m == 0x12 ? 0x104u : 0x100u, new IntPtr(m), KeyBits(m, false));
|
|
322
|
+
PostMessage(to, down, new IntPtr(vk), KeyBits(vk, false));
|
|
323
|
+
if (hold > 0) Thread.Sleep(hold);
|
|
324
|
+
PostMessage(to, up, new IntPtr(vk), KeyBits(vk, true));
|
|
325
|
+
for (int i = modifiers.Length - 1; i >= 0; i--) PostMessage(to, modifiers[i] == 0x12 ? 0x105u : 0x101u, new IntPtr(modifiers[i]), KeyBits(modifiers[i], true));
|
|
326
|
+
// The program reads the state when it takes the messages, which is a moment later.
|
|
327
|
+
if (shared) Thread.Sleep(200);
|
|
328
|
+
} finally {
|
|
329
|
+
if (shared) { SetKeyboardState(saved); AttachThreadInput(me, thread, false); }
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** A minimized window back to its place, without activating it: the partner keeps the focus. */
|
|
334
|
+
public static bool Restore(long hwnd) {
|
|
335
|
+
var window = Live(hwnd);
|
|
336
|
+
ShowWindow(window, 4);
|
|
337
|
+
Thread.Sleep(300);
|
|
338
|
+
return !IsIconic(window);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Gives the foreground back to `hwnd`; only the thread that has it may hand it over, so its input is joined first. */
|
|
342
|
+
public static bool Refocus(long hwnd) {
|
|
343
|
+
var to = new IntPtr(hwnd);
|
|
344
|
+
if (hwnd == 0 || !IsWindow(to)) return false;
|
|
345
|
+
var front = GetForegroundWindow();
|
|
346
|
+
if (front == to) return true;
|
|
347
|
+
uint pid;
|
|
348
|
+
uint thread = GetWindowThreadProcessId(front, out pid), me = GetCurrentThreadId();
|
|
349
|
+
bool joined = thread != 0 && AttachThreadInput(me, thread, true);
|
|
350
|
+
// Windows lets only the process that had the last input take the foreground, and a program that just pulled
|
|
351
|
+
// itself in front holds it; a key event with no key (code 0) makes this one that process without typing anything.
|
|
352
|
+
keybd_event(0, 0, 0, UIntPtr.Zero);
|
|
353
|
+
keybd_event(0, 0, 2, UIntPtr.Zero);
|
|
354
|
+
try { SetForegroundWindow(to); } finally { if (joined) AttachThreadInput(me, thread, false); }
|
|
355
|
+
return GetForegroundWindow() == to;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
static void Send(INPUT input) { SendInput(1, new[] { input }, Marshal.SizeOf(typeof(INPUT))); }
|
|
359
|
+
static void MouseAt(int x, int y) {
|
|
360
|
+
int left = GetSystemMetrics(76), top = GetSystemMetrics(77), width = GetSystemMetrics(78), height = GetSystemMetrics(79);
|
|
361
|
+
var move = new INPUT { type = 0 };
|
|
362
|
+
move.u.mouse = new MOUSEINPUT { dx = (int)Math.Round((x - left) * 65535.0 / Math.Max(1, width - 1)), dy = (int)Math.Round((y - top) * 65535.0 / Math.Max(1, height - 1)), flags = 0x0001 | 0x8000 | 0x4000 };
|
|
363
|
+
Send(move);
|
|
364
|
+
}
|
|
365
|
+
static void MouseButton(uint flags) { var input = new INPUT { type = 0 }; input.u.mouse = new MOUSEINPUT { flags = flags }; Send(input); }
|
|
366
|
+
static void KeyEvent(ushort vk, ushort scan, uint flags) { var input = new INPUT { type = 1 }; input.u.key = new KEYBDINPUT { vk = vk, scan = scan, flags = flags }; Send(input); }
|
|
367
|
+
/** Moves the partner's pointer there and clicks: x, y are screen pixels. */
|
|
368
|
+
public static void RealClick(int x, int y, bool right, int count, int hold) {
|
|
369
|
+
Aware();
|
|
370
|
+
MouseAt(x, y); Thread.Sleep(60);
|
|
371
|
+
if (hold > 0) { MouseButton(right ? 0x0008u : 0x0002u); Thread.Sleep(hold); MouseButton(right ? 0x0010u : 0x0004u); return; }
|
|
372
|
+
for (int i = 0; i < Math.Max(1, count); i++) { MouseButton(right ? 0x0008u : 0x0002u); Thread.Sleep(40); MouseButton(right ? 0x0010u : 0x0004u); Thread.Sleep(60); }
|
|
373
|
+
}
|
|
374
|
+
/** The same drag with the partner's real pointer; both ends are screen pixels. */
|
|
375
|
+
public static void RealDrag(int x, int y, int x2, int y2, bool right, int hold) {
|
|
376
|
+
Aware();
|
|
377
|
+
MouseAt(x, y); Thread.Sleep(60);
|
|
378
|
+
MouseButton(right ? 0x0008u : 0x0002u);
|
|
379
|
+
Thread.Sleep(hold > 0 ? hold : 80);
|
|
380
|
+
for (int i = 1; i <= DragSteps; i++) { MouseAt(x + (x2 - x) * i / DragSteps, y + (y2 - y) * i / DragSteps); Thread.Sleep(20); }
|
|
381
|
+
Thread.Sleep(40);
|
|
382
|
+
MouseButton(right ? 0x0010u : 0x0004u);
|
|
383
|
+
}
|
|
384
|
+
/** Types as characters, not keys, so any language goes in as written; a line break is the Enter key. */
|
|
385
|
+
public static void RealType(string text) {
|
|
386
|
+
foreach (var ch in text) {
|
|
387
|
+
if (ch == '\r') continue;
|
|
388
|
+
if (ch == '\n') { RealKey(0x0D, new int[0], 0); continue; }
|
|
389
|
+
KeyEvent(0, ch, 0x0004); KeyEvent(0, ch, 0x0004 | 0x0002); Thread.Sleep(8);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/** A key with its scan code too: games read the keyboard by scan code. */
|
|
393
|
+
public static void RealKey(int vk, int[] modifiers, int hold) {
|
|
394
|
+
foreach (var m in modifiers) KeyEvent((ushort)m, (ushort)MapVirtualKey((uint)m, 0), 0);
|
|
395
|
+
KeyEvent((ushort)vk, (ushort)MapVirtualKey((uint)vk, 0), 0); Thread.Sleep(hold > 0 ? hold : 60);
|
|
396
|
+
KeyEvent((ushort)vk, (ushort)MapVirtualKey((uint)vk, 0), 0x0002);
|
|
397
|
+
for (int i = modifiers.Length - 1; i >= 0; i--) KeyEvent((ushort)modifiers[i], (ushort)MapVirtualKey((uint)modifiers[i], 0), 0x0002);
|
|
398
|
+
}
|
|
399
|
+
public static void RealScroll(int x, int y, int up, int right) {
|
|
400
|
+
Aware();
|
|
401
|
+
MouseAt(x, y); Thread.Sleep(60);
|
|
402
|
+
if (up != 0) { var input = new INPUT { type = 0 }; input.u.mouse = new MOUSEINPUT { data = (uint)(up * 120), flags = 0x0800 }; Send(input); }
|
|
403
|
+
if (right != 0) { var input = new INPUT { type = 0 }; input.u.mouse = new MOUSEINPUT { data = (uint)(right * 120), flags = 0x1000 }; Send(input); }
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
public static long[] Cursor() { POINT p; GetCursorPos(out p); return new long[] { p.X, p.Y }; }
|
|
407
|
+
|
|
408
|
+
static AutomationElement Child(AutomationElement under, string className) {
|
|
409
|
+
return under == null ? null : under.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, className));
|
|
410
|
+
}
|
|
411
|
+
/** On the taskbar only notification icons count: a taskbar button such as "WeChat - 1 running window" cannot be invoked. */
|
|
412
|
+
static AutomationElement Icon(AutomationElement area, string name, bool notifyOnly) {
|
|
413
|
+
if (area == null) return null;
|
|
414
|
+
foreach (AutomationElement button in area.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button))) {
|
|
415
|
+
if (notifyOnly && button.Current.AutomationId != "NotifyItemIcon") continue;
|
|
416
|
+
var label = (button.Current.Name ?? "").Trim();
|
|
417
|
+
if (label.StartsWith(name, StringComparison.OrdinalIgnoreCase)) return button;
|
|
418
|
+
}
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
static void Invoke(AutomationElement element) { ((InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern)).Invoke(); }
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Clicks a program's notification-area icon through UI Automation, which is how a program closed to the tray is
|
|
425
|
+
* brought back as itself; showing its hidden window from outside leaves one that takes no input. Looks in the
|
|
426
|
+
* visible icons, then in the hidden-icons flyout. Returns "missing", or "opened" once the click is made.
|
|
427
|
+
*/
|
|
428
|
+
public static string Tray(string name) {
|
|
429
|
+
var root = AutomationElement.RootElement;
|
|
430
|
+
var bar = Child(root, "Shell_TrayWnd");
|
|
431
|
+
var icon = Icon(bar, name, true);
|
|
432
|
+
if (icon == null) {
|
|
433
|
+
var flyout = Child(root, "TopLevelWindowForOverflowXamlIsland");
|
|
434
|
+
if (flyout == null || flyout.Current.IsOffscreen) {
|
|
435
|
+
// The hidden-icons chevron shares its id with the input indicator, but comes first; its name is localized.
|
|
436
|
+
var chevron = bar == null ? null : bar.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "SystemTrayIcon"));
|
|
437
|
+
if (chevron == null) return "missing";
|
|
438
|
+
Invoke(chevron);
|
|
439
|
+
Thread.Sleep(600);
|
|
440
|
+
flyout = Child(root, "TopLevelWindowForOverflowXamlIsland");
|
|
441
|
+
}
|
|
442
|
+
icon = Icon(flyout, name, false);
|
|
443
|
+
}
|
|
444
|
+
if (icon == null) return "missing";
|
|
445
|
+
Invoke(icon);
|
|
446
|
+
return "opened";
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
'@
|
|
450
|
+
|
|
451
|
+
[Desk]::Aware()
|
|
452
|
+
$utf8 = New-Object System.Text.UTF8Encoding $false
|
|
453
|
+
if ($Pipe) {
|
|
454
|
+
# Foreground mode: started as administrator through UAC, which leaves no stdin or stdout shared with DSH, so it
|
|
455
|
+
# dials back to the pipe DSH opened and says the key DSH gave it before anything else. The pipe closing ends it.
|
|
456
|
+
$stream = New-Object System.IO.Pipes.NamedPipeClientStream('.', $Pipe, [System.IO.Pipes.PipeDirection]::InOut)
|
|
457
|
+
$stream.Connect(15000)
|
|
458
|
+
$reader = New-Object System.IO.StreamReader($stream, $utf8)
|
|
459
|
+
$writer = New-Object System.IO.StreamWriter($stream, $utf8)
|
|
460
|
+
$writer.AutoFlush = $true
|
|
461
|
+
$writer.WriteLine((@{ hello = $Key } | ConvertTo-Json -Compress))
|
|
462
|
+
} else {
|
|
463
|
+
$reader = New-Object System.IO.StreamReader([Console]::OpenStandardInput(), $utf8)
|
|
464
|
+
$writer = New-Object System.IO.StreamWriter([Console]::OpenStandardOutput(), $utf8)
|
|
465
|
+
$writer.AutoFlush = $true
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
# The notification listener is loaded on first use: a machine without it still gets every other operation.
|
|
469
|
+
$listener = $null
|
|
470
|
+
$asTask = $null
|
|
471
|
+
function Await($operation, $type) {
|
|
472
|
+
$task = $asTask.MakeGenericMethod($type).Invoke($null, @($operation))
|
|
473
|
+
if (-not $task.Wait(15000)) { throw 'the notification listener did not answer' }
|
|
474
|
+
return $task.Result
|
|
475
|
+
}
|
|
476
|
+
function Notifications {
|
|
477
|
+
if ($null -eq $script:listener) {
|
|
478
|
+
Add-Type -AssemblyName System.Runtime.WindowsRuntime
|
|
479
|
+
$null = [Windows.UI.Notifications.Management.UserNotificationListener, Windows.UI.Notifications, ContentType = WindowsRuntime]
|
|
480
|
+
$null = [Windows.UI.Notifications.NotificationKinds, Windows.UI.Notifications, ContentType = WindowsRuntime]
|
|
481
|
+
$script:asTask = [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' } | Select-Object -First 1
|
|
482
|
+
$script:listener = [Windows.UI.Notifications.Management.UserNotificationListener]::Current
|
|
483
|
+
}
|
|
484
|
+
$status = "$($script:listener.GetAccessStatus())"
|
|
485
|
+
if ($status -eq 'Unspecified') { $status = "$(Await ($script:listener.RequestAccessAsync()) ([Windows.UI.Notifications.Management.UserNotificationListenerAccessStatus]))" }
|
|
486
|
+
if ($status -ne 'Allowed') { throw 'notification access denied' }
|
|
487
|
+
$posted = Await ($script:listener.GetNotificationsAsync([Windows.UI.Notifications.NotificationKinds]::Toast)) ([System.Collections.Generic.IReadOnlyList[Windows.UI.Notifications.UserNotification]])
|
|
488
|
+
$list = foreach ($one in $posted) {
|
|
489
|
+
$app = ''; $appId = ''; $texts = @()
|
|
490
|
+
# A sender that has been uninstalled has no AppInfo; a notification without the generic template has no text.
|
|
491
|
+
try { $appId = [string]$one.AppInfo.AppUserModelId; $app = [string]$one.AppInfo.DisplayInfo.DisplayName } catch { }
|
|
492
|
+
try { $binding = $one.Notification.Visual.GetBinding('ToastGeneric'); if ($binding) { $texts = @($binding.GetTextElements() | ForEach-Object { [string]$_.Text }) } } catch { }
|
|
493
|
+
@{ id = [long]$one.Id; app = $app; appId = $appId; at = $one.CreationTime.UtcDateTime.ToString('o'); texts = @($texts) }
|
|
494
|
+
}
|
|
495
|
+
return @{ notifications = @($list) }
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
# What the partner sees while she works: a capsule at the top of the screen and a soft glow around the window she
|
|
499
|
+
# is on, and a way to stop her (Esc twice, or the capsule's button). Loaded on first use, like the notification
|
|
500
|
+
# listener, so a machine where it cannot be drawn still has every other operation. Its words come from the caller:
|
|
501
|
+
# this file has no byte-order mark, and Windows PowerShell would misread any that were written here.
|
|
502
|
+
$signLoaded = $false
|
|
503
|
+
function Sign($a) {
|
|
504
|
+
if (-not $script:signLoaded) {
|
|
505
|
+
Add-Type -ReferencedAssemblies System.Windows.Forms, System.Drawing -TypeDefinition @'
|
|
506
|
+
using System;
|
|
507
|
+
using System.Drawing;
|
|
508
|
+
using System.Drawing.Drawing2D;
|
|
509
|
+
using System.Drawing.Imaging;
|
|
510
|
+
using System.Drawing.Text;
|
|
511
|
+
using System.IO;
|
|
512
|
+
using System.Runtime.InteropServices;
|
|
513
|
+
using System.Threading;
|
|
514
|
+
using System.Windows.Forms;
|
|
515
|
+
|
|
516
|
+
public static class Sign {
|
|
517
|
+
[StructLayout(LayoutKind.Sequential)] struct RECT { public int L, T, R, B; }
|
|
518
|
+
[StructLayout(LayoutKind.Sequential)] struct POINT { public int X, Y; }
|
|
519
|
+
[StructLayout(LayoutKind.Sequential)] struct SIZE { public int W, H; }
|
|
520
|
+
[StructLayout(LayoutKind.Sequential, Pack = 1)] struct BLEND { public byte Op, Flags, Alpha, Format; }
|
|
521
|
+
delegate IntPtr HookProc(int code, IntPtr w, IntPtr l);
|
|
522
|
+
[DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int id, HookProc proc, IntPtr module, uint thread);
|
|
523
|
+
[DllImport("user32.dll")] static extern IntPtr CallNextHookEx(IntPtr hook, int code, IntPtr w, IntPtr l);
|
|
524
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] static extern IntPtr GetModuleHandle(string name);
|
|
525
|
+
[DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr h, out RECT r);
|
|
526
|
+
[DllImport("user32.dll")] static extern bool IsWindow(IntPtr h);
|
|
527
|
+
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
|
|
528
|
+
[DllImport("user32.dll")] static extern bool IsIconic(IntPtr h);
|
|
529
|
+
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")] static extern IntPtr SetWindowLongPtr(IntPtr h, int index, IntPtr value);
|
|
530
|
+
[DllImport("user32.dll")] static extern uint GetDpiForSystem();
|
|
531
|
+
[DllImport("user32.dll")] static extern IntPtr SetThreadDpiAwarenessContext(IntPtr c);
|
|
532
|
+
[DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr h);
|
|
533
|
+
[DllImport("user32.dll")] static extern int ReleaseDC(IntPtr h, IntPtr dc);
|
|
534
|
+
[DllImport("gdi32.dll")] static extern IntPtr CreateCompatibleDC(IntPtr dc);
|
|
535
|
+
[DllImport("gdi32.dll")] static extern bool DeleteDC(IntPtr dc);
|
|
536
|
+
[DllImport("gdi32.dll")] static extern IntPtr SelectObject(IntPtr dc, IntPtr o);
|
|
537
|
+
[DllImport("gdi32.dll")] static extern bool DeleteObject(IntPtr o);
|
|
538
|
+
[DllImport("user32.dll")] static extern bool SetWindowDisplayAffinity(IntPtr h, uint affinity);
|
|
539
|
+
[DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr h, IntPtr after, int x, int y, int w, int ht, uint flags);
|
|
540
|
+
[DllImport("user32.dll")] static extern bool UpdateLayeredWindow(IntPtr h, IntPtr dst, ref POINT at, ref SIZE size, IntPtr src, ref POINT from, int key, ref BLEND blend, int flags);
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* A window drawn from a picture with its own transparency, so edges and shadows are smooth. It never takes the focus
|
|
544
|
+
* and is in neither the taskbar nor Alt+Tab; `Through` lets every click fall to what is under it.
|
|
545
|
+
*/
|
|
546
|
+
class Layer : Form {
|
|
547
|
+
public bool Through;
|
|
548
|
+
protected override bool ShowWithoutActivation { get { return true; } }
|
|
549
|
+
protected override CreateParams CreateParams {
|
|
550
|
+
get { var p = base.CreateParams; p.ExStyle |= 0x80000 | 0x08000000 | 0x80 | (Through ? 0x20 : 0); return p; }
|
|
551
|
+
}
|
|
552
|
+
protected override void OnPaint(PaintEventArgs e) { }
|
|
553
|
+
protected override void OnPaintBackground(PaintEventArgs e) { }
|
|
554
|
+
public void Put(Bitmap picture, Point at) {
|
|
555
|
+
IntPtr screen = GetDC(IntPtr.Zero), memory = CreateCompatibleDC(screen), bits = picture.GetHbitmap(Color.FromArgb(0)), old = SelectObject(memory, bits);
|
|
556
|
+
try {
|
|
557
|
+
var size = new SIZE { W = picture.Width, H = picture.Height };
|
|
558
|
+
var source = new POINT(); var where = new POINT { X = at.X, Y = at.Y };
|
|
559
|
+
var blend = new BLEND { Op = 0, Flags = 0, Alpha = 255, Format = 1 };
|
|
560
|
+
UpdateLayeredWindow(Handle, screen, ref where, ref size, memory, ref source, 0, ref blend, 2);
|
|
561
|
+
} finally { SelectObject(memory, old); DeleteObject(bits); DeleteDC(memory); ReleaseDC(IntPtr.Zero, screen); }
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** The helper's stdout, shared with the request loop: a line is written whole, under this object's lock. */
|
|
566
|
+
public static StreamWriter Out;
|
|
567
|
+
static readonly Color Blue = Color.FromArgb(64, 140, 255);
|
|
568
|
+
static readonly ManualResetEvent ready = new ManualResetEvent(false);
|
|
569
|
+
static Layer capsule, glow;
|
|
570
|
+
static System.Windows.Forms.Timer follow, fade, breathe;
|
|
571
|
+
static HookProc hook;
|
|
572
|
+
static Font strong, quiet;
|
|
573
|
+
static IntPtr target = IntPtr.Zero;
|
|
574
|
+
static bool active;
|
|
575
|
+
static string name = "", hint = "", stopText = "", stoppedText = "";
|
|
576
|
+
/** Where the button is on the capsule, for the click; empty once she has been stopped. */
|
|
577
|
+
static Rectangle button = Rectangle.Empty;
|
|
578
|
+
static Rectangle glowAt = Rectangle.Empty;
|
|
579
|
+
static long lastEscape;
|
|
580
|
+
static double scale = 1, phase;
|
|
581
|
+
static int S(double v) { return (int)Math.Round(v * scale); }
|
|
582
|
+
|
|
583
|
+
static void Emit(string line) { var o = Out; if (o == null) return; lock (o) { o.WriteLine(line); o.Flush(); } }
|
|
584
|
+
|
|
585
|
+
static GraphicsPath Round(RectangleF r, float radius) {
|
|
586
|
+
var path = new GraphicsPath(); float d = Math.Min(radius * 2, Math.Min(r.Width, r.Height));
|
|
587
|
+
path.AddArc(r.X, r.Y, d, d, 180, 90); path.AddArc(r.Right - d, r.Y, d, d, 270, 90);
|
|
588
|
+
path.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90); path.AddArc(r.X, r.Bottom - d, d, d, 90, 90);
|
|
589
|
+
path.CloseFigure(); return path;
|
|
590
|
+
}
|
|
591
|
+
static SizeF Measure(Graphics g, string text, Font font) { return text.Length == 0 ? SizeF.Empty : g.MeasureString(text, font, PointF.Empty, StringFormat.GenericTypographic); }
|
|
592
|
+
|
|
593
|
+
/** The capsule, drawn whole: its width follows the words, so they never run under the button. */
|
|
594
|
+
static void DrawCapsule() {
|
|
595
|
+
int shadow = S(10), height = S(38), pad = S(16), dot = S(8), gap = S(10);
|
|
596
|
+
SizeF nameSize, hintSize, stopSize;
|
|
597
|
+
using (var probe = new Bitmap(1, 1)) using (var g = Graphics.FromImage(probe)) {
|
|
598
|
+
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
599
|
+
nameSize = Measure(g, active ? name : stoppedText, strong); hintSize = active ? Measure(g, hint, quiet) : SizeF.Empty; stopSize = Measure(g, stopText, quiet);
|
|
600
|
+
}
|
|
601
|
+
int buttonWidth = active ? (int)stopSize.Width + S(24) : 0;
|
|
602
|
+
int inner = pad + dot + gap + (int)Math.Ceiling(nameSize.Width) + (active ? S(12) + (int)Math.Ceiling(hintSize.Width) + S(14) + buttonWidth - S(6) : pad - S(6)) + S(6) + (active ? S(5) : 0);
|
|
603
|
+
int width = inner + shadow * 2, total = height + shadow * 2;
|
|
604
|
+
using (var picture = new Bitmap(width, total, PixelFormat.Format32bppPArgb))
|
|
605
|
+
using (var g = Graphics.FromImage(picture)) {
|
|
606
|
+
g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
607
|
+
var body = new RectangleF(shadow, shadow - S(4), inner, height);
|
|
608
|
+
for (int i = shadow; i > 0; i--) using (var path = Round(new RectangleF(body.X - i, body.Y - i + S(3), body.Width + i * 2, body.Height + i * 2), height / 2f + i))
|
|
609
|
+
using (var soft = new SolidBrush(Color.FromArgb((int)(26 * Math.Pow(1 - (double)i / shadow, 2)), 0, 0, 0))) g.FillPath(soft, path);
|
|
610
|
+
using (var path = Round(body, height / 2f)) {
|
|
611
|
+
using (var fill = new SolidBrush(Color.FromArgb(236, 24, 26, 32))) g.FillPath(fill, path);
|
|
612
|
+
using (var line = new Pen(Color.FromArgb(40, 255, 255, 255), Math.Max(1, S(1)))) g.DrawPath(line, path);
|
|
613
|
+
}
|
|
614
|
+
float cx = body.X + pad + dot / 2f, cy = body.Y + height / 2f;
|
|
615
|
+
if (active) {
|
|
616
|
+
// A slow breath around the dot: something is happening, without anything flashing.
|
|
617
|
+
double b = (Math.Sin(phase) + 1) / 2; float halo = dot / 2f + S(2) + (float)(b * S(4));
|
|
618
|
+
using (var ring = new SolidBrush(Color.FromArgb((int)(70 * (1 - b) + 20), Blue))) g.FillEllipse(ring, cx - halo, cy - halo, halo * 2, halo * 2);
|
|
619
|
+
}
|
|
620
|
+
using (var core = new SolidBrush(active ? Blue : Color.FromArgb(150, 155, 165))) g.FillEllipse(core, cx - dot / 2f, cy - dot / 2f, dot, dot);
|
|
621
|
+
float x = body.X + pad + dot + gap;
|
|
622
|
+
using (var white = new SolidBrush(Color.FromArgb(245, 247, 250))) g.DrawString(active ? name : stoppedText, strong, white, x, cy - nameSize.Height / 2f, StringFormat.GenericTypographic);
|
|
623
|
+
button = Rectangle.Empty;
|
|
624
|
+
if (active) {
|
|
625
|
+
x += nameSize.Width + S(12);
|
|
626
|
+
using (var grey = new SolidBrush(Color.FromArgb(150, 156, 168))) g.DrawString(hint, quiet, grey, x, cy - hintSize.Height / 2f, StringFormat.GenericTypographic);
|
|
627
|
+
var pill = new RectangleF(body.Right - S(5) - buttonWidth, body.Y + S(5), buttonWidth, height - S(10));
|
|
628
|
+
using (var path = Round(pill, pill.Height / 2f)) using (var fill = new SolidBrush(Color.FromArgb(34, 255, 255, 255))) g.FillPath(fill, path);
|
|
629
|
+
using (var white = new SolidBrush(Color.FromArgb(240, 242, 246))) g.DrawString(stopText, quiet, white, pill.X + (pill.Width - stopSize.Width) / 2f, pill.Y + (pill.Height - stopSize.Height) / 2f, StringFormat.GenericTypographic);
|
|
630
|
+
button = Rectangle.Round(pill);
|
|
631
|
+
}
|
|
632
|
+
var screen = Screen.PrimaryScreen.WorkingArea;
|
|
633
|
+
capsule.Put(picture, new Point(screen.Left + (screen.Width - width) / 2, screen.Top + S(6)));
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/** A soft blue glow with rounded corners just around the window; owned by it, so above it and under whatever covers it. */
|
|
637
|
+
static void Follow() {
|
|
638
|
+
RECT r;
|
|
639
|
+
int spread = S(9);
|
|
640
|
+
// No window: foreground mode, where she has the whole screen; the glow runs just inside its edge.
|
|
641
|
+
if (active && target == IntPtr.Zero) {
|
|
642
|
+
var all = Screen.PrimaryScreen.Bounds;
|
|
643
|
+
r = new RECT { L = all.Left + spread, T = all.Top + spread, R = all.Right - spread, B = all.Bottom - spread };
|
|
644
|
+
} else if (!active || !IsWindow(target) || !IsWindowVisible(target) || IsIconic(target) || !GetWindowRect(target, out r)) { if (glow.Visible) glow.Hide(); glowAt = Rectangle.Empty; return; }
|
|
645
|
+
// A maximized window reaches past the screen and under the taskbar; the glow is kept where it can be seen.
|
|
646
|
+
var area = target == IntPtr.Zero ? Screen.PrimaryScreen.Bounds : Screen.FromHandle(target).WorkingArea;
|
|
647
|
+
var bounds = Rectangle.Intersect(new Rectangle(r.L - spread, r.T - spread, r.R - r.L + spread * 2, r.B - r.T + spread * 2), area);
|
|
648
|
+
if (bounds.Width <= spread * 2 || bounds.Height <= spread * 2) { if (glow.Visible) glow.Hide(); glowAt = Rectangle.Empty; return; }
|
|
649
|
+
if (bounds != glowAt) {
|
|
650
|
+
var edge = Rectangle.Intersect(new Rectangle(r.L, r.T, r.R - r.L, r.B - r.T), area);
|
|
651
|
+
using (var picture = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb))
|
|
652
|
+
using (var g = Graphics.FromImage(picture)) {
|
|
653
|
+
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
654
|
+
var rim = new RectangleF(edge.X - bounds.X, edge.Y - bounds.Y, edge.Width, edge.Height);
|
|
655
|
+
float radius = S(8);
|
|
656
|
+
for (int i = spread; i >= 1; i--) using (var pen = new Pen(Color.FromArgb((int)(60 * Math.Pow(1 - (double)i / (spread + 1), 2)), Blue), 2))
|
|
657
|
+
using (var path = Round(new RectangleF(rim.X - i, rim.Y - i, rim.Width + i * 2, rim.Height + i * 2), radius + i)) g.DrawPath(pen, path);
|
|
658
|
+
using (var pen = new Pen(Color.FromArgb(230, Blue), Math.Max(2, S(2)))) using (var path = Round(rim, radius)) g.DrawPath(pen, path);
|
|
659
|
+
glow.Put(picture, bounds.Location);
|
|
660
|
+
}
|
|
661
|
+
glowAt = bounds;
|
|
662
|
+
}
|
|
663
|
+
if (!glow.Visible) glow.Show();
|
|
664
|
+
// A full-screen game is often always-on-top too and comes back over the sign each time she clicks into it:
|
|
665
|
+
// while she is on the whole screen, the sign is put back on top, without taking the focus.
|
|
666
|
+
if (target == IntPtr.Zero) { SetWindowPos(glow.Handle, new IntPtr(-1), 0, 0, 0, 0, 0x13); SetWindowPos(capsule.Handle, new IntPtr(-1), 0, 0, 0, 0, 0x13); }
|
|
667
|
+
}
|
|
668
|
+
static void Halt() {
|
|
669
|
+
if (!active) return;
|
|
670
|
+
active = false;
|
|
671
|
+
Follow(); DrawCapsule();
|
|
672
|
+
fade.Stop(); fade.Start();
|
|
673
|
+
Emit("{\"event\":\"stop\"}");
|
|
674
|
+
}
|
|
675
|
+
/** Looks at every key press and passes it on untouched: the partner's own Esc is theirs; only two in half a second stop her. */
|
|
676
|
+
static IntPtr Keys(int code, IntPtr w, IntPtr l) {
|
|
677
|
+
try {
|
|
678
|
+
// LLKHF_INJECTED: a key she sent herself in foreground mode is not the partner asking her to stop.
|
|
679
|
+
if (code >= 0 && active && w.ToInt64() == 0x100 && Marshal.ReadInt32(l) == 27 && (Marshal.ReadInt32(l, 8) & 0x10) == 0) {
|
|
680
|
+
long now = Environment.TickCount;
|
|
681
|
+
if (lastEscape != 0 && now - lastEscape < 500) { lastEscape = 0; capsule.BeginInvoke((MethodInvoker)Halt); } else lastEscape = now;
|
|
682
|
+
}
|
|
683
|
+
} catch { }
|
|
684
|
+
return CallNextHookEx(IntPtr.Zero, code, w, l);
|
|
685
|
+
}
|
|
686
|
+
static void Run() {
|
|
687
|
+
try {
|
|
688
|
+
SetThreadDpiAwarenessContext(new IntPtr(-4));
|
|
689
|
+
scale = GetDpiForSystem() / 96.0;
|
|
690
|
+
strong = new Font("Microsoft YaHei UI", (float)(14 * scale), FontStyle.Bold, GraphicsUnit.Pixel);
|
|
691
|
+
quiet = new Font("Microsoft YaHei UI", (float)(12.5 * scale), FontStyle.Regular, GraphicsUnit.Pixel);
|
|
692
|
+
capsule = new Layer { FormBorderStyle = FormBorderStyle.None, StartPosition = FormStartPosition.Manual, TopMost = true, ShowInTaskbar = false, Cursor = Cursors.Hand };
|
|
693
|
+
capsule.MouseUp += delegate(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && button.Contains(e.Location)) Halt(); };
|
|
694
|
+
glow = new Layer { Through = true, FormBorderStyle = FormBorderStyle.None, StartPosition = FormStartPosition.Manual, ShowInTaskbar = false };
|
|
695
|
+
var made = capsule.Handle; made = glow.Handle;
|
|
696
|
+
// WDA_EXCLUDEFROMCAPTURE: her screenshots of the screen do not show the sign over what she is looking at.
|
|
697
|
+
SetWindowDisplayAffinity(capsule.Handle, 0x11); SetWindowDisplayAffinity(glow.Handle, 0x11);
|
|
698
|
+
follow = new System.Windows.Forms.Timer { Interval = 150 };
|
|
699
|
+
follow.Tick += delegate { try { Follow(); } catch { } };
|
|
700
|
+
breathe = new System.Windows.Forms.Timer { Interval = 50 };
|
|
701
|
+
breathe.Tick += delegate { if (!active) return; phase += 0.12; try { DrawCapsule(); } catch { } };
|
|
702
|
+
fade = new System.Windows.Forms.Timer { Interval = 2500 };
|
|
703
|
+
fade.Tick += delegate { fade.Stop(); if (!active) { capsule.Hide(); follow.Stop(); breathe.Stop(); } };
|
|
704
|
+
hook = Keys;
|
|
705
|
+
SetWindowsHookEx(13, hook, GetModuleHandle(null), 0);
|
|
706
|
+
} finally { ready.Set(); }
|
|
707
|
+
Application.Run();
|
|
708
|
+
}
|
|
709
|
+
static void OnUi(MethodInvoker work) {
|
|
710
|
+
if (capsule == null) {
|
|
711
|
+
var thread = new Thread(Run) { IsBackground = true };
|
|
712
|
+
thread.SetApartmentState(ApartmentState.STA);
|
|
713
|
+
thread.Start();
|
|
714
|
+
ready.WaitOne(10000);
|
|
715
|
+
}
|
|
716
|
+
if (capsule == null || !capsule.IsHandleCreated) throw new Exception("the sign could not be drawn");
|
|
717
|
+
capsule.BeginInvoke(work);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
public static void Show(long hwnd, string who, string help, string stop, string stopped) {
|
|
721
|
+
OnUi(delegate {
|
|
722
|
+
fade.Stop();
|
|
723
|
+
var next = new IntPtr(hwnd);
|
|
724
|
+
if (next != target) { glow.Hide(); glowAt = Rectangle.Empty; target = next; SetWindowLongPtr(glow.Handle, -8, target); }
|
|
725
|
+
// Set every time: the elevated helper's first sign is on the whole screen, which is no change from where it starts.
|
|
726
|
+
glow.TopMost = target == IntPtr.Zero;
|
|
727
|
+
name = who; hint = help; stopText = stop; stoppedText = stopped;
|
|
728
|
+
if (!active) lastEscape = 0;
|
|
729
|
+
active = true;
|
|
730
|
+
DrawCapsule();
|
|
731
|
+
if (!capsule.Visible) capsule.Show();
|
|
732
|
+
follow.Start(); breathe.Start();
|
|
733
|
+
Follow();
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
public static void Hide() {
|
|
737
|
+
if (capsule == null) return;
|
|
738
|
+
OnUi(delegate { if (!active) return; active = false; Follow(); capsule.Hide(); follow.Stop(); breathe.Stop(); });
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
'@
|
|
742
|
+
[Sign]::Out = $writer
|
|
743
|
+
$script:signLoaded = $true
|
|
744
|
+
}
|
|
745
|
+
if ($a.show) { [Sign]::Show([long]$a.hwnd, [string]$a.text, [string]$a.hint, [string]$a.stop, [string]$a.stopped) } else { [Sign]::Hide() }
|
|
746
|
+
return @{}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
# Taskbar buttons that ask for attention, for her inbox: the shell tells a window registered for its hook which window
|
|
750
|
+
# flashed, so no picture is looked at. Started on first use, on a thread of its own; `flashes` hands over what came
|
|
751
|
+
# since the last call. A program is named by its own description, never by its window title, which can hold a message.
|
|
752
|
+
$flashLoaded = $false
|
|
753
|
+
function Flashes {
|
|
754
|
+
if (-not $script:flashLoaded) {
|
|
755
|
+
Add-Type -ReferencedAssemblies System.Windows.Forms -TypeDefinition @'
|
|
756
|
+
using System;
|
|
757
|
+
using System.Collections.Generic;
|
|
758
|
+
using System.Diagnostics;
|
|
759
|
+
using System.Runtime.InteropServices;
|
|
760
|
+
using System.Threading;
|
|
761
|
+
using System.Windows.Forms;
|
|
762
|
+
|
|
763
|
+
public static class Flash {
|
|
764
|
+
[DllImport("user32.dll")] static extern bool RegisterShellHookWindow(IntPtr h);
|
|
765
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern uint RegisterWindowMessage(string name);
|
|
766
|
+
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
|
|
767
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetWindowText(IntPtr h, System.Text.StringBuilder s, int n);
|
|
768
|
+
delegate bool EnumProc(IntPtr h, IntPtr l);
|
|
769
|
+
[DllImport("user32.dll")] static extern bool EnumWindows(EnumProc cb, IntPtr l);
|
|
770
|
+
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
|
|
771
|
+
public class Seen { public string app { get; set; } public string exe { get; set; } public string at { get; set; } }
|
|
772
|
+
class Ear : NativeWindow {
|
|
773
|
+
readonly uint shell;
|
|
774
|
+
public Ear() { CreateHandle(new CreateParams()); shell = RegisterWindowMessage("SHELLHOOK"); RegisterShellHookWindow(Handle); }
|
|
775
|
+
protected override void WndProc(ref Message m) {
|
|
776
|
+
// A flashing button comes as HSHELL_FLASH on some systems and as HSHELL_REDRAW on others (Windows 11 here), which
|
|
777
|
+
// is also what a new title sends; so a redraw counts only when the window's title is the one it already had.
|
|
778
|
+
if (m.Msg == shell) {
|
|
779
|
+
long code = m.WParam.ToInt64();
|
|
780
|
+
if (code == 0x8006) Heard(m.LParam);
|
|
781
|
+
else if (code == 6) { string was, now = Title(m.LParam); lock (titles) { if (titles.TryGetValue(m.LParam, out was) && was == now) Heard(m.LParam); titles[m.LParam] = now; } }
|
|
782
|
+
}
|
|
783
|
+
base.WndProc(ref m);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
static readonly List<Seen> heard = new List<Seen>();
|
|
787
|
+
static readonly Dictionary<IntPtr, string> titles = new Dictionary<IntPtr, string>();
|
|
788
|
+
static string Title(IntPtr h) { var t = new System.Text.StringBuilder(512); GetWindowText(h, t, 512); return t.ToString(); }
|
|
789
|
+
static readonly ManualResetEvent ready = new ManualResetEvent(false);
|
|
790
|
+
static Thread thread;
|
|
791
|
+
static void Heard(IntPtr window) {
|
|
792
|
+
uint pid; GetWindowThreadProcessId(window, out pid);
|
|
793
|
+
if (pid == (uint)Process.GetCurrentProcess().Id) return;
|
|
794
|
+
string exe = "", app = "";
|
|
795
|
+
try {
|
|
796
|
+
var process = Process.GetProcessById((int)pid);
|
|
797
|
+
exe = process.ProcessName;
|
|
798
|
+
try { app = process.MainModule.FileVersionInfo.FileDescription; } catch { }
|
|
799
|
+
} catch { }
|
|
800
|
+
if (string.IsNullOrWhiteSpace(app)) app = exe;
|
|
801
|
+
lock (heard) { heard.Add(new Seen { app = app.Trim(), exe = exe, at = DateTime.UtcNow.ToString("o") }); if (heard.Count > 200) heard.RemoveAt(0); }
|
|
802
|
+
}
|
|
803
|
+
public static Seen[] Take() {
|
|
804
|
+
if (thread == null) {
|
|
805
|
+
// The titles windows have now, so the first flash of a window already open is told from a new title.
|
|
806
|
+
EnumWindows((h, l) => { if (IsWindowVisible(h)) titles[h] = Title(h); return true; }, IntPtr.Zero);
|
|
807
|
+
thread = new Thread(() => { new Ear(); ready.Set(); Application.Run(); }) { IsBackground = true };
|
|
808
|
+
thread.SetApartmentState(ApartmentState.STA);
|
|
809
|
+
thread.Start();
|
|
810
|
+
if (!ready.WaitOne(10000)) throw new Exception("the taskbar listener did not start");
|
|
811
|
+
}
|
|
812
|
+
lock (heard) { var all = heard.ToArray(); heard.Clear(); return all; }
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
'@
|
|
816
|
+
$script:flashLoaded = $true
|
|
817
|
+
}
|
|
818
|
+
return @{ flashes = @([Flash]::Take()) }
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
function Run($op, $a) {
|
|
822
|
+
switch ($op) {
|
|
823
|
+
'windows' { return @{ windows = @([Desk]::Windows()); foreground = [Desk]::Foreground() } }
|
|
824
|
+
'apps' { return @{ apps = @(Get-StartApps | ForEach-Object { @{ name = $_.Name; appId = $_.AppID } }) } }
|
|
825
|
+
'launch' { Start-Process "shell:AppsFolder\$($a.appId)"; return @{} }
|
|
826
|
+
'shot' { return [Desk]::Shot([long]$a.hwnd, [string]$a.file, [int]$a.edge, [double]$a.grow, [int]$a.x, [int]$a.y, [int]$a.w, [int]$a.h, [int]$a.markX, [int]$a.markY) }
|
|
827
|
+
'click' { [Desk]::Click([long]$a.hwnd, [int]$a.x, [int]$a.y, [bool]$a.right, [int]$a.count, [int]$a.hold); return @{} }
|
|
828
|
+
'scroll' { [Desk]::Scroll([long]$a.hwnd, [int]$a.x, [int]$a.y, [int]$a.up, [int]$a.right); return @{} }
|
|
829
|
+
'type' { [Desk]::Type([long]$a.hwnd, [string]$a.text); return @{} }
|
|
830
|
+
'key' { [Desk]::Key([long]$a.hwnd, [int]$a.vk, [int[]]@($a.modifiers | Where-Object { $null -ne $_ }), [int]$a.hold); return @{} }
|
|
831
|
+
'restore' { return @{ restored = [Desk]::Restore([long]$a.hwnd) } }
|
|
832
|
+
'refocus' { return @{ refocused = [Desk]::Refocus([long]$a.hwnd) } }
|
|
833
|
+
'tray' { return @{ result = [Desk]::Tray([string]$a.name); cursor = [Desk]::Cursor() } }
|
|
834
|
+
'cursor' { return @{ cursor = [Desk]::Cursor() } }
|
|
835
|
+
'screen' { return [Desk]::Screen([string]$a.file, [int]$a.edge, [double]$a.grow, [int]$a.x, [int]$a.y, [int]$a.w, [int]$a.h, [int]$a.markX, [int]$a.markY) }
|
|
836
|
+
'drag' { [Desk]::Drag([long]$a.hwnd, [int]$a.x, [int]$a.y, [int]$a.x2, [int]$a.y2, [bool]$a.right, [int]$a.hold); return @{} }
|
|
837
|
+
'rdrag' { [Desk]::RealDrag([int]$a.x, [int]$a.y, [int]$a.x2, [int]$a.y2, [bool]$a.right, [int]$a.hold); return @{} }
|
|
838
|
+
'rclick' { [Desk]::RealClick([int]$a.x, [int]$a.y, [bool]$a.right, [int]$a.count, [int]$a.hold); return @{} }
|
|
839
|
+
'rtype' { [Desk]::RealType([string]$a.text); return @{} }
|
|
840
|
+
'rkey' { [Desk]::RealKey([int]$a.vk, [int[]]@($a.modifiers | Where-Object { $null -ne $_ }), [int]$a.hold); return @{} }
|
|
841
|
+
'rscroll' { [Desk]::RealScroll([int]$a.x, [int]$a.y, [int]$a.up, [int]$a.right); return @{} }
|
|
842
|
+
'sign' { return Sign $a }
|
|
843
|
+
'sheet' { return [Desk]::Sheet([string[]]@($a.files), [string[]]@($a.labels), [string]$a.file, [int]$a.columns, [int]$a.tile) }
|
|
844
|
+
'notifications' { return Notifications }
|
|
845
|
+
'flashes' { return Flashes }
|
|
846
|
+
default { throw "unknown op $op" }
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
while ($null -ne ($line = $reader.ReadLine())) {
|
|
851
|
+
$request = $line | ConvertFrom-Json
|
|
852
|
+
try { $reply = @{ id = $request.id; data = (Run $request.op $request.args) } }
|
|
853
|
+
catch { $reply = @{ id = $request.id; error = $_.Exception.Message } }
|
|
854
|
+
# The sign's own thread writes its events to the same stream, so a line is written whole under one lock.
|
|
855
|
+
$text = $reply | ConvertTo-Json -Compress -Depth 6
|
|
856
|
+
[System.Threading.Monitor]::Enter($writer)
|
|
857
|
+
try { $writer.WriteLine($text) } finally { [System.Threading.Monitor]::Exit($writer) }
|
|
858
|
+
}
|