Skip to content

Commit c02bc4f

Browse files
authored
Add _NET_WM_PID atom to Linux X11 window (#17470)
* Add `_NET_WM_PID` atom to Linux X11 window #17444 * Also append WM_CLIENT_MACHINE * Get the host name from uname method to avoid read the file * Fix build fail on .NET Standard 2.0 * The render window is the child window. And it should not append pid atom. * Using the atom from atoms * Replace UtsName with gethostname
1 parent d3b61cd commit c02bc4f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/Avalonia.X11/X11Window.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,20 @@ public X11Window(AvaloniaX11Platform platform, IWindowImpl? popupParent, X11Wind
168168
(int)CreateWindowArgs.InputOutput,
169169
visual,
170170
new UIntPtr((uint)valueMask), ref attr);
171+
AppendPid(_handle);
171172

172173
if (_useRenderWindow)
174+
{
173175
_renderHandle = XCreateWindow(_x11.Display, _handle, 0, 0, defaultWidth, defaultHeight, 0, depth,
174176
(int)CreateWindowArgs.InputOutput,
175177
visual,
176178
new UIntPtr((uint)(SetWindowValuemask.BorderPixel | SetWindowValuemask.BitGravity |
177179
SetWindowValuemask.WinGravity | SetWindowValuemask.BackingStore)), ref attr);
180+
}
178181
else
182+
{
179183
_renderHandle = _handle;
184+
}
180185

181186
Handle = new PlatformHandle(_handle, "XID");
182187

@@ -335,6 +340,55 @@ private void UpdateMotifHints()
335340
PropertyMode.Replace, ref hints, 5);
336341
}
337342

343+
/// <summary>
344+
/// Append `_NET_WM_PID` atom to X11 window
345+
/// </summary>
346+
/// <param name="windowXId"></param>
347+
private void AppendPid(IntPtr windowXId)
348+
{
349+
// See https://github.com/AvaloniaUI/Avalonia/issues/17444
350+
var pid = (uint)s_pid;
351+
// The type of `_NET_WM_PID` is `CARDINAL` which is 32-bit unsigned integer, see https://specifications.freedesktop.org/wm-spec/1.3/ar01s05.html
352+
XChangeProperty(_x11.Display, windowXId,
353+
_x11.Atoms._NET_WM_PID, _x11.Atoms.XA_CARDINAL, 32,
354+
PropertyMode.Replace, ref pid, 1);
355+
356+
const int maxLength = 1024;
357+
var name = stackalloc byte[maxLength];
358+
var result = gethostname(name, maxLength);
359+
if (result != 0)
360+
{
361+
// Fail
362+
return;
363+
}
364+
365+
var length = 0;
366+
while (length < maxLength && name[length] != 0)
367+
{
368+
length++;
369+
}
370+
371+
XChangeProperty(_x11.Display, windowXId,
372+
_x11.Atoms.XA_WM_CLIENT_MACHINE, _x11.Atoms.XA_STRING, 8,
373+
PropertyMode.Replace, name, length);
374+
}
375+
376+
[DllImport("libc")]
377+
private static extern int gethostname(byte* name, int len);
378+
379+
private static readonly int s_pid = GetProcessId();
380+
381+
private static int GetProcessId()
382+
{
383+
#if NET6_0_OR_GREATER
384+
var pid = Environment.ProcessId;
385+
#else
386+
using var currentProcess = Process.GetCurrentProcess();
387+
var pid = currentProcess.Id;
388+
#endif
389+
return pid;
390+
}
391+
338392
private void UpdateSizeHints(PixelSize? preResize, bool forceDisableResize = false)
339393
{
340394
if (_overrideRedirect)

0 commit comments

Comments
 (0)