c++ - X11:获取伪造的窗口大小和位置

标签 c++ x11

我在虚拟机中使用 Ubuntu 12.04。

左上角永远不会正确。大约 90% 的时间宽度和高度是正确的。

XMoveWindow 和 friend 对窗口的渲染位置没有影响。

来源:

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/X.h>

int main(int argc, char **argv)
{
    Display *disp = XOpenDisplay(0);

    GLint attr[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GX_DOUBLEBUFFER, None};
    XVisualInfo *vinfo = glXChooseVisual(disp,0,attr);

    Window rootWnd = DefaultRootWindow(disp);

    XSetWindowAttributes setWndAttr = {0};
    setWndAttr.colormap = XCreateColormap(disp,rootWnd,vinfo->visual,AllocNone);
    setWndAttr.event_mask =
        ExposureMask|
        StructureNotifyMask;

    Window wnd = XCreateWindow(
        disp,rootWnd,
        64,64,  // can be ignored (asinine)
        512,512,
        0,vinfo->depth,
        InputOutput,
        vinfo->visual,
        CWColormap|CWEventMask,
        &setWndAttr
        );

    XStoreName(disp,wnd,"What is this crap?");
    XMapWindow(disp,wnd);

    // WMs allowed to completely ignore these, too?
    //XMoveWindow(disp,wnd,128,128);
    //XMoveResizeWindow(disp,wnd,128,128,256,256);

    Atom closeWndAtom = XInternAtom(disp,"WM_DELETE_WINDOW",0);
    XSetWMProtocols(disp,wnd,&closeWndAtom,1);

    GLXContext ctx = glCreateContext(disp,vinfo,0,GL_TRUE);
    glXMakeCurrent(disp,wnd,ctx);

    bool run = true;
    XEvent evt;
    while(run){
        XNextEvent(disp,&evt);
        switch(evt.type){
        case Expose:
            {
                XWindowAttributes wndAttr;
                XGetWindowAttributes(disp,wnd,&wndAttr);

                // these are NEVER correct (0,0 most of the time)
                printf("%i, %i\n",wndAttr.x,wndAttr.y);

                // these are correct, most of the time
                //
                // occasionally, either width or height will be 0
                glViewport(0,0,wndAttr.width,wndAttr.height);

                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
                glBegin(GL_TRIANGLES);
                glColor3f(1,0,0);
                glVertex2f(0,0);
                glColor3f(0,1,0);
                glVertex2f(1,0);
                glColor3f(0,0,1);
                glVertex2f(0,1);
                glEnd();

                glXSwapBuffers(disp,wnd);
            }break;
        case ClientMessage:
            {
                run = false;
            }break;
        }
    }

    glXDestroyContext(disp,ctx);
    XDestroyWindow(disp,wnd);
    XCloseDisplay(disp);
    return 0;
}

注意:两个可能存在拼写错误,因为从 VM 中粘贴不会正确格式化。结果,我不得不重新输入。

编辑:

因为这里需要清楚:我不关心窗口管理器如何处理我给它的位置,我感兴趣的是可靠地检索来自窗口管理器的信息。我给出的位置与窗口在屏幕上的渲染位置不对应。例如:窗口出现在屏幕右下方,返回给我的坐标是(0,0)。使用鼠标四处移动窗口不会改变 XGetWindowAttributes 返回的内容。

最佳答案

您似乎正在从 Expose 事件中轮询窗口信息,该事件当时可能没有关于窗口的最新信息。使用 ConfigureNotify 事件及其属性来获取更新的位置和大小:

// you need to have this in your event mask(you've already got that):
EVENT_MASK |= StructureNotifyMask;

// in your event loop
// ...
case ConfigureNotify: // resize or move event
    printf("x: %d, y:%d, width: %d, height: %d\n", 
           event.xconfigure.x,
           event.xconfigure.y,
           event.xconfigure.width,
           event.xconfigure.height);
    break;

关于c++ - X11:获取伪造的窗口大小和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14374857/

相关文章:

c - 如何在使用 XCB 启动新应用程序时获取事件

c - 使用 X11 的窗口 ID

c++ - 带有参数 std::unique_ptr<T>&& 的 std::move 或 std::forward

c++ - 非原子类型如何实现std::atomic_ref?

c++ - 关于 reinterpret_cast 的问题

c++ - 相当于 X11 中的 "Invalidate Rect"/"WM_PAINT"

c++ - 在相反方向上具有相同常数的C++移位,在较小的代码更改下结果不同

c++ - VS2015 : Variadic template specialization

java - java AWT 可以在没有 X11 或 wayland 的情况下工作吗?

ssh - 停止键盘在 Pycharm 启动时变得无响应