unify session and request

This commit is contained in:
vaxerski 2023-09-02 19:53:58 +02:00
parent f8a847109e
commit 3faa42ca5b
6 changed files with 96 additions and 47 deletions

View file

@ -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) { CGlobalShortcutsPortal::SSession* CGlobalShortcutsPortal::getSession(sdbus::ObjectPath& path) {
for (auto& s : m_vSessions) { for (auto& s : m_vSessions) {
if (s->sessionHandle == path) if (s->sessionHandle == path)
@ -73,14 +49,10 @@ void CGlobalShortcutsPortal::onCreateSession(sdbus::MethodCall& call) {
const auto PSESSION = m_vSessions.emplace_back(std::make_unique<SSession>(appID, requestHandle, sessionHandle)).get(); const auto PSESSION = m_vSessions.emplace_back(std::make_unique<SSession>(appID, requestHandle, sessionHandle)).get();
// create objects // create objects
PSESSION->request = sdbus::createObject(*g_pPortalManager->getConnection(), requestHandle); PSESSION->session = createDBusSession(sessionHandle);
PSESSION->session = sdbus::createObject(*g_pPortalManager->getConnection(), sessionHandle); PSESSION->session->onDestroy = [PSESSION]() { PSESSION->session.release(); };
PSESSION->request = createDBusRequest(requestHandle);
PSESSION->request->registerMethod("org.freedesktop.impl.portal.Request", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseRequest(c, PSESSION); }); PSESSION->request->onDestroy = [PSESSION]() { PSESSION->request.release(); };
PSESSION->session->registerMethod("org.freedesktop.impl.portal.Session", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseSession(c, PSESSION); });
PSESSION->request->finishRegistration();
PSESSION->session->finishRegistration();
std::unordered_map<std::string, sdbus::Variant> opts; std::unordered_map<std::string, sdbus::Variant> opts;
call >> opts; call >> opts;

View file

@ -2,6 +2,7 @@
#include <sdbus-c++/sdbus-c++.h> #include <sdbus-c++/sdbus-c++.h>
#include <protocols/hyprland-global-shortcuts-v1-protocol.h> #include <protocols/hyprland-global-shortcuts-v1-protocol.h>
#include "../shared/Session.hpp"
struct SKeybind { struct SKeybind {
std::string id, description, preferredTrigger; std::string id, description, preferredTrigger;
@ -23,7 +24,8 @@ class CGlobalShortcutsPortal {
struct SSession { struct SSession {
std::string appid; std::string appid;
sdbus::ObjectPath requestHandle, sessionHandle; sdbus::ObjectPath requestHandle, sessionHandle;
std::unique_ptr<sdbus::IObject> request, session; std::unique_ptr<SDBusRequest> request;
std::unique_ptr<SDBusSession> session;
bool registered = false; bool registered = false;

View file

@ -321,14 +321,10 @@ void CScreencopyPortal::onCreateSession(sdbus::MethodCall& call) {
const auto PSESSION = m_vSessions.emplace_back(std::make_unique<SSession>(appID, requestHandle, sessionHandle)).get(); const auto PSESSION = m_vSessions.emplace_back(std::make_unique<SSession>(appID, requestHandle, sessionHandle)).get();
// create objects // create objects
PSESSION->request = sdbus::createObject(*g_pPortalManager->getConnection(), requestHandle); PSESSION->session = createDBusSession(sessionHandle);
PSESSION->session = sdbus::createObject(*g_pPortalManager->getConnection(), sessionHandle); PSESSION->session->onDestroy = [PSESSION]() { PSESSION->session.release(); };
PSESSION->request = createDBusRequest(requestHandle);
PSESSION->request->registerMethod("org.freedesktop.impl.portal.Request", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseRequest(c, PSESSION); }); PSESSION->request->onDestroy = [PSESSION]() { PSESSION->request.release(); };
PSESSION->session->registerMethod("org.freedesktop.impl.portal.Session", "Close", "", "", [PSESSION](sdbus::MethodCall c) { onCloseSession(c, PSESSION); });
PSESSION->request->finishRegistration();
PSESSION->session->finishRegistration();
auto reply = call.createReply(); auto reply = call.createReply();
reply << (uint32_t)0; reply << (uint32_t)0;

View file

@ -5,6 +5,7 @@
#include <sdbus-c++/sdbus-c++.h> #include <sdbus-c++/sdbus-c++.h>
#include "../shared/ScreencopyShared.hpp" #include "../shared/ScreencopyShared.hpp"
#include <gbm.h> #include <gbm.h>
#include "../shared/Session.hpp"
enum cursorModes enum cursorModes
{ {
@ -61,13 +62,14 @@ class CScreencopyPortal {
void onStart(sdbus::MethodCall& call); void onStart(sdbus::MethodCall& call);
struct SSession { struct SSession {
std::string appid; std::string appid;
sdbus::ObjectPath requestHandle, sessionHandle; sdbus::ObjectPath requestHandle, sessionHandle;
uint32_t cursorMode = HIDDEN; uint32_t cursorMode = HIDDEN;
uint32_t persistMode = 0; uint32_t persistMode = 0;
std::unique_ptr<sdbus::IObject> request, session; std::unique_ptr<SDBusRequest> request;
SSelectionData selection; std::unique_ptr<SDBusSession> session;
SSelectionData selection;
struct { struct {
bool active = false; bool active = false;

59
src/shared/Session.cpp Normal file
View file

@ -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<SDBusSession> createDBusSession(sdbus::ObjectPath handle) {
Debug::log(TRACE, "[internal] Create Session {}", handle.c_str());
std::unique_ptr<SDBusSession> pSession = std::make_unique<SDBusSession>();
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<SDBusRequest> createDBusRequest(sdbus::ObjectPath handle) {
Debug::log(TRACE, "[internal] Create Request {}", handle.c_str());
std::unique_ptr<SDBusRequest> pRequest = std::make_unique<SDBusRequest>();
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;
}

18
src/shared/Session.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include <sdbus-c++/sdbus-c++.h>
struct SDBusSession {
std::unique_ptr<sdbus::IObject> object;
sdbus::ObjectPath handle;
std::function<void()> onDestroy;
};
struct SDBusRequest {
std::unique_ptr<sdbus::IObject> object;
sdbus::ObjectPath handle;
std::function<void()> onDestroy;
};
std::unique_ptr<SDBusSession> createDBusSession(sdbus::ObjectPath handle);
std::unique_ptr<SDBusRequest> createDBusRequest(sdbus::ObjectPath handle);