From 0492cb954987d94a28a2228510e2913eae8ee047 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 15 Nov 2018 00:16:12 -0500 Subject: [PATCH] refactor --- GUI/host/host.cc | 34 ++++++++++++++++++++-------------- vnrhook/texthook.cc | 2 +- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/GUI/host/host.cc b/GUI/host/host.cc index 3190531..5a67da8 100644 --- a/GUI/host/host.cc +++ b/GUI/host/host.cc @@ -143,28 +143,34 @@ namespace }).detach(); } + std::optional GetClipboardText() + { + if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {}; + if (!OpenClipboard(NULL)) return {}; + + if (HANDLE clipboardHandle = GetClipboardData(CF_UNICODETEXT)) + { + std::wstring ret = (wchar_t*)GlobalLock(clipboardHandle); + GlobalUnlock(clipboardHandle); + CloseClipboard(); + return ret; + } + CloseClipboard(); + return {}; + } + void StartCapturingClipboard() { std::thread([] { + std::wstring last; while (true) { Sleep(50); - if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL)) - { - if (HANDLE clipboardHandle = GetClipboardData(CF_UNICODETEXT)) - { - if (wchar_t* clipboardData = (wchar_t*)GlobalLock(clipboardHandle)) - { - static std::wstring last; - if (last != clipboardData) Host::GetThread(CLIPBOARD)->AddSentence(last = clipboardData); - GlobalUnlock(clipboardHandle); - } - } - CloseClipboard(); - } + if (auto text = GetClipboardText()) + if (last != text.value()) + Host::GetThread(CLIPBOARD)->AddSentence(last = text.value()); } - }).detach(); } } diff --git a/vnrhook/texthook.cc b/vnrhook/texthook.cc index 0017f51..dd9e4c8 100644 --- a/vnrhook/texthook.cc +++ b/vnrhook/texthook.cc @@ -291,7 +291,7 @@ bool TextHook::InsertReadCode() void TextHook::InitHook(HookParam h, LPCSTR name, DWORD set_flag) { LOCK(*sectionMutex); - this->hp = h; + hp = h; hp.insertion_address = hp.address; hp.type |= set_flag; strcpy_s(hookName, name);