创建显示后,我使用XCreateWindow创建一个窗口.然后,如
here所述,我调用XMapWindow然后立即调用XUnmapWindow;这让X服务器知道窗口,以便命令(例如XMoveWindow)不会无声地失败.
此时窗口是不可见的.我可以用例如停止执行getchar函数.绝对是看不见的.
然后我调用glXCreateContext,窗口出现,就像我再次调用XMapWindow一样!巫术!我已经在之前和之后立即停止执行,所以我知道它是glXCreateContext.
这毫无意义.我浏览了文档,但实际上没有办法可能发生这种情况.任何猜测?
编辑:这是一个简单的例子:
//Compile with "g++ <filename>.cpp -std=c++11 -lX11 -lGL"
#include <cassert>
#include <cstdio>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <GL/glx.h>
static Display* display;
static void _x11_map_window(Window window) {
printf("Mapping window %lu; RETURN to continue\n",window); getchar();
XMapWindow(display, window);
printf("Mapped window! RETURN to continue\n"); getchar();
}
static void _x11_unmap_window(Window window) {
printf("Unmapping window %lu; RETURN to continue\n",window); getchar();
XUnmapWindow(display, window);
printf("Unmapped window! RETURN to continue\n"); getchar();
}
int main(int argc, char* argv[]) {
/* ##### MAKE DISPLAY ##### */
display = XOpenDisplay(nullptr);
/* ##### MAKE VISUAL INFO. ##### */
int attributes[] = { //can't be const b/c X11 doesn't like it. Not sure if that's intentional or just stupid.
GLX_RGBA, //apparently nothing comes after this?
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
//Ideally, the size would be 32 (or at least 24), but I have actually seen
// this size (on a modern OS even).
GLX_DEPTH_SIZE, 16,
GLX_DOUBLEBUFFER, True,
None
};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast" //Because of X11's cruft in "DefaultScreen".
XVisualInfo* visual_info = glXChooseVisual(display, DefaultScreen(display), attributes);
#pragma GCC diagnostic pop
assert(visual_info!=nullptr);
/* ##### MAKE WINDOW ##### */
Window parent = XDefaultRootWindow(display);
Colormap colormap = XCreateColormap(display, parent, visual_info->visual, AllocNone);
XSetWindowAttributes window_attributes_set;
window_attributes_set.colormap = colormap;
window_attributes_set.background_pixel = 0; //This and next b/c of https://stackoverflow.com/questions/3645632/how-to-create-a-window-with-a-bit-depth-of-32
window_attributes_set.border_pixel = 0; //especially resulting in BadMatch error on Raspberry Pi. Also changes bit fields below in XCreateWindow.
window_attributes_set.event_mask = ExposureMask | KeyPressMask;
int position[2]={50,50}, dimensions[2]={128,128};
Window window = XCreateWindow(
display, parent,
position[0],position[1], static_cast<unsigned int>(dimensions[0]),static_cast<unsigned int>(dimensions[1]), //Note: the documentation must be wrong; this thing wants unsigned ints.
0u,
visual_info->depth,
InputOutput,
visual_info->visual,
//CWColormap|CWEventMask,
CWBackPixel|CWColormap|CWBorderPixel | CWEventMask,
&window_attributes_set
);
assert(window!=0);
printf("Created window %lu\n",window);
XStoreName(display, window, "[default title]");
XSelectInput(display, window,
//http://www.tronche.com/gui/x/xlib/events/mask.html#NoEventMask
//http://www.tronche.com/gui/x/xlib/events/processing-overview.html
ExposureMask |
KeyPressMask | KeyReleaseMask |
ButtonPressMask | ButtonReleaseMask | //ButtonMotionMask |
//EnterWindowMask | LeaveWindowMask |
PointerMotionMask |
//KeymapStateMask | FocusChangeMask | ColormapChangeMask |
StructureNotifyMask //Resizing, etc.
//PropertyChangeMask
);
Atom wm_delete = XInternAtom(display, "WM_DELETE_WINDOW", True);
XSetWMProtocols(display, window, &wm_delete, 1);
XMoveWindow(display, window, 100,100);
//As described here: https://stackoverflow.com/questions/14801536/xmovewindow-not-working-before-xmapwindow
// "the X server doesn't have to know about a window before it is mapped for the first time". Hence,
// map the window and then unmap it so that the X server knows about it. This is important because some
// functions silently fail (e.g. XMoveWindow) when the X server is oblivious.
_x11_map_window(window);
_x11_unmap_window(window);
/* ##### MAKE RENDER CONTEXT ##### */
GLXContext render_context = glXCreateContext(display, visual_info, nullptr, True);
assert(render_context!=nullptr);
/* ##### MORE STUFF WOULD GO HERE ##### */
while (1);
return 0;
}
还演示了XCreateWindow或XMoveWindow在map / unmap之前设置窗口位置的失败.
最佳答案 调查这一点很困难,但我已经解决了它和TL; DR是:
>这实际上并不是关于glXCreateContext(…).这是关于X的某些实现中的明显时间错误.
>我在错误信息的基础上撰写的解决方法暴露了这个问题.我想要做的事情应该通过不同的解决方法来完成.
相关问题的描述
创建窗口时,只要未设置覆盖重定向,窗口管理器就会将其包装在新窗口中(属性.override_redirect和标志CWOverrideRedirect on
窗口创建).这样它可以做一些事情,比如添加一个框架和按钮.
不幸的是,窗口管理器可以(并且至少在窗口映射之前)使用它作为忽略XMoveWindow(…)等行为的借口.这导致the misconception应该是map and then unmap the window,以便X服务器“了解它”.
这暴露了明显的错误.在有问题的系统(VirtualBox中的库存Ubuntu)中,映射然后立即取消映射窗口会导致窗口保持映射状态.
我尝试了很多东西,例如在map / unmap调用周围放置XFlush(…)或XSync(…)调用(这也让我可以证明glXCreateContext(…)没有问题).然而,最终让它按预期工作的是添加睡眠.延迟0.1秒使窗口出现并消失.延迟0.01秒使窗口保持映射.这是相当令人沮丧的弄清楚(我有前面提到的getchar()和printf(…)s,这引入了足够的延迟,同时调试无法重现问题).
以下(可能是非最小的)代码按写入方式工作,但删除nanosleep(…)调用将导致问题:
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 200000000;
XFlush(_display);
nanosleep(&ts, nullptr);
XMapWindow(display, window);
XFlush(display);
nanosleep(&ts, nullptr);
XUnmapWindow(display, window);
XFlush(_display);
据推测,延迟可以追赶地图/取消地图事件等.我不确定这是一个完整的错误,但它肯定是一个可用性缺陷. (如果这给你足够的信息来解释这里发生了什么,请随时编辑这个答案.)
但是,如上所述,这种解决方法是基于误解! X服务器已经知道新窗口.这只是公然无视你.为了解决这个问题,we can hint到了窗口系统,它不应该那么粗鲁.由于这不依赖于map / unmap,因此不再发生错误行为.