forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX11NativeElementHostingExtension.helper.cs
194 lines (164 loc) · 5.22 KB
/
X11NativeElementHostingExtension.helper.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.UI.Xaml.Controls;
using Uno.Foundation.Logging;
using Uno.UI.Runtime.Skia;
namespace Uno.WinUI.Runtime.Skia.X11;
// https://www.x.org/releases/X11R7.6/doc/xextproto/shape.html
// Thanks to Jörg Seebohn for providing an example on how to use X SHAPE
// https://gist.github.com/je-so/903479/834dfd78705b16ec5f7bbd10925980ace4049e17
internal partial class X11NativeElementHostingExtension : ContentPresenter.INativeElementHostingExtension
{
private const string SampleVideoLink = "https://uno-assets.platform.uno/tests/uno/big_buck_bunny_720p_5mb.mp4";
/// replace the executable and the args with whatever you have locally. This is only used
/// for internal debugging. However, make sure that you can set a unique title to the window,
/// so that you can then look it up.
public object? CreateSampleComponent(string text)
{
if (XamlRoot is { } xamlRoot && X11Manager.XamlRootMap.GetHostForRoot(xamlRoot) is X11XamlRootHost host)
{
if (!Exists("mpv") && !Exists("vlc") && !Exists("xterm"))
{
return null;
}
// xterm is by far the most reliable since it opens up quickly. X11 doesn't have a nice way of
// waiting for a specific window to be present, so we make do with a Thread.Sleep
var filename = Exists("xterm") ? "xterm" : (Exists("vlc") ? "vlc" : "mpv");
if (this.Log().IsEnabled(LogLevel.Information))
{
this.Log().Info($"Using {filename} as the X11 native application for {nameof(CreateSampleComponent)}.");
}
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = filename,
UseShellExecute = false
}
};
string title;
if (filename == "vlc")
{
title = $"Sample Video {Random.Shared.Next()} {text}"; // used to maintain unique titles
process.StartInfo.ArgumentList.Add(SampleVideoLink);
process.StartInfo.ArgumentList.Add("--meta-title");
process.StartInfo.ArgumentList.Add(title);
title += " - VLC media player";
}
else if (filename == "mpv")
{
title = $"Sample Video {Random.Shared.Next()} {text}"; // used to maintain unique titles
process.StartInfo.ArgumentList.Add("--keep-open=always");
process.StartInfo.ArgumentList.Add($"--title={title}");
process.StartInfo.ArgumentList.Add(SampleVideoLink);
}
else if (filename == "alacritty")
{
title = $"Sample terminal {Random.Shared.Next()} {text}"; // used to maintain unique titles
process.StartInfo.ArgumentList.Add("--title");
process.StartInfo.ArgumentList.Add(title);
}
else if (filename == "xterm")
{
title = $"Sample terminal {Random.Shared.Next()} {text}"; // used to maintain unique titles
process.StartInfo.ArgumentList.Add("-iconic");
process.StartInfo.ArgumentList.Add("-xrm");
process.StartInfo.ArgumentList.Add("XTerm.vt100.allowTitleOps: false");
process.StartInfo.ArgumentList.Add("-T");
process.StartInfo.ArgumentList.Add(title);
process.StartInfo.ArgumentList.Add("-e");
process.StartInfo.ArgumentList.Add("top");
}
else
{
return null;
}
process.Start();
using var lockDiposable = X11Helper.XLock(Display);
_ = XLib.XQueryTree(Display, host.RootX11Window.Window, out IntPtr root, out _, out var children, out _);
_ = XLib.XFree(children);
// Wait for the window to open.
Thread.Sleep(500);
IntPtr window = FindWindowByTitle(Display, root, title);
if (window == IntPtr.Zero)
{
process.Kill();
return null;
}
return new X11NativeWindow(window);
}
// For debugging: replace the above with a hardcoded window id, obtainable using e.g. wmctrl
// if (owner is XamlRoot xamlRoot
// && X11Manager.XamlRootMap.GetHostForRoot(xamlRoot) is X11XamlRootHost host)
// {
// return new X11Window(host.X11Window.Display, 0x04a00002);
// }
return null;
}
private static bool Exists(string fileName)
{
if (File.Exists(fileName))
{
return true;
}
var values = Environment.GetEnvironmentVariable("PATH");
if (values is null)
{
return false;
}
return values
.Split(Path.PathSeparator)
.Select(path => Path.Combine(path, fileName))
.Any(File.Exists);
}
private unsafe static IntPtr FindWindowByTitle(IntPtr display, IntPtr current, string title)
{
_ = X11Helper.XFetchName(display, current, out var name);
if (name == title)
{
return current;
}
_ = XLib.XQueryTree(display,
current,
out _,
out _,
out IntPtr children,
out int nChildren);
var span = new Span<IntPtr>(children.ToPointer(), nChildren);
for (var i = 0; i < nChildren; ++i)
{
IntPtr window = FindWindowByTitle(display, span[i], title);
if (window != IntPtr.Zero)
{
return window;
}
}
return IntPtr.Zero;
}
private unsafe static IntPtr FindWindowById(IntPtr display, IntPtr current, IntPtr id)
{
if (current == id)
{
return current;
}
_ = XLib.XQueryTree(display,
current,
out _,
out _,
out IntPtr children,
out int nChildren);
var span = new Span<IntPtr>(children.ToPointer(), nChildren);
for (var i = 0; i < nChildren; ++i)
{
IntPtr window = FindWindowById(display, span[i], id);
if (window != IntPtr.Zero)
{
return window;
}
}
return IntPtr.Zero;
}
}