diff --git a/src/portals/GlobalShortcuts.cpp b/src/portals/GlobalShortcuts.cpp index 920e3d9..37712aa 100644 --- a/src/portals/GlobalShortcuts.cpp +++ b/src/portals/GlobalShortcuts.cpp @@ -23,30 +23,6 @@ static const hyprland_global_shortcut_v1_listener shortcutListener = { // -static void onCloseRequest(sdbus::MethodCall& call, CGlobalShortcutsPortal::SSession* sess) { - Debug::log(TRACE, "[globalshortcuts] Close Request {}", (void*)sess); - - if (!sess || !sess->request) - return; - - auto r = call.createReply(); - r.send(); - - sess->request.release(); -} - -static void onCloseSession(sdbus::MethodCall& call, CGlobalShortcutsPortal::SSession* sess) { - Debug::log(TRACE, "[globalshortcuts] Close Session {}", (void*)sess); - - if (!sess || !sess->session) - return; - - auto r = call.createReply(); - r.send(); - - sess->session.release(); -} - CGlobalShortcutsPortal::SSession* CGlobalShortcutsPortal::getSession(sdbus::ObjectPath& path) { for (auto& s : m_vSessions) { if (s->sessionHandle == path) @@ -73,14 +49,10 @@ void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) { const auto PSESSION = m_vSessions.emplace_back(std::make_unique(appID, requestHandle, sessionHandle)).get(); // create objects - PSESSION->request = sdbus::createObject(*g_pPortalManager->getConnection(), requestHandle); - PSESSION->session = sdbus::createObject(*g_pPortalManager->getConnection(), sessionHandle); - - PSESSION->request->registerMethod("org.freedesktop.impl.portal.Request", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseRequest(c, PSESSION); }); - PSESSION->session->registerMethod("org.freedesktop.impl.portal.Session", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseSession(c, PSESSION); }); - - PSESSION->request->finishRegistration(); - PSESSION->session->finishRegistration(); + PSESSION->session = createDBusSession(sessionHandle); + PSESSION->session->onDestroy = [PSESSION]() { PSESSION->session.release(); }; + PSESSION->request = createDBusRequest(requestHandle); + PSESSION->request->onDestroy = [PSESSION]() { PSESSION->request.release(); }; std::unordered_map opts; call >> opts; diff --git a/src/portals/GlobalShortcuts.hpp b/src/portals/GlobalShortcuts.hpp index 5019cc5..0d4d987 100644 --- a/src/portals/GlobalShortcuts.hpp +++ b/src/portals/GlobalShortcuts.hpp @@ -2,6 +2,7 @@ #include #include +#include "../shared/Session.hpp" struct SKeybind { std::string id, description, preferredTrigger; @@ -23,7 +24,8 @@ class CGlobalShortcutsPortal { struct SSession { std::string appid; sdbus::ObjectPath requestHandle, sessionHandle; - std::unique_ptr request, session; + std::unique_ptr request; + std::unique_ptr session; bool registered = false; diff --git a/src/portals/Screencopy.cpp b/src/portals/Screencopy.cpp index 84f67f1..46f61b4 100644 --- a/src/portals/Screencopy.cpp +++ b/src/portals/Screencopy.cpp @@ -321,14 +321,10 @@ void CScreencopyPortal::onCreateSession(sdbus::MethodCall& call) { const auto PSESSION = m_vSessions.emplace_back(std::make_unique(appID, requestHandle, sessionHandle)).get(); // create objects - PSESSION->request = sdbus::createObject(*g_pPortalManager->getConnection(), requestHandle); - PSESSION->session = sdbus::createObject(*g_pPortalManager->getConnection(), sessionHandle); - - PSESSION->request->registerMethod("org.freedesktop.impl.portal.Request", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseRequest(c, PSESSION); }); - PSESSION->session->registerMethod("org.freedesktop.impl.portal.Session", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseSession(c, PSESSION); }); - - PSESSION->request->finishRegistration(); - PSESSION->session->finishRegistration(); + PSESSION->session = createDBusSession(sessionHandle); + PSESSION->session->onDestroy = [PSESSION]() { PSESSION->session.release(); }; + PSESSION->request = createDBusRequest(requestHandle); + PSESSION->request->onDestroy = [PSESSION]() { PSESSION->request.release(); }; auto reply = call.createReply(); reply << (uint32_t)0; diff --git a/src/portals/Screencopy.hpp b/src/portals/Screencopy.hpp index e6d2651..78d49e3 100644 --- a/src/portals/Screencopy.hpp +++ b/src/portals/Screencopy.hpp @@ -5,6 +5,7 @@ #include #include "../shared/ScreencopyShared.hpp" #include +#include "../shared/Session.hpp" enum cursorModes { @@ -61,13 +62,14 @@ class CScreencopyPortal { void onStart(sdbus::MethodCall& call); struct SSession { - std::string appid; - sdbus::ObjectPath requestHandle, sessionHandle; - uint32_t cursorMode = HIDDEN; - uint32_t persistMode = 0; + std::string appid; + sdbus::ObjectPath requestHandle, sessionHandle; + uint32_t cursorMode = HIDDEN; + uint32_t persistMode = 0; - std::unique_ptr request, session; - SSelectionData selection; + std::unique_ptr request; + std::unique_ptr session; + SSelectionData selection; struct { bool active = false; diff --git a/src/shared/Session.cpp b/src/shared/Session.cpp new file mode 100644 index 0000000..2305afb --- /dev/null +++ b/src/shared/Session.cpp @@ -0,0 +1,59 @@ +#include "Session.hpp" +#include "../core/PortalManager.hpp" +#include "../helpers/Log.hpp" + +static void onCloseRequest(sdbus::MethodCall& call, SDBusRequest* req) { + Debug::log(TRACE, "[internal] Close Request {}", (void*)req); + + if (!req) + return; + + auto r = call.createReply(); + r.send(); + + req->onDestroy(); + req->object.release(); +} + +static void onCloseSession(sdbus::MethodCall& call, SDBusSession* sess) { + Debug::log(TRACE, "[internal] Close Session {}", (void*)sess); + + if (!sess) + return; + + auto r = call.createReply(); + r.send(); + + sess->onDestroy(); + sess->object.release(); +} + +std::unique_ptr createDBusSession(sdbus::ObjectPath handle) { + Debug::log(TRACE, "[internal] Create Session {}", handle.c_str()); + + std::unique_ptr pSession = std::make_unique(); + const auto PSESSION = pSession.get(); + + pSession->object = sdbus::createObject(*g_pPortalManager->getConnection(), handle); + + pSession->object->registerMethod("org.freedesktop.impl.portal.Session", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseSession(c, PSESSION); }); + + pSession->object->finishRegistration(); + + return pSession; +} + +std::unique_ptr createDBusRequest(sdbus::ObjectPath handle) { + Debug::log(TRACE, "[internal] Create Request {}", handle.c_str()); + + std::unique_ptr pRequest = std::make_unique(); + const auto PREQUEST = pRequest.get(); + + pRequest->object = sdbus::createObject(*g_pPortalManager->getConnection(), handle); + + pRequest->object->registerMethod("org.freedesktop.impl.portal.Request", "Close", "", "", [PREQUEST](sdbus::MethodCall c) { onCloseRequest(c, PREQUEST); }); + + pRequest->object->finishRegistration(); + + return pRequest; +} \ No newline at end of file diff --git a/src/shared/Session.hpp b/src/shared/Session.hpp new file mode 100644 index 0000000..a006bc9 --- /dev/null +++ b/src/shared/Session.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +struct SDBusSession { + std::unique_ptr object; + sdbus::ObjectPath handle; + std::function onDestroy; +}; + +struct SDBusRequest { + std::unique_ptr object; + sdbus::ObjectPath handle; + std::function onDestroy; +}; + +std::unique_ptr createDBusSession(sdbus::ObjectPath handle); +std::unique_ptr createDBusRequest(sdbus::ObjectPath handle); \ No newline at end of file