-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClaudeCodeControl.TerminalIO.cs
More file actions
634 lines (572 loc) · 26.9 KB
/
ClaudeCodeControl.TerminalIO.cs
File metadata and controls
634 lines (572 loc) · 26.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/* *******************************************************************************************************************
* Application: ClaudeCodeExtension
*
* Autor: Daniel Carvalho Liedke
*
* Copyright © Daniel Carvalho Liedke 2026
* Usage and reproduction in any manner whatsoever without the written permission of Daniel Carvalho Liedke is strictly forbidden.
*
* Purpose: Terminal input/output communication - sending text and keyboard events
*
* *******************************************************************************************************************/
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.VisualStudio.Shell;
namespace ClaudeCodeVS
{
public partial class ClaudeCodeControl
{
#region Terminal Communication
/// <summary>
/// Timeout for clipboard operations in milliseconds
/// </summary>
private const int ClipboardTimeoutMs = 2000;
/// <summary>
/// Maximum number of retry attempts for clipboard operations
/// </summary>
private const int ClipboardMaxRetries = 10;
/// <summary>
/// Delay between clipboard retry attempts in milliseconds
/// </summary>
private const int ClipboardRetryDelayMs = 100;
/// <summary>
/// Sends text to the embedded terminal by copying to clipboard and simulating paste
/// Preserves the original clipboard content and restores it after sending
/// This is the synchronous wrapper for backward compatibility
/// </summary>
/// <param name="text">The text to send to the terminal</param>
private void SendTextToTerminal(string text)
{
// Fire and forget with error handling - the async version handles the actual work
ThreadHelper.JoinableTaskFactory.Run(async delegate
{
await SendTextToTerminalAsync(text);
});
}
/// <summary>
/// Sends text to the embedded terminal asynchronously
/// Preserves the original clipboard content and restores it after sending
/// </summary>
/// <param name="text">The text to send to the terminal</param>
private async Task SendTextToTerminalAsync(string text)
{
// Dictionary to store all original clipboard formats and their data
System.Collections.Generic.Dictionary<string, object> originalClipboardData = null;
try
{
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
// Make sure we're on the UI thread for clipboard operations
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// Save the current clipboard content before modifying it
originalClipboardData = await ClipboardRetryAsync(() => SaveClipboardContent());
// Clear clipboard before copying new text to prevent stale content
await ClipboardRetryAsync(() => Clipboard.Clear());
await Task.Delay(50);
// Copy text to clipboard
await ClipboardRetryAsync(() => Clipboard.SetText(text));
// If terminal is detached, ensure the detached window tab is visible
// (auto-open changes may have activated the diff viewer tab instead)
if (_isTerminalDetached && _detachedTerminalWindow?.Frame is Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame detachedFrame)
{
detachedFrame.Show();
await Task.Delay(200);
}
// Set focus to terminal window
SetForegroundWindow(terminalHandle);
SetFocus(terminalHandle);
await Task.Delay(500); // Reduced from 700ms
// Paste text into the terminal
// Windows Terminal: use Ctrl+Shift+V (right-click opens context menu instead of pasting)
// OpenCode: use Shift+Right-click
// Others (Command Prompt): use right-click
if (_wtTabBarHeight > 0)
{
await PasteViaCtrlShiftVAsync();
await Task.Delay(500);
}
else if (_currentRunningProvider == AiProvider.OpenCode)
{
await ShiftRightClickTerminalCenterAsync();
await Task.Delay(800);
}
else
{
await RightClickTerminalCenterAsync();
await Task.Delay(800);
}
// Send Enter key to execute the command
SendEnterKey();
}
else
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
MessageBox.Show("Terminal is not available. Please restart the terminal.",
"Terminal Error", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
catch (Exception ex)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
MessageBox.Show($"Error sending text to terminal: {ex.Message}",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
// Restore the original clipboard content on UI thread
try
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await Task.Delay(100); // Small delay to ensure paste completed
await ClipboardRetryAsync(() => RestoreClipboardContent(originalClipboardData));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error restoring clipboard: {ex.Message}");
}
}
}
/// <summary>
/// Executes a clipboard operation with automatic retry logic
/// Handles CLIPBRD_E_CANT_OPEN (0x800401D0) errors that occur when another application holds the clipboard
/// </summary>
/// <param name="action">The clipboard action to execute</param>
/// <param name="maxRetries">Maximum number of retry attempts</param>
/// <param name="retryDelayMs">Delay between retries in milliseconds</param>
private async Task ClipboardRetryAsync(Action action, int maxRetries = ClipboardMaxRetries, int retryDelayMs = ClipboardRetryDelayMs)
{
for (int attempt = 0; attempt <= maxRetries; attempt++)
{
try
{
action();
return;
}
catch (System.Runtime.InteropServices.COMException ex) when (ex.ErrorCode == unchecked((int)0x800401D0))
{
if (attempt == maxRetries)
throw;
await Task.Delay(retryDelayMs);
}
}
}
/// <summary>
/// Executes a clipboard operation with automatic retry logic, returning a value
/// Handles CLIPBRD_E_CANT_OPEN (0x800401D0) errors that occur when another application holds the clipboard
/// </summary>
/// <typeparam name="T">The return type</typeparam>
/// <param name="func">The clipboard function to execute</param>
/// <param name="maxRetries">Maximum number of retry attempts</param>
/// <param name="retryDelayMs">Delay between retries in milliseconds</param>
/// <returns>The result of the clipboard operation</returns>
private async Task<T> ClipboardRetryAsync<T>(Func<T> func, int maxRetries = ClipboardMaxRetries, int retryDelayMs = ClipboardRetryDelayMs)
{
for (int attempt = 0; attempt <= maxRetries; attempt++)
{
try
{
return func();
}
catch (System.Runtime.InteropServices.COMException ex) when (ex.ErrorCode == unchecked((int)0x800401D0))
{
if (attempt == maxRetries)
throw;
await Task.Delay(retryDelayMs);
}
}
return default; // Should never reach here
}
/// <summary>
/// Executes a clipboard operation with synchronous retry logic
/// Handles CLIPBRD_E_CANT_OPEN (0x800401D0) errors that occur when another application holds the clipboard
/// </summary>
/// <typeparam name="T">The return type</typeparam>
/// <param name="func">The clipboard function to execute</param>
/// <param name="maxRetries">Maximum number of retry attempts</param>
/// <param name="retryDelayMs">Delay between retries in milliseconds</param>
/// <returns>The result of the clipboard operation</returns>
private T ClipboardRetrySync<T>(Func<T> func, int maxRetries = ClipboardMaxRetries, int retryDelayMs = ClipboardRetryDelayMs)
{
for (int attempt = 0; attempt <= maxRetries; attempt++)
{
try
{
return func();
}
catch (System.Runtime.InteropServices.COMException ex) when (ex.ErrorCode == unchecked((int)0x800401D0))
{
if (attempt == maxRetries)
throw;
Thread.Sleep(retryDelayMs);
}
}
return default; // Should never reach here
}
/// <summary>
/// Saves all clipboard content formats for later restoration
/// Preserves all formats including Office-specific formats (Excel, Word, etc.)
/// </summary>
/// <returns>Dictionary of format names to data objects, or null if clipboard is empty</returns>
private System.Collections.Generic.Dictionary<string, object> SaveClipboardContent()
{
try
{
IDataObject dataObject = Clipboard.GetDataObject();
if (dataObject == null)
return null;
string[] formats = dataObject.GetFormats();
if (formats == null || formats.Length == 0)
return null;
var savedData = new System.Collections.Generic.Dictionary<string, object>();
foreach (string format in formats)
{
try
{
// Skip formats that can cause issues
if (format == "EnhancedMetafile" ||
format == "MetaFilePict" ||
format == "DeviceIndependentBitmap" ||
format == "System.Drawing.Bitmap" ||
format.StartsWith("Object Descriptor") ||
format.StartsWith("Link Source") ||
format.StartsWith("Ole Private Data"))
continue;
object data = dataObject.GetData(format);
if (data != null)
{
// For MemoryStream, we need to copy it as the original may be disposed
if (data is System.IO.MemoryStream ms)
{
try
{
if (ms.CanRead && ms.CanSeek)
{
var copy = new System.IO.MemoryStream();
ms.Position = 0;
ms.CopyTo(copy);
copy.Position = 0;
savedData[format] = copy;
}
}
catch
{
// Skip streams that can't be copied
}
}
// For other Stream types, skip them as they can cause issues
else if (data is System.IO.Stream)
{
continue;
}
// Save primitive types and strings directly
else if (data is string || data is string[] || data.GetType().IsPrimitive)
{
savedData[format] = data;
}
// For byte arrays, make a copy
else if (data is byte[] bytes)
{
var copy = new byte[bytes.Length];
Array.Copy(bytes, copy, bytes.Length);
savedData[format] = copy;
}
}
}
catch
{
// Skip formats that can't be read
}
}
return savedData.Count > 0 ? savedData : null;
}
catch
{
// Silently fail if we can't access clipboard
return null;
}
}
/// <summary>
/// Restores previously saved clipboard content with all formats
/// This preserves Office application data (Excel cells, Word content, etc.)
/// </summary>
/// <param name="savedData">Dictionary of format names to data objects</param>
private void RestoreClipboardContent(System.Collections.Generic.Dictionary<string, object> savedData)
{
try
{
if (savedData == null || savedData.Count == 0)
{
// Original clipboard was empty, clear it
Clipboard.Clear();
return;
}
// Create a new DataObject and add all saved formats
DataObject newDataObject = new DataObject();
foreach (var kvp in savedData)
{
try
{
// Reset stream position if it's a stream
if (kvp.Value is System.IO.MemoryStream ms)
{
ms.Position = 0;
}
newDataObject.SetData(kvp.Key, kvp.Value);
}
catch
{
// Skip formats that can't be set
}
}
Clipboard.SetDataObject(newDataObject, true);
}
catch
{
// Silently fail if we can't restore clipboard
}
}
/// <summary>
/// Pastes text using Ctrl+Shift+V keyboard shortcut (for Windows Terminal).
/// Windows Terminal right-click opens a context menu instead of pasting directly,
/// so we use the keyboard shortcut which always pastes reliably.
/// </summary>
private async Task PasteViaCtrlShiftVAsync()
{
if (terminalHandle == IntPtr.Zero || !IsWindow(terminalHandle)) return;
SetForegroundWindow(terminalHandle);
SetFocus(terminalHandle);
await Task.Delay(100);
// Ctrl+Shift+V
keybd_event(VK_CONTROL, 0, 0, UIntPtr.Zero);
keybd_event(VK_SHIFT, 0, 0, UIntPtr.Zero);
await Task.Delay(30);
keybd_event(0x56, 0, 0, UIntPtr.Zero); // VK_V = 0x56
await Task.Delay(30);
keybd_event(0x56, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}
/// <summary>
/// Simulates a right-click at the specified screen coordinates (async version)
/// </summary>
/// <param name="x">Screen X coordinate</param>
/// <param name="y">Screen Y coordinate</param>
private async Task SendRightClickAsync(int x, int y)
{
SetCursorPos(x, y);
await Task.Delay(30); // Reduced from 50ms
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, UIntPtr.Zero);
await Task.Delay(30); // Reduced from 50ms
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, UIntPtr.Zero);
}
/// <summary>
/// Simulates a right-click at the specified screen coordinates (sync version for backward compat)
/// </summary>
private void SendRightClick(int x, int y)
{
SetCursorPos(x, y);
System.Threading.Thread.Sleep(30);
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(30);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, UIntPtr.Zero);
}
/// <summary>
/// Right-clicks on the center of the terminal window (async version)
/// For Windows Terminal, adjusts Y coordinate to account for hidden tab bar
/// </summary>
private async Task RightClickTerminalCenterAsync()
{
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
GetWindowRect(terminalHandle, out RECT rect);
int centerX = rect.Left + (rect.Right - rect.Left) / 2;
int centerY = rect.Top + (rect.Bottom - rect.Top) / 2;
// For Windows Terminal with hidden tab bar, adjust Y coordinate
// The window is positioned at Y = -_wtTabBarHeight, so add it back to get visible center
if (_wtTabBarHeight > 0)
{
centerY += _wtTabBarHeight;
}
await SendRightClickAsync(centerX, centerY);
}
}
/// <summary>
/// Right-clicks on the center of the terminal window (sync version for backward compat)
/// For Windows Terminal, adjusts Y coordinate to account for hidden tab bar
/// </summary>
private void RightClickTerminalCenter()
{
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
GetWindowRect(terminalHandle, out RECT rect);
int centerX = rect.Left + (rect.Right - rect.Left) / 2;
int centerY = rect.Top + (rect.Bottom - rect.Top) / 2;
// For Windows Terminal with hidden tab bar, adjust Y coordinate
if (_wtTabBarHeight > 0)
{
centerY += _wtTabBarHeight;
}
SendRightClick(centerX, centerY);
}
}
/// <summary>
/// Performs SHIFT+Right-click on the center of the terminal window (async version)
/// Required for Open Code to paste text properly
/// For Windows Terminal, adjusts Y coordinate to account for hidden tab bar
/// </summary>
private async Task ShiftRightClickTerminalCenterAsync()
{
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
GetWindowRect(terminalHandle, out RECT rect);
int centerX = rect.Left + (rect.Right - rect.Left) / 2;
int centerY = rect.Top + (rect.Bottom - rect.Top) / 2;
// For Windows Terminal with hidden tab bar, adjust Y coordinate
if (_wtTabBarHeight > 0)
{
centerY += _wtTabBarHeight;
}
// Move cursor to center
SetCursorPos(centerX, centerY);
await Task.Delay(30); // Reduced from 50ms
// Hold SHIFT key down
keybd_event(VK_SHIFT, 0, 0, UIntPtr.Zero);
await Task.Delay(30); // Reduced from 50ms
// Perform right-click
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, UIntPtr.Zero);
await Task.Delay(30); // Reduced from 50ms
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, UIntPtr.Zero);
await Task.Delay(30); // Reduced from 50ms
// Release SHIFT key
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}
}
/// <summary>
/// Performs SHIFT+Right-click on the center of the terminal window (sync version)
/// Required for Open Code to paste text properly
/// For Windows Terminal, adjusts Y coordinate to account for hidden tab bar
/// </summary>
private void ShiftRightClickTerminalCenter()
{
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
GetWindowRect(terminalHandle, out RECT rect);
int centerX = rect.Left + (rect.Right - rect.Left) / 2;
int centerY = rect.Top + (rect.Bottom - rect.Top) / 2;
// For Windows Terminal with hidden tab bar, adjust Y coordinate
if (_wtTabBarHeight > 0)
{
centerY += _wtTabBarHeight;
}
// Move cursor to center
SetCursorPos(centerX, centerY);
System.Threading.Thread.Sleep(30);
// Hold SHIFT key down
keybd_event(VK_SHIFT, 0, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(30);
// Perform right-click
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(30);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(30);
// Release SHIFT key
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}
}
/// <summary>
/// Sends the Enter key to the terminal window
/// Uses different methods depending on the provider (WSL-based vs Windows-based)
/// </summary>
private void SendEnterKey()
{
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
// Check CURRENTLY RUNNING provider (not the next one being set)
bool isClaudeCodeWSL = _currentRunningProvider == AiProvider.ClaudeCodeWSL;
// Check if we're using other WSL-based providers (Codex WSL, CursorAgent)
bool isOtherWSLProvider = _currentRunningProvider == AiProvider.Codex ||
_currentRunningProvider == AiProvider.CursorAgent;
bool isCodexNative = _currentRunningProvider == AiProvider.CodexNative;
bool isQwenCode = _currentRunningProvider == AiProvider.QwenCode;
bool isOpenCode = _currentRunningProvider == AiProvider.OpenCode;
// Check if Windows Terminal is active (tab bar height > 0)
bool isWindowsTerminal = _wtTabBarHeight > 0;
if (isWindowsTerminal)
{
// For Windows Terminal, use KEYDOWN/KEYUP approach (works better with embedded window)
SendEnterKeyDownUp();
}
else if (isClaudeCodeWSL)
{
// For Claude Code (WSL), send Enter using WM_CHAR
PostMessage(terminalHandle, WM_CHAR, new IntPtr(VK_RETURN), IntPtr.Zero);
}
else if (isCodexNative)
{
// For Codex (Windows native), use KEYDOWN/KEYUP approach (Codex requires double Enter)
SendEnterKeyDownUp();
}
else if (isOtherWSLProvider)
{
// For other WSL-based providers (Codex, CursorAgent), use KEYDOWN/KEYUP approach
SendEnterKeyDownUp();
}
else if (isQwenCode)
{
// For Qwen Code, use single WM_CHAR (similar to Claude Code)
PostMessage(terminalHandle, WM_CHAR, new IntPtr(VK_RETURN), IntPtr.Zero);
}
else if (isOpenCode)
{
// For Open Code, use single WM_CHAR (similar to Claude Code)
PostMessage(terminalHandle, WM_CHAR, new IntPtr(VK_RETURN), IntPtr.Zero);
}
else
{
// For Windows-based providers (Claude Code), use single WM_CHAR
PostMessage(terminalHandle, WM_CHAR, new IntPtr(VK_RETURN), IntPtr.Zero);
}
}
}
/// <summary>
/// Sends Enter key using KEYDOWN/KEYUP messages (required for Codex and Windows Terminal)
/// For Windows Terminal, uses keybd_event for better compatibility with embedded windows
/// Sends the key twice to ensure submission
/// </summary>
private void SendEnterKeyDownUp()
{
if (terminalHandle != IntPtr.Zero && IsWindow(terminalHandle))
{
// For Windows Terminal, use keybd_event (works better with embedded windows)
if (_wtTabBarHeight > 0)
{
// First Enter attempt
keybd_event(VK_RETURN, 0, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(50);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
System.Threading.Thread.Sleep(100);
// Second Enter attempt to ensure submission
keybd_event(VK_RETURN, 0, 0, UIntPtr.Zero);
System.Threading.Thread.Sleep(50);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}
else
{
// For other providers (Codex, etc), use PostMessage
// First Enter attempt
PostMessage(terminalHandle, WM_KEYDOWN, new IntPtr(VK_RETURN), IntPtr.Zero);
System.Threading.Thread.Sleep(50);
PostMessage(terminalHandle, WM_KEYUP, new IntPtr(VK_RETURN), IntPtr.Zero);
System.Threading.Thread.Sleep(100);
// Second Enter attempt to ensure submission
PostMessage(terminalHandle, WM_KEYDOWN, new IntPtr(VK_RETURN), IntPtr.Zero);
System.Threading.Thread.Sleep(50);
PostMessage(terminalHandle, WM_KEYUP, new IntPtr(VK_RETURN), IntPtr.Zero);
}
}
}
#endregion
}
}