多重XOpenDisplay(NULL)导致不映射窗口
Multiple XOpenDisplay(NULL) Causes Window not to Be Mapped
下面是导致XWindow不被映射的代码。我很清楚,我可以使用一个显示器,它会工作。这个例子是我的图书馆中目前正在发生的事情的简化版本。我的窗口是在一个类内的动态库中创建的,该类拥有自己的Display结构。然后,在链接到我的库的应用程序的可执行文件中,我有一个主循环,它处理事件,还需要访问Display。我可以将Display存储在系统对象中,但我不希望这样做,因为Windows在创建窗口时没有遵循相同的模式,而且我的库将是跨平台的。有没有一种方法可以让两个显示对象同时正确映射X11窗口?我尝试过XFlush(),但似乎不起作用。如果需要更多的代码,我可以提供。
#include <X11/Xlib.h>
#include <cstring>
int main (int argc, char *argv []) {
Display *display = XOpenDisplay(NULL);
int screen = DefaultScreen(display);
XSetWindowAttributes window_attributes;
window_attributes.border_pixel = 5;
window_attributes.border_pixel = BlackPixel(display, screen);
window_attributes.background_pixel = WhitePixel(display, screen);
window_attributes.event_mask = ExposureMask | KeyPressMask | ButtonPress |
StructureNotifyMask | ButtonReleaseMask | KeyReleaseMask |
EnterWindowMask | LeaveWindowMask | PointerMotionMask | Button1MotionMask |
VisibilityChangeMask | ColormapChangeMask;
unsigned long valuemask = CWEventMask | CWBackPixel | CWBorderPixel;
Window window = XCreateWindow(display, RootWindow(display, screen),
0, 0, 800, 600, 5, DefaultDepth(display, screen),
InputOutput, CopyFromParent, valuemask, &window_attributes);
Display *map_display = XOpenDisplay(NULL);
XMapWindow(map_display, window);
XEvent event;
bool done = False;
Display *loop_display = XOpenDisplay(NULL);
while (!done) {
while (XPending(loop_display) > 0) {
XNextEvent(loop_display, &event);
switch (event.type) {
case Expose: {
if (event.xexpose.count != 0) {
break;
}
}
break;
case ClientMessage: {
if (strcmp(XGetAtomName(loop_display, event.xclient.message_type),
"WM_PROTOCOLS") == 0) {
done = True;
}
}
break;
}
}
}
}
我认为这很有用,尤其是在图形库中创建与窗口库分离的glx设备上下文时。
在我的单元测试中运行valgrind显示:
==23214== 5,104 bytes in 1 blocks are still reachable in loss record 37 of 41
==23214== at 0x4A082F7: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==23214== by 0x3A1E609D92: xcb_connect_to_fd (in /usr/lib64/libxcb.so.1.1.0)
==23214== by 0x3A1E60CC22: xcb_connect_to_display_with_auth_info (in /usr/lib64/libxcb.so.1.1.0)
==23214== by 0x3EBB4409C9: _XConnectXCB (in /usr/lib64/libX11.so.6.3.0)
==23214== by 0x3EBB431F3E: XOpenDisplay (in /usr/lib64/libX11.so.6.3.0)
==23214== by 0x4C11586: gl_wrapper::graphics::context_descriptor::context_descriptor()
(platform.h:16)
==23214== by 0x40CE56: context_descriptor_tests::test_context_descriptor_ctor()
(context_descriptors.h:14)
==23214== by 0x40D1BF: TestDescription_suite_context_descriptor_tests_test_context_descriptor_ctor::runTest()
(runner.cpp:34)
==23214== by 0x403A67: CxxTest::RealTestDescription::run() (RealDescriptions.cpp:106)
==23214== by 0x406A53: CxxTest::TestRunner::runTest(CxxTest::TestDescription&)
(TestRunner.h:87)
==23214== by 0x406962: CxxTest::TestRunner::runSuite(CxxTest::SuiteDescription&)
(TestRunner.h:73)
==23214== by 0x406836: CxxTest::TestRunner::runWorld() (TestRunner.h:57)
您需要在XCreateWindow
和XMapWindow
之后添加具有相关显示的XSync
。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 问:如何使用C++中的按钮从窗口打开窗口
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 在c++中访问int到类对象的映射时出错
- 调整窗口内存映射文件的大小,而不会使指针失效
- 如何在窗口中按偏移量和大小映射 RAM 中的文件
- 缓存 PID 到端口映射窗口的安全方法
- 多重XOpenDisplay(NULL)导致不映射窗口