如何在 Gnome 的 alt-tab 视图中显示 XCB window 的名称?

How to display XCB window's name in Gnome's alt-tab view?

我有一个简单的 XCB window。当我 运行 我的应用程序和 alt-tab 时,Gnome 的 window 列表将我的应用程序显示为 "unknown"。 window 的标题栏显示正确的标题。

这是我设置标题的方式:

xcb_change_property(WindowGlobal::connection,
                    XCB_PROP_MODE_REPLACE,
                    WindowGlobal::window,
                    XCB_ATOM_WM_NAME,
                    XCB_ATOM_STRING,
                    8,
                    strlen (title),
                    title );

这是我的大部分 window 创建代码:

int visualID = screen->root_visual;

xcb_colormap_t colormap = xcb_generate_id( connection );
WindowGlobal::window = xcb_generate_id( connection );

WindowGlobal::windowWidth = width == 0 ? screen->width_in_pixels : width;
WindowGlobal::windowHeight = height == 0 ? screen->height_in_pixels : height;

xcb_create_colormap( connection, XCB_COLORMAP_ALLOC_NONE, colormap, screen->root, visualID );

const uint32_t eventmask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
                           XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION;
const uint32_t valuelist[] = { eventmask, colormap, 0 };
const uint32_t valuemask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;

xcb_create_window(
                  connection,
                  XCB_COPY_FROM_PARENT,
                  WindowGlobal::window,
                  screen->root,
                  0, 0,
                  WindowGlobal::windowWidth, WindowGlobal::windowHeight,
                  0,
                  XCB_WINDOW_CLASS_INPUT_OUTPUT,
                  visualID,
                  valuemask,
                  valuelist
                  );

xcb_map_window( connection, WindowGlobal::window );

xcb_size_hints_t hints = {};

xcb_icccm_size_hints_set_min_size( &hints, WindowGlobal::windowWidth, WindowGlobal::windowHeight );
xcb_icccm_size_hints_set_max_size( &hints, WindowGlobal::windowWidth, WindowGlobal::windowHeight );

xcb_icccm_set_wm_size_hints( WindowGlobal::connection, WindowGlobal::window, XCB_ATOM_WM_NORMAL_HINTS, &hints );

xcb_atom_t protocols[] =
{
    WindowGlobal::wm_delete_window
};
xcb_icccm_set_wm_protocols( WindowGlobal::connection, WindowGlobal::window,
                            WindowGlobal::wm_protocols, 1, protocols );

如何让 XCB 设置在 Gnome 的 window 列表中显示的文本?

xcb中右边的属性是XCB_ATOM_WM_CLASS.

我在 GitHub 上看到了一些代码,其中两个字符串与 "[=13=]" 连接在一起。 (我在 awesomeWM/awesome/xwindow.h 和 GitHub 上的其他 window 经理中看到了它)

这对我有用:

xcb_change_property(WindowGlobal::connection,
                    XCB_PROP_MODE_REPLACE,
                    WindowGlobal::window,
                    XCB_ATOM_WM_CLASS,
                    XCB_ATOM_STRING,
                    8,
                    sizeof("title""[=10=]""Title"),
                    "title[=10=]Title" );

字符串的第二部分是 gnome 中显示的内容。

这是我见过的另一种常见方式:

char *res_name = "title";
char *res_class = "Title";

char *class_hint;
size_t class_len;
class_len = strlen(res_name) + 1 + strlen(res_class) + 1;
class_hint = (char *)malloc(class_len);
strcpy(class_hint, res_name);
strcpy(class_hint + strlen(res_name) + 1, res_class);

xcb_change_property (WindowGlobal::connection,
                     XCB_PROP_MODE_REPLACE,
                     WindowGlobal::window,
                     XCB_ATOM_WM_CLASS,
                     XCB_ATOM_STRING,
                     8,
                     class_len,
                     class_hint);

free(class_hint);

char *res_class 是 gnome 中显示的内容。


对于 Xlib,您将使用函数 XSetClassHint。 在那里,您将使用由两个字符串组成的 XClassHint 结构: char *res_namechar *res_class.

https://www.x.org/releases/X11R7.7/doc/man/man3/XAllocClassHint.3.xhtml 我发现了这个:

The res_name member contains the application name, and the res_class member contains the application class. Note that the name set in this property may differ from the name set as WM_NAME. That is, WM_NAME specifies what should be displayed in the title bar and, therefore, can contain temporal information (for example, the name of a file currently in an editor’s buffer). On the other hand, the name specified as part of WM_CLASS is the formal name of the application that should be used when retrieving the application’s resources from the resource database.

我可以找到更多信息 here and here