From b3b372377cda1447916a9ef46d7b1fb8abcde9d2 Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Thu, 29 Aug 2024 13:30:17 +0200 Subject: [PATCH] windows: use waitable timers --- gral_windows.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gral_windows.cpp b/gral_windows.cpp index d0e4c9e..6197ba5 100644 --- a/gral_windows.cpp +++ b/gral_windows.cpp @@ -203,6 +203,7 @@ struct WindowData { struct gral_timer { void (*callback)(void *user_data); void *user_data; + HANDLE timer; }; @@ -1064,16 +1065,27 @@ void gral_window_clipboard_paste(gral_window *window, void (*callback)(char cons CloseClipboard(); } +static void CALLBACK timer_callback(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue) { + gral_timer *timer = (gral_timer *)lpArgToCompletionRoutine; + timer->callback(timer->user_data); +} + gral_timer *gral_window_create_timer(gral_window *window, int milliseconds, void (*callback)(void *user_data), void *user_data) { gral_timer *timer = new gral_timer(); timer->callback = callback; timer->user_data = user_data; - SetTimer((HWND)window, (UINT_PTR)timer, milliseconds, NULL); + //SetTimer((HWND)window, (UINT_PTR)timer, milliseconds, NULL); + timer->timer = CreateWaitableTimer(NULL, FALSE, NULL); + LARGE_INTEGER due_time; + due_time.QuadPart = milliseconds * -10000; + SetWaitableTimer(timer->timer, &due_time, milliseconds, timer_callback, timer, FALSE); return timer; } void gral_window_delete_timer(gral_window *window, gral_timer *timer) { - KillTimer((HWND)window, (UINT_PTR)timer); + //KillTimer((HWND)window, (UINT_PTR)timer); + CancelWaitableTimer(timer->timer); + CloseHandle(timer->timer); delete timer; }