XCB - 如何设置WM_SIZE_HINTS?

3

我正在编写一个用于在xcb中创建窗口的简约库。 我希望能够创建一个不可调整大小的窗口。我发现,可以使用以下方法向窗口管理器提供提示:

xcb_void_cookie_t xcb_change_property (xcb_connection_t *c,       /* Connection to the X server */
                                       uint8_t          mode,     /* Property mode */
                                       xcb_window_t     window,   /* Window */
                                       xcb_atom_t       property, /* Property to change */
                                       xcb_atom_t       type,     /* Type of the property */
                                       uint8_t          format,   /* Format of the property (8, 16, 32) */
                                       uint32_t         data_len, /* Length of the data parameter */
                                       const void      *data);    /* Data */

我尝试使用这个函数来更改WM_NORMAL_HINTS和WM_SIZE_HINTS,但是我应该在*data参数中放入什么数据呢?XCB_ATOM_INTEGER类型还是其他类型?


你有查找关于 WM_SIZE_HINTS 是什么/包含什么的文档吗? - Etan Reisner
我没有找到任何有用的东西。我试了几个小时。 - bakkaa
1
请返回已翻译的文本:http://cgit.freedesktop.org/xcb/util-wm/tree/icccm/icccm.c#n725和http://cgit.freedesktop.org/xcb/util-wm/tree/icccm/xcb_icccm.h#n763等。此外,还有关于“WM_SIZE_HINTS”的搜索中的*第一个链接 http://tronche.com/gui/x/xlib/ICC/client-to-window-manager/wm-normal-hints.html。 - Etan Reisner
现在它可以工作了。非常感谢你。你是怎么发现这个问题的? - bakkaa
1
过去我使用了tronche.com的链接,我几乎就停在那里了,但我觉得这里也一定有关于xcb的特定文档。因此,我搜索了“WM_SIZE_HINTS”和xcb。当没有找到任何超级有用的东西时,我开始寻找xcb文档(这是一个损坏的链接),然后干脆直接去源码看了。 - Etan Reisner
2个回答

10
以下是解决方案:

这里是解决方案:

#include <xcb/xcb.h>
#include <xcb/xcb_icccm.h>

#define WIDTH  900
#define HEIGHT 600

int main(){
    //...
    //Connect to X Server and
    //Create a window
    //...

    xcb_size_hints_t hints;

    xcb_icccm_size_hints_set_min_size(&hints, WIDTH, HEIGHT);
    xcb_icccm_size_hints_set_max_size(&hints, WIDTH, HEIGHT);

    xcb_icccm_set_wm_size_hints(connection, window, XCB_ATOM_WM_NORMAL_HINTS, &hints);
    return 0;
}

非常感谢您的分享! - Noitidart

10

如果你不想依赖于 xcb_icccm ,你可以直接更改该属性。

struct WMSizeHints
{
  uint32_t flags;
  int32_t  x, y;
  int32_t  width, height;
  int32_t  min_width, min_height;
  int32_t  max_width, max_height;
  int32_t  width_inc, height_inc;
  int32_t  min_aspect_num, min_aspect_den;
  int32_t  max_aspect_num, max_aspect_den;
  int32_t  base_width, base_height;
  uint32_t win_gravity;
};

enum WMSizeHintsFlag
{
  WM_SIZE_HINT_US_POSITION   = 1U << 0,
  WM_SIZE_HINT_US_SIZE       = 1U << 1,
  WM_SIZE_HINT_P_POSITION    = 1U << 2,
  WM_SIZE_HINT_P_SIZE        = 1U << 3,
  WM_SIZE_HINT_P_MIN_SIZE    = 1U << 4,
  WM_SIZE_HINT_P_MAX_SIZE    = 1U << 5,
  WM_SIZE_HINT_P_RESIZE_INC  = 1U << 6,
  WM_SIZE_HINT_P_ASPECT      = 1U << 7,
  WM_SIZE_HINT_BASE_SIZE     = 1U << 8,
  WM_SIZE_HINT_P_WIN_GRAVITY = 1U << 9
};

struct WMSizeHints hints =
{
  .flags       = WM_SIZE_HINT_P_WIN_GRAVITY,
  .win_gravity = XCB_GRAVITY_STATIC
};

if (centerWindow)
  hints.win_gravity = XCB_GRAVITY_CENTER;
else
{
  hints.flags |= WM_SIZE_HINT_P_SIZE;
  hints.x = x;
  hints.y = y;
}

xcb_change_property(xcb, XCB_PROP_MODE_REPLACE, window,
  XCB_ATOM_WM_NORMAL_HINTS, XCB_ATOM_WM_SIZE_HINTS,
    32, sizeof(struct WMSizeHints) >> 2, &hints);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接