dsh-windows-parallel-mcp 0.1.0 → 0.1.2
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 +126 -0
- package/INSTALL.md +2 -2
- package/README.md +19 -3
- package/dsh.plugin.json +1 -1
- package/lib/config.js +125 -125
- package/lib/index.js +393 -330
- package/lib/proof-window.mjs +241 -0
- package/package.json +6 -1
- package/src/engine/engine.ps1 +363 -313
- package/src/engine/overlay.ps1 +383 -368
- package/src/engine/win.ps1 +621 -581
- package/src/engine/worker.ps1 +4 -0
package/src/engine/overlay.ps1
CHANGED
|
@@ -1,368 +1,383 @@
|
|
|
1
|
-
# overlay.ps1 -- the product's visible surface: an AI marker that never takes the foreground,
|
|
2
|
-
# and a global hotkey that pauses the engine server-side. (task_54)
|
|
3
|
-
#
|
|
4
|
-
# WHAT THIS IS FOR. Two README claims need a real surface:
|
|
5
|
-
# * "AI cursor is a click-through topmost overlay, not a system cursor" (R2.4) -- the trio
|
|
6
|
-
# WS_EX_TOPMOST + WS_EX_NOACTIVATE + WS_EX_TRANSPARENT must exist and be MEASURED: ex-style
|
|
7
|
-
# read-back, GetForegroundWindow unchanged across Show(), WindowFromPoint passing through the
|
|
8
|
-
# marker (test/overlay-test.ps1 does all three).
|
|
9
|
-
# * "the owner pauses explicitly and the SERVER refuses" (R2.5) -- one global key from ANY window
|
|
10
|
-
# flips the engine's own pause flag. The flag lives in [WpmCtl] (a static, process-wide holder),
|
|
11
|
-
# not in a script variable: the hotkey fires on the overlay's thread while actions run on the
|
|
12
|
-
# worker's main thread, and two copies of one switch is the bug pattern this repo keeps meeting.
|
|
13
|
-
#
|
|
14
|
-
# WHY A RUNSPACE AND NOT A RAW .NET THREAD. Measured, three times over, before this shape won:
|
|
15
|
-
# * [System.Threading.Thread]::new($scriptblock) -- a PowerShell scriptblock is NOT a
|
|
16
|
-
# ParameterizedThreadStart; the cast produces a delegate that dies at invoke time and the
|
|
17
|
-
# thread runs NOTHING (reason stayed empty, hwnd stayed 0, exit code 2).
|
|
18
|
-
# * SetApartmentState on a Thread object -- returns false here; the apartment never sticks.
|
|
19
|
-
# * CreateRunspace([ApartmentState]) -- no such overload on PS 5.1.
|
|
20
|
-
# What works (verified in .tmp before it was deleted): InitialSessionState.CreateDefault2 ->
|
|
21
|
-
# CreateRunspace($iss) -> $rs.ApartmentState = STA -> Open. The runspace owns its pipeline thread,
|
|
22
|
-
# WPF shows a window on it, and types compiled by the main thread ([WpmCtl]/[WpmWin]) are
|
|
23
|
-
# process-wide and reachable. The body therefore lives in a here-string compiled INSIDE the
|
|
24
|
-
# runspace -- no cross-thread scriptblock capture at all, which kills the whole GetNewClosure
|
|
25
|
-
# hazard class in this file.
|
|
26
|
-
#
|
|
27
|
-
# THREADING RULE (unchanged): the WPF window is touched only on its own thread. The main thread
|
|
28
|
-
# goes through [WpmCtl]::Dispatcher.Invoke.
|
|
29
|
-
|
|
30
|
-
Set-StrictMode -Version 2.0
|
|
31
|
-
|
|
32
|
-
if (-not ('WpmCtl' -as [type])) {
|
|
33
|
-
. (Join-Path $PSScriptRoot 'win.ps1')
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
# IDEMPOTENT INITIALISATION. This file is dot-sourced again on every `overlay`/`marker` request
|
|
37
|
-
# (worker.ps1 does that so a fresh process can answer them), and the first version reset the
|
|
38
|
-
# runspace/handle to $null at the top of every load. The overlay kept running -- the WPF thread
|
|
39
|
-
# does not care about our variables -- but the caller lost the handle it needs to EndInvoke, so
|
|
40
|
-
# Stop-WpmOverlay could not see the pipeline end and had to fall back to the hard bound. Measured
|
|
41
|
-
# cost before this guard: `marker` 1844ms and `overlay off` 2618ms over the real worker protocol
|
|
42
|
-
# (.tmp/worker-overlay.mjs), against a few ms for the same calls in one dot-sourced process.
|
|
43
|
-
if (-not (Get-Variable -Name WpmOvRunspace -Scope Script -ErrorAction SilentlyContinue)) {
|
|
44
|
-
$script:WpmOvRunspace = $null
|
|
45
|
-
$script:WpmOvPowerShell = $null
|
|
46
|
-
$script:WpmOvHandle = $null
|
|
47
|
-
}
|
|
48
|
-
$script:WpmHotkeyId = 0xB001
|
|
49
|
-
|
|
50
|
-
# The documented chord comes first, then fallbacks -- because the documented one is not always
|
|
51
|
-
# available. Measured on this machine (scripts/hotkey-availability.ps1): Ctrl+Alt+Q is TAKEN by
|
|
52
|
-
# another app (Win32 1409, ERROR_HOTKEY_ALREADY_REGISTERED) while all seven other candidates are
|
|
53
|
-
# free. Shipping only Ctrl+Alt+Q means the pause switch silently does not exist on some desktops,
|
|
54
|
-
# and the alternative -- one global key quietly not working -- is the failure mode R2.5 forbids.
|
|
55
|
-
# Whatever registers is written to [WpmCtl]::HotkeyLabel and surfaced by wpm_status.
|
|
56
|
-
$script:WpmHotkeyChords = @(
|
|
57
|
-
@{ label = 'Ctrl+Alt+Q'; mods = 0x0003; vk = 0x51 }, # MOD_CONTROL|MOD_ALT
|
|
58
|
-
@{ label = 'Ctrl+Alt+W'; mods = 0x0003; vk = 0x57 },
|
|
59
|
-
@{ label = 'Ctrl+Alt+P'; mods = 0x0003; vk = 0x50 },
|
|
60
|
-
@{ label = 'Ctrl+Alt+Space'; mods = 0x0003; vk = 0x20 },
|
|
61
|
-
@{ label = 'Ctrl+Shift+Q'; mods = 0x0006; vk = 0x51 }, # MOD_CONTROL|MOD_SHIFT
|
|
62
|
-
@{ label = 'Ctrl+Alt+Shift+Q'; mods = 0x0007; vk = 0x51 }
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
$script:WpmOverlayBody = @'
|
|
66
|
-
param([int]$MarkerSize, [int]$HotkeyId, [object[]]$Chords)
|
|
67
|
-
try {
|
|
68
|
-
Add-Type -AssemblyName PresentationFramework
|
|
69
|
-
Add-Type -AssemblyName PresentationCore
|
|
70
|
-
Add-Type -AssemblyName WindowsBase
|
|
71
|
-
|
|
72
|
-
$win = [System.Windows.Window]::new()
|
|
73
|
-
$win.Title = 'WpmOverlay'
|
|
74
|
-
$win.WindowStyle = [System.Windows.WindowStyle]::None
|
|
75
|
-
$win.AllowsTransparency = $true
|
|
76
|
-
$win.ShowActivated = $false # WPF-side half of NOACTIVATE: Show() must not steal foreground
|
|
77
|
-
$win.Focusable = $false
|
|
78
|
-
$win.IsHitTestVisible = $false
|
|
79
|
-
$win.ResizeMode = [System.Windows.ResizeMode]::NoResize
|
|
80
|
-
$win.Width = [double]$MarkerSize
|
|
81
|
-
$win.Height = [double]$MarkerSize
|
|
82
|
-
$win.Left = -2000.0
|
|
83
|
-
$win.Top = -2000.0
|
|
84
|
-
$win.WindowStartupLocation = [System.Windows.WindowStartupLocation]::Manual
|
|
85
|
-
$win.ShowInTaskbar = $false
|
|
86
|
-
$win.Topmost = $true
|
|
87
|
-
$win.Opacity = 0.85
|
|
88
|
-
|
|
89
|
-
$ring = [System.Windows.Shapes.Ellipse]::new()
|
|
90
|
-
$ring.Width = [double]$MarkerSize
|
|
91
|
-
$ring.Height = [double]$MarkerSize
|
|
92
|
-
$ring.Stroke = [System.Windows.Media.Brushes]::DeepSkyBlue
|
|
93
|
-
$ring.StrokeThickness = 2.0
|
|
94
|
-
$ring.Fill = [System.Windows.Media.SolidColorBrush]::new(
|
|
95
|
-
[System.Windows.Media.Color]::FromArgb(60, 0, 191, 255))
|
|
96
|
-
$grid = [System.Windows.Controls.Grid]::new()
|
|
97
|
-
[void]$grid.Children.Add($ring)
|
|
98
|
-
$win.Content = $grid
|
|
99
|
-
|
|
100
|
-
$win.Show()
|
|
101
|
-
$hwnd = (New-Object System.Windows.Interop.WindowInteropHelper($win)).Handle
|
|
102
|
-
[WpmCtl]::OverlayHwnd = [int64]$hwnd
|
|
103
|
-
|
|
104
|
-
# The trio, applied once the HWND exists. TOPMOST is also WPF-side, but the measured promise is
|
|
105
|
-
# the ex-style value itself, so all four bits are set explicitly and read back by the test.
|
|
106
|
-
$bits = [WpmWin]::WS_EX_TOPMOST -bor [WpmWin]::WS_EX_NOACTIVATE -bor
|
|
107
|
-
[WpmWin]::WS_EX_TRANSPARENT -bor [WpmWin]::WS_EX_LAYERED
|
|
108
|
-
[void][WpmWin]::AddExStyles($hwnd, $bits)
|
|
109
|
-
|
|
110
|
-
# The marker's PHYSICAL size, read once from Win32. Placement is done with SetWindowPos (physical
|
|
111
|
-
# units) because WPF's own coordinates are device independent and did not agree with this desktop
|
|
112
|
-
# (measured: marker at (303,263) for a target of (470,410) at 150%). Reading the real rect once is
|
|
113
|
-
# cheaper and more honest than assuming MarkerSize is the physical edge length.
|
|
114
|
-
$rc0 = New-Object 'WpmWin+RECT'
|
|
115
|
-
[void][WpmWin]::GetWindowRect([IntPtr]$hwnd, [ref]$rc0)
|
|
116
|
-
$script:OvPhysW = [int]($rc0.Right - $rc0.Left)
|
|
117
|
-
$script:OvPhysH = [int]($rc0.Bottom - $rc0.Top)
|
|
118
|
-
if ($script:OvPhysW -le 0) { $script:OvPhysW = $MarkerSize }
|
|
119
|
-
if ($script:OvPhysH -le 0) { $script:OvPhysH = $MarkerSize }
|
|
120
|
-
|
|
121
|
-
$hkOk = $false
|
|
122
|
-
$hkLabel = ''
|
|
123
|
-
$tried = @()
|
|
124
|
-
foreach ($c in $Chords) {
|
|
125
|
-
$label = [string]$c.label
|
|
126
|
-
$tried += $label
|
|
127
|
-
if ([WpmWin]::RegisterHotKey($hwnd, $HotkeyId, [uint32]$c.mods, [uint32]$c.vk)) {
|
|
128
|
-
$hkOk = $true
|
|
129
|
-
$hkLabel = $label
|
|
130
|
-
break
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
[WpmCtl]::HotkeyOk = [bool]$hkOk
|
|
134
|
-
[WpmCtl]::HotkeyLabel = $hkLabel
|
|
135
|
-
if (-not $hkOk) {
|
|
136
|
-
[WpmCtl]::Reason = 'RegisterHotKey false for every candidate (' + ($tried -join ', ') + '); the settings switch still pauses'
|
|
137
|
-
} elseif ($hkLabel -ne 'Ctrl+Alt+Q') {
|
|
138
|
-
# Reported, not hidden: the promise in the README is the first chord, and when the desktop
|
|
139
|
-
# already owns it this is the only place the owner can learn which key actually works.
|
|
140
|
-
[WpmCtl]::Reason = "Ctrl+Alt+Q is owned by another app (RegisterHotKey 1409); registered $hkLabel instead"
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
# WM_HOTKEY -> flip the ONE pause flag. The owner's switch: server-side refusal, not a hint.
|
|
144
|
-
$src = [System.Windows.Interop.HwndSource]::FromHwnd($hwnd)
|
|
145
|
-
# No DIP matrix is kept here on purpose. Converting to device-independent units is exactly the
|
|
146
|
-
# step that put the marker at (303,263) for a target of (470,410): this process's idea of the
|
|
147
|
-
# scale and the window's actual rendering disagreed. Placement is SetWindowPos in physical pixels
|
|
148
|
-
# (see the pump), so there is nothing to convert and no second coordinate system to get wrong.
|
|
149
|
-
if ($src) {
|
|
150
|
-
$hook = [System.Windows.Interop.HwndSourceHook] {
|
|
151
|
-
param($h, $m, $wp, $lp, [ref]$handled)
|
|
152
|
-
if ($m -eq [WpmWin]::WM_HOTKEY -and [int]$wp -eq $HotkeyId) {
|
|
153
|
-
[WpmCtl]::Paused = -not [WpmCtl]::Paused
|
|
154
|
-
$handled.Value = $true
|
|
155
|
-
return [IntPtr]::Zero
|
|
156
|
-
}
|
|
157
|
-
$handled.Value = $false
|
|
158
|
-
return [IntPtr]::Zero
|
|
159
|
-
}
|
|
160
|
-
[void]$src.AddHook($hook)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
# THE MARKER IS DRIVEN FROM THIS THREAD, ON A PUMP -- not by cross-thread Invokes.
|
|
164
|
-
#
|
|
165
|
-
# Why: Set-WpmOverlayMarker used to marshal a PowerShell scriptblock onto this thread with
|
|
166
|
-
# Dispatcher.Invoke. Measured (.tmp/ov-seg.log): that call took 1818ms and once never returned.
|
|
167
|
-
# The delegate is a scriptblock and this thread is inside Dispatcher.Run(), so the engine has to
|
|
168
|
-
# reach a runspace that is already busy -- a deadlock with a timeout, not a slow call. Writing
|
|
169
|
-
# three static fields costs nothing; this pump (a DispatcherTimer, which runs on THIS thread the
|
|
170
|
-
# same way the hotkey hook does) applies them at 16ms.
|
|
171
|
-
#
|
|
172
|
-
# Auto-hide is measured from the LAST placement, so the deadline is re-armed per request: a
|
|
173
|
-
# free-running 1.5s timer kicks the marker off screen at an arbitrary phase, which can be the
|
|
174
|
-
# same millisecond it was placed (measured in .tmp/ov-hide.log: -2000 within 400ms of placing).
|
|
175
|
-
# That version passed a naive "is it hidden" glance while breaking the promise in the other
|
|
176
|
-
# direction -- it never stayed visible long enough to be pointed at.
|
|
177
|
-
$script:OvSeenSeq = [WpmCtl]::MarkerSeq
|
|
178
|
-
$script:OvHideAt = [DateTime]::MinValue
|
|
179
|
-
$pump = [System.Windows.Threading.DispatcherTimer]::new()
|
|
180
|
-
$pump.Interval = [TimeSpan]::FromMilliseconds(16)
|
|
181
|
-
$pump.Add_Tick({
|
|
182
|
-
$w = [WpmCtl]::Window
|
|
183
|
-
if (-not $w -or -not [WpmCtl]::OverlayOn) { return }
|
|
184
|
-
$seq = [WpmCtl]::MarkerSeq
|
|
185
|
-
if ($seq -ne $script:OvSeenSeq) {
|
|
186
|
-
$script:OvSeenSeq = $seq
|
|
187
|
-
if ([WpmCtl]::MarkerHide) {
|
|
188
|
-
[void][WpmWin]::MoveNoActivate([IntPtr][WpmCtl]::OverlayHwnd, -2000, -2000, 1, 1)
|
|
189
|
-
$script:OvHideAt = [DateTime]::MinValue
|
|
190
|
-
} else {
|
|
191
|
-
$mx = [WpmCtl]::MarkerX
|
|
192
|
-
$my = [WpmCtl]::MarkerY
|
|
193
|
-
if ($mx -ge 0 -and $my -ge 0) {
|
|
194
|
-
$pw = [int]$script:OvPhysW
|
|
195
|
-
$ph = [int]$script:OvPhysH
|
|
196
|
-
[void][WpmWin]::MoveNoActivate([IntPtr][WpmCtl]::OverlayHwnd, $mx - [int][Math]::Floor($pw / 2), $my - [int][Math]::Floor($ph / 2), $pw, $ph)
|
|
197
|
-
$script:OvHideAt = [DateTime]::UtcNow.AddMilliseconds(1500)
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
} elseif ($script:OvHideAt -ne [DateTime]::MinValue -and [DateTime]::UtcNow -ge $script:OvHideAt) {
|
|
201
|
-
[void][WpmWin]::MoveNoActivate([IntPtr][WpmCtl]::OverlayHwnd, -2000, -2000, 1, 1)
|
|
202
|
-
$script:OvHideAt = [DateTime]::MinValue
|
|
203
|
-
}
|
|
204
|
-
})
|
|
205
|
-
$pump.Start()
|
|
206
|
-
|
|
207
|
-
[WpmCtl]::Dispatcher = [System.Windows.Threading.Dispatcher]::CurrentDispatcher
|
|
208
|
-
[WpmCtl]::Window = $win
|
|
209
|
-
[WpmCtl]::Timer = $pump
|
|
210
|
-
[WpmCtl]::OverlayOn = $true
|
|
211
|
-
|
|
212
|
-
# Block this runspace thread pumping the dispatcher until the window closes (Stop-WpmOverlay).
|
|
213
|
-
[System.Windows.Threading.Dispatcher]::Run()
|
|
214
|
-
} catch {
|
|
215
|
-
[WpmCtl]::Reason = $_.Exception.Message
|
|
216
|
-
} finally {
|
|
217
|
-
[WpmCtl]::OverlayOn = $false
|
|
218
|
-
[WpmCtl]::HotkeyOk = $false
|
|
219
|
-
[WpmCtl]::HotkeyLabel = ''
|
|
220
|
-
[WpmCtl]::Window = $null
|
|
221
|
-
[WpmCtl]::Timer = $null
|
|
222
|
-
[WpmCtl]::Dispatcher = $null
|
|
223
|
-
[WpmCtl]::OverlayHwnd = 0
|
|
224
|
-
}
|
|
225
|
-
'@
|
|
226
|
-
|
|
227
|
-
function Start-WpmOverlay {
|
|
228
|
-
param([int]$MarkerSize = 22)
|
|
229
|
-
if ($script:WpmOvRunspace -and $script:WpmOvRunspace.RunspaceStateInfo.State -eq 'Opened') {
|
|
230
|
-
return @{ started = $false; reason = 'already running' }
|
|
231
|
-
}
|
|
232
|
-
[WpmCtl]::Reason = ''
|
|
233
|
-
$iss = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault2()
|
|
234
|
-
$rs = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($iss)
|
|
235
|
-
$rs.ApartmentState = [System.Threading.ApartmentState]::STA
|
|
236
|
-
$rs.ThreadOptions = [System.Management.Automation.Runspaces.PSThreadOptions]::Default
|
|
237
|
-
$rs.Open()
|
|
238
|
-
if ($rs.RunspaceStateInfo.State -ne 'Opened') {
|
|
239
|
-
return @{ started = $false; reason = "runspace failed to open: $($rs.RunspaceStateInfo)" }
|
|
240
|
-
}
|
|
241
|
-
$ps = [PowerShell]::Create()
|
|
242
|
-
$ps.Runspace = $rs
|
|
243
|
-
[void]$ps.AddScript($script:WpmOverlayBody).AddArgument($MarkerSize).AddArgument($script:WpmHotkeyId).AddArgument($script:WpmHotkeyChords)
|
|
244
|
-
$script:WpmOvHandle = $ps.BeginInvoke()
|
|
245
|
-
$script:WpmOvRunspace = $rs
|
|
246
|
-
$script:WpmOvPowerShell = $ps
|
|
247
|
-
return @{ started = $true; runspace = [string]$rs.Id }
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
function Get-WpmOverlayState {
|
|
251
|
-
param([int]$WaitMs = 2500)
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function
|
|
290
|
-
#
|
|
291
|
-
#
|
|
292
|
-
#
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
#
|
|
332
|
-
#
|
|
333
|
-
#
|
|
334
|
-
#
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
if ($script:
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
[
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
[
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
1
|
+
# overlay.ps1 -- the product's visible surface: an AI marker that never takes the foreground,
|
|
2
|
+
# and a global hotkey that pauses the engine server-side. (task_54)
|
|
3
|
+
#
|
|
4
|
+
# WHAT THIS IS FOR. Two README claims need a real surface:
|
|
5
|
+
# * "AI cursor is a click-through topmost overlay, not a system cursor" (R2.4) -- the trio
|
|
6
|
+
# WS_EX_TOPMOST + WS_EX_NOACTIVATE + WS_EX_TRANSPARENT must exist and be MEASURED: ex-style
|
|
7
|
+
# read-back, GetForegroundWindow unchanged across Show(), WindowFromPoint passing through the
|
|
8
|
+
# marker (test/overlay-test.ps1 does all three).
|
|
9
|
+
# * "the owner pauses explicitly and the SERVER refuses" (R2.5) -- one global key from ANY window
|
|
10
|
+
# flips the engine's own pause flag. The flag lives in [WpmCtl] (a static, process-wide holder),
|
|
11
|
+
# not in a script variable: the hotkey fires on the overlay's thread while actions run on the
|
|
12
|
+
# worker's main thread, and two copies of one switch is the bug pattern this repo keeps meeting.
|
|
13
|
+
#
|
|
14
|
+
# WHY A RUNSPACE AND NOT A RAW .NET THREAD. Measured, three times over, before this shape won:
|
|
15
|
+
# * [System.Threading.Thread]::new($scriptblock) -- a PowerShell scriptblock is NOT a
|
|
16
|
+
# ParameterizedThreadStart; the cast produces a delegate that dies at invoke time and the
|
|
17
|
+
# thread runs NOTHING (reason stayed empty, hwnd stayed 0, exit code 2).
|
|
18
|
+
# * SetApartmentState on a Thread object -- returns false here; the apartment never sticks.
|
|
19
|
+
# * CreateRunspace([ApartmentState]) -- no such overload on PS 5.1.
|
|
20
|
+
# What works (verified in .tmp before it was deleted): InitialSessionState.CreateDefault2 ->
|
|
21
|
+
# CreateRunspace($iss) -> $rs.ApartmentState = STA -> Open. The runspace owns its pipeline thread,
|
|
22
|
+
# WPF shows a window on it, and types compiled by the main thread ([WpmCtl]/[WpmWin]) are
|
|
23
|
+
# process-wide and reachable. The body therefore lives in a here-string compiled INSIDE the
|
|
24
|
+
# runspace -- no cross-thread scriptblock capture at all, which kills the whole GetNewClosure
|
|
25
|
+
# hazard class in this file.
|
|
26
|
+
#
|
|
27
|
+
# THREADING RULE (unchanged): the WPF window is touched only on its own thread. The main thread
|
|
28
|
+
# goes through [WpmCtl]::Dispatcher.Invoke.
|
|
29
|
+
|
|
30
|
+
Set-StrictMode -Version 2.0
|
|
31
|
+
|
|
32
|
+
if (-not ('WpmCtl' -as [type])) {
|
|
33
|
+
. (Join-Path $PSScriptRoot 'win.ps1')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
# IDEMPOTENT INITIALISATION. This file is dot-sourced again on every `overlay`/`marker` request
|
|
37
|
+
# (worker.ps1 does that so a fresh process can answer them), and the first version reset the
|
|
38
|
+
# runspace/handle to $null at the top of every load. The overlay kept running -- the WPF thread
|
|
39
|
+
# does not care about our variables -- but the caller lost the handle it needs to EndInvoke, so
|
|
40
|
+
# Stop-WpmOverlay could not see the pipeline end and had to fall back to the hard bound. Measured
|
|
41
|
+
# cost before this guard: `marker` 1844ms and `overlay off` 2618ms over the real worker protocol
|
|
42
|
+
# (.tmp/worker-overlay.mjs), against a few ms for the same calls in one dot-sourced process.
|
|
43
|
+
if (-not (Get-Variable -Name WpmOvRunspace -Scope Script -ErrorAction SilentlyContinue)) {
|
|
44
|
+
$script:WpmOvRunspace = $null
|
|
45
|
+
$script:WpmOvPowerShell = $null
|
|
46
|
+
$script:WpmOvHandle = $null
|
|
47
|
+
}
|
|
48
|
+
$script:WpmHotkeyId = 0xB001
|
|
49
|
+
|
|
50
|
+
# The documented chord comes first, then fallbacks -- because the documented one is not always
|
|
51
|
+
# available. Measured on this machine (scripts/hotkey-availability.ps1): Ctrl+Alt+Q is TAKEN by
|
|
52
|
+
# another app (Win32 1409, ERROR_HOTKEY_ALREADY_REGISTERED) while all seven other candidates are
|
|
53
|
+
# free. Shipping only Ctrl+Alt+Q means the pause switch silently does not exist on some desktops,
|
|
54
|
+
# and the alternative -- one global key quietly not working -- is the failure mode R2.5 forbids.
|
|
55
|
+
# Whatever registers is written to [WpmCtl]::HotkeyLabel and surfaced by wpm_status.
|
|
56
|
+
$script:WpmHotkeyChords = @(
|
|
57
|
+
@{ label = 'Ctrl+Alt+Q'; mods = 0x0003; vk = 0x51 }, # MOD_CONTROL|MOD_ALT
|
|
58
|
+
@{ label = 'Ctrl+Alt+W'; mods = 0x0003; vk = 0x57 },
|
|
59
|
+
@{ label = 'Ctrl+Alt+P'; mods = 0x0003; vk = 0x50 },
|
|
60
|
+
@{ label = 'Ctrl+Alt+Space'; mods = 0x0003; vk = 0x20 },
|
|
61
|
+
@{ label = 'Ctrl+Shift+Q'; mods = 0x0006; vk = 0x51 }, # MOD_CONTROL|MOD_SHIFT
|
|
62
|
+
@{ label = 'Ctrl+Alt+Shift+Q'; mods = 0x0007; vk = 0x51 }
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
$script:WpmOverlayBody = @'
|
|
66
|
+
param([int]$MarkerSize, [int]$HotkeyId, [object[]]$Chords)
|
|
67
|
+
try {
|
|
68
|
+
Add-Type -AssemblyName PresentationFramework
|
|
69
|
+
Add-Type -AssemblyName PresentationCore
|
|
70
|
+
Add-Type -AssemblyName WindowsBase
|
|
71
|
+
|
|
72
|
+
$win = [System.Windows.Window]::new()
|
|
73
|
+
$win.Title = 'WpmOverlay'
|
|
74
|
+
$win.WindowStyle = [System.Windows.WindowStyle]::None
|
|
75
|
+
$win.AllowsTransparency = $true
|
|
76
|
+
$win.ShowActivated = $false # WPF-side half of NOACTIVATE: Show() must not steal foreground
|
|
77
|
+
$win.Focusable = $false
|
|
78
|
+
$win.IsHitTestVisible = $false
|
|
79
|
+
$win.ResizeMode = [System.Windows.ResizeMode]::NoResize
|
|
80
|
+
$win.Width = [double]$MarkerSize
|
|
81
|
+
$win.Height = [double]$MarkerSize
|
|
82
|
+
$win.Left = -2000.0
|
|
83
|
+
$win.Top = -2000.0
|
|
84
|
+
$win.WindowStartupLocation = [System.Windows.WindowStartupLocation]::Manual
|
|
85
|
+
$win.ShowInTaskbar = $false
|
|
86
|
+
$win.Topmost = $true
|
|
87
|
+
$win.Opacity = 0.85
|
|
88
|
+
|
|
89
|
+
$ring = [System.Windows.Shapes.Ellipse]::new()
|
|
90
|
+
$ring.Width = [double]$MarkerSize
|
|
91
|
+
$ring.Height = [double]$MarkerSize
|
|
92
|
+
$ring.Stroke = [System.Windows.Media.Brushes]::DeepSkyBlue
|
|
93
|
+
$ring.StrokeThickness = 2.0
|
|
94
|
+
$ring.Fill = [System.Windows.Media.SolidColorBrush]::new(
|
|
95
|
+
[System.Windows.Media.Color]::FromArgb(60, 0, 191, 255))
|
|
96
|
+
$grid = [System.Windows.Controls.Grid]::new()
|
|
97
|
+
[void]$grid.Children.Add($ring)
|
|
98
|
+
$win.Content = $grid
|
|
99
|
+
|
|
100
|
+
$win.Show()
|
|
101
|
+
$hwnd = (New-Object System.Windows.Interop.WindowInteropHelper($win)).Handle
|
|
102
|
+
[WpmCtl]::OverlayHwnd = [int64]$hwnd
|
|
103
|
+
|
|
104
|
+
# The trio, applied once the HWND exists. TOPMOST is also WPF-side, but the measured promise is
|
|
105
|
+
# the ex-style value itself, so all four bits are set explicitly and read back by the test.
|
|
106
|
+
$bits = [WpmWin]::WS_EX_TOPMOST -bor [WpmWin]::WS_EX_NOACTIVATE -bor
|
|
107
|
+
[WpmWin]::WS_EX_TRANSPARENT -bor [WpmWin]::WS_EX_LAYERED
|
|
108
|
+
[void][WpmWin]::AddExStyles($hwnd, $bits)
|
|
109
|
+
|
|
110
|
+
# The marker's PHYSICAL size, read once from Win32. Placement is done with SetWindowPos (physical
|
|
111
|
+
# units) because WPF's own coordinates are device independent and did not agree with this desktop
|
|
112
|
+
# (measured: marker at (303,263) for a target of (470,410) at 150%). Reading the real rect once is
|
|
113
|
+
# cheaper and more honest than assuming MarkerSize is the physical edge length.
|
|
114
|
+
$rc0 = New-Object 'WpmWin+RECT'
|
|
115
|
+
[void][WpmWin]::GetWindowRect([IntPtr]$hwnd, [ref]$rc0)
|
|
116
|
+
$script:OvPhysW = [int]($rc0.Right - $rc0.Left)
|
|
117
|
+
$script:OvPhysH = [int]($rc0.Bottom - $rc0.Top)
|
|
118
|
+
if ($script:OvPhysW -le 0) { $script:OvPhysW = $MarkerSize }
|
|
119
|
+
if ($script:OvPhysH -le 0) { $script:OvPhysH = $MarkerSize }
|
|
120
|
+
|
|
121
|
+
$hkOk = $false
|
|
122
|
+
$hkLabel = ''
|
|
123
|
+
$tried = @()
|
|
124
|
+
foreach ($c in $Chords) {
|
|
125
|
+
$label = [string]$c.label
|
|
126
|
+
$tried += $label
|
|
127
|
+
if ([WpmWin]::RegisterHotKey($hwnd, $HotkeyId, [uint32]$c.mods, [uint32]$c.vk)) {
|
|
128
|
+
$hkOk = $true
|
|
129
|
+
$hkLabel = $label
|
|
130
|
+
break
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
[WpmCtl]::HotkeyOk = [bool]$hkOk
|
|
134
|
+
[WpmCtl]::HotkeyLabel = $hkLabel
|
|
135
|
+
if (-not $hkOk) {
|
|
136
|
+
[WpmCtl]::Reason = 'RegisterHotKey false for every candidate (' + ($tried -join ', ') + '); the settings switch still pauses'
|
|
137
|
+
} elseif ($hkLabel -ne 'Ctrl+Alt+Q') {
|
|
138
|
+
# Reported, not hidden: the promise in the README is the first chord, and when the desktop
|
|
139
|
+
# already owns it this is the only place the owner can learn which key actually works.
|
|
140
|
+
[WpmCtl]::Reason = "Ctrl+Alt+Q is owned by another app (RegisterHotKey 1409); registered $hkLabel instead"
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
# WM_HOTKEY -> flip the ONE pause flag. The owner's switch: server-side refusal, not a hint.
|
|
144
|
+
$src = [System.Windows.Interop.HwndSource]::FromHwnd($hwnd)
|
|
145
|
+
# No DIP matrix is kept here on purpose. Converting to device-independent units is exactly the
|
|
146
|
+
# step that put the marker at (303,263) for a target of (470,410): this process's idea of the
|
|
147
|
+
# scale and the window's actual rendering disagreed. Placement is SetWindowPos in physical pixels
|
|
148
|
+
# (see the pump), so there is nothing to convert and no second coordinate system to get wrong.
|
|
149
|
+
if ($src) {
|
|
150
|
+
$hook = [System.Windows.Interop.HwndSourceHook] {
|
|
151
|
+
param($h, $m, $wp, $lp, [ref]$handled)
|
|
152
|
+
if ($m -eq [WpmWin]::WM_HOTKEY -and [int]$wp -eq $HotkeyId) {
|
|
153
|
+
[WpmCtl]::Paused = -not [WpmCtl]::Paused
|
|
154
|
+
$handled.Value = $true
|
|
155
|
+
return [IntPtr]::Zero
|
|
156
|
+
}
|
|
157
|
+
$handled.Value = $false
|
|
158
|
+
return [IntPtr]::Zero
|
|
159
|
+
}
|
|
160
|
+
[void]$src.AddHook($hook)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
# THE MARKER IS DRIVEN FROM THIS THREAD, ON A PUMP -- not by cross-thread Invokes.
|
|
164
|
+
#
|
|
165
|
+
# Why: Set-WpmOverlayMarker used to marshal a PowerShell scriptblock onto this thread with
|
|
166
|
+
# Dispatcher.Invoke. Measured (.tmp/ov-seg.log): that call took 1818ms and once never returned.
|
|
167
|
+
# The delegate is a scriptblock and this thread is inside Dispatcher.Run(), so the engine has to
|
|
168
|
+
# reach a runspace that is already busy -- a deadlock with a timeout, not a slow call. Writing
|
|
169
|
+
# three static fields costs nothing; this pump (a DispatcherTimer, which runs on THIS thread the
|
|
170
|
+
# same way the hotkey hook does) applies them at 16ms.
|
|
171
|
+
#
|
|
172
|
+
# Auto-hide is measured from the LAST placement, so the deadline is re-armed per request: a
|
|
173
|
+
# free-running 1.5s timer kicks the marker off screen at an arbitrary phase, which can be the
|
|
174
|
+
# same millisecond it was placed (measured in .tmp/ov-hide.log: -2000 within 400ms of placing).
|
|
175
|
+
# That version passed a naive "is it hidden" glance while breaking the promise in the other
|
|
176
|
+
# direction -- it never stayed visible long enough to be pointed at.
|
|
177
|
+
$script:OvSeenSeq = [WpmCtl]::MarkerSeq
|
|
178
|
+
$script:OvHideAt = [DateTime]::MinValue
|
|
179
|
+
$pump = [System.Windows.Threading.DispatcherTimer]::new()
|
|
180
|
+
$pump.Interval = [TimeSpan]::FromMilliseconds(16)
|
|
181
|
+
$pump.Add_Tick({
|
|
182
|
+
$w = [WpmCtl]::Window
|
|
183
|
+
if (-not $w -or -not [WpmCtl]::OverlayOn) { return }
|
|
184
|
+
$seq = [WpmCtl]::MarkerSeq
|
|
185
|
+
if ($seq -ne $script:OvSeenSeq) {
|
|
186
|
+
$script:OvSeenSeq = $seq
|
|
187
|
+
if ([WpmCtl]::MarkerHide) {
|
|
188
|
+
[void][WpmWin]::MoveNoActivate([IntPtr][WpmCtl]::OverlayHwnd, -2000, -2000, 1, 1)
|
|
189
|
+
$script:OvHideAt = [DateTime]::MinValue
|
|
190
|
+
} else {
|
|
191
|
+
$mx = [WpmCtl]::MarkerX
|
|
192
|
+
$my = [WpmCtl]::MarkerY
|
|
193
|
+
if ($mx -ge 0 -and $my -ge 0) {
|
|
194
|
+
$pw = [int]$script:OvPhysW
|
|
195
|
+
$ph = [int]$script:OvPhysH
|
|
196
|
+
[void][WpmWin]::MoveNoActivate([IntPtr][WpmCtl]::OverlayHwnd, $mx - [int][Math]::Floor($pw / 2), $my - [int][Math]::Floor($ph / 2), $pw, $ph)
|
|
197
|
+
$script:OvHideAt = [DateTime]::UtcNow.AddMilliseconds(1500)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} elseif ($script:OvHideAt -ne [DateTime]::MinValue -and [DateTime]::UtcNow -ge $script:OvHideAt) {
|
|
201
|
+
[void][WpmWin]::MoveNoActivate([IntPtr][WpmCtl]::OverlayHwnd, -2000, -2000, 1, 1)
|
|
202
|
+
$script:OvHideAt = [DateTime]::MinValue
|
|
203
|
+
}
|
|
204
|
+
})
|
|
205
|
+
$pump.Start()
|
|
206
|
+
|
|
207
|
+
[WpmCtl]::Dispatcher = [System.Windows.Threading.Dispatcher]::CurrentDispatcher
|
|
208
|
+
[WpmCtl]::Window = $win
|
|
209
|
+
[WpmCtl]::Timer = $pump
|
|
210
|
+
[WpmCtl]::OverlayOn = $true
|
|
211
|
+
|
|
212
|
+
# Block this runspace thread pumping the dispatcher until the window closes (Stop-WpmOverlay).
|
|
213
|
+
[System.Windows.Threading.Dispatcher]::Run()
|
|
214
|
+
} catch {
|
|
215
|
+
[WpmCtl]::Reason = $_.Exception.Message
|
|
216
|
+
} finally {
|
|
217
|
+
[WpmCtl]::OverlayOn = $false
|
|
218
|
+
[WpmCtl]::HotkeyOk = $false
|
|
219
|
+
[WpmCtl]::HotkeyLabel = ''
|
|
220
|
+
[WpmCtl]::Window = $null
|
|
221
|
+
[WpmCtl]::Timer = $null
|
|
222
|
+
[WpmCtl]::Dispatcher = $null
|
|
223
|
+
[WpmCtl]::OverlayHwnd = 0
|
|
224
|
+
}
|
|
225
|
+
'@
|
|
226
|
+
|
|
227
|
+
function Start-WpmOverlay {
|
|
228
|
+
param([int]$MarkerSize = 22)
|
|
229
|
+
if ($script:WpmOvRunspace -and $script:WpmOvRunspace.RunspaceStateInfo.State -eq 'Opened') {
|
|
230
|
+
return @{ started = $false; reason = 'already running' }
|
|
231
|
+
}
|
|
232
|
+
[WpmCtl]::Reason = ''
|
|
233
|
+
$iss = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault2()
|
|
234
|
+
$rs = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($iss)
|
|
235
|
+
$rs.ApartmentState = [System.Threading.ApartmentState]::STA
|
|
236
|
+
$rs.ThreadOptions = [System.Management.Automation.Runspaces.PSThreadOptions]::Default
|
|
237
|
+
$rs.Open()
|
|
238
|
+
if ($rs.RunspaceStateInfo.State -ne 'Opened') {
|
|
239
|
+
return @{ started = $false; reason = "runspace failed to open: $($rs.RunspaceStateInfo)" }
|
|
240
|
+
}
|
|
241
|
+
$ps = [PowerShell]::Create()
|
|
242
|
+
$ps.Runspace = $rs
|
|
243
|
+
[void]$ps.AddScript($script:WpmOverlayBody).AddArgument($MarkerSize).AddArgument($script:WpmHotkeyId).AddArgument($script:WpmHotkeyChords)
|
|
244
|
+
$script:WpmOvHandle = $ps.BeginInvoke()
|
|
245
|
+
$script:WpmOvRunspace = $rs
|
|
246
|
+
$script:WpmOvPowerShell = $ps
|
|
247
|
+
return @{ started = $true; runspace = [string]$rs.Id }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function Get-WpmOverlayState {
|
|
251
|
+
param([int]$WaitMs = 2500)
|
|
252
|
+
# THE READINESS FLAG MUST BE THE FLAG THE VERDICT USES. The first version polled OverlayHwnd --
|
|
253
|
+
# set right after Show(), BEFORE the ex-style trio is applied -- but reported `running = OverlayOn`,
|
|
254
|
+
# set only after the trio, the hotkey and the pump are all up. Inside that window the state read
|
|
255
|
+
# back a HALF-STYLED window (measured: exStyle=0x00080008 -- WPF's own TOPMOST|LAYERED, no
|
|
256
|
+
# TRANSPARENT) and declared an initialising overlay "not running"; the caller's marker request was
|
|
257
|
+
# then skipped for a click that had already answered, and the fire-and-forget path never retried
|
|
258
|
+
# (measured in the gate: marker parked at (-2000,-2000) with one WARN, then silence forever).
|
|
259
|
+
# So: wait for OverlayOn -- the same flag the verdict reports. A budget that merely EXPIRED is a
|
|
260
|
+
# third state, not a crash: `starting` below, from the runspace handle, keeps "slow" and "dead for
|
|
261
|
+
# a named reason" from sharing one message.
|
|
262
|
+
$deadline = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() + $WaitMs
|
|
263
|
+
while ([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() -lt $deadline -and -not [WpmCtl]::OverlayOn) {
|
|
264
|
+
Start-Sleep -Milliseconds 50
|
|
265
|
+
}
|
|
266
|
+
$hwnd = [WpmCtl]::OverlayHwnd
|
|
267
|
+
$ex = 0
|
|
268
|
+
if ($hwnd -ne 0) { $ex = [WpmWin]::GetWindowLong([IntPtr]$hwnd, [WpmWin]::GWL_EXSTYLE) }
|
|
269
|
+
return @{
|
|
270
|
+
running = [bool][WpmCtl]::OverlayOn
|
|
271
|
+
# A timeout is not a crash, and the caller must be able to tell them apart. Before this, a
|
|
272
|
+
# wait that expired mid-init produced `running=false` with an empty reason -- indistinguishable
|
|
273
|
+
# from a body that threw. 'starting' names the third state: still alive, just not done.
|
|
274
|
+
starting = (-not [WpmCtl]::OverlayOn) -and ($script:WpmOvHandle -ne $null) -and
|
|
275
|
+
(-not $script:WpmOvHandle.AsyncWaitHandle.WaitOne(0))
|
|
276
|
+
hwnd = [string]$hwnd
|
|
277
|
+
hotkeyRegistered = [bool][WpmCtl]::HotkeyOk
|
|
278
|
+
hotkeyLabel = [string][WpmCtl]::HotkeyLabel
|
|
279
|
+
paused = [bool][WpmCtl]::Paused
|
|
280
|
+
reason = [string][WpmCtl]::Reason
|
|
281
|
+
exStyle = '0x{0:X8}' -f $ex
|
|
282
|
+
topmost = [bool]($ex -band [WpmWin]::WS_EX_TOPMOST)
|
|
283
|
+
noActivate = [bool]($ex -band [WpmWin]::WS_EX_NOACTIVATE)
|
|
284
|
+
transparent = [bool]($ex -band [WpmWin]::WS_EX_TRANSPARENT)
|
|
285
|
+
foregroundNow = [WpmWin]::Fg()
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function Set-WpmOverlayMarker {
|
|
290
|
+
# A REQUEST, NOT AN ACTION. The fields are written here and applied by the overlay's own pump;
|
|
291
|
+
# nothing is marshalled across threads. Returns as soon as the request is posted (sub-millisecond),
|
|
292
|
+
# which is the property that matters: this is called after every successful action.
|
|
293
|
+
param([int]$X, [int]$Y, [switch]$Hide)
|
|
294
|
+
if (-not [WpmCtl]::OverlayOn -or [WpmCtl]::OverlayHwnd -eq 0) {
|
|
295
|
+
return @{ ok = $false; reason = 'overlay not running' }
|
|
296
|
+
}
|
|
297
|
+
[WpmCtl]::MarkerX = $X
|
|
298
|
+
[WpmCtl]::MarkerY = $Y
|
|
299
|
+
[WpmCtl]::MarkerHide = [bool]$Hide
|
|
300
|
+
[WpmCtl]::MarkerSeq = [WpmCtl]::MarkerSeq + 1
|
|
301
|
+
return @{ ok = $true; x = $X; y = $Y; hidden = [bool]$Hide }
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function Get-WpmOverlayMarker {
|
|
305
|
+
# Read the marker's REAL on-screen rect with GetWindowRect -- a Win32 call that is safe from any
|
|
306
|
+
# thread. The previous version read WPF's Window.Left through Dispatcher.Invoke (1818ms, see the
|
|
307
|
+
# pump comment in the body) and, before that, read the property straight off the wrong thread,
|
|
308
|
+
# which is a DispatcherObject.VerifyAccess violation rather than a measurement.
|
|
309
|
+
$hwnd = [WpmCtl]::OverlayHwnd
|
|
310
|
+
if ($hwnd -eq 0) {
|
|
311
|
+
return @{ ok = $false; reason = 'overlay not running'; left = 0.0; top = 0.0; offscreen = $true }
|
|
312
|
+
}
|
|
313
|
+
$rc = New-Object 'WpmWin+RECT'
|
|
314
|
+
if (-not [WpmWin]::GetWindowRect([IntPtr]$hwnd, [ref]$rc)) {
|
|
315
|
+
$err = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
|
|
316
|
+
return @{ ok = $false; reason = "GetWindowRect failed win32=$err"; left = 0.0; top = 0.0; offscreen = $true }
|
|
317
|
+
}
|
|
318
|
+
$left = [double]$rc.Left
|
|
319
|
+
return @{
|
|
320
|
+
ok = $true
|
|
321
|
+
reason = ''
|
|
322
|
+
left = $left
|
|
323
|
+
top = [double]$rc.Top
|
|
324
|
+
width = [int]($rc.Right - $rc.Left)
|
|
325
|
+
height = [int]($rc.Bottom - $rc.Top)
|
|
326
|
+
offscreen = ($left -lt -1000.0)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function Stop-WpmOverlay {
|
|
331
|
+
# TEARDOWN IS A WINDOW MESSAGE, NOT A MARSHALLED SCRIPTBLOCK -- the same lesson as the pump.
|
|
332
|
+
# First version: $d.Invoke(...) closing the window never returned (the runspace thread is parked
|
|
333
|
+
# in Dispatcher.Run(); .tmp/ov-step.log shows the trace stopping right there). Second version
|
|
334
|
+
# posted the same scriptblock with BeginInvoke, which returned immediately but took ~1.8s or more
|
|
335
|
+
# to actually run, so `overlay off` measured 2618-3654ms over the worker protocol
|
|
336
|
+
# (.tmp/worker-overlay.mjs). WM_CLOSE is handled by WPF itself: the window closes, Dispatcher.Run
|
|
337
|
+
# returns, the pipeline ends. PostMessage returns immediately and needs no runspace at all.
|
|
338
|
+
# RegisterHotKey is owned by the window, so destroying it unregisters the chord automatically.
|
|
339
|
+
$hwnd = [WpmCtl]::OverlayHwnd
|
|
340
|
+
$posted = $false
|
|
341
|
+
if ($hwnd -ne 0) {
|
|
342
|
+
try { $posted = [bool][WpmWin]::PostMessageW([IntPtr]$hwnd, [uint32][WpmWin]::WM_CLOSE, [IntPtr]::Zero, [IntPtr]::Zero) } catch { $posted = $false }
|
|
343
|
+
}
|
|
344
|
+
$completed = $false
|
|
345
|
+
# WM_CLOSE only closes the WINDOW. Dispatcher.Run() (which is what the overlay thread is parked
|
|
346
|
+
# in) keeps running until Shutdown is requested -- measured: with the message alone the pipeline
|
|
347
|
+
# was still alive after 1000ms of waiting and only ended once InvokeShutdown was forced, for
|
|
348
|
+
# 1542ms total. So: give the window a short grace period to close cleanly, then shut the
|
|
349
|
+
# dispatcher down, which is the call that actually ends the thread.
|
|
350
|
+
if ($script:WpmOvHandle) {
|
|
351
|
+
$spin = 0
|
|
352
|
+
while (-not $script:WpmOvHandle.IsCompleted -and $spin -lt 6) { Start-Sleep -Milliseconds 50; $spin++ }
|
|
353
|
+
$completed = [bool]$script:WpmOvHandle.IsCompleted
|
|
354
|
+
}
|
|
355
|
+
if (-not $completed) {
|
|
356
|
+
$d = [WpmCtl]::Dispatcher
|
|
357
|
+
if ($d) { try { [void]$d.InvokeShutdown() } catch { } }
|
|
358
|
+
$spin = 0
|
|
359
|
+
while ($script:WpmOvHandle -and -not $script:WpmOvHandle.IsCompleted -and $spin -lt 20) { Start-Sleep -Milliseconds 25; $spin++ }
|
|
360
|
+
$completed = [bool]($script:WpmOvHandle -and $script:WpmOvHandle.IsCompleted)
|
|
361
|
+
}
|
|
362
|
+
if ($script:WpmOvHandle) {
|
|
363
|
+
try { $script:WpmOvPowerShell.EndInvoke($script:WpmOvHandle) | Out-Null } catch { }
|
|
364
|
+
}
|
|
365
|
+
if ($script:WpmOvPowerShell) { try { $script:WpmOvPowerShell.Dispose() } catch { }; $script:WpmOvPowerShell = $null }
|
|
366
|
+
if ($script:WpmOvRunspace) { try { $script:WpmOvRunspace.Dispose() } catch { }; $script:WpmOvRunspace = $null }
|
|
367
|
+
$script:WpmOvHandle = $null
|
|
368
|
+
# The body's finally block owns the flags, but it only runs if the pipeline really ended. If the
|
|
369
|
+
# bound expired we must not leave a lie in [WpmCtl]: clear the state here and report the fact.
|
|
370
|
+
$st = Get-WpmOverlayState -WaitMs 200
|
|
371
|
+
if (-not $completed) {
|
|
372
|
+
[WpmCtl]::OverlayOn = $false
|
|
373
|
+
[WpmCtl]::HotkeyOk = $false
|
|
374
|
+
[WpmCtl]::HotkeyLabel = ''
|
|
375
|
+
[WpmCtl]::Window = $null
|
|
376
|
+
[WpmCtl]::Timer = $null
|
|
377
|
+
[WpmCtl]::Dispatcher = $null
|
|
378
|
+
[WpmCtl]::OverlayHwnd = [int64]0
|
|
379
|
+
$st = Get-WpmOverlayState -WaitMs 200
|
|
380
|
+
$st.reason = 'overlay pipeline did not end within the bound; state cleared by Stop-WpmOverlay'
|
|
381
|
+
}
|
|
382
|
+
return @{ stopped = $true; completed = $completed; posted = $posted; state = $st }
|
|
383
|
+
}
|