SDL_sysmain_callbacks.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "../SDL_main_callbacks.h"
  20. #ifdef SDL_PLATFORM_IOS
  21. #import <UIKit/UIKit.h>
  22. @interface SDLIosMainCallbacksDisplayLink : NSObject
  23. @property(nonatomic, retain) CADisplayLink *displayLink;
  24. - (void)appIteration:(CADisplayLink *)sender;
  25. - (instancetype)init:(SDL_AppIterate_func)_appiter quitfunc:(SDL_AppQuit_func)_appquit;
  26. @end
  27. static SDLIosMainCallbacksDisplayLink *globalDisplayLink;
  28. @implementation SDLIosMainCallbacksDisplayLink
  29. - (instancetype)init:(SDL_AppIterate_func)_appiter quitfunc:(SDL_AppQuit_func)_appquit;
  30. {
  31. if ((self = [super init])) {
  32. self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(appIteration:)];
  33. [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  34. }
  35. return self;
  36. }
  37. - (void)appIteration:(CADisplayLink *)sender
  38. {
  39. const int rc = SDL_IterateMainCallbacks(SDL_TRUE);
  40. if (rc != 0) {
  41. [self.displayLink invalidate];
  42. self.displayLink = nil;
  43. globalDisplayLink = nil;
  44. SDL_QuitMainCallbacks();
  45. exit((rc < 0) ? 1 : 0);
  46. }
  47. }
  48. @end
  49. // SDL_RunApp will land in UIApplicationMain, which calls SDL_main from postFinishLaunch, which calls this.
  50. // When we return from here, we're living in the RunLoop, and a CADisplayLink is firing regularly for us.
  51. int SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
  52. {
  53. const int rc = SDL_InitMainCallbacks(argc, argv, appinit, appiter, appevent, appquit);
  54. if (rc == 0) {
  55. globalDisplayLink = [[SDLIosMainCallbacksDisplayLink alloc] init:appiter quitfunc:appquit];
  56. if (globalDisplayLink != nil) {
  57. return 0; // this will fall all the way out of SDL_main, where UIApplicationMain will keep running the RunLoop.
  58. }
  59. }
  60. // appinit requested quit, just bounce out now.
  61. SDL_QuitMainCallbacks();
  62. exit((rc < 0) ? 1 : 0);
  63. return 1; // just in case.
  64. }
  65. #endif