fix(xcb): Deallocate using deleter

This commit is contained in:
Michael Carlberg 2016-12-03 16:44:08 +01:00
parent 086e498388
commit ef9b37447b
9 changed files with 39 additions and 23 deletions

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <unistd.h>
#include "common.hpp" #include "common.hpp"
POLYBAR_NS POLYBAR_NS
@ -10,6 +12,14 @@ namespace factory_util {
void operator()(T*) const {} void operator()(T*) const {}
}; };
struct fd_deleter {
void operator()(int* fd) const {
if (fd != nullptr && *fd > 0) {
close(*fd);
}
}
};
template <class InstanceType, class... Deps> template <class InstanceType, class... Deps>
unique_ptr<InstanceType> generic_instance(Deps... deps) { unique_ptr<InstanceType> generic_instance(Deps... deps) {
return make_unique<InstanceType>(deps...); return make_unique<InstanceType>(deps...);

View File

@ -20,6 +20,8 @@ class connection : public xpp_connection {
public: public:
explicit connection() {} explicit connection() {}
explicit connection(xcb_connection_t* conn) : xpp_connection(conn) {} 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&) { connection& operator=(const connection&) {
return *this; return *this;
@ -92,7 +94,8 @@ class connection : public xpp_connection {
protected: protected:
registry m_registry{*this}; registry m_registry{*this};
xcb_screen_t* m_screen = nullptr; xcb_screen_t* m_screen{nullptr};
int m_connection_fd{0};
}; };
di::injector<connection&> configure_connection(); di::injector<connection&> configure_connection();

View File

@ -12,8 +12,6 @@ struct position;
using ewmh_connection_t = memory_util::malloc_ptr_t<xcb_ewmh_connection_t>; using ewmh_connection_t = memory_util::malloc_ptr_t<xcb_ewmh_connection_t>;
namespace ewmh_util { namespace ewmh_util {
extern ewmh_connection_t g_ewmh_connection;
ewmh_connection_t initialize(); ewmh_connection_t initialize();
bool supports(xcb_ewmh_connection_t* ewmh, xcb_atom_t atom, int screen = 0); bool supports(xcb_ewmh_connection_t* ewmh, xcb_atom_t atom, int screen = 0);

View File

@ -4,6 +4,7 @@
#include <xcb/xcb_util.h> #include <xcb/xcb_util.h>
#include "common.hpp" #include "common.hpp"
#include "utils/memory.hpp"
#include "x11/randr.hpp" #include "x11/randr.hpp"
POLYBAR_NS POLYBAR_NS
@ -13,6 +14,7 @@ class config;
namespace xutils { namespace xutils {
xcb_connection_t* get_connection(); 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_button_press_event_t&);
uint32_t event_timer_ms(const config& conf, const xcb_randr_notify_event_t&); uint32_t event_timer_ms(const config& conf, const xcb_randr_notify_event_t&);

View File

@ -118,7 +118,6 @@ controller::~controller() {
} }
m_connection.flush(); m_connection.flush();
m_connection.disconnect();
} }
/** /**

View File

@ -20,10 +20,6 @@ struct exit_success {};
struct exit_failure {}; struct exit_failure {};
int main(int argc, char** argv) { int main(int argc, char** argv) {
uint8_t exit_code{EXIT_SUCCESS};
bool reload{false};
int xfd;
// clang-format off // clang-format off
const command_line::options opts{ const command_line::options opts{
command_line::option{"-h", "--help", "Show help options"}, command_line::option{"-h", "--help", "Show help options"},
@ -40,21 +36,20 @@ int main(int argc, char** argv) {
logger& logger{configure_logger<decltype(logger)>(loglevel::WARNING).create<decltype(logger)>()}; logger& logger{configure_logger<decltype(logger)>(loglevel::WARNING).create<decltype(logger)>()};
uint8_t exit_code{EXIT_SUCCESS};
bool reload{false};
try { try {
//================================================== //==================================================
// Connect to X server // Connect to X server
//================================================== //==================================================
XInitThreads(); XInitThreads();
xcb_connection_t* connection{nullptr}; if (!xutils::get_connection()) {
if ((connection = xutils::get_connection()) == nullptr) {
logger.err("A connection to X could not be established... "); logger.err("A connection to X could not be established... ");
throw exit_failure{}; throw exit_failure{};
} }
xfd = xcb_get_file_descriptor(connection);
//================================================== //==================================================
// Parse command line arguments // Parse command line arguments
//================================================== //==================================================
@ -139,10 +134,6 @@ int main(int argc, char** argv) {
exit_code = EXIT_FAILURE; exit_code = EXIT_FAILURE;
} }
if (xfd != 0) {
close(xfd);
}
if (!reload) { if (!reload) {
logger.info("Reached end of application..."); logger.info("Reached end of application...");
return exit_code; return exit_code;

View File

@ -12,7 +12,8 @@ POLYBAR_NS
* Configure injection module * Configure injection module
*/ */
di::injector<connection&> configure_connection() { di::injector<connection&> configure_connection() {
return di::make_injector(di::bind<>().to(factory_util::generic_singleton<connection>(xutils::get_connection()))); return di::make_injector(di::bind<>().to(
factory_util::generic_singleton<connection>(xutils::get_connection(), xutils::get_connection_fd())));
} }
/** /**

View File

@ -6,7 +6,6 @@ POLYBAR_NS
namespace ewmh_util { namespace ewmh_util {
ewmh_connection_t g_ewmh_connection{nullptr}; ewmh_connection_t g_ewmh_connection{nullptr};
ewmh_connection_t initialize() { ewmh_connection_t initialize() {
if (!g_ewmh_connection) { if (!g_ewmh_connection) {
g_ewmh_connection = memory_util::make_malloc_ptr<xcb_ewmh_connection_t>( g_ewmh_connection = memory_util::make_malloc_ptr<xcb_ewmh_connection_t>(

View File

@ -10,16 +10,29 @@
POLYBAR_NS POLYBAR_NS
namespace xutils { namespace xutils {
xcb_connection_t* g_connection_ptr{nullptr}; shared_ptr<int> g_connection_fd;
shared_ptr<xcb_connection_t> g_connection_ptr;
xcb_connection_t* get_connection() { xcb_connection_t* get_connection() {
if (g_connection_ptr == nullptr) { if (!g_connection_ptr) {
Display* dsp{xlib::get_display()}; Display* dsp{xlib::get_display()};
if (dsp != nullptr) { if (dsp != nullptr) {
XSetEventQueueOwner(dsp, XCBOwnsEventQueue); XSetEventQueueOwner(dsp, XCBOwnsEventQueue);
g_connection_ptr = XGetXCBConnection(dsp); g_connection_ptr = shared_ptr<xcb_connection_t>(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<int>(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&) { uint32_t event_timer_ms(const config& conf, const xcb_button_press_event_t&) {