[英]X11 - XCB: Window information not up-to-date?
我正在使用 XCB 創建我的 x11 窗口,並且在代碼的某處,我想移動它。
我做了一個小測試來打印窗口位置 (0, 0),然后移動它,並再次打印位置 (200, 100)。
可悲的是,我總是 x:10 和 y:10。
這里的代碼:
// g++ -o test test_xcb.cpp -lX11 -lxcb
#include <xcb/xcb.h>
#include <iostream>
using namespace std;
void print_window_xywh(xcb_connection_t *conn, xcb_drawable_t win)
{
auto geo = xcb_get_geometry_reply(
conn, xcb_get_geometry(conn, win), nullptr);
cout << "Window( " << win << ") - x: " << geo->x
<< " - y: " << geo->y
<< " - w: " << geo->width
<< " - h: " << geo->height << endl;
}
int main(void) {
xcb_connection_t *c;
xcb_screen_t *screen;
xcb_window_t win;
/* Open the connection to the X server */
c = xcb_connect (NULL, NULL);
/* Get the first screen */
screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
/* Ask for our window's Id */
win = xcb_generate_id(c);
/* Create the window */
xcb_create_window (c, /* Connection */
XCB_COPY_FROM_PARENT, /* depth (same as root)*/
win, /* window Id */
screen->root, /* parent window */
10, 10, /* x, y */
150, 150, /* width, height */
10, /* border_width */
XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
screen->root_visual, /* visual */
0, NULL); /* masks, not used yet */
/* Map the window on the screen */
xcb_map_window (c, win);
/* Make sure commands are sent, so window is shown */
xcb_flush (c);
// Print the position and size of the window
print_window_xywh(c, win);
// Move the window
const static uint32_t values[] = { 200, 100 };
xcb_configure_window(c, win, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values);
// Print again, should be 200 for x and 100 for y
print_window_xywh(c, win);
return 0;
}
我錯過了什么? 謝謝你。
簡短版本:您忽略了與窗口管理器的交互。
長版:
首先, GetGeometry
給你的位置是相對於父窗口的。 使用重新父窗口管理器,WM 將在您的窗口周圍添加一個框架窗口以繪制窗口裝飾(標題欄、關閉按鈕等)。 如果您想要窗口在屏幕上的位置,您應該使用xcb_translate_coordinates(c, win, screen->root, 0, 0)
。 對此請求的回復將向您提供轉換為根窗口的窗口的0,0
位置。
但是,在您的示例中,這仍然不起作用。 這是因為窗口管理器的工作方式。 它基本上禁止您的程序移動其窗口( XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
)。 因此,當您嘗試移動窗口時,X11 服務器僅將此請求作為事件發送到窗口管理器。 然后窗口管理器需要一些時間來處理這個請求。 由於您立即再次檢查窗口位置,因此尚未處理該請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.