123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using System.Runtime.InteropServices;
- using Unix.Terminal;
- namespace Terminal.Gui;
- /// <summary>A clipboard implementation for Linux. This implementation uses the xclip command to access the clipboard.</summary>
- /// <remarks>If xclip is not installed, this implementation will not work.</remarks>
- internal class CursesClipboard : ClipboardBase
- {
- private string _xclipPath = string.Empty;
- public CursesClipboard () { IsSupported = CheckSupport (); }
- public override bool IsSupported { get; }
- protected override string GetClipboardDataImpl ()
- {
- string tempFileName = Path.GetTempFileName ();
- var xclipargs = "-selection clipboard -o";
- try
- {
- (int exitCode, string result) =
- ClipboardProcessRunner.Bash ($"{_xclipPath} {xclipargs} > {tempFileName}", waitForOutput: false);
- if (exitCode == 0)
- {
- if (Application.Driver is CursesDriver)
- {
- Curses.raw ();
- Curses.noecho ();
- }
- return File.ReadAllText (tempFileName);
- }
- }
- catch (Exception e)
- {
- throw new NotSupportedException ($"\"{_xclipPath} {xclipargs}\" failed.", e);
- }
- finally
- {
- File.Delete (tempFileName);
- }
- return string.Empty;
- }
- protected override void SetClipboardDataImpl (string text)
- {
- var xclipargs = "-selection clipboard -i";
- try
- {
- (int exitCode, _) = ClipboardProcessRunner.Bash ($"{_xclipPath} {xclipargs}", text);
- if (exitCode == 0 && Application.Driver is CursesDriver)
- {
- Curses.raw ();
- Curses.noecho ();
- }
- }
- catch (Exception e)
- {
- throw new NotSupportedException ($"\"{_xclipPath} {xclipargs} < {text}\" failed", e);
- }
- }
- private bool CheckSupport ()
- {
- #pragma warning disable RCS1075 // Avoid empty catch clause that catches System.Exception.
- try
- {
- (int exitCode, string result) = ClipboardProcessRunner.Bash ("which xclip", waitForOutput: true);
- if (exitCode == 0 && result.FileExists ())
- {
- _xclipPath = result;
- return true;
- }
- }
- catch (Exception)
- {
- // Permissions issue.
- }
- #pragma warning restore RCS1075 // Avoid empty catch clause that catches System.Exception.
- return false;
- }
- }
- /// <summary>
- /// A clipboard implementation for MacOSX. This implementation uses the Mac clipboard API (via P/Invoke) to
- /// copy/paste. The existance of the Mac pbcopy and pbpaste commands is used to determine if copy/paste is supported.
- /// </summary>
- internal class MacOSXClipboard : ClipboardBase
- {
- private readonly nint _allocRegister = sel_registerName ("alloc");
- private readonly nint _clearContentsRegister = sel_registerName ("clearContents");
- private readonly nint _generalPasteboard;
- private readonly nint _generalPasteboardRegister = sel_registerName ("generalPasteboard");
- private readonly nint _initWithUtf8Register = sel_registerName ("initWithUTF8String:");
- private readonly nint _nsPasteboard = objc_getClass ("NSPasteboard");
- private readonly nint _nsString = objc_getClass ("NSString");
- private readonly nint _nsStringPboardType;
- private readonly nint _setStringRegister = sel_registerName ("setString:forType:");
- private readonly nint _stringForTypeRegister = sel_registerName ("stringForType:");
- private readonly nint _utf8Register = sel_registerName ("UTF8String");
- private readonly nint _utfTextType;
- public MacOSXClipboard ()
- {
- _utfTextType = objc_msgSend (
- objc_msgSend (_nsString, _allocRegister),
- _initWithUtf8Register,
- "public.utf8-plain-text"
- );
- _nsStringPboardType = objc_msgSend (
- objc_msgSend (_nsString, _allocRegister),
- _initWithUtf8Register,
- "NSStringPboardType"
- );
- _generalPasteboard = objc_msgSend (_nsPasteboard, _generalPasteboardRegister);
- IsSupported = CheckSupport ();
- }
- public override bool IsSupported { get; }
- protected override string GetClipboardDataImpl ()
- {
- nint ptr = objc_msgSend (_generalPasteboard, _stringForTypeRegister, _nsStringPboardType);
- nint charArray = objc_msgSend (ptr, _utf8Register);
- return Marshal.PtrToStringAnsi (charArray);
- }
- protected override void SetClipboardDataImpl (string text)
- {
- nint str = default;
- try
- {
- str = objc_msgSend (objc_msgSend (_nsString, _allocRegister), _initWithUtf8Register, text);
- objc_msgSend (_generalPasteboard, _clearContentsRegister);
- objc_msgSend (_generalPasteboard, _setStringRegister, str, _utfTextType);
- }
- finally
- {
- if (str != default (nint))
- {
- objc_msgSend (str, sel_registerName ("release"));
- }
- }
- }
- private bool CheckSupport ()
- {
- (int exitCode, string result) = ClipboardProcessRunner.Bash ("which pbcopy", waitForOutput: true);
- if (exitCode != 0 || !result.FileExists ())
- {
- return false;
- }
- (exitCode, result) = ClipboardProcessRunner.Bash ("which pbpaste", waitForOutput: true);
- return exitCode == 0 && result.FileExists ();
- }
- [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
- private static extern nint objc_getClass (string className);
- [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
- private static extern nint objc_msgSend (nint receiver, nint selector);
- [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
- private static extern nint objc_msgSend (nint receiver, nint selector, string arg1);
- [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
- private static extern nint objc_msgSend (nint receiver, nint selector, nint arg1);
- [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
- private static extern nint objc_msgSend (nint receiver, nint selector, nint arg1, nint arg2);
- [DllImport ("/System/Library/Frameworks/AppKit.framework/AppKit")]
- private static extern nint sel_registerName (string selectorName);
- }
- /// <summary>
- /// A clipboard implementation for Linux, when running under WSL. This implementation uses the Windows clipboard
- /// to store the data, and uses Windows' powershell.exe (launched via WSL interop services) to set/get the Windows
- /// clipboard.
- /// </summary>
- internal class WSLClipboard : ClipboardBase
- {
- private static string _powershellPath = string.Empty;
- public override bool IsSupported => CheckSupport ();
- protected override string GetClipboardDataImpl ()
- {
- if (!IsSupported)
- {
- return string.Empty;
- }
- (int exitCode, string output) =
- ClipboardProcessRunner.Process (_powershellPath, "-noprofile -command \"Get-Clipboard\"");
- if (exitCode == 0)
- {
- if (Application.Driver is CursesDriver)
- {
- Curses.raw ();
- Curses.noecho ();
- }
- if (output.EndsWith ("\r\n"))
- {
- output = output.Substring (0, output.Length - 2);
- }
- return output;
- }
- return string.Empty;
- }
- protected override void SetClipboardDataImpl (string text)
- {
- if (!IsSupported)
- {
- return;
- }
- (int exitCode, string output) = ClipboardProcessRunner.Process (
- _powershellPath,
- $"-noprofile -command \"Set-Clipboard -Value \\\"{text}\\\"\""
- );
- if (exitCode == 0)
- {
- if (Application.Driver is CursesDriver)
- {
- Curses.raw ();
- Curses.noecho ();
- }
- }
- }
- private bool CheckSupport ()
- {
- if (string.IsNullOrEmpty (_powershellPath))
- {
- // Specify pwsh.exe (not pwsh) to ensure we get the Windows version (invoked via WSL)
- (int exitCode, string result) = ClipboardProcessRunner.Bash ("which pwsh.exe", waitForOutput: true);
- if (exitCode > 0)
- {
- (exitCode, result) = ClipboardProcessRunner.Bash ("which powershell.exe", waitForOutput: true);
- }
- if (exitCode == 0)
- {
- _powershellPath = result;
- }
- }
- return !string.IsNullOrEmpty (_powershellPath);
- }
- }
|