简体   繁体   English

XCB - 如何设置 WM_SIZE_HINTS?

[英]XCB - How to set WM_SIZE_HINTS?

I'm writing a minimalistic library for window creation in xcb.我正在编写一个用于在 xcb 中创建窗口的简约库。 I want to be able to create a non-resizeable window.我希望能够创建一个不可调整大小的窗口。 I found out, that it is possible to give hints to the window manager with:我发现,可以通过以下方式向窗口管理器提供提示:

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 */

I tried to change the WM_NORMAL_HINTS and WM_SIZE_HINTS with this function, but how do I know what data I have to put in the *data parameter?我试图用这个函数更改 WM_NORMAL_HINTS 和 WM_SIZE_HINTS,但我怎么知道我必须在 *data 参数中放入哪些数据? Is the type XCB_ATOM_INTEGER or something else?类型是 XCB_ATOM_INTEGER 还是其他类型?

Here is the solution:这是解决方案:

#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;
}

If you want to do this without depending on xcb_icccm you can directly change the property.如果您想在不依赖xcb_icccm情况下执行此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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM