From f0bcadc6ad82029a4acf543b2dc7dc0be37053c5 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 5 Mar 2024 23:32:31 +0000 Subject: [PATCH] restore: limit to once every 4s --- src/core/hyprlock.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/core/hyprlock.cpp b/src/core/hyprlock.cpp index afab72c..9c0f36a 100644 --- a/src/core/hyprlock.cpp +++ b/src/core/hyprlock.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include CHyprlock::CHyprlock(const std::string& wlDisplay) { m_sWaylandState.display = wl_display_connect(wlDisplay.empty() ? nullptr : wlDisplay.c_str()); @@ -885,6 +887,34 @@ zwlr_screencopy_manager_v1* CHyprlock::getScreencopy() { void CHyprlock::attemptRestoreOnDeath() { // dirty hack + uint64_t timeNowMs = std::chrono::duration_cast(std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t({0})).count(); + + constexpr char* LASTRESTARTPATH = "/tmp/hypr/.hyprlockrestart"; + + if (std::filesystem::exists(LASTRESTARTPATH)) { + std::ifstream ifs(LASTRESTARTPATH); + std::string content((std::istreambuf_iterator(ifs)), (std::istreambuf_iterator())); + uint64_t timeEncoded = 0; + try { + timeEncoded = std::stoull(content); + } catch (std::exception& e) { + // oops? + ifs.close(); + std::filesystem::remove(LASTRESTARTPATH); + return; + } + ifs.close(); + + if (timeNowMs - timeEncoded < 4000 /* 4s, seems reasonable */) { + Debug::log(LOG, "Not restoring on death; less than 4s since last death"); + return; + } + } + + std::ofstream ofs(LASTRESTARTPATH, std::ios::trunc); + ofs << timeNowMs; + ofs.close(); + spawnSync("hyprctl keyword misc:allow_session_lock_restore true"); spawnAsync("sleep 2 && hyprlock & disown"); } \ No newline at end of file