From 4626e70f90b76a2e99b4f5a5e5f61edbe1afc3bf Mon Sep 17 00:00:00 2001 From: lindexi Date: Mon, 11 Nov 2024 10:19:21 +0800 Subject: [PATCH] Add `_NET_WM_PID` atom to Linux X11 window https://github.com/AvaloniaUI/Avalonia/issues/17444 --- src/Avalonia.X11/X11Window.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index 0a254b6b2b3..2932d7742c6 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -151,15 +151,21 @@ public X11Window(AvaloniaX11Platform platform, IWindowImpl? popupParent, bool ov (int)CreateWindowArgs.InputOutput, visual, new UIntPtr((uint)valueMask), ref attr); + AppendPid(_handle); if (_useRenderWindow) + { _renderHandle = XCreateWindow(_x11.Display, _handle, 0, 0, defaultWidth, defaultHeight, 0, depth, (int)CreateWindowArgs.InputOutput, visual, new UIntPtr((uint)(SetWindowValuemask.BorderPixel | SetWindowValuemask.BitGravity | SetWindowValuemask.WinGravity | SetWindowValuemask.BackingStore)), ref attr); + AppendPid(_renderHandle); + } else + { _renderHandle = _handle; + } Handle = new PlatformHandle(_handle, "XID"); @@ -323,6 +329,25 @@ private void UpdateMotifHints() PropertyMode.Replace, ref hints, 5); } + /// + /// Append `_NET_WM_PID` atom to X11 window + /// + /// + private void AppendPid(IntPtr windowXId) + { + // See https://github.com/AvaloniaUI/Avalonia/issues/17444 +#if NET6_0_OR_GREATER + var pid = (uint) Environment.ProcessId; +#else + using var currentProcess = Process.GetCurrentProcess(); + var pid = (uint) currentProcess.Id; +#endif + // 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 + XChangeProperty(_x11.Display, windowXId, + _x11.Atoms._NET_WM_PID, _x11.Atoms.XA_CARDINAL, 32, + PropertyMode.Replace, ref pid, 1); + } + private void UpdateSizeHints(PixelSize? preResize, bool forceDisableResize = false) { if (_overrideRedirect)