Skip to content

Commit

Permalink
restore: limit to once every 4s
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Mar 5, 2024
1 parent 81ec54c commit f0bcadc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/core/hyprlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <assert.h>
#include <string.h>
#include <xf86drm.h>
#include <filesystem>
#include <fstream>

CHyprlock::CHyprlock(const std::string& wlDisplay) {
m_sWaylandState.display = wl_display_connect(wlDisplay.empty() ? nullptr : wlDisplay.c_str());
Expand Down Expand Up @@ -885,6 +887,34 @@ zwlr_screencopy_manager_v1* CHyprlock::getScreencopy() {

void CHyprlock::attemptRestoreOnDeath() {
// dirty hack
uint64_t timeNowMs = std::chrono::duration_cast<std::chrono::milliseconds>(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<char>(ifs)), (std::istreambuf_iterator<char>()));
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");
}

0 comments on commit f0bcadc

Please sign in to comment.