Added support for mixing Qt and SDL on iOS
authorSam Lantinga
Tue, 09 May 2017 10:10:42 -0700
changeset 10995b2a06697db74
parent 10994 50ffb2b7ee1c
child 10996 98cb4438d83e
Added support for mixing Qt and SDL on iOS

You should call SDL_SetMainReady(), and then customize the QIOSApplicationDelegate like this, in your application code:

/* Additional support for applications mixing Qt and SDL */
@interface QIOSApplicationDelegate : UIResponder <UIApplicationDelegate>
@end

extern "C"
{
void SDL_OnApplicationWillResignActive();
void SDL_OnApplicationDidEnterBackground();
void SDL_OnApplicationWillEnterForeground();
void SDL_OnApplicationDidBecomeActive();
}

@interface QIOSApplicationDelegate (SDL)

- (void)applicationWillResignActive:(UIApplication*)application;
- (void)applicationDidEnterBackground:(UIApplication*)application;
- (void)applicationWillEnterForeground:(UIApplication*)application;
- (void)applicationDidBecomeActive:(UIApplication*)application;

@end

@implementation QIOSApplicationDelegate (SDL)

- (void)applicationWillResignActive:(UIApplication*)application
{
SDL_OnApplicationWillResignActive();
}

- (void)applicationDidEnterBackground:(UIApplication*)application
{
SDL_OnApplicationDidEnterBackground();
}

- (void)applicationWillEnterForeground:(UIApplication*)application
{
SDL_OnApplicationWillEnterForeground();
}

- (void)applicationDidBecomeActive:(UIApplication*)application
{
SDL_OnApplicationDidBecomeActive();
}

@end // QIOSApplicationDelegate
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/uikit/SDL_uikitappdelegate.m
     1.1 --- a/src/video/SDL_sysvideo.h	Sun May 07 21:02:46 2017 +0200
     1.2 +++ b/src/video/SDL_sysvideo.h	Tue May 09 10:10:42 2017 -0700
     1.3 @@ -418,6 +418,13 @@
     1.4  
     1.5  extern float SDL_ComputeDiagonalDPI(int hpix, int vpix, float hinches, float vinches);
     1.6  
     1.7 +extern void SDL_OnApplicationWillTerminate();
     1.8 +extern void SDL_OnApplicationDidReceiveMemoryWarning();
     1.9 +extern void SDL_OnApplicationWillResignActive();
    1.10 +extern void SDL_OnApplicationDidEnterBackground();
    1.11 +extern void SDL_OnApplicationWillEnterForeground();
    1.12 +extern void SDL_OnApplicationDidBecomeActive();
    1.13 +
    1.14  #endif /* SDL_sysvideo_h_ */
    1.15  
    1.16  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/video/SDL_video.c	Sun May 07 21:02:46 2017 +0200
     2.2 +++ b/src/video/SDL_video.c	Tue May 09 10:10:42 2017 -0700
     2.3 @@ -3857,4 +3857,51 @@
     2.4  				   SDL_sqrt((double)den2));
     2.5  }
     2.6  
     2.7 +void SDL_OnApplicationWillTerminate()
     2.8 +{
     2.9 +    SDL_SendAppEvent(SDL_APP_TERMINATING);
    2.10 +}
    2.11 +
    2.12 +void SDL_OnApplicationDidReceiveMemoryWarning()
    2.13 +{
    2.14 +    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
    2.15 +}
    2.16 +
    2.17 +void SDL_OnApplicationWillResignActive()
    2.18 +{
    2.19 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    2.20 +    if (_this) {
    2.21 +        SDL_Window *window;
    2.22 +        for (window = _this->windows; window != NULL; window = window->next) {
    2.23 +            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
    2.24 +            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
    2.25 +        }
    2.26 +    }
    2.27 +    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
    2.28 +}
    2.29 +
    2.30 +void SDL_OnApplicationDidEnterBackground()
    2.31 +{
    2.32 +    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
    2.33 +}
    2.34 +
    2.35 +void SDL_OnApplicationWillEnterForeground()
    2.36 +{
    2.37 +    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
    2.38 +}
    2.39 +
    2.40 +void SDL_OnApplicationDidBecomeActive()
    2.41 +{
    2.42 +    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
    2.43 +
    2.44 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    2.45 +    if (_this) {
    2.46 +        SDL_Window *window;
    2.47 +        for (window = _this->windows; window != NULL; window = window->next) {
    2.48 +            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
    2.49 +            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
    2.50 +        }
    2.51 +    }
    2.52 +}
    2.53 +
    2.54  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/uikit/SDL_uikitappdelegate.m	Sun May 07 21:02:46 2017 +0200
     3.2 +++ b/src/video/uikit/SDL_uikitappdelegate.m	Tue May 09 10:10:42 2017 -0700
     3.3 @@ -442,16 +442,6 @@
     3.4      /* Do nothing. */
     3.5  }
     3.6  
     3.7 -- (void)applicationWillTerminate:(UIApplication *)application
     3.8 -{
     3.9 -    SDL_SendAppEvent(SDL_APP_TERMINATING);
    3.10 -}
    3.11 -
    3.12 -- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
    3.13 -{
    3.14 -    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
    3.15 -}
    3.16 -
    3.17  #if !TARGET_OS_TV
    3.18  - (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
    3.19  {
    3.20 @@ -482,41 +472,34 @@
    3.21  }
    3.22  #endif
    3.23  
    3.24 +- (void)applicationWillTerminate:(UIApplication *)application
    3.25 +{
    3.26 +    SDL_OnApplicationWillTerminate();
    3.27 +}
    3.28 +
    3.29 +- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
    3.30 +{
    3.31 +    SDL_OnApplicationDidReceiveMemoryWarning();
    3.32 +}
    3.33 +
    3.34  - (void)applicationWillResignActive:(UIApplication*)application
    3.35  {
    3.36 -    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    3.37 -    if (_this) {
    3.38 -        SDL_Window *window;
    3.39 -        for (window = _this->windows; window != NULL; window = window->next) {
    3.40 -            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
    3.41 -            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
    3.42 -        }
    3.43 -    }
    3.44 -    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
    3.45 +    SDL_OnApplicationWillResignActive();
    3.46  }
    3.47  
    3.48  - (void)applicationDidEnterBackground:(UIApplication*)application
    3.49  {
    3.50 -    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
    3.51 +    SDL_OnApplicationDidEnterBackground();
    3.52  }
    3.53  
    3.54  - (void)applicationWillEnterForeground:(UIApplication*)application
    3.55  {
    3.56 -    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
    3.57 +    SDL_OnApplicationWillEnterForeground();
    3.58  }
    3.59  
    3.60  - (void)applicationDidBecomeActive:(UIApplication*)application
    3.61  {
    3.62 -    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
    3.63 -
    3.64 -    SDL_VideoDevice *_this = SDL_GetVideoDevice();
    3.65 -    if (_this) {
    3.66 -        SDL_Window *window;
    3.67 -        for (window = _this->windows; window != nil; window = window->next) {
    3.68 -            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
    3.69 -            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
    3.70 -        }
    3.71 -    }
    3.72 +    SDL_OnApplicationDidBecomeActive();
    3.73  }
    3.74  
    3.75  - (void)sendDropFileForURL:(NSURL *)url