diff --git a/windows/main.cpp b/windows/main.cpp index 00ae26c2..26a04e9e 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -1,6 +1,7 @@ // 6 april 2015 #include "uipriv_windows.hpp" +std::map timerHandlers; static HHOOK filter; static LRESULT CALLBACK filterProc(int code, WPARAM wParam, LPARAM lParam) @@ -131,4 +132,8 @@ void uiQueueMain(void (*f)(void *data), void *data) void uiTimer(int milliseconds, int (*f)(void *data), void *data) { + UINT_PTR id = timerHandlers.size() + 1; + if (SetTimer(utilWindow, id, milliseconds, NULL) == 0) + logLastError(L"SetTimer()"); + timerHandlers[id] = TimerHandler(f, data); } diff --git a/windows/uipriv_windows.hpp b/windows/uipriv_windows.hpp index 3244b7b4..c96d7a1a 100644 --- a/windows/uipriv_windows.hpp +++ b/windows/uipriv_windows.hpp @@ -77,6 +77,17 @@ extern void setWindowInsertAfter(HWND hwnd, HWND insertAfter); extern HWND getDlgItem(HWND hwnd, int id); extern void invalidateRect(HWND hwnd, RECT *r, BOOL erase); +struct TimerHandler { + int(*f)(void *data); + void *data; + TimerHandler() {} + TimerHandler(int(*f)(void *data), void *data) { + this->f = f; + this->data = data; + } +}; +extern std::map timerHandlers; + // text.cpp extern WCHAR *windowTextAndLen(HWND hwnd, LRESULT *len); extern WCHAR *windowText(HWND hwnd); diff --git a/windows/utilwin.cpp b/windows/utilwin.cpp index 28950674..7a27df4a 100644 --- a/windows/utilwin.cpp +++ b/windows/utilwin.cpp @@ -18,6 +18,7 @@ static LRESULT CALLBACK utilWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, L { void (*qf)(void *); LRESULT lResult; + UINT_PTR id; if (handleParentMessages(hwnd, uMsg, wParam, lParam, &lResult) != FALSE) return lResult; @@ -36,6 +37,15 @@ static LRESULT CALLBACK utilWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, L qf = (void (*)(void *)) wParam; (*qf)((void *) lParam); return 0; + case WM_TIMER: + id = (UINT_PTR)wParam; + TimerHandler timer = timerHandlers[id]; + if (!timer.f(timer.data)) { + if (!KillTimer(utilWindow, id)) + logLastError(L"KillTimer()"); + timerHandlers.erase(id); + } + return 0; } return DefWindowProcW(hwnd, uMsg, wParam, lParam); }