Skip to content

Commit 527c5a9

Browse files
authored
Merge pull request #493 from Bamboy/master
Improved Clipboard on Linux
2 parents 7dc2bac + ce0fba8 commit 527c5a9

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

samples/Myra.Samples.NonModalWindows/UI/MainPanel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* Generated by MyraPad at 04.12.2019 21:54:46 */
22
using Microsoft.Xna.Framework;
3+
using Myra.Graphics2D.UI;
34

45
namespace Myra.Samples.NonModalWindows.UI
56
{

src/Myra/TextCopy/Clipboard.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ static Action<string> CreateSet()
5151

5252
if (CurrentPlatform.OS == OS.Linux)
5353
{
54-
return LinuxClipboard.SetText;
54+
if(LinuxClipboard.SystemIsCompatible())
55+
return LinuxClipboard.SetText;
56+
Console.WriteLine(LinuxClipboard.GetCompatibilityProblem());
57+
UseLocalClipboard = true;
5558
}
5659

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

7275
if (CurrentPlatform.OS == OS.Linux)
7376
{
74-
return LinuxClipboard.GetText;
77+
if(LinuxClipboard.SystemIsCompatible())
78+
return LinuxClipboard.GetText;
79+
Console.WriteLine(LinuxClipboard.GetCompatibilityProblem());
80+
UseLocalClipboard = true;
7581
}
7682

7783
return () => throw new NotSupportedException();

src/Myra/TextCopy/LinuxClipboard.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,98 @@
1+
using System;
12
using System.IO;
23

34
static class LinuxClipboard
45
{
6+
private enum ClipCmd
7+
{
8+
Incompatible,
9+
XClip,
10+
XSel,
11+
}
12+
private static ClipCmd Cmd = ClipCmd.Incompatible;
13+
14+
public static string GetCompatibilityProblem()
15+
{
16+
return @"Myra: At least one package needs to be installed on the system: 'xclip', 'xsel'. Clipboard function will be limited.";
17+
}
18+
19+
/// <summary>
20+
/// Return true if 'xclip' or 'xsel' is installed on the system.
21+
/// </summary>
22+
public static bool SystemIsCompatible()
23+
{
24+
ClipCmd clipCmd = ClipCmd.Incompatible;
25+
26+
if(TestBashCommandExists("xsel --version")) //dash differences are intentional
27+
{
28+
clipCmd = ClipCmd.XSel;
29+
}
30+
else if (TestBashCommandExists("xclip -version"))
31+
{
32+
clipCmd = ClipCmd.XClip;
33+
}
34+
35+
Cmd = clipCmd;
36+
return Cmd != ClipCmd.Incompatible;
37+
}
38+
private static bool TestBashCommandExists(string cmd)
39+
{
40+
string bashMsg;
41+
try
42+
{
43+
bashMsg = BashRunner.Run(cmd);
44+
}
45+
catch (Exception e)
46+
{
47+
bashMsg = e.Message;
48+
}
49+
return !bashMsg.Contains("Could not execute process.");
50+
}
51+
552
public static void SetText(string text)
653
{
754
var tempFileName = Path.GetTempFileName();
855
File.WriteAllText(tempFileName, text);
956
try
1057
{
11-
BashRunner.Run($"cat {tempFileName} | xclip");
58+
switch (Cmd)
59+
{
60+
case ClipCmd.XClip:
61+
BashRunner.Run($"cat {tempFileName} | xclip -sel clip");
62+
break;
63+
case ClipCmd.XSel:
64+
BashRunner.Run($"cat {tempFileName} | xsel -ib");
65+
break;
66+
case ClipCmd.Incompatible:
67+
throw new SystemException(GetCompatibilityProblem());
68+
default:
69+
throw new NotImplementedException(Cmd.ToString());
70+
}
1271
}
1372
finally
1473
{
1574
File.Delete(tempFileName);
1675
}
1776
}
18-
77+
1978
public static string GetText()
2079
{
2180
var tempFileName = Path.GetTempFileName();
2281
try
2382
{
24-
BashRunner.Run($"xclip -o > {tempFileName}");
83+
switch (Cmd)
84+
{
85+
case ClipCmd.XClip:
86+
BashRunner.Run($"xclip -o -sel clip > {tempFileName}");
87+
break;
88+
case ClipCmd.XSel:
89+
BashRunner.Run($"xsel -ob > {tempFileName}");
90+
break;
91+
case ClipCmd.Incompatible:
92+
throw new SystemException(GetCompatibilityProblem());
93+
default:
94+
throw new NotImplementedException(Cmd.ToString());
95+
}
2596
return File.ReadAllText(tempFileName);
2697
}
2798
finally

0 commit comments

Comments
 (0)