diff options
author | David Robillard <d@drobilla.net> | 2020-03-16 20:32:28 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-03-16 21:21:15 +0100 |
commit | a54361853bdfa08437c2858e603ce6202fb341b2 (patch) | |
tree | 5e86a841cc14dd4ca04d7b7b54b05f9c37e6feaf /pugl/detail/mac.m | |
parent | 7de08cd2a57d26f546060183944632da71f643f2 (diff) | |
download | pugl-a54361853bdfa08437c2858e603ce6202fb341b2.tar.gz pugl-a54361853bdfa08437c2858e603ce6202fb341b2.tar.bz2 pugl-a54361853bdfa08437c2858e603ce6202fb341b2.zip |
Add timer events
Diffstat (limited to 'pugl/detail/mac.m')
-rw-r--r-- | pugl/detail/mac.m | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/pugl/detail/mac.m b/pugl/detail/mac.m index 23fad7b..1c81abe 100644 --- a/pugl/detail/mac.m +++ b/pugl/detail/mac.m @@ -640,6 +640,14 @@ handleCrossing(PuglWrapperView* view, NSEvent* event, const PuglEventType type) [puglview->world->impl->app requestUserAttention:NSInformationalRequest]; } +- (void) timerTick:(NSTimer*)userTimer +{ + const NSNumber* userInfo = userTimer.userInfo; + const PuglEventTimer ev = {PUGL_TIMER, 0, userInfo.unsignedLongValue}; + + puglDispatchEvent(puglview, (const PuglEvent*)&ev); +} + - (void) viewDidEndLiveResize { [super viewDidEndLiveResize]; @@ -763,6 +771,7 @@ puglCreateWindow(PuglView* view, const char* title) // Create wrapper view to handle input impl->wrapperView = [PuglWrapperView alloc]; impl->wrapperView->puglview = view; + impl->wrapperView->userTimers = [[NSMutableDictionary alloc] init]; impl->wrapperView->markedText = [[NSMutableAttributedString alloc] init]; [impl->wrapperView setAutoresizesSubviews:YES]; [impl->wrapperView initWithFrame: @@ -920,6 +929,41 @@ puglRequestAttention(PuglView* view) return PUGL_SUCCESS; } +PuglStatus +puglStartTimer(PuglView* view, uintptr_t id, double timeout) +{ + puglStopTimer(view, id); + + NSNumber* idNumber = [NSNumber numberWithUnsignedLong:id]; + + NSTimer* timer = [NSTimer timerWithTimeInterval:timeout + target:view->impl->wrapperView + selector:@selector(timerTick:) + userInfo:idNumber + repeats:YES]; + + [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; + + view->impl->wrapperView->userTimers[idNumber] = timer; + + return PUGL_SUCCESS; +} + +PuglStatus +puglStopTimer(PuglView* view, uintptr_t id) +{ + NSNumber* idNumber = [NSNumber numberWithUnsignedLong:id]; + NSTimer* timer = view->impl->wrapperView->userTimers[idNumber]; + + if (timer) { + [view->impl->wrapperView->userTimers removeObjectForKey:timer]; + [timer invalidate]; + return PUGL_SUCCESS; + } + + return PUGL_UNKNOWN_ERROR; +} + PuglStatus puglSendEvent(PuglView* view, const PuglEvent* event) { if (event->type == PUGL_CLIENT) { |