Skip to content

Commit

Permalink
windows: use waitable timers
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Aug 29, 2024
1 parent d6b676a commit 690a26c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions gral_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ struct WindowData {
struct gral_timer {
void (*callback)(void *user_data);
void *user_data;
HANDLE timer;
};


Expand Down Expand Up @@ -1064,16 +1065,25 @@ void gral_window_clipboard_paste(gral_window *window, void (*callback)(char cons
CloseClipboard();
}

static void CALLBACK timer_completion_routine(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);
timer->timer = CreateWaitableTimer(NULL, FALSE, NULL);
LARGE_INTEGER due_time;
due_time.QuadPart = milliseconds * -10000;
SetWaitableTimer(timer->timer, &due_time, milliseconds, &timer_completion_routine, timer, FALSE);
return timer;
}

void gral_window_delete_timer(gral_window *window, gral_timer *timer) {
KillTimer((HWND)window, (UINT_PTR)timer);
CancelWaitableTimer(timer->timer);
CloseHandle(timer->timer);
delete timer;
}

Expand Down

0 comments on commit 690a26c

Please sign in to comment.