From bdc083a62a7d30cd3484732eb79c4ee981d6802f Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 28 Dec 2018 11:13:02 -0500 Subject: [PATCH] more sane way to capture clipboard, also move extension dispatch off main thread --- GUI/host/host.cpp | 13 ++++++------- GUI/host/textthread.cpp | 14 ++++++++------ GUI/host/textthread.h | 3 ++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp index 40098fb..5bc3f34 100644 --- a/GUI/host/host.cpp +++ b/GUI/host/host.cpp @@ -117,13 +117,12 @@ namespace void StartCapturingClipboard() { - std::thread([] + SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam) { - for (std::wstring last; true; Sleep(500)) - if (auto text = Util::GetClipboardText()) - if (last != text.value()) - Host::GetThread(CLIPBOARD)->AddSentence(last = text.value()); - }).detach(); + if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE) + if (auto text = Util::GetClipboardText()) Host::GetThread(CLIPBOARD)->PushSentence(text.value()); + return CallNextHookEx(NULL, statusCode, wParam, lParam); + }, NULL, GetCurrentThreadId()); } } @@ -207,6 +206,6 @@ namespace Host void AddConsoleOutput(std::wstring text) { - GetThread(CONSOLE)->AddSentence(text); + GetThread(CONSOLE)->PushSentence(text); } } diff --git a/GUI/host/textthread.cpp b/GUI/host/textthread.cpp index 565a95c..b7d6f81 100644 --- a/GUI/host/textthread.cpp +++ b/GUI/host/textthread.cpp @@ -19,11 +19,6 @@ TextThread::~TextThread() OnDestroy(this); } -void TextThread::AddSentence(std::wstring sentence) -{ - if (Output(this, sentence)) storage->append(sentence); -} - void TextThread::Push(const BYTE* data, int len) { if (len < 0) return; @@ -35,6 +30,13 @@ void TextThread::Push(const BYTE* data, int len) lastPushTime = GetTickCount(); } +void TextThread::PushSentence(std::wstring sentence) +{ + LOCK(bufferMutex); + buffer += sentence; + lastPushTime = 0; +} + void TextThread::Flush() { std::wstring sentence; @@ -48,5 +50,5 @@ void TextThread::Flush() if (Util::RemoveRepetition(sentence)) repeatingChars = std::unordered_set(sentence.begin(), sentence.end()); else repeatingChars.clear(); } - AddSentence(sentence); + if (Output(this, sentence)) storage->append(sentence); } diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index 2e80404..e03b60c 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -19,8 +19,9 @@ public: TextThread(ThreadParam tp, HookParam hp, std::optional name = {}); ~TextThread(); - void AddSentence(std::wstring sentence); void Push(const BYTE* data, int len); + // Flushes ASAP + void PushSentence(std::wstring sentence); ThreadSafe storage; const int64_t handle;