多个XOpenDisplay(NULL)导致窗口不被映射

时间:2014-09-28 05:45:07

标签: c++ x11

下面是导致XWindow不被映射的代码。我非常清楚我可以使用一个显示器,它会工作。此示例是我的库中当前正在发生的内容的简化版本。我的窗口是在一个动态库中创建的,该类在一个获得自己的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)

1 个答案:

答案 0 :(得分:0)

您需要在XSyncXCreateWindow之后添加XMapWindow相关展示。