From 4adb6c4c41ee5014bfe608123bfeddb26e5f5cea Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 23 Sep 2024 17:35:43 +0100 Subject: [PATCH] screencopy: fix handles not being restored fixes #264 --- src/portals/Screencopy.cpp | 20 ++++++++++---------- src/shared/ToplevelManager.cpp | 11 ++++++++++- src/shared/ToplevelManager.hpp | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/portals/Screencopy.cpp b/src/portals/Screencopy.cpp index 0b113bd..2cf9a95 100644 --- a/src/portals/Screencopy.cpp +++ b/src/portals/Screencopy.cpp @@ -153,18 +153,15 @@ void CScreencopyPortal::onSelectSources(sdbus::MethodCall& call) { restoreData.timeIssued); } else { // ver 3 - auto sv = data.get>(); + auto sv = data.get>(); - uint64_t windowHandle = 0; - - restoreData.windowHandle = 0; - restoreData.exists = true; + restoreData.exists = true; for (auto& [tkkey, tkval] : sv) { if (tkkey == "output") restoreData.output = tkval.get(); else if (tkkey == "windowHandle") - windowHandle = tkval.get(); + restoreData.windowHandle = tkval.get(); else if (tkkey == "windowClass") restoreData.windowClass = tkval.get(); else if (tkkey == "withCursor") @@ -177,8 +174,8 @@ void CScreencopyPortal::onSelectSources(sdbus::MethodCall& call) { Debug::log(LOG, "[screencopy] restore token v3, unknown prop {}", tkkey); } - Debug::log(LOG, "[screencopy] Restore token v3 {} with data: {} {} {} {} {}", restoreData.token, windowHandle, restoreData.windowClass, restoreData.output, - restoreData.withCursor, restoreData.timeIssued); + Debug::log(LOG, "[screencopy] Restore token v3 {} with data: {} {} {} {} {}", restoreData.token, restoreData.windowHandle, restoreData.windowClass, + restoreData.output, restoreData.withCursor, restoreData.timeIssued); } } else if (key == "persist_mode") { @@ -201,9 +198,12 @@ void CScreencopyPortal::onSelectSources(sdbus::MethodCall& call) { if (RESTOREDATAVALID) { Debug::log(LOG, "[screencopy] restore data valid, not prompting"); + const bool WINDOW = !restoreData.windowClass.empty(); + const auto HANDLEMATCH = WINDOW && restoreData.windowHandle != 0 ? g_pPortalManager->m_sHelpers.toplevel->handleFromHandleFull(restoreData.windowHandle) : nullptr; + SHAREDATA.output = restoreData.output; - SHAREDATA.type = !restoreData.windowClass.empty() ? TYPE_WINDOW : TYPE_OUTPUT; - SHAREDATA.windowHandle = !restoreData.windowClass.empty() ? g_pPortalManager->m_sHelpers.toplevel->handleFromClass(restoreData.windowClass)->handle : nullptr; + SHAREDATA.type = WINDOW ? TYPE_WINDOW : TYPE_OUTPUT; + SHAREDATA.windowHandle = WINDOW ? (HANDLEMATCH ? HANDLEMATCH->handle : g_pPortalManager->m_sHelpers.toplevel->handleFromClass(restoreData.windowClass)->handle) : nullptr; SHAREDATA.windowClass = restoreData.windowClass; SHAREDATA.allowToken = true; // user allowed token before PSESSION->cursorMode = restoreData.withCursor; diff --git a/src/shared/ToplevelManager.cpp b/src/shared/ToplevelManager.cpp index 7d586da..c3d072a 100644 --- a/src/shared/ToplevelManager.cpp +++ b/src/shared/ToplevelManager.cpp @@ -80,4 +80,13 @@ SP CToplevelManager::handleFromHandleLower(uint32_t handle) { } return nullptr; -} \ No newline at end of file +} + +SP CToplevelManager::handleFromHandleFull(uint64_t handle) { + for (auto& tl : m_vToplevels) { + if ((uint64_t)tl->handle->resource() == handle) + return tl; + } + + return nullptr; +} diff --git a/src/shared/ToplevelManager.hpp b/src/shared/ToplevelManager.hpp index 2e73075..1c2d97d 100644 --- a/src/shared/ToplevelManager.hpp +++ b/src/shared/ToplevelManager.hpp @@ -25,6 +25,7 @@ class CToplevelManager { void deactivate(); SP handleFromClass(const std::string& windowClass); SP handleFromHandleLower(uint32_t handle); + SP handleFromHandleFull(uint64_t handle); std::vector> m_vToplevels;