Skip to content

Commit

Permalink
Fix WindowFilter not actually working
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Oct 23, 2023
1 parent 9e7c188 commit 42e3906
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
29 changes: 19 additions & 10 deletions src/WindowFilter.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "WindowFilter.hpp"

// To prevent usage of statics (TLS breaks the present thread...?)
WindowFilter g_window_filter{};
std::unique_ptr<WindowFilter> g_window_filter{};

WindowFilter& WindowFilter::get() {
return g_window_filter;
if (g_window_filter == nullptr) {
g_window_filter = std::make_unique<WindowFilter>();
}

return *g_window_filter;
}

WindowFilter::WindowFilter() {
Expand All @@ -14,22 +18,21 @@ WindowFilter::WindowFilter() {
while (!s.stop_requested()) {
std::this_thread::sleep_for(std::chrono::milliseconds{100});

m_last_job_tick = std::chrono::steady_clock::now();

if (m_window_jobs.empty()) {
return;
}

std::vector<HWND> window_jobs{};

{
std::scoped_lock _{m_mutex};
window_jobs = std::move(m_window_jobs);
}
std::scoped_lock _{m_mutex};

for (const auto hwnd : window_jobs) {
for (const auto hwnd : m_window_jobs) {
if (is_filtered_nocache(hwnd)) {
filter_window(hwnd);
}
}

m_window_jobs.clear();
}
});
}
Expand All @@ -50,11 +53,17 @@ bool WindowFilter::is_filtered(HWND hwnd) {
return true;
}

// If there is a job for this window, filter it until the job is done
if (m_window_jobs.find(hwnd) != m_window_jobs.end()) {
// If the thread is dead for some reason, do not filter it.
return std::chrono::steady_clock::now() - m_last_job_tick <= std::chrono::seconds{2};
}

// if we havent even seen this window yet, add it to the job queue
// and return true;
if (m_seen_windows.find(hwnd) == m_seen_windows.end()) {
m_seen_windows.insert(hwnd);
m_window_jobs.push_back(hwnd);
m_window_jobs.insert(hwnd);
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion src/WindowFilter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <thread>
#include <vector>
#include <mutex>
#include <chrono>

class WindowFilter {
public:
Expand All @@ -27,9 +28,10 @@ class WindowFilter {
bool is_filtered_nocache(HWND hwnd);

std::recursive_mutex m_mutex{};
std::vector<HWND> m_window_jobs{};
std::unordered_set<HWND> m_window_jobs{};
std::unique_ptr<std::jthread> m_job_thread{};

std::unordered_set<HWND> m_seen_windows{};
std::unordered_set<HWND> m_filtered_windows{};
std::chrono::time_point<std::chrono::steady_clock> m_last_job_tick{};
};

0 comments on commit 42e3906

Please sign in to comment.