From ef9b37447b48a5ba63a4e30f6a437c3bee80afd6 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sat, 3 Dec 2016 16:44:08 +0100 Subject: [PATCH] fix(xcb): Deallocate using deleter --- include/utils/factory.hpp | 10 ++++++++++ include/x11/connection.hpp | 5 ++++- include/x11/ewmh.hpp | 2 -- include/x11/xutils.hpp | 2 ++ src/components/controller.cpp | 1 - src/main.cpp | 17 ++++------------- src/x11/connection.cpp | 3 ++- src/x11/ewmh.cpp | 1 - src/x11/xutils.cpp | 21 +++++++++++++++++---- 9 files changed, 39 insertions(+), 23 deletions(-) diff --git a/include/utils/factory.hpp b/include/utils/factory.hpp index 99370524..ba77c228 100644 --- a/include/utils/factory.hpp +++ b/include/utils/factory.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "common.hpp" POLYBAR_NS @@ -10,6 +12,14 @@ namespace factory_util { void operator()(T*) const {} }; + struct fd_deleter { + void operator()(int* fd) const { + if (fd != nullptr && *fd > 0) { + close(*fd); + } + } + }; + template unique_ptr generic_instance(Deps... deps) { return make_unique(deps...); diff --git a/include/x11/connection.hpp b/include/x11/connection.hpp index f67e81ee..1f28a7b7 100644 --- a/include/x11/connection.hpp +++ b/include/x11/connection.hpp @@ -20,6 +20,8 @@ class connection : public xpp_connection { public: explicit connection() {} explicit connection(xcb_connection_t* conn) : xpp_connection(conn) {} + explicit connection(xcb_connection_t* conn, int connection_fd) + : xpp_connection(conn), m_connection_fd(connection_fd) {} connection& operator=(const connection&) { return *this; @@ -92,7 +94,8 @@ class connection : public xpp_connection { protected: registry m_registry{*this}; - xcb_screen_t* m_screen = nullptr; + xcb_screen_t* m_screen{nullptr}; + int m_connection_fd{0}; }; di::injector configure_connection(); diff --git a/include/x11/ewmh.hpp b/include/x11/ewmh.hpp index 4b75b9ef..7ec30e37 100644 --- a/include/x11/ewmh.hpp +++ b/include/x11/ewmh.hpp @@ -12,8 +12,6 @@ struct position; using ewmh_connection_t = memory_util::malloc_ptr_t; namespace ewmh_util { - extern ewmh_connection_t g_ewmh_connection; - ewmh_connection_t initialize(); bool supports(xcb_ewmh_connection_t* ewmh, xcb_atom_t atom, int screen = 0); diff --git a/include/x11/xutils.hpp b/include/x11/xutils.hpp index a3d991ca..6c574c45 100644 --- a/include/x11/xutils.hpp +++ b/include/x11/xutils.hpp @@ -4,6 +4,7 @@ #include #include "common.hpp" +#include "utils/memory.hpp" #include "x11/randr.hpp" POLYBAR_NS @@ -13,6 +14,7 @@ class config; namespace xutils { xcb_connection_t* get_connection(); + int get_connection_fd(); uint32_t event_timer_ms(const config& conf, const xcb_button_press_event_t&); uint32_t event_timer_ms(const config& conf, const xcb_randr_notify_event_t&); diff --git a/src/components/controller.cpp b/src/components/controller.cpp index bc17b711..72b9131d 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -118,7 +118,6 @@ controller::~controller() { } m_connection.flush(); - m_connection.disconnect(); } /** diff --git a/src/main.cpp b/src/main.cpp index b0a441aa..87424a7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,10 +20,6 @@ struct exit_success {}; struct exit_failure {}; int main(int argc, char** argv) { - uint8_t exit_code{EXIT_SUCCESS}; - bool reload{false}; - int xfd; - // clang-format off const command_line::options opts{ command_line::option{"-h", "--help", "Show help options"}, @@ -40,21 +36,20 @@ int main(int argc, char** argv) { logger& logger{configure_logger(loglevel::WARNING).create()}; + uint8_t exit_code{EXIT_SUCCESS}; + bool reload{false}; + try { //================================================== // Connect to X server //================================================== XInitThreads(); - xcb_connection_t* connection{nullptr}; - - if ((connection = xutils::get_connection()) == nullptr) { + if (!xutils::get_connection()) { logger.err("A connection to X could not be established... "); throw exit_failure{}; } - xfd = xcb_get_file_descriptor(connection); - //================================================== // Parse command line arguments //================================================== @@ -139,10 +134,6 @@ int main(int argc, char** argv) { exit_code = EXIT_FAILURE; } - if (xfd != 0) { - close(xfd); - } - if (!reload) { logger.info("Reached end of application..."); return exit_code; diff --git a/src/x11/connection.cpp b/src/x11/connection.cpp index 41c930ec..f6d042a8 100644 --- a/src/x11/connection.cpp +++ b/src/x11/connection.cpp @@ -12,7 +12,8 @@ POLYBAR_NS * Configure injection module */ di::injector configure_connection() { - return di::make_injector(di::bind<>().to(factory_util::generic_singleton(xutils::get_connection()))); + return di::make_injector(di::bind<>().to( + factory_util::generic_singleton(xutils::get_connection(), xutils::get_connection_fd()))); } /** diff --git a/src/x11/ewmh.cpp b/src/x11/ewmh.cpp index 8c579223..050d22a6 100644 --- a/src/x11/ewmh.cpp +++ b/src/x11/ewmh.cpp @@ -6,7 +6,6 @@ POLYBAR_NS namespace ewmh_util { ewmh_connection_t g_ewmh_connection{nullptr}; - ewmh_connection_t initialize() { if (!g_ewmh_connection) { g_ewmh_connection = memory_util::make_malloc_ptr( diff --git a/src/x11/xutils.cpp b/src/x11/xutils.cpp index 292c7e3a..953aec19 100644 --- a/src/x11/xutils.cpp +++ b/src/x11/xutils.cpp @@ -10,16 +10,29 @@ POLYBAR_NS namespace xutils { - xcb_connection_t* g_connection_ptr{nullptr}; + shared_ptr g_connection_fd; + shared_ptr g_connection_ptr; + xcb_connection_t* get_connection() { - if (g_connection_ptr == nullptr) { + if (!g_connection_ptr) { Display* dsp{xlib::get_display()}; + if (dsp != nullptr) { XSetEventQueueOwner(dsp, XCBOwnsEventQueue); - g_connection_ptr = XGetXCBConnection(dsp); + g_connection_ptr = shared_ptr(XGetXCBConnection(dsp), bind(xcb_disconnect, placeholders::_1)); } } - return g_connection_ptr; + + return g_connection_ptr.get(); + } + + int get_connection_fd() { + if (!g_connection_fd) { + auto fd = xcb_get_file_descriptor(get_connection()); + g_connection_fd = shared_ptr(new int{fd}, factory_util::fd_deleter{}); + } + + return *g_connection_fd.get(); } uint32_t event_timer_ms(const config& conf, const xcb_button_press_event_t&) {