Skip to content

Commit

Permalink
Fixed life cycle events on iOS when using main callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jul 24, 2024
1 parent 45fc548 commit fff783d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/main/ios/SDL_sysmain_callbacks.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

#import <UIKit/UIKit.h>

#include "../../video/uikit/SDL_uikitevents.h" // For SDL_UpdateLifecycleObserver()


@interface SDLIosMainCallbacksDisplayLink : NSObject
@property(nonatomic, retain) CADisplayLink *displayLink;
- (void)appIteration:(CADisplayLink *)sender;
Expand Down Expand Up @@ -53,6 +56,7 @@ - (void)appIteration:(CADisplayLink *)sender
self.displayLink = nil;
globalDisplayLink = nil;
SDL_QuitMainCallbacks();
SDL_UpdateLifecycleObserver();
exit((rc < 0) ? 1 : 0);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/video/uikit/SDL_uikitevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include "../SDL_sysvideo.h"

extern void SDL_UpdateLifecycleObserver(void);

extern Uint64 UIKit_GetEventTimestamp(NSTimeInterval nsTimestamp);
extern void UIKit_PumpEvents(SDL_VideoDevice *_this);

Expand Down
36 changes: 21 additions & 15 deletions src/video/uikit/SDL_uikitevents.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#ifdef SDL_VIDEO_DRIVER_UIKIT

#include "../../events/SDL_events_c.h"
#include "../../main/SDL_main_callbacks.h"

#include "SDL_uikitevents.h"
#include "SDL_uikitopengles.h"
Expand All @@ -46,10 +47,10 @@ @interface SDL_LifecycleObserver : NSObject

@implementation SDL_LifecycleObserver

- (void)eventPumpChanged
- (void)update
{
NSNotificationCenter *notificationCenter = NSNotificationCenter.defaultCenter;
if (UIKit_EventPumpEnabled && !self.isObservingNotifications) {
if ((UIKit_EventPumpEnabled || SDL_HasMainCallbacks()) && !self.isObservingNotifications) {
self.isObservingNotifications = YES;
[notificationCenter addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
[notificationCenter addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
Expand All @@ -63,7 +64,7 @@ - (void)eventPumpChanged
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
#endif
} else if (!UIKit_EventPumpEnabled && self.isObservingNotifications) {
} else if (!UIKit_EventPumpEnabled && !SDL_HasMainCallbacks() && self.isObservingNotifications) {
self.isObservingNotifications = NO;
[notificationCenter removeObserver:self];
}
Expand Down Expand Up @@ -108,6 +109,23 @@ - (void)applicationDidChangeStatusBarOrientation

@end

void SDL_UpdateLifecycleObserver(void)
{
static SDL_LifecycleObserver *lifecycleObserver;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
lifecycleObserver = [SDL_LifecycleObserver new];
});
[lifecycleObserver update];
}

void SDL_SetiOSEventPump(SDL_bool enabled)
{
UIKit_EventPumpEnabled = enabled;

SDL_UpdateLifecycleObserver();
}

Uint64 UIKit_GetEventTimestamp(NSTimeInterval nsTimestamp)
{
static Uint64 timestamp_offset;
Expand All @@ -126,18 +144,6 @@ Uint64 UIKit_GetEventTimestamp(NSTimeInterval nsTimestamp)
return timestamp;
}

void SDL_SetiOSEventPump(SDL_bool enabled)
{
UIKit_EventPumpEnabled = enabled;

static SDL_LifecycleObserver *lifecycleObserver;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
lifecycleObserver = [SDL_LifecycleObserver new];
});
[lifecycleObserver eventPumpChanged];
}

void UIKit_PumpEvents(SDL_VideoDevice *_this)
{
if (!UIKit_EventPumpEnabled) {
Expand Down

0 comments on commit fff783d

Please sign in to comment.