xcbOrg/Window: Destroy connections when no longer in use

This commit is contained in:
2025-07-25 01:21:26 -04:00
parent a17c940377
commit 270437fdd4
4 changed files with 61 additions and 12 deletions
+20 -7
View File
@@ -18,6 +18,7 @@ struct XcbXorgDllState
{
get_or_create_connection_fn* getOrCreateConnection = nullptr;
cleanup_connections_fn* cleanupConnections = nullptr;
dereference_connection_fn* dereferenceConnection = nullptr;
find_window_by_id_fn* findWindowById = nullptr;
find_window_by_name_fn* findWindowByName = nullptr;
} fns;
@@ -54,11 +55,12 @@ std::string WindowSelector::stringify() const
}
AttachedWindow::AttachedWindow(const smo::device::SenseDeviceSpec& spec)
: deviceSpec(spec), xcbConnection(nullptr)
: deviceSpec(spec)
{
// Validate required function pointers are available
if (!xcbXorg.fns.getOrCreateConnection ||
!xcbXorg.fns.findWindowById || !xcbXorg.fns.findWindowByName)
!xcbXorg.fns.findWindowById || !xcbXorg.fns.findWindowByName
|| !xcbXorg.fns.dereferenceConnection)
{
throw std::runtime_error("xcbWindow:" + std::string(__func__) +
": Required xcbXorg function pointers not available");
@@ -73,7 +75,7 @@ AttachedWindow::AttachedWindow(const smo::device::SenseDeviceSpec& spec)
(*xcbXorg.fns.getOrCreateConnection)(
windowSelector.display, windowSelector.screen);
xcbConnection = conn.get();
xcbConnectionShared = conn;
// Find the target window
xcb_window_t foundWindow = 0;
@@ -83,13 +85,13 @@ AttachedWindow::AttachedWindow(const smo::device::SenseDeviceSpec& spec)
if (windowSelector.matchType == xcb_xorg::window_search::MatchType::ID)
{
foundWindow = (*xcbXorg.fns.findWindowById)(
xcbConnection, screen->root,
conn.get(), screen->root,
windowSelector.windowId);
}
else
{
foundWindow = (*xcbXorg.fns.findWindowByName)(
xcbConnection, screen->root,
conn.get(), screen->root,
windowSelector.windowName, actualWindowName,
windowSelector.matchType);
}
@@ -221,6 +223,13 @@ std::string AttachedWindow::stringify() const {
return os.str();
}
AttachedWindow::~AttachedWindow()
{
if (xcbConnectionShared && xcbXorg.fns.dereferenceConnection) {
(*xcbXorg.fns.dereferenceConnection)(xcbConnectionShared);
}
}
} // namespace xcb_window
// SenseApi functions
@@ -254,9 +263,13 @@ static int xcbWindow_initializeInd(void)
dlsym(xcbXorg.dlopenHandle, "xcb_xorg_find_window_by_id"));
xcbXorg.fns.findWindowByName = reinterpret_cast<find_window_by_name_fn*>(
dlsym(xcbXorg.dlopenHandle, "xcb_xorg_find_window_by_name"));
xcbXorg.fns.dereferenceConnection =
reinterpret_cast<dereference_connection_fn*>(
dlsym(xcbXorg.dlopenHandle, "xcb_xorg_dereference_connection"));
if (!xcbXorg.fns.getOrCreateConnection || !xcbXorg.fns.cleanupConnections
|| !xcbXorg.fns.findWindowById || !xcbXorg.fns.findWindowByName)
|| !xcbXorg.fns.findWindowById || !xcbXorg.fns.findWindowByName
|| !xcbXorg.fns.dereferenceConnection)
{
throw std::runtime_error(
std::string("xcbWindow:") + __func__ +
@@ -274,7 +287,7 @@ static int xcbWindow_finalizeInd(void)
{
dlclose(xcbXorg.dlopenHandle);
xcbXorg.dlopenHandle = nullptr;
xcbXorg.fns = { nullptr, nullptr, nullptr, nullptr };
xcbXorg.fns = { nullptr, nullptr, nullptr, nullptr, nullptr };
}
return 0;