From d6b676aed4d009832b859f9e0a3088f3c9b3cdfa Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Sat, 17 Feb 2024 13:10:54 +0100 Subject: [PATCH] simplify the timer --- demos/events.c | 9 ++------- gral.h | 2 +- gral_linux.c | 10 ++++------ gral_macos.m | 14 +++----------- gral_windows.cpp | 13 +++---------- 5 files changed, 13 insertions(+), 35 deletions(-) diff --git a/demos/events.c b/demos/events.c index 6c8ae20..145c041 100644 --- a/demos/events.c +++ b/demos/events.c @@ -101,13 +101,8 @@ static void focus_leave(void *user_data) { printf("focus leave\n"); } -static int timer(void *user_data) { +static void timer(void *user_data) { printf("timer %f\n", gral_time_get_monotonic()); - return 1; -} - -static void timer_destroy(void *user_data) { - printf("timer destroy\n"); } static void create_window(void *user_data) { @@ -131,7 +126,7 @@ static void create_window(void *user_data) { &focus_leave }; demo->window = gral_window_create(demo->application, 600, 400, "gral events demo", &interface, demo); - demo->timer = gral_window_create_timer(demo->window, 1000, &timer, &timer_destroy, demo); + demo->timer = gral_window_create_timer(demo->window, 1000, &timer, demo); } static void open_empty(void *user_data) { diff --git a/gral.h b/gral.h index 9c2bbcc..f440589 100644 --- a/gral.h +++ b/gral.h @@ -157,7 +157,7 @@ void gral_window_show_open_file_dialog(struct gral_window *window, void (*callba void gral_window_show_save_file_dialog(struct gral_window *window, void (*callback)(char const *file, void *user_data), void *user_data); void gral_window_clipboard_copy(struct gral_window *window, char const *text); void gral_window_clipboard_paste(struct gral_window *window, void (*callback)(char const *text, void *user_data), void *user_data); -struct gral_timer *gral_window_create_timer(struct gral_window *window, int milliseconds, int (*callback)(void *user_data), void (*destroy)(void *user_data), void *user_data); +struct gral_timer *gral_window_create_timer(struct gral_window *window, int milliseconds, void (*callback)(void *user_data), void *user_data); void gral_window_delete_timer(struct gral_window *window, struct gral_timer *timer); void gral_window_run_on_main_thread(struct gral_window *window, void (*callback)(void *user_data), void *user_data); diff --git a/gral_linux.c b/gral_linux.c index e590158..da9881a 100644 --- a/gral_linux.c +++ b/gral_linux.c @@ -639,24 +639,22 @@ void gral_window_clipboard_paste(struct gral_window *window, void (*callback)(ch } typedef struct { - int (*callback)(void *user_data); - void (*destroy)(void *user_data); + void (*callback)(void *user_data); void *user_data; } TimerCallbackData; static gboolean timer_callback(gpointer user_data) { TimerCallbackData *callback_data = user_data; - return callback_data->callback(callback_data->user_data); + callback_data->callback(callback_data->user_data); + return G_SOURCE_CONTINUE; } static void timer_destroy(gpointer user_data) { TimerCallbackData *callback_data = user_data; - callback_data->destroy(callback_data->user_data); g_slice_free(TimerCallbackData, callback_data); } -struct gral_timer *gral_window_create_timer(struct gral_window *window, int milliseconds, int (*callback)(void *user_data), void (*destroy)(void *user_data), void *user_data) { +struct gral_timer *gral_window_create_timer(struct gral_window *window, int milliseconds, void (*callback)(void *user_data), void *user_data) { TimerCallbackData *callback_data = g_slice_new(TimerCallbackData); callback_data->callback = callback; - callback_data->destroy = destroy; callback_data->user_data = user_data; return (struct gral_timer *)(intptr_t)g_timeout_add_full(G_PRIORITY_DEFAULT, milliseconds, timer_callback, callback_data, timer_destroy); } diff --git a/gral_macos.m b/gral_macos.m index 06b5823..ab4221f 100644 --- a/gral_macos.m +++ b/gral_macos.m @@ -669,26 +669,18 @@ void gral_window_clipboard_paste(struct gral_window *window, void (*callback)(ch @interface TimerCallbackObject: NSObject { @public - int (*callback)(void *user_data); - void (*destroy)(void *user_data); + void (*callback)(void *user_data); void *user_data; } @end @implementation TimerCallbackObject - (void)invoke:(NSTimer *)timer { - if (!callback(user_data)) { - [timer invalidate]; - } -} -- (void)dealloc { - destroy(user_data); - [super dealloc]; + callback(user_data); } @end -struct gral_timer *gral_window_create_timer(struct gral_window *window, int milliseconds, int (*callback)(void *user_data), void (*destroy)(void *user_data), void *user_data) { +struct gral_timer *gral_window_create_timer(struct gral_window *window, int milliseconds, void (*callback)(void *user_data), void *user_data) { TimerCallbackObject *callback_object = [[TimerCallbackObject alloc] init]; callback_object->callback = callback; - callback_object->destroy = destroy; callback_object->user_data = user_data; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:milliseconds/1000.0 target:callback_object selector:@selector(invoke:) userInfo:nil repeats:YES]; [callback_object release]; diff --git a/gral_windows.cpp b/gral_windows.cpp index ca7e11c..d0e4c9e 100644 --- a/gral_windows.cpp +++ b/gral_windows.cpp @@ -201,8 +201,7 @@ struct WindowData { }; struct gral_timer { - int (*callback)(void *user_data); - void (*destroy)(void *user_data); + void (*callback)(void *user_data); void *user_data; }; @@ -462,11 +461,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { case WM_TIMER: { gral_timer *timer = (gral_timer *)wParam; - if (!timer->callback(timer->user_data)) { - KillTimer(hwnd, (UINT_PTR)timer); - timer->destroy(timer->user_data); - delete timer; - } + timer->callback(timer->user_data); return 0; } case WM_SIZE: @@ -1069,10 +1064,9 @@ void gral_window_clipboard_paste(gral_window *window, void (*callback)(char cons CloseClipboard(); } -gral_timer *gral_window_create_timer(gral_window *window, int milliseconds, int (*callback)(void *user_data), void (*destroy)(void *user_data), void *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->destroy = destroy; timer->user_data = user_data; SetTimer((HWND)window, (UINT_PTR)timer, milliseconds, NULL); return timer; @@ -1080,7 +1074,6 @@ gral_timer *gral_window_create_timer(gral_window *window, int milliseconds, int void gral_window_delete_timer(gral_window *window, gral_timer *timer) { KillTimer((HWND)window, (UINT_PTR)timer); - timer->destroy(timer->user_data); delete timer; }