Skip to content

Commit

Permalink
simplify the timer
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Feb 17, 2024
1 parent 1c4eb1e commit d6b676a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 35 deletions.
9 changes: 2 additions & 7 deletions demos/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion gral.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 4 additions & 6 deletions gral_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 3 additions & 11 deletions gral_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
13 changes: 3 additions & 10 deletions gral_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -1069,18 +1064,16 @@ 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;
}

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

Expand Down

0 comments on commit d6b676a

Please sign in to comment.