From 896bd51b22772b1543031d211d2cb846c9417a34 Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Tue, 14 Oct 2014 10:10:20 +0200 Subject: [PATCH] getPosition workaround for X11. Fixed memory leaks. --- src/SFML/Window/Unix/WindowImplX11.cpp | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index acf7cf71e..778898eb7 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -288,6 +288,8 @@ m_useSizeHints(false) const unsigned char* ptr = reinterpret_cast(&hints); xcb_change_property(m_connection, XCB_PROP_MODE_REPLACE, m_window, hintsAtomReply->atom, XA_WM_HINTS, 32, 5, ptr); + + free(hintsAtomReply); } // This is a hack to force some windows managers to disable resizing @@ -429,11 +431,26 @@ void WindowImplX11::processEvents() Vector2i WindowImplX11::getPosition() const { ::Window rootWindow = RootWindow(m_display, m_screen); - xcb_translate_coordinates_cookie_t translateCookie = xcb_translate_coordinates(m_connection, m_window, rootWindow, 0, 0); - xcb_translate_coordinates_reply_t* translateReply = xcb_translate_coordinates_reply(m_connection, translateCookie, NULL); + ::Window topLevelWindow = m_window; + ::Window nextWindow = topLevelWindow; + xcb_query_tree_cookie_t treeCookie; + xcb_query_tree_reply_t* treeReply = NULL; - Vector2i result(translateReply->dst_x, translateReply->dst_y); - free(translateReply); + // Get "top level" window, i.e. the window with the root window as its parent. + while (nextWindow != rootWindow) + { + topLevelWindow = nextWindow; + + treeCookie = xcb_query_tree(m_connection, topLevelWindow); + treeReply = xcb_query_tree_reply(m_connection, treeCookie, NULL); + nextWindow = treeReply->parent; + free(treeReply); + } + + xcb_get_geometry_cookie_t geometryCookie = xcb_get_geometry(m_connection, topLevelWindow); + xcb_get_geometry_reply_t* geometryReply = xcb_get_geometry_reply(m_connection, geometryCookie, NULL); + sf::Vector2i result(geometryReply->x, geometryReply->y); + free(geometryReply); return result; } @@ -764,6 +781,9 @@ void WindowImplX11::initialize() std::cerr << "Failed to request WM_PROTOCOLS/WM_DELETE_WINDOW_NAME atoms." << std::endl; } + free(protocolsAtomReply); + free(deleteWindowAtomReply); + // Create the input context m_inputMethod = XOpenIM(m_display, NULL, NULL, NULL); if (m_inputMethod)