diff --git a/darwin/main.m b/darwin/main.m index 52537882..184a90c8 100644 --- a/darwin/main.m +++ b/darwin/main.m @@ -247,13 +247,13 @@ void uiQueueMain(void (*f)(void *data), void *data) int (*f)(void *data); void *data; } -- (id)initWithCallback:(int (*)(void *))callback data:(void*)callbackData; +- (id)initWithCallback:(int (*)(void *))callback data:(void *)callbackData; - (void)doTimer:(NSTimer *)timer; @end @implementation uiprivTimerDelegate -- (id)initWithCallback:(int (*)(void *))callback data:(void*)callbackData +- (id)initWithCallback:(int (*)(void *))callback data:(void *)callbackData { self = [super init]; if (self) { @@ -265,7 +265,7 @@ void uiQueueMain(void (*f)(void *data), void *data) - (void)doTimer:(NSTimer *)timer { - if (!self->f(self->data)) + if (!(*(self->f))(self->data)) [timer invalidate]; } @@ -276,7 +276,7 @@ void uiTimer(int milliseconds, int (*f)(void *data), void *data) uiprivTimerDelegate *delegate; delegate = [[uiprivTimerDelegate alloc] initWithCallback:f data:data]; - [NSTimer scheduledTimerWithTimeInterval:milliseconds / 1000.0 + [NSTimer scheduledTimerWithTimeInterval:(milliseconds / 1000.0) target:delegate selector:@selector(doTimer:) userInfo:nil diff --git a/unix/main.c b/unix/main.c index 338cf716..650fe06f 100644 --- a/unix/main.c +++ b/unix/main.c @@ -112,24 +112,23 @@ struct timer { void *data; }; -static gboolean dotimer(gpointer data) +static gboolean doTimer(gpointer data) { struct timer *t = (struct timer *) data; - if((*(t->f))(t->data)) - return TRUE; - else { - uiFree(t); + if (!(*(t->f))(t->data)) { + uiprivFree(t); return FALSE; } + return TRUE; } void uiTimer(int milliseconds, int (*f)(void *data), void *data) { struct timer *t; - t = uiNew(struct timer); + t = uiprivNew(struct timer); t->f = f; t->data = data; - g_timeout_add(milliseconds, dotimer, t); + g_timeout_add(milliseconds, doTimer, t); } diff --git a/windows/main.cpp b/windows/main.cpp index 65e49dbb..100c1873 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -131,9 +131,11 @@ void uiQueueMain(void (*f)(void *data), void *data) void uiTimer(int milliseconds, int (*f)(void *data), void *data) { - UINT_PTR timer; - - timer = (UINT_PTR) new TimerHandler(f, data); - if (SetTimer(utilWindow, timer, milliseconds, NULL) == 0) - logLastError(L"SetTimer()"); + uiprivTimer *timer; + + timer = uiprivNew(uiprivTimer); + timer->f = f; + timer->data = data; + if (SetTimer(utilWindow, (UINT_PTR) timer, milliseconds, NULL) == 0) + logLastError(L"error calling SetTimer() in uiTimer()"); } diff --git a/windows/uipriv_windows.hpp b/windows/uipriv_windows.hpp index e77cf439..64a7e8b7 100644 --- a/windows/uipriv_windows.hpp +++ b/windows/uipriv_windows.hpp @@ -95,20 +95,9 @@ extern const char *initUtilWindow(HICON hDefaultIcon, HCURSOR hDefaultCursor); extern void uninitUtilWindow(void); // main.cpp -struct TimerHandler { -public: - TimerHandler() : TimerHandler(NULL, NULL) {} - TimerHandler(int(*f)(void *data), void *data) - { - this->f = f; - this->data = data; - } - int operator()() - { - return this->f(this->data); - } -private: - int(*f)(void *data); +typedef struct uiprivTimer; +struct uiprivTimer { + void (*f)(void *); void *data; }; extern int registerMessageFilter(void); diff --git a/windows/utilwin.cpp b/windows/utilwin.cpp index fce3af09..fc2d15d4 100644 --- a/windows/utilwin.cpp +++ b/windows/utilwin.cpp @@ -18,7 +18,7 @@ static LRESULT CALLBACK utilWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, L { void (*qf)(void *); LRESULT lResult; - TimerHandler *timer; + uiprivTimer *timer; if (handleParentMessages(hwnd, uMsg, wParam, lParam, &lResult) != FALSE) return lResult; @@ -38,11 +38,11 @@ static LRESULT CALLBACK utilWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, L (*qf)((void *) lParam); return 0; case WM_TIMER: - timer = (TimerHandler *) wParam; - if (!(*timer)()) { - if (!KillTimer(utilWindow, (UINT_PTR) timer)) - logLastError(L"KillTimer()"); - delete timer; + timer = (uiprivTimer *) wParam; + if (!(*(timer->f))(timer->data)) { + if (KillTimer(utilWindow, (UINT_PTR) timer) == 0) + logLastError(L"error calling KillTimer() to end uiTimer() procedure"); + uiprivFree(timer); } return 0; }