diff --git a/host/host.cc b/host/host.cc index 8c4648f..f3d1edc 100644 --- a/host/host.cc +++ b/host/host.cc @@ -22,25 +22,9 @@ ThreadEventCallback onCreate(nullptr), onRemove(nullptr); ProcessEventCallback onAttach(nullptr), onDetach(nullptr); WORD nextThreadNumber(0); -HWND dummyWindow; #define HOST_LOCK CriticalSectionLocker hostLocker(&hostCs) // Synchronized scope for accessing private data -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID unused) -{ - switch (fdwReason) - { - case DLL_PROCESS_ATTACH: - DisableThreadLibraryCalls(hinstDLL); - // jichi 8/24/2013: Create hidden window so that ITH can access timer and events - dummyWindow = CreateWindowW(L"Button", L"InternalWindow", 0, 0, 0, 0, 0, 0, 0, hinstDLL, 0); - break; - default: - break; - } - return true; -} - namespace Host { DLLEXPORT bool Start() diff --git a/host/textthread.cc b/host/textthread.cc index 37d824d..bdd21cd 100644 --- a/host/textthread.cc +++ b/host/textthread.cc @@ -9,25 +9,32 @@ #include "../vnrhook/include/const.h" #include "winmutex.h" -extern HWND dummyWindow; - #define TT_LOCK CriticalSectionLocker ttLocker(&ttCs) // Synchronized scope for accessing private data TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, DWORD status) : storage(), sentenceBuffer(), status(status), + timestamp(GetTickCount()), threadNumber(threadNumber), output(nullptr), tp(tp) { InitializeCriticalSection(&ttCs); + flushThread = CreateThread(nullptr, 0, [](void* textThread) + { + while (true) + { + Sleep(100); + ((TextThread*)textThread)->FlushSentenceBuffer(); + } + return (DWORD)0; + }, this, 0, nullptr); } TextThread::~TextThread() { EnterCriticalSection(&ttCs); - KillTimer(dummyWindow, (UINT_PTR)this); LeaveCriticalSection(&ttCs); DeleteCriticalSection(&ttCs); } @@ -45,9 +52,10 @@ std::wstring TextThread::GetStore() return storage; } -void TextThread::AddSentence() +void TextThread::FlushSentenceBuffer() { TT_LOCK; + if (timestamp - GetTickCount() < 250 || sentenceBuffer.size() == 0) return; // TODO: let user change delay before sentence is flushed std::wstring sentence; if (status & USING_UNICODE) { @@ -80,12 +88,7 @@ void TextThread::AddText(const BYTE *con, int len) { TT_LOCK; sentenceBuffer.insert(sentenceBuffer.end(), con, con + len); - SetTimer(dummyWindow, (UINT_PTR)this, 250, // TODO: Let user change delay before sentenceBuffer is flushed - [](HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) - { - KillTimer(hWnd, idEvent); - ((TextThread*)idEvent)->AddSentence(); - }); + timestamp = GetTickCount(); } // EOF diff --git a/host/textthread.h b/host/textthread.h index dc05fa8..2b68b6f 100644 --- a/host/textthread.h +++ b/host/textthread.h @@ -42,7 +42,7 @@ public: void Clear(); void AddText(const BYTE *con, int len); - void AddSentence(); + void FlushSentenceBuffer(); void AddSentence(std::wstring sentence); private: @@ -52,7 +52,9 @@ private: std::wstring storage; ThreadParameter tp; + HANDLE flushThread; unsigned int threadNumber; + DWORD timestamp; DWORD status; };