app_delegate.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* app_delegate.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #import "app_delegate.h"
  31. #include "core/project_settings.h"
  32. #include "drivers/coreaudio/audio_driver_coreaudio.h"
  33. #import "godot_view.h"
  34. #include "main/main.h"
  35. #include "os_iphone.h"
  36. #import "view_controller.h"
  37. #import <AudioToolbox/AudioServices.h>
  38. #define kRenderingFrequency 60
  39. extern int gargc;
  40. extern char **gargv;
  41. extern int iphone_main(int, char **, String);
  42. extern void iphone_finish();
  43. @implementation AppDelegate
  44. static ViewController *mainViewController = nil;
  45. + (ViewController *)viewController {
  46. return mainViewController;
  47. }
  48. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  49. // Create a full-screen window
  50. CGRect windowBounds = [[UIScreen mainScreen] bounds];
  51. self.window = [[UIWindow alloc] initWithFrame:windowBounds];
  52. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  53. NSUserDomainMask, YES);
  54. NSString *documentsDirectory = [paths objectAtIndex:0];
  55. int err = iphone_main(gargc, gargv, String::utf8([documentsDirectory UTF8String]));
  56. if (err != 0) {
  57. // bail, things did not go very well for us, should probably output a message on screen with our error code...
  58. exit(0);
  59. return FALSE;
  60. }
  61. // WARNING: We must *always* create the GodotView after we have constructed the
  62. // OS with iphone_main. This allows the GodotView to access project settings so
  63. // it can properly initialize the OpenGL context
  64. ViewController *viewController = [[ViewController alloc] init];
  65. viewController.godotView.useCADisplayLink = bool(GLOBAL_DEF("display.iOS/use_cadisplaylink", true)) ? YES : NO;
  66. viewController.godotView.renderingInterval = 1.0 / kRenderingFrequency;
  67. self.window.rootViewController = viewController;
  68. // Show the window
  69. [self.window makeKeyAndVisible];
  70. [[NSNotificationCenter defaultCenter]
  71. addObserver:self
  72. selector:@selector(onAudioInterruption:)
  73. name:AVAudioSessionInterruptionNotification
  74. object:[AVAudioSession sharedInstance]];
  75. mainViewController = viewController;
  76. // prevent to stop music in another background app
  77. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
  78. bool keep_screen_on = bool(GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true));
  79. OSIPhone::get_singleton()->set_keep_screen_on(keep_screen_on);
  80. return TRUE;
  81. }
  82. - (void)onAudioInterruption:(NSNotification *)notification {
  83. if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
  84. if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
  85. NSLog(@"Audio interruption began");
  86. OSIPhone::get_singleton()->on_focus_out();
  87. } else if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]) {
  88. NSLog(@"Audio interruption ended");
  89. OSIPhone::get_singleton()->on_focus_in();
  90. }
  91. }
  92. }
  93. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  94. if (OS::get_singleton()->get_main_loop()) {
  95. OS::get_singleton()->get_main_loop()->notification(
  96. MainLoop::NOTIFICATION_OS_MEMORY_WARNING);
  97. }
  98. }
  99. - (void)applicationWillTerminate:(UIApplication *)application {
  100. iphone_finish();
  101. }
  102. // When application goes to background (e.g. user switches to another app or presses Home),
  103. // then applicationWillResignActive -> applicationDidEnterBackground are called.
  104. // When user opens the inactive app again,
  105. // applicationWillEnterForeground -> applicationDidBecomeActive are called.
  106. // There are cases when applicationWillResignActive -> applicationDidBecomeActive
  107. // sequence is called without the app going to background. For example, that happens
  108. // if you open the app list without switching to another app or open/close the
  109. // notification panel by swiping from the upper part of the screen.
  110. - (void)applicationWillResignActive:(UIApplication *)application {
  111. OSIPhone::get_singleton()->on_focus_out();
  112. }
  113. - (void)applicationDidBecomeActive:(UIApplication *)application {
  114. OSIPhone::get_singleton()->on_focus_in();
  115. }
  116. - (void)dealloc {
  117. self.window = nil;
  118. }
  119. @end