Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/Myra.Samples.NonModalWindows/UI/MainPanel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Generated by MyraPad at 04.12.2019 21:54:46 */
using Microsoft.Xna.Framework;
using Myra.Graphics2D.UI;

namespace Myra.Samples.NonModalWindows.UI
{
Expand Down
10 changes: 8 additions & 2 deletions src/Myra/TextCopy/Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ static Action<string> CreateSet()

if (CurrentPlatform.OS == OS.Linux)
{
return LinuxClipboard.SetText;
if(LinuxClipboard.SystemIsCompatible())
return LinuxClipboard.SetText;
Console.WriteLine(LinuxClipboard.GetCompatibilityProblem());
UseLocalClipboard = true;
}

return s => throw new NotSupportedException();
Expand All @@ -71,7 +74,10 @@ static Func<string> CreateGet()

if (CurrentPlatform.OS == OS.Linux)
{
return LinuxClipboard.GetText;
if(LinuxClipboard.SystemIsCompatible())
return LinuxClipboard.GetText;
Console.WriteLine(LinuxClipboard.GetCompatibilityProblem());
UseLocalClipboard = true;
}

return () => throw new NotSupportedException();
Expand Down
77 changes: 74 additions & 3 deletions src/Myra/TextCopy/LinuxClipboard.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,98 @@
using System;
using System.IO;

static class LinuxClipboard
{
private enum ClipCmd
{
Incompatible,
XClip,
XSel,
}
private static ClipCmd Cmd = ClipCmd.Incompatible;

public static string GetCompatibilityProblem()
{
return @"Myra: At least one package needs to be installed on the system: 'xclip', 'xsel'. Clipboard function will be limited.";
}

/// <summary>
/// Return true if 'xclip' or 'xsel' is installed on the system.
/// </summary>
public static bool SystemIsCompatible()
{
ClipCmd clipCmd = ClipCmd.Incompatible;

if(TestBashCommandExists("xsel --version")) //dash differences are intentional
{
clipCmd = ClipCmd.XSel;
}
else if (TestBashCommandExists("xclip -version"))
{
clipCmd = ClipCmd.XClip;
}

Cmd = clipCmd;
return Cmd != ClipCmd.Incompatible;
}
private static bool TestBashCommandExists(string cmd)
{
string bashMsg;
try
{
bashMsg = BashRunner.Run(cmd);
}
catch (Exception e)
{
bashMsg = e.Message;
}
return !bashMsg.Contains("Could not execute process.");
}

public static void SetText(string text)
{
var tempFileName = Path.GetTempFileName();
File.WriteAllText(tempFileName, text);
try
{
BashRunner.Run($"cat {tempFileName} | xclip");
switch (Cmd)
{
case ClipCmd.XClip:
BashRunner.Run($"cat {tempFileName} | xclip -sel clip");
break;
case ClipCmd.XSel:
BashRunner.Run($"cat {tempFileName} | xsel -ib");
break;
case ClipCmd.Incompatible:
throw new SystemException(GetCompatibilityProblem());
default:
throw new NotImplementedException(Cmd.ToString());
}
}
finally
{
File.Delete(tempFileName);
}
}

public static string GetText()
{
var tempFileName = Path.GetTempFileName();
try
{
BashRunner.Run($"xclip -o > {tempFileName}");
switch (Cmd)
{
case ClipCmd.XClip:
BashRunner.Run($"xclip -o -sel clip > {tempFileName}");
break;
case ClipCmd.XSel:
BashRunner.Run($"xsel -ob > {tempFileName}");
break;
case ClipCmd.Incompatible:
throw new SystemException(GetCompatibilityProblem());
default:
throw new NotImplementedException(Cmd.ToString());
}
return File.ReadAllText(tempFileName);
}
finally
Expand Down
Loading