more sane way to capture clipboard, also move extension dispatch off main thread

This commit is contained in:
Akash Mozumdar 2018-12-28 11:13:02 -05:00
parent 96e9b66e9f
commit bdc083a62a
3 changed files with 16 additions and 14 deletions

View File

@ -117,13 +117,12 @@ namespace
void StartCapturingClipboard() void StartCapturingClipboard()
{ {
std::thread([] SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam)
{ {
for (std::wstring last; true; Sleep(500)) if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE)
if (auto text = Util::GetClipboardText()) if (auto text = Util::GetClipboardText()) Host::GetThread(CLIPBOARD)->PushSentence(text.value());
if (last != text.value()) return CallNextHookEx(NULL, statusCode, wParam, lParam);
Host::GetThread(CLIPBOARD)->AddSentence(last = text.value()); }, NULL, GetCurrentThreadId());
}).detach();
} }
} }
@ -207,6 +206,6 @@ namespace Host
void AddConsoleOutput(std::wstring text) void AddConsoleOutput(std::wstring text)
{ {
GetThread(CONSOLE)->AddSentence(text); GetThread(CONSOLE)->PushSentence(text);
} }
} }

View File

@ -19,11 +19,6 @@ TextThread::~TextThread()
OnDestroy(this); OnDestroy(this);
} }
void TextThread::AddSentence(std::wstring sentence)
{
if (Output(this, sentence)) storage->append(sentence);
}
void TextThread::Push(const BYTE* data, int len) void TextThread::Push(const BYTE* data, int len)
{ {
if (len < 0) return; if (len < 0) return;
@ -35,6 +30,13 @@ void TextThread::Push(const BYTE* data, int len)
lastPushTime = GetTickCount(); lastPushTime = GetTickCount();
} }
void TextThread::PushSentence(std::wstring sentence)
{
LOCK(bufferMutex);
buffer += sentence;
lastPushTime = 0;
}
void TextThread::Flush() void TextThread::Flush()
{ {
std::wstring sentence; std::wstring sentence;
@ -48,5 +50,5 @@ void TextThread::Flush()
if (Util::RemoveRepetition(sentence)) repeatingChars = std::unordered_set(sentence.begin(), sentence.end()); if (Util::RemoveRepetition(sentence)) repeatingChars = std::unordered_set(sentence.begin(), sentence.end());
else repeatingChars.clear(); else repeatingChars.clear();
} }
AddSentence(sentence); if (Output(this, sentence)) storage->append(sentence);
} }

View File

@ -19,8 +19,9 @@ public:
TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name = {}); TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name = {});
~TextThread(); ~TextThread();
void AddSentence(std::wstring sentence);
void Push(const BYTE* data, int len); void Push(const BYTE* data, int len);
// Flushes ASAP
void PushSentence(std::wstring sentence);
ThreadSafe<std::wstring> storage; ThreadSafe<std::wstring> storage;
const int64_t handle; const int64_t handle;