Skip to content

Commit

Permalink
Merge pull request #8597 from AvaloniaUI/feature/x11-wmclass-spec
Browse files Browse the repository at this point in the history
Set WM_CLASS property according to ICCCM spec
  • Loading branch information
kekekeks authored and grokys committed Aug 3, 2022
1 parent d2f8fe6 commit 1628b20
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Avalonia.X11/X11Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ public class X11PlatformOptions
// and sometimes attempts to use GLX might cause a segfault
"llvmpipe"
};
public string WmClass { get; set; } = Assembly.GetEntryAssembly()?.GetName()?.Name ?? "AvaloniaApplication";

public string WmClass { get; set; } = Assembly.GetEntryAssembly()?.GetName()?.Name;

/// <summary>
/// Enables multitouch support. The default value is true.
Expand Down
23 changes: 17 additions & 6 deletions src/Avalonia.X11/X11Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ public X11Window(AvaloniaX11Platform platform, IWindowImpl popupParent)
XChangeProperty(_x11.Display, _handle, _x11.Atoms._NET_WM_WINDOW_TYPE, _x11.Atoms.XA_ATOM,
32, PropertyMode.Replace, new[] {_x11.Atoms._NET_WM_WINDOW_TYPE_NORMAL}, 1);

if (platform.Options.WmClass != null)
SetWmClass(platform.Options.WmClass);
SetWmClass(_platform.Options.WmClass);

var surfaces = new List<object>
{
Expand Down Expand Up @@ -1068,12 +1067,24 @@ public void SetTitle(string title)

public void SetWmClass(string wmClass)
{
var data = Encoding.ASCII.GetBytes(wmClass);
fixed (void* pdata = data)
// See https://tronche.com/gui/x/icccm/sec-4.html#WM_CLASS
// We don't actually parse the application's command line, so we only use RESOURCE_NAME and argv[0]
var appId = Environment.GetEnvironmentVariable("RESOURCE_NAME")
?? Process.GetCurrentProcess().ProcessName;

var encodedAppId = Encoding.ASCII.GetBytes(appId);
var encodedWmClass = Encoding.ASCII.GetBytes(wmClass ?? appId);

var hint = XAllocClassHint();
fixed(byte* pAppId = encodedAppId)
fixed (byte* pWmClass = encodedWmClass)
{
XChangeProperty(_x11.Display, _handle, _x11.Atoms.XA_WM_CLASS, _x11.Atoms.XA_STRING, 8,
PropertyMode.Replace, pdata, data.Length);
hint->res_name = pAppId;
hint->res_class = pWmClass;
XSetClassHint(_x11.Display, _handle, hint);
}

XFree(hint);
}

public void SetMinMaxSize(Size minSize, Size maxSize)
Expand Down
14 changes: 14 additions & 0 deletions src/Avalonia.X11/XLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public static extern int XQueryTree(IntPtr display, IntPtr window, out IntPtr ro

[DllImport(libX11)]
public static extern int XFree(IntPtr data);

[DllImport(libX11)]
public static extern int XFree(void* data);

[DllImport(libX11)]
public static extern int XRaiseWindow(IntPtr display, IntPtr window);
Expand Down Expand Up @@ -628,6 +631,12 @@ public static Status XiSelectEvents(IntPtr display, IntPtr window, Dictionary<in
return XISelectEvents(display, window, emasks, devices.Count);

}

[DllImport(libX11)]
public static extern XClassHint* XAllocClassHint();

[DllImport(libX11)]
public static extern int XSetClassHint(IntPtr display, IntPtr window, XClassHint* class_hints);

public struct XGeometry
{
Expand All @@ -639,6 +648,11 @@ public struct XGeometry
public int bw;
public int d;
}
public struct XClassHint
{
public byte* res_name;
public byte* res_class;
}

public struct XSyncValue {
public int Hi;
Expand Down

0 comments on commit 1628b20

Please sign in to comment.