ai-battery 0.2.0 → 0.2.1
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/README.md +30 -14
- package/bin/ai-battery-macos-status.applescript +5 -12
- package/bin/ai-battery-run +701 -31
- package/bin/ai-battery-tmux.js +17 -0
- package/bin/terminal-compositor.js +111 -0
- package/docs/REFERENCE.md +23 -3
- package/docs/RELEASING.md +24 -7
- package/docs/WINDOWS-TERMINAL-SHARED-BAR.md +74 -0
- package/docs/hud-preview.svg +43 -20
- package/docs/terminal-vertical-preview.svg +73 -0
- package/lib/cli.js +945 -219
- package/lib/core/atomic-rename.js +18 -0
- package/lib/core/cli-help.js +35 -7
- package/lib/core/cli-messages.js +45 -1
- package/lib/core/cli-output.js +277 -72
- package/lib/core/cli-wrappers.js +410 -0
- package/lib/core/config-editor.js +177 -29
- package/lib/core/display-config.js +45 -0
- package/lib/core/integration-files.js +32 -3
- package/lib/core/shared-bar.js +603 -0
- package/lib/core/statusline-git.js +179 -15
- package/lib/core/terminal.js +25 -7
- package/lib/core/tmux-status.js +172 -0
- package/lib/core/uninstall.js +20 -7
- package/lib/core/usage-store.js +85 -0
- package/lib/hud/launcher.js +29 -31
- package/lib/platforms/linux/hud.js +1 -1
- package/lib/platforms/linux/panel.js +1 -1
- package/lib/platforms/macos/hud.js +5 -5
- package/lib/platforms/process-scanner.js +77 -29
- package/lib/platforms/windows/autostart-cleanup.js +1 -1
- package/lib/platforms/windows/codex-runner.js +178 -21
- package/lib/platforms/windows/hud/controls.ps1 +44 -10
- package/lib/platforms/windows/hud/main.ps1 +350 -43
- package/lib/platforms/windows/hud/rendering.ps1 +1227 -1
- package/lib/platforms/windows/hud/snapshot.ps1 +26 -46
- package/lib/platforms/windows/hud-launcher.js +153 -79
- package/lib/platforms/windows/shared-bar-platform.js +972 -0
- package/lib/platforms/windows/terminal-compositor.js +210 -25
- package/lib/platforms/windows/wt-bar.js +729 -0
- package/lib/platforms/windows/wt-fragment.js +665 -0
- package/lib/platforms/windows/wt-keybinding.js +300 -0
- package/lib/providers/antigravity.js +155 -8
- package/package.json +5 -2
- package/vendor/PaneContext.cs +875 -0
|
@@ -0,0 +1,875 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Collections.Generic;
|
|
3
|
+
using System.Diagnostics;
|
|
4
|
+
using System.IO;
|
|
5
|
+
using System.Runtime.InteropServices;
|
|
6
|
+
using System.Text;
|
|
7
|
+
using System.Threading;
|
|
8
|
+
using System.Windows.Automation;
|
|
9
|
+
using Microsoft.Win32.SafeHandles;
|
|
10
|
+
|
|
11
|
+
// Windows Terminal pane discovery + targeted bare-VK attach trigger.
|
|
12
|
+
// Never pops up UI, never touches clipboard, never modifies WT layout directly.
|
|
13
|
+
internal static class PaneContext
|
|
14
|
+
{
|
|
15
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
16
|
+
private struct Rect
|
|
17
|
+
{
|
|
18
|
+
public int Left, Top, Right, Bottom;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
22
|
+
private struct Coord
|
|
23
|
+
{
|
|
24
|
+
public short X, Y;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
28
|
+
private struct SmallRect
|
|
29
|
+
{
|
|
30
|
+
public short Left, Top, Right, Bottom;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
34
|
+
private struct ConsoleScreenBufferInfo
|
|
35
|
+
{
|
|
36
|
+
public Coord Size;
|
|
37
|
+
public Coord CursorPosition;
|
|
38
|
+
public short Attributes;
|
|
39
|
+
public SmallRect Window;
|
|
40
|
+
public Coord MaximumWindowSize;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
44
|
+
private struct ProcessEntry
|
|
45
|
+
{
|
|
46
|
+
public uint Size, Usage, ProcessId;
|
|
47
|
+
public IntPtr Heap;
|
|
48
|
+
public uint ModuleId, Threads, ParentId;
|
|
49
|
+
public int Priority;
|
|
50
|
+
public uint Flags;
|
|
51
|
+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string Name;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
55
|
+
private struct Input
|
|
56
|
+
{
|
|
57
|
+
public uint Type;
|
|
58
|
+
public InputUnion Union;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
[StructLayout(LayoutKind.Explicit)]
|
|
62
|
+
private struct InputUnion
|
|
63
|
+
{
|
|
64
|
+
[FieldOffset(0)] public MouseInput Mouse;
|
|
65
|
+
[FieldOffset(0)] public KeybdInput Keybd;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
69
|
+
private struct MouseInput
|
|
70
|
+
{
|
|
71
|
+
public int Dx, Dy;
|
|
72
|
+
public uint MouseData, Flags, Time;
|
|
73
|
+
public IntPtr ExtraInfo;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
77
|
+
private struct KeybdInput
|
|
78
|
+
{
|
|
79
|
+
public ushort Vk, Scan;
|
|
80
|
+
public uint Flags, Time;
|
|
81
|
+
public IntPtr ExtraInfo;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private struct Candidate
|
|
85
|
+
{
|
|
86
|
+
public uint Pid;
|
|
87
|
+
public bool Persistent;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
[DllImport("kernel32.dll")] private static extern IntPtr GetStdHandle(int id);
|
|
91
|
+
[DllImport("kernel32.dll")] private static extern bool FreeConsole();
|
|
92
|
+
[DllImport("kernel32.dll", SetLastError = true)] private static extern bool AttachConsole(uint pid);
|
|
93
|
+
[DllImport("kernel32.dll")] private static extern IntPtr GetConsoleWindow();
|
|
94
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
95
|
+
private static extern IntPtr CreateFileW(
|
|
96
|
+
string fileName, uint desiredAccess, uint shareMode,
|
|
97
|
+
IntPtr securityAttributes, uint creationDisposition,
|
|
98
|
+
uint flagsAndAttributes, IntPtr templateFile);
|
|
99
|
+
[DllImport("kernel32.dll", SetLastError = true)]
|
|
100
|
+
private static extern bool GetConsoleScreenBufferInfo(IntPtr handle, out ConsoleScreenBufferInfo info);
|
|
101
|
+
[DllImport("kernel32.dll")] private static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint pid);
|
|
102
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] private static extern bool Process32FirstW(IntPtr snapshot, ref ProcessEntry entry);
|
|
103
|
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] private static extern bool Process32NextW(IntPtr snapshot, ref ProcessEntry entry);
|
|
104
|
+
[DllImport("kernel32.dll")] private static extern bool CloseHandle(IntPtr handle);
|
|
105
|
+
[DllImport("kernel32.dll", SetLastError = true)] private static extern IntPtr OpenProcess(uint desiredAccess, bool inheritHandle, uint pid);
|
|
106
|
+
[DllImport("kernel32.dll", SetLastError = true)] private static extern bool GetExitCodeProcess(IntPtr handle, out uint exitCode);
|
|
107
|
+
[DllImport("user32.dll")] private static extern IntPtr GetWindow(IntPtr window, uint command);
|
|
108
|
+
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow();
|
|
109
|
+
[DllImport("user32.dll")] private static extern short GetAsyncKeyState(int vKey);
|
|
110
|
+
[DllImport("user32.dll", SetLastError = true)] private static extern uint SendInput(uint count, Input[] inputs, int size);
|
|
111
|
+
[DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr window);
|
|
112
|
+
[DllImport("user32.dll")] private static extern bool GetClientRect(IntPtr window, out Rect rect);
|
|
113
|
+
[DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr window, out uint pid);
|
|
114
|
+
[DllImport("user32.dll")] private static extern bool SetProcessDpiAwarenessContext(IntPtr value);
|
|
115
|
+
[DllImport("user32.dll")] private static extern uint GetDpiForWindow(IntPtr window);
|
|
116
|
+
|
|
117
|
+
private static int ConsoleViewportColumns()
|
|
118
|
+
{
|
|
119
|
+
const uint GENERIC_READ = 0x80000000;
|
|
120
|
+
const uint FILE_SHARE_READ = 1;
|
|
121
|
+
const uint FILE_SHARE_WRITE = 2;
|
|
122
|
+
const uint OPEN_EXISTING = 3;
|
|
123
|
+
var handle = CreateFileW("CONOUT$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
|
|
124
|
+
if (handle == IntPtr.Zero || handle == new IntPtr(-1)) return 0;
|
|
125
|
+
try
|
|
126
|
+
{
|
|
127
|
+
ConsoleScreenBufferInfo info;
|
|
128
|
+
if (GetConsoleScreenBufferInfo(handle, out info))
|
|
129
|
+
{
|
|
130
|
+
int width = info.Window.Right - info.Window.Left + 1;
|
|
131
|
+
if (width > 0) return width;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
finally { CloseHandle(handle); }
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private static int ActiveTabPaneCount(IntPtr window)
|
|
139
|
+
{
|
|
140
|
+
try
|
|
141
|
+
{
|
|
142
|
+
var root = AutomationElement.FromHandle(window);
|
|
143
|
+
var panes = root.FindAll(TreeScope.Descendants,
|
|
144
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
145
|
+
return panes != null ? panes.Count : 0;
|
|
146
|
+
}
|
|
147
|
+
catch
|
|
148
|
+
{
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private static bool TryFocusedPaneBounds(IntPtr window, out int paneLeft, out int paneTop, out int paneRight, out int paneBottom, out int paneCount)
|
|
154
|
+
{
|
|
155
|
+
paneLeft = 0; paneTop = 0; paneRight = 0; paneBottom = 0; paneCount = 0;
|
|
156
|
+
try
|
|
157
|
+
{
|
|
158
|
+
var root = AutomationElement.FromHandle(window);
|
|
159
|
+
var rootRect = root.Current.BoundingRectangle;
|
|
160
|
+
if (rootRect.IsEmpty || rootRect.Width <= 0 || rootRect.Height <= 0) return false;
|
|
161
|
+
var panes = root.FindAll(TreeScope.Descendants,
|
|
162
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
163
|
+
if (panes == null || panes.Count == 0) return false;
|
|
164
|
+
paneCount = panes.Count;
|
|
165
|
+
|
|
166
|
+
AutomationElement target = null;
|
|
167
|
+
if (panes.Count == 1)
|
|
168
|
+
{
|
|
169
|
+
target = panes[0];
|
|
170
|
+
}
|
|
171
|
+
else if (GetForegroundWindow() == window)
|
|
172
|
+
{
|
|
173
|
+
foreach (AutomationElement pane in panes)
|
|
174
|
+
{
|
|
175
|
+
try
|
|
176
|
+
{
|
|
177
|
+
if (pane.Current.HasKeyboardFocus)
|
|
178
|
+
{
|
|
179
|
+
target = pane;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch { }
|
|
184
|
+
}
|
|
185
|
+
if (target == null)
|
|
186
|
+
{
|
|
187
|
+
try
|
|
188
|
+
{
|
|
189
|
+
var focused = AutomationElement.FocusedElement;
|
|
190
|
+
if (focused != null)
|
|
191
|
+
{
|
|
192
|
+
var fRect = focused.Current.BoundingRectangle;
|
|
193
|
+
if (!fRect.IsEmpty)
|
|
194
|
+
{
|
|
195
|
+
double cx = fRect.Left + (fRect.Width / 2.0);
|
|
196
|
+
double cy = fRect.Top + (fRect.Height / 2.0);
|
|
197
|
+
foreach (AutomationElement pane in panes)
|
|
198
|
+
{
|
|
199
|
+
try
|
|
200
|
+
{
|
|
201
|
+
var pRect = pane.Current.BoundingRectangle;
|
|
202
|
+
if (!pRect.IsEmpty && cx >= pRect.Left && cx <= pRect.Right && cy >= pRect.Top && cy <= pRect.Bottom)
|
|
203
|
+
{
|
|
204
|
+
target = pane;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
catch { }
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch { }
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (target == null) return false;
|
|
217
|
+
var rect = target.Current.BoundingRectangle;
|
|
218
|
+
if (rect.IsEmpty || rect.Width <= 0 || rect.Height <= 0) return false;
|
|
219
|
+
paneLeft = (int)Math.Round(rect.Left - rootRect.Left);
|
|
220
|
+
paneTop = (int)Math.Round(rect.Top - rootRect.Top);
|
|
221
|
+
paneRight = (int)Math.Round(rect.Right - rootRect.Left);
|
|
222
|
+
paneBottom = (int)Math.Round(rect.Bottom - rootRect.Top);
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
catch
|
|
226
|
+
{
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private static string SelectedTab(IntPtr window)
|
|
232
|
+
{
|
|
233
|
+
for (int attempt = 0; attempt < 2; attempt++)
|
|
234
|
+
{
|
|
235
|
+
try
|
|
236
|
+
{
|
|
237
|
+
var root = AutomationElement.FromHandle(window);
|
|
238
|
+
var tabs = root.FindAll(TreeScope.Descendants,
|
|
239
|
+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
|
|
240
|
+
string firstTab = "";
|
|
241
|
+
int count = 0;
|
|
242
|
+
foreach (AutomationElement tab in tabs)
|
|
243
|
+
{
|
|
244
|
+
count++;
|
|
245
|
+
var rid = string.Join("-", Array.ConvertAll(tab.GetRuntimeId(), value => value.ToString()));
|
|
246
|
+
if (count == 1) firstTab = rid;
|
|
247
|
+
object pattern;
|
|
248
|
+
if (tab.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern) &&
|
|
249
|
+
((SelectionItemPattern)pattern).Current.IsSelected)
|
|
250
|
+
return rid;
|
|
251
|
+
}
|
|
252
|
+
if (count == 1 && firstTab.Length > 0) return firstTab;
|
|
253
|
+
// When WT hides the tab strip (alwaysShowTabs: false, Focus Mode, Fullscreen),
|
|
254
|
+
// there is exactly one tab with 0 TabItem UIA elements.
|
|
255
|
+
if (count == 0 && ActiveTabPaneCount(window) >= 1) return "single-tab";
|
|
256
|
+
}
|
|
257
|
+
catch { }
|
|
258
|
+
if (attempt == 0) Thread.Sleep(30);
|
|
259
|
+
}
|
|
260
|
+
return "";
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private static bool AnyModifierHeld()
|
|
264
|
+
{
|
|
265
|
+
int[] modifiers = { 0x10, 0x11, 0x12, 0x5B, 0x5C }; // Shift, Ctrl, Alt, LWin, RWin
|
|
266
|
+
foreach (var vk in modifiers)
|
|
267
|
+
{
|
|
268
|
+
if ((GetAsyncKeyState(vk) & 0x8000) != 0) return true;
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private static string RuntimeIdString(AutomationElement element)
|
|
274
|
+
{
|
|
275
|
+
if (element == null) return "";
|
|
276
|
+
try
|
|
277
|
+
{
|
|
278
|
+
var rid = element.GetRuntimeId();
|
|
279
|
+
if (rid == null || rid.Length == 0) return "";
|
|
280
|
+
return string.Join("-", Array.ConvertAll(rid, value => value.ToString()));
|
|
281
|
+
}
|
|
282
|
+
catch { return ""; }
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private static AutomationElement FindFocusedPane(AutomationElement root, AutomationElementCollection panes)
|
|
286
|
+
{
|
|
287
|
+
if (panes == null || panes.Count == 0) return null;
|
|
288
|
+
foreach (AutomationElement pane in panes)
|
|
289
|
+
{
|
|
290
|
+
try
|
|
291
|
+
{
|
|
292
|
+
if (pane.Current.HasKeyboardFocus) return pane;
|
|
293
|
+
}
|
|
294
|
+
catch { }
|
|
295
|
+
}
|
|
296
|
+
try
|
|
297
|
+
{
|
|
298
|
+
var focused = AutomationElement.FocusedElement;
|
|
299
|
+
if (focused != null)
|
|
300
|
+
{
|
|
301
|
+
var fRect = focused.Current.BoundingRectangle;
|
|
302
|
+
if (!fRect.IsEmpty)
|
|
303
|
+
{
|
|
304
|
+
double cx = fRect.Left + (fRect.Width / 2.0);
|
|
305
|
+
double cy = fRect.Top + (fRect.Height / 2.0);
|
|
306
|
+
foreach (AutomationElement pane in panes)
|
|
307
|
+
{
|
|
308
|
+
try
|
|
309
|
+
{
|
|
310
|
+
var pRect = pane.Current.BoundingRectangle;
|
|
311
|
+
if (!pRect.IsEmpty && cx >= pRect.Left && cx <= pRect.Right && cy >= pRect.Top && cy <= pRect.Bottom)
|
|
312
|
+
return pane;
|
|
313
|
+
}
|
|
314
|
+
catch { }
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
catch { }
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private static string RestoreFocusAfterExit(long windowHandle, string expectedTab, uint barPid)
|
|
324
|
+
{
|
|
325
|
+
var target = new IntPtr(windowHandle);
|
|
326
|
+
if (target == IntPtr.Zero || !IsWindowVisible(target)) return "{\"restored\":false,\"reason\":\"not-visible\"}";
|
|
327
|
+
if (GetForegroundWindow() != target) return "{\"restored\":false,\"reason\":\"not-foreground\"}";
|
|
328
|
+
if (!string.IsNullOrEmpty(expectedTab) && SelectedTab(target) != expectedTab)
|
|
329
|
+
return "{\"restored\":false,\"reason\":\"tab-mismatch\"}";
|
|
330
|
+
|
|
331
|
+
string savedRid = "";
|
|
332
|
+
double savedCx = 0, savedCy = 0;
|
|
333
|
+
bool hasSaved = false;
|
|
334
|
+
int initialPaneCount = 0;
|
|
335
|
+
try
|
|
336
|
+
{
|
|
337
|
+
var root = AutomationElement.FromHandle(target);
|
|
338
|
+
var panes = root.FindAll(TreeScope.Descendants,
|
|
339
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
340
|
+
initialPaneCount = panes != null ? panes.Count : 0;
|
|
341
|
+
var focused = FindFocusedPane(root, panes);
|
|
342
|
+
if (focused != null)
|
|
343
|
+
{
|
|
344
|
+
savedRid = RuntimeIdString(focused);
|
|
345
|
+
var r = focused.Current.BoundingRectangle;
|
|
346
|
+
if (!r.IsEmpty)
|
|
347
|
+
{
|
|
348
|
+
savedCx = r.Left + (r.Width / 2.0);
|
|
349
|
+
savedCy = r.Top + (r.Height / 2.0);
|
|
350
|
+
hasSaved = true;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
catch { }
|
|
355
|
+
|
|
356
|
+
// Wait for the bar process to finish exiting AND for WT to remove the bar pane
|
|
357
|
+
// from the UIA tree before checking/restoring focus (4-pane grids can take ~300ms).
|
|
358
|
+
if (barPid > 0)
|
|
359
|
+
{
|
|
360
|
+
for (int i = 0; i < 40 && IsWin32PidAlive(barPid); i++) Thread.Sleep(25);
|
|
361
|
+
}
|
|
362
|
+
if (initialPaneCount >= 2)
|
|
363
|
+
{
|
|
364
|
+
for (int i = 0; i < 24 && ActiveTabPaneCount(target) >= initialPaneCount; i++) Thread.Sleep(40);
|
|
365
|
+
}
|
|
366
|
+
Thread.Sleep(120);
|
|
367
|
+
|
|
368
|
+
for (int attempt = 0; attempt < 4; attempt++)
|
|
369
|
+
{
|
|
370
|
+
if (GetForegroundWindow() != target) return "{\"restored\":false,\"reason\":\"focus-changed\"}";
|
|
371
|
+
if (!string.IsNullOrEmpty(expectedTab) && SelectedTab(target) != expectedTab)
|
|
372
|
+
return "{\"restored\":false,\"reason\":\"tab-changed\"}";
|
|
373
|
+
try
|
|
374
|
+
{
|
|
375
|
+
var root = AutomationElement.FromHandle(target);
|
|
376
|
+
var panes = root.FindAll(TreeScope.Descendants,
|
|
377
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
378
|
+
if (panes == null || panes.Count == 0) return "{\"restored\":false,\"reason\":\"no-panes\"}";
|
|
379
|
+
|
|
380
|
+
AutomationElement desired = null;
|
|
381
|
+
if (!string.IsNullOrEmpty(savedRid))
|
|
382
|
+
{
|
|
383
|
+
foreach (AutomationElement pane in panes)
|
|
384
|
+
{
|
|
385
|
+
if (RuntimeIdString(pane) == savedRid) { desired = pane; break; }
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (desired == null && hasSaved)
|
|
389
|
+
{
|
|
390
|
+
double bestDist = double.MaxValue;
|
|
391
|
+
foreach (AutomationElement pane in panes)
|
|
392
|
+
{
|
|
393
|
+
try
|
|
394
|
+
{
|
|
395
|
+
var r = pane.Current.BoundingRectangle;
|
|
396
|
+
if (r.IsEmpty) continue;
|
|
397
|
+
double dx = (r.Left + (r.Width / 2.0)) - savedCx;
|
|
398
|
+
double dy = (r.Top + (r.Height / 2.0)) - savedCy;
|
|
399
|
+
double dist = (dx * dx) + (dy * dy);
|
|
400
|
+
if (dist < bestDist) { bestDist = dist; desired = pane; }
|
|
401
|
+
}
|
|
402
|
+
catch { }
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (desired == null) desired = panes[0];
|
|
406
|
+
|
|
407
|
+
var currentFocused = FindFocusedPane(root, panes);
|
|
408
|
+
if (currentFocused != null && RuntimeIdString(currentFocused) == RuntimeIdString(desired))
|
|
409
|
+
{
|
|
410
|
+
return "{\"restored\":true,\"alreadyFocused\":true}";
|
|
411
|
+
}
|
|
412
|
+
desired.SetFocus();
|
|
413
|
+
Thread.Sleep(70);
|
|
414
|
+
var afterFocused = FindFocusedPane(root, panes);
|
|
415
|
+
if (afterFocused != null) return "{\"restored\":true}";
|
|
416
|
+
}
|
|
417
|
+
catch { }
|
|
418
|
+
Thread.Sleep(100);
|
|
419
|
+
}
|
|
420
|
+
return "{\"restored\":false,\"reason\":\"setfocus-failed\"}";
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
private static string TriggerAttach(long windowHandle, string expectedTab, ushort vk, int minPanes, int delayMs)
|
|
424
|
+
{
|
|
425
|
+
if (string.IsNullOrEmpty(expectedTab)) return "{\"triggered\":false,\"reason\":\"missing-tab\"}";
|
|
426
|
+
var target = new IntPtr(windowHandle);
|
|
427
|
+
if (target == IntPtr.Zero || !IsWindowVisible(target)) return "{\"triggered\":false,\"reason\":\"not-visible\"}";
|
|
428
|
+
if (delayMs > 0)
|
|
429
|
+
{
|
|
430
|
+
Thread.Sleep(delayMs > 1500 ? 1500 : delayMs);
|
|
431
|
+
}
|
|
432
|
+
// Wait for Ctrl/Alt/Shift/Win release (e.g. user just pressed Alt+Shift+D to split)
|
|
433
|
+
// so bare F24 never interferes and never false-aborts into a 4s cooldown.
|
|
434
|
+
for (int i = 0; i < 40 && AnyModifierHeld(); i++) Thread.Sleep(20);
|
|
435
|
+
if (AnyModifierHeld()) return "{\"triggered\":false,\"reason\":\"modifier-held\"}";
|
|
436
|
+
if (GetForegroundWindow() != target) return "{\"triggered\":false,\"reason\":\"not-foreground\"}";
|
|
437
|
+
if (SelectedTab(target) != expectedTab) return "{\"triggered\":false,\"reason\":\"tab-mismatch\"}";
|
|
438
|
+
int requiredPanes = minPanes < 1 ? 1 : minPanes;
|
|
439
|
+
|
|
440
|
+
AutomationElement root;
|
|
441
|
+
AutomationElementCollection panes;
|
|
442
|
+
int beforePaneCount;
|
|
443
|
+
try
|
|
444
|
+
{
|
|
445
|
+
root = AutomationElement.FromHandle(target);
|
|
446
|
+
panes = root.FindAll(TreeScope.Descendants,
|
|
447
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
448
|
+
beforePaneCount = panes != null ? panes.Count : 0;
|
|
449
|
+
}
|
|
450
|
+
catch
|
|
451
|
+
{
|
|
452
|
+
return "{\"triggered\":false,\"reason\":\"uia-failed\"}";
|
|
453
|
+
}
|
|
454
|
+
if (beforePaneCount < requiredPanes) return "{\"triggered\":false,\"reason\":\"not-split\"}";
|
|
455
|
+
|
|
456
|
+
// Snapshot the currently focused pane BEFORE sending F24 so we can restore
|
|
457
|
+
// focus to the exact pane the user is working in (e.g. a newly opened split pane)
|
|
458
|
+
// after AiBattery.AttachBar creates the root-split bar.
|
|
459
|
+
AutomationElement focusedBefore = FindFocusedPane(root, panes);
|
|
460
|
+
if (focusedBefore == null)
|
|
461
|
+
{
|
|
462
|
+
// A freshly opened split pane can take ~20-80ms to claim UIA keyboard focus.
|
|
463
|
+
for (int wait = 0; wait < 4 && focusedBefore == null; wait++)
|
|
464
|
+
{
|
|
465
|
+
Thread.Sleep(25);
|
|
466
|
+
if (GetForegroundWindow() != target) return "{\"triggered\":false,\"reason\":\"focus-changed\"}";
|
|
467
|
+
try
|
|
468
|
+
{
|
|
469
|
+
panes = root.FindAll(TreeScope.Descendants,
|
|
470
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
471
|
+
beforePaneCount = panes != null ? panes.Count : 0;
|
|
472
|
+
if (beforePaneCount < requiredPanes) return "{\"triggered\":false,\"reason\":\"not-split\"}";
|
|
473
|
+
focusedBefore = FindFocusedPane(root, panes);
|
|
474
|
+
}
|
|
475
|
+
catch { }
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
string savedRid = "";
|
|
480
|
+
double savedCx = 0, savedCy = 0;
|
|
481
|
+
bool hasSaved = false;
|
|
482
|
+
var beforeRids = new HashSet<string>();
|
|
483
|
+
if (panes != null)
|
|
484
|
+
{
|
|
485
|
+
foreach (AutomationElement pane in panes)
|
|
486
|
+
{
|
|
487
|
+
var rid = RuntimeIdString(pane);
|
|
488
|
+
if (!string.IsNullOrEmpty(rid)) beforeRids.Add(rid);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
if (focusedBefore != null)
|
|
492
|
+
{
|
|
493
|
+
savedRid = RuntimeIdString(focusedBefore);
|
|
494
|
+
try
|
|
495
|
+
{
|
|
496
|
+
var r = focusedBefore.Current.BoundingRectangle;
|
|
497
|
+
if (!r.IsEmpty)
|
|
498
|
+
{
|
|
499
|
+
savedCx = r.Left + (r.Width / 2.0);
|
|
500
|
+
savedCy = r.Top + (r.Height / 2.0);
|
|
501
|
+
hasSaved = true;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
catch { }
|
|
505
|
+
}
|
|
506
|
+
else if (panes != null && panes.Count > 0)
|
|
507
|
+
{
|
|
508
|
+
// Prefer the last TermControl (newest split pane) if UIA focus is still settling,
|
|
509
|
+
// so we never yank focus back to panes[0].
|
|
510
|
+
var fallback = panes[panes.Count - 1];
|
|
511
|
+
savedRid = RuntimeIdString(fallback);
|
|
512
|
+
try
|
|
513
|
+
{
|
|
514
|
+
var r = fallback.Current.BoundingRectangle;
|
|
515
|
+
if (!r.IsEmpty)
|
|
516
|
+
{
|
|
517
|
+
savedCx = r.Left + (r.Width / 2.0);
|
|
518
|
+
savedCy = r.Top + (r.Height / 2.0);
|
|
519
|
+
hasSaved = true;
|
|
520
|
+
}
|
|
521
|
+
fallback.SetFocus();
|
|
522
|
+
Thread.Sleep(30);
|
|
523
|
+
}
|
|
524
|
+
catch { }
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (GetForegroundWindow() != target) return "{\"triggered\":false,\"reason\":\"focus-changed\"}";
|
|
528
|
+
|
|
529
|
+
const uint INPUT_KEYBOARD = 1;
|
|
530
|
+
const uint KEYEVENTF_KEYUP = 0x0002;
|
|
531
|
+
var inputs = new Input[2];
|
|
532
|
+
inputs[0].Type = INPUT_KEYBOARD;
|
|
533
|
+
inputs[0].Union.Keybd.Vk = vk;
|
|
534
|
+
inputs[1].Type = INPUT_KEYBOARD;
|
|
535
|
+
inputs[1].Union.Keybd.Vk = vk;
|
|
536
|
+
inputs[1].Union.Keybd.Flags = KEYEVENTF_KEYUP;
|
|
537
|
+
var sent = SendInput(2, inputs, Marshal.SizeOf(typeof(Input)));
|
|
538
|
+
if (sent != 2) return "{\"triggered\":false,\"reason\":\"sendinput-failed\"}";
|
|
539
|
+
|
|
540
|
+
// Restore focus to the exact pre-attach pane once WT inserts the root-split bar pane.
|
|
541
|
+
// Without this, WT's moveFocus(returnFocusDirection) from a full-edge bar jumps
|
|
542
|
+
// to the first child pane (the old AI pane) instead of a newly opened sibling split.
|
|
543
|
+
if (!string.IsNullOrEmpty(savedRid) || hasSaved)
|
|
544
|
+
{
|
|
545
|
+
bool barAppeared = false;
|
|
546
|
+
for (int i = 0; i < 45; i++)
|
|
547
|
+
{
|
|
548
|
+
Thread.Sleep(20);
|
|
549
|
+
if (GetForegroundWindow() != target) return "{\"triggered\":true}";
|
|
550
|
+
try
|
|
551
|
+
{
|
|
552
|
+
var livePanes = root.FindAll(TreeScope.Descendants,
|
|
553
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
554
|
+
if (livePanes != null && livePanes.Count > beforePaneCount)
|
|
555
|
+
{
|
|
556
|
+
barAppeared = true;
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
catch { }
|
|
561
|
+
}
|
|
562
|
+
if (barAppeared)
|
|
563
|
+
{
|
|
564
|
+
// Run two tight passes: immediately when the bar TermControl appears and
|
|
565
|
+
// once more ~70ms later in case WT's multipleActions moveFocus runs right after.
|
|
566
|
+
for (int pass = 0; pass < 2; pass++)
|
|
567
|
+
{
|
|
568
|
+
if (pass > 0) Thread.Sleep(70);
|
|
569
|
+
if (GetForegroundWindow() != target) break;
|
|
570
|
+
if (SelectedTab(target) != expectedTab) break;
|
|
571
|
+
try
|
|
572
|
+
{
|
|
573
|
+
var livePanes = root.FindAll(TreeScope.Descendants,
|
|
574
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, "TermControl"));
|
|
575
|
+
if (livePanes == null || livePanes.Count == 0) break;
|
|
576
|
+
|
|
577
|
+
AutomationElement desired = null;
|
|
578
|
+
if (!string.IsNullOrEmpty(savedRid))
|
|
579
|
+
{
|
|
580
|
+
foreach (AutomationElement pane in livePanes)
|
|
581
|
+
{
|
|
582
|
+
if (RuntimeIdString(pane) == savedRid) { desired = pane; break; }
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
if (desired == null && hasSaved)
|
|
586
|
+
{
|
|
587
|
+
double bestDist = double.MaxValue;
|
|
588
|
+
foreach (AutomationElement pane in livePanes)
|
|
589
|
+
{
|
|
590
|
+
var rid = RuntimeIdString(pane);
|
|
591
|
+
if (beforeRids.Count > 0 && !string.IsNullOrEmpty(rid) && !beforeRids.Contains(rid)) continue;
|
|
592
|
+
try
|
|
593
|
+
{
|
|
594
|
+
var r = pane.Current.BoundingRectangle;
|
|
595
|
+
if (r.IsEmpty) continue;
|
|
596
|
+
double dx = (r.Left + (r.Width / 2.0)) - savedCx;
|
|
597
|
+
double dy = (r.Top + (r.Height / 2.0)) - savedCy;
|
|
598
|
+
double dist = (dx * dx) + (dy * dy);
|
|
599
|
+
if (dist < bestDist) { bestDist = dist; desired = pane; }
|
|
600
|
+
}
|
|
601
|
+
catch { }
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
if (desired == null) break;
|
|
605
|
+
var currentFocused = FindFocusedPane(root, livePanes);
|
|
606
|
+
if (currentFocused == null || RuntimeIdString(currentFocused) != RuntimeIdString(desired))
|
|
607
|
+
{
|
|
608
|
+
desired.SetFocus();
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
catch { }
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
return "{\"triggered\":true}";
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
private static Dictionary<uint, ProcessEntry> SnapshotProcesses()
|
|
619
|
+
{
|
|
620
|
+
var parents = new Dictionary<uint, ProcessEntry>();
|
|
621
|
+
var snapshot = CreateToolhelp32Snapshot(2, 0);
|
|
622
|
+
if (snapshot == new IntPtr(-1)) return parents;
|
|
623
|
+
try
|
|
624
|
+
{
|
|
625
|
+
var entry = new ProcessEntry();
|
|
626
|
+
entry.Size = (uint)Marshal.SizeOf(typeof(ProcessEntry));
|
|
627
|
+
if (Process32FirstW(snapshot, ref entry))
|
|
628
|
+
do { parents[entry.ProcessId] = entry; } while (Process32NextW(snapshot, ref entry));
|
|
629
|
+
}
|
|
630
|
+
finally { CloseHandle(snapshot); }
|
|
631
|
+
return parents;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
private static List<Candidate> CandidateChain(uint start, string provider)
|
|
635
|
+
{
|
|
636
|
+
var ordered = new List<Candidate>();
|
|
637
|
+
if (provider == "direct")
|
|
638
|
+
{
|
|
639
|
+
ordered.Add(new Candidate { Pid = start, Persistent = true });
|
|
640
|
+
return ordered;
|
|
641
|
+
}
|
|
642
|
+
var parents = SnapshotProcesses();
|
|
643
|
+
var ancestors = new List<uint>();
|
|
644
|
+
uint namedMatch = 0;
|
|
645
|
+
uint nodeAncestor = 0;
|
|
646
|
+
uint current = start;
|
|
647
|
+
for (int depth = 0; depth < 24; depth++)
|
|
648
|
+
{
|
|
649
|
+
ProcessEntry entry;
|
|
650
|
+
if (!parents.TryGetValue(current, out entry)) break;
|
|
651
|
+
ancestors.Add(current);
|
|
652
|
+
var name = Path.GetFileNameWithoutExtension(entry.Name).ToLowerInvariant();
|
|
653
|
+
if (namedMatch == 0 && (
|
|
654
|
+
(provider == "antigravity" && (name == "agy" || name == "gemini")) ||
|
|
655
|
+
(provider == "claude" && (name == "claude" || name == "claude-code")) ||
|
|
656
|
+
(provider == "codex" && name == "codex")))
|
|
657
|
+
{
|
|
658
|
+
namedMatch = current;
|
|
659
|
+
}
|
|
660
|
+
if (nodeAncestor == 0 && (provider == "claude" || provider == "antigravity") && depth > 0 && name == "node")
|
|
661
|
+
{
|
|
662
|
+
nodeAncestor = current;
|
|
663
|
+
}
|
|
664
|
+
if (name == "windowsterminal" || entry.ParentId == current || entry.ParentId == 0) break;
|
|
665
|
+
current = entry.ParentId;
|
|
666
|
+
}
|
|
667
|
+
if (namedMatch != 0) ordered.Add(new Candidate { Pid = namedMatch, Persistent = true });
|
|
668
|
+
if (nodeAncestor != 0 && nodeAncestor != namedMatch) ordered.Add(new Candidate { Pid = nodeAncestor, Persistent = true });
|
|
669
|
+
foreach (var pid in ancestors)
|
|
670
|
+
{
|
|
671
|
+
if (pid != namedMatch && pid != nodeAncestor)
|
|
672
|
+
ordered.Add(new Candidate { Pid = pid, Persistent = false });
|
|
673
|
+
}
|
|
674
|
+
return ordered;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
private static bool TryResolveTerminalConsole(uint candidate, out IntPtr console, out IntPtr window)
|
|
678
|
+
{
|
|
679
|
+
console = IntPtr.Zero;
|
|
680
|
+
window = IntPtr.Zero;
|
|
681
|
+
FreeConsole();
|
|
682
|
+
if (!AttachConsole(candidate)) return false;
|
|
683
|
+
console = GetConsoleWindow();
|
|
684
|
+
if (console == IntPtr.Zero) return false;
|
|
685
|
+
window = GetWindow(console, 4); // GW_OWNER: ConPTY's actual hosting window.
|
|
686
|
+
if (window == IntPtr.Zero) return false;
|
|
687
|
+
uint terminalPid;
|
|
688
|
+
GetWindowThreadProcessId(window, out terminalPid);
|
|
689
|
+
if (terminalPid == 0) return false;
|
|
690
|
+
try
|
|
691
|
+
{
|
|
692
|
+
using (var proc = Process.GetProcessById((int)terminalPid))
|
|
693
|
+
{
|
|
694
|
+
return proc.ProcessName.Equals("WindowsTerminal", StringComparison.OrdinalIgnoreCase);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
catch
|
|
698
|
+
{
|
|
699
|
+
return false;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
private static bool IsWin32PidAlive(uint pid)
|
|
704
|
+
{
|
|
705
|
+
if (pid == 0) return false;
|
|
706
|
+
const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
|
|
707
|
+
const uint STILL_ACTIVE = 259;
|
|
708
|
+
var handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
|
|
709
|
+
if (handle == IntPtr.Zero) return false;
|
|
710
|
+
try
|
|
711
|
+
{
|
|
712
|
+
uint code;
|
|
713
|
+
return GetExitCodeProcess(handle, out code) && code == STILL_ACTIVE;
|
|
714
|
+
}
|
|
715
|
+
finally { CloseHandle(handle); }
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
private static string CheckPidsAlive(string csv)
|
|
719
|
+
{
|
|
720
|
+
var alive = new List<string>();
|
|
721
|
+
foreach (var part in (csv ?? "").Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries))
|
|
722
|
+
{
|
|
723
|
+
uint pid;
|
|
724
|
+
if (uint.TryParse(part.Trim(), out pid) && IsWin32PidAlive(pid))
|
|
725
|
+
{
|
|
726
|
+
alive.Add(pid.ToString());
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return "{\"alive\":[" + string.Join(",", alive.ToArray()) + "]}";
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
private static List<Candidate> SelfAncestorChain()
|
|
733
|
+
{
|
|
734
|
+
var ordered = new List<Candidate>();
|
|
735
|
+
var parents = SnapshotProcesses();
|
|
736
|
+
uint selfPid = (uint)Process.GetCurrentProcess().Id;
|
|
737
|
+
ProcessEntry selfEntry;
|
|
738
|
+
if (!parents.TryGetValue(selfPid, out selfEntry)) return ordered;
|
|
739
|
+
uint current = selfEntry.ParentId;
|
|
740
|
+
for (int depth = 0; depth < 24 && current != 0 && current != selfPid; depth++)
|
|
741
|
+
{
|
|
742
|
+
ProcessEntry entry;
|
|
743
|
+
if (!parents.TryGetValue(current, out entry)) break;
|
|
744
|
+
ordered.Add(new Candidate { Pid = current, Persistent = true });
|
|
745
|
+
var name = Path.GetFileNameWithoutExtension(entry.Name).ToLowerInvariant();
|
|
746
|
+
if (name == "windowsterminal" || entry.ParentId == current || entry.ParentId == 0) break;
|
|
747
|
+
current = entry.ParentId;
|
|
748
|
+
}
|
|
749
|
+
return ordered;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
[STAThread]
|
|
753
|
+
public static int Main(string[] args)
|
|
754
|
+
{
|
|
755
|
+
try { SetProcessDpiAwarenessContext(new IntPtr(-4)); } catch { }
|
|
756
|
+
var stdout = GetStdHandle(-11);
|
|
757
|
+
string result = "{}";
|
|
758
|
+
try
|
|
759
|
+
{
|
|
760
|
+
if (args.Length >= 4 && args[0] == "--trigger-attach")
|
|
761
|
+
{
|
|
762
|
+
long hwnd;
|
|
763
|
+
ushort vk;
|
|
764
|
+
int minPanes = 2;
|
|
765
|
+
int delayMs = 0;
|
|
766
|
+
if (args.Length >= 5) int.TryParse(args[4], out minPanes);
|
|
767
|
+
if (args.Length >= 6) int.TryParse(args[5], out delayMs);
|
|
768
|
+
if (long.TryParse(args[1], out hwnd) && ushort.TryParse(args[3], out vk) && vk >= 0x84 && vk <= 0x87)
|
|
769
|
+
{
|
|
770
|
+
result = TriggerAttach(hwnd, args[2], vk, minPanes, delayMs);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
else if (args.Length >= 4 && args[0] == "--restore-focus")
|
|
774
|
+
{
|
|
775
|
+
long hwnd;
|
|
776
|
+
uint barPid = 0;
|
|
777
|
+
if (long.TryParse(args[1], out hwnd) && hwnd != 0 && uint.TryParse(args[3], out barPid))
|
|
778
|
+
{
|
|
779
|
+
result = RestoreFocusAfterExit(hwnd, args[2], barPid);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
else if (args.Length >= 2 && args[0] == "--tab-state")
|
|
783
|
+
{
|
|
784
|
+
long hwnd;
|
|
785
|
+
if (long.TryParse(args[1], out hwnd) && hwnd != 0)
|
|
786
|
+
{
|
|
787
|
+
var target = new IntPtr(hwnd);
|
|
788
|
+
var tab = SelectedTab(target);
|
|
789
|
+
var paneCount = ActiveTabPaneCount(target);
|
|
790
|
+
Rect clientRect;
|
|
791
|
+
bool hasClientRect = GetClientRect(target, out clientRect);
|
|
792
|
+
int clientWidth = hasClientRect ? Math.Max(0, clientRect.Right - clientRect.Left) : 0;
|
|
793
|
+
uint dpi = 96;
|
|
794
|
+
try { uint d = GetDpiForWindow(target); if (d >= 72) dpi = d; } catch { }
|
|
795
|
+
result = "{\"window\":" + hwnd +
|
|
796
|
+
",\"tab\":\"" + tab +
|
|
797
|
+
"\",\"paneCount\":" + paneCount +
|
|
798
|
+
",\"foreground\":" + (GetForegroundWindow() == target ? "true" : "false") +
|
|
799
|
+
",\"clientWidth\":" + clientWidth +
|
|
800
|
+
",\"dpi\":" + dpi + "}";
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
else if (args.Length >= 2 && args[0] == "--pids-alive")
|
|
804
|
+
{
|
|
805
|
+
result = CheckPidsAlive(args[1]);
|
|
806
|
+
}
|
|
807
|
+
else
|
|
808
|
+
{
|
|
809
|
+
bool isSelf = args.Length > 0 && args[0] == "self";
|
|
810
|
+
uint pid = 0;
|
|
811
|
+
if (args.Length == 0 || (!isSelf && !uint.TryParse(args[0], out pid))) return 2;
|
|
812
|
+
var provider = args.Length > 1 ? args[1] : "direct";
|
|
813
|
+
var tabMode = args.Length > 2 ? args[2] : "";
|
|
814
|
+
var includeTab = tabMode == "tab" || tabMode == "tab-only";
|
|
815
|
+
var includePaneCount = tabMode == "tab";
|
|
816
|
+
var candidates = isSelf ? SelfAncestorChain() : CandidateChain(pid, provider);
|
|
817
|
+
foreach (var candidate in candidates)
|
|
818
|
+
{
|
|
819
|
+
IntPtr console;
|
|
820
|
+
IntPtr window;
|
|
821
|
+
if (!TryResolveTerminalConsole(candidate.Pid, out console, out window)) continue;
|
|
822
|
+
string started = "";
|
|
823
|
+
try
|
|
824
|
+
{
|
|
825
|
+
using (var process = Process.GetProcessById((int)candidate.Pid))
|
|
826
|
+
started = process.StartTime.ToUniversalTime().Ticks.ToString();
|
|
827
|
+
}
|
|
828
|
+
catch { started = ""; }
|
|
829
|
+
var tab = includeTab ? SelectedTab(window) : "";
|
|
830
|
+
int paneLeft = 0, paneTop = 0, paneRight = 0, paneBottom = 0, measuredPaneCount = 0;
|
|
831
|
+
bool paneRectKnown = includeTab && TryFocusedPaneBounds(window, out paneLeft, out paneTop, out paneRight, out paneBottom, out measuredPaneCount);
|
|
832
|
+
var paneCount = measuredPaneCount > 0 ? measuredPaneCount : (includePaneCount ? ActiveTabPaneCount(window) : 0);
|
|
833
|
+
var columns = ConsoleViewportColumns();
|
|
834
|
+
Rect clientRect;
|
|
835
|
+
bool hasClientRect = GetClientRect(window, out clientRect);
|
|
836
|
+
int clientWidth = hasClientRect ? Math.Max(0, clientRect.Right - clientRect.Left) : 0;
|
|
837
|
+
int clientHeight = hasClientRect ? Math.Max(0, clientRect.Bottom - clientRect.Top) : 0;
|
|
838
|
+
uint dpi = 96;
|
|
839
|
+
try { uint d = GetDpiForWindow(window); if (d >= 72) dpi = d; } catch { }
|
|
840
|
+
result = "{\"ownerPid\":" + candidate.Pid +
|
|
841
|
+
",\"winHostPid\":" + candidate.Pid +
|
|
842
|
+
",\"persistentOwner\":" + (candidate.Persistent ? "true" : "false") +
|
|
843
|
+
",\"startedAt\":\"" + started +
|
|
844
|
+
"\",\"window\":" + window.ToInt64() + ",\"console\":" + console.ToInt64() +
|
|
845
|
+
",\"columns\":" + columns +
|
|
846
|
+
",\"clientWidth\":" + clientWidth +
|
|
847
|
+
",\"clientHeight\":" + clientHeight +
|
|
848
|
+
",\"dpi\":" + dpi +
|
|
849
|
+
",\"paneCount\":" + paneCount +
|
|
850
|
+
",\"paneRectKnown\":" + (paneRectKnown ? "true" : "false") +
|
|
851
|
+
",\"paneLeft\":" + paneLeft +
|
|
852
|
+
",\"paneTop\":" + paneTop +
|
|
853
|
+
",\"paneRight\":" + paneRight +
|
|
854
|
+
",\"paneBottom\":" + paneBottom +
|
|
855
|
+
",\"foreground\":" + (GetForegroundWindow() == window ? "true" : "false") +
|
|
856
|
+
",\"visible\":" + (IsWindowVisible(console) ? "true" : "false") +
|
|
857
|
+
",\"tab\":\"" + tab + "\"}";
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
catch { result = "{}"; }
|
|
863
|
+
finally { FreeConsole(); }
|
|
864
|
+
if (stdout != IntPtr.Zero && stdout != new IntPtr(-1))
|
|
865
|
+
{
|
|
866
|
+
try
|
|
867
|
+
{
|
|
868
|
+
using (var stream = new FileStream(new SafeFileHandle(stdout, false), FileAccess.Write))
|
|
869
|
+
using (var writer = new StreamWriter(stream, new UTF8Encoding(false))) writer.WriteLine(result);
|
|
870
|
+
}
|
|
871
|
+
catch { }
|
|
872
|
+
}
|
|
873
|
+
return 0;
|
|
874
|
+
}
|
|
875
|
+
}
|