app_delegate_service.mm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**************************************************************************/
  2. /* app_delegate_service.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_service.h"
  31. #import "godot_view_apple_embedded.h"
  32. #import "os_apple_embedded.h"
  33. #import "view_controller.h"
  34. #include "core/config/project_settings.h"
  35. #import "drivers/coreaudio/audio_driver_coreaudio.h"
  36. #include "main/main.h"
  37. #import <AVFoundation/AVFoundation.h>
  38. #import <AudioToolbox/AudioServices.h>
  39. #define kRenderingFrequency 60
  40. extern int gargc;
  41. extern char **gargv;
  42. extern int apple_embedded_main(int, char **);
  43. extern void apple_embedded_finish();
  44. @implementation GDTAppDelegateService
  45. enum {
  46. SESSION_CATEGORY_AMBIENT,
  47. SESSION_CATEGORY_MULTI_ROUTE,
  48. SESSION_CATEGORY_PLAY_AND_RECORD,
  49. SESSION_CATEGORY_PLAYBACK,
  50. SESSION_CATEGORY_RECORD,
  51. SESSION_CATEGORY_SOLO_AMBIENT
  52. };
  53. static GDTViewController *mainViewController = nil;
  54. + (GDTViewController *)viewController {
  55. return mainViewController;
  56. }
  57. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  58. // TODO: might be required to make an early return, so app wouldn't crash because of timeout.
  59. // TODO: logo screen is not displayed while shaders are compiling
  60. // DummyViewController(Splash/LoadingViewController) -> setup -> GodotViewController
  61. #if !defined(VISIONOS_ENABLED)
  62. // Create a full-screen window
  63. CGRect windowBounds = [[UIScreen mainScreen] bounds];
  64. self.window = [[UIWindow alloc] initWithFrame:windowBounds];
  65. #else
  66. self.window = [[UIWindow alloc] init];
  67. #endif
  68. int err = apple_embedded_main(gargc, gargv);
  69. if (err != 0) {
  70. // bail, things did not go very well for us, should probably output a message on screen with our error code...
  71. exit(0);
  72. return NO;
  73. }
  74. GDTViewController *viewController = [[GDTViewController alloc] init];
  75. viewController.godotView.useCADisplayLink = bool(GLOBAL_DEF("display.iOS/use_cadisplaylink", true)) ? YES : NO;
  76. viewController.godotView.renderingInterval = 1.0 / kRenderingFrequency;
  77. self.window.rootViewController = viewController;
  78. // Show the window
  79. [self.window makeKeyAndVisible];
  80. [[NSNotificationCenter defaultCenter]
  81. addObserver:self
  82. selector:@selector(onAudioInterruption:)
  83. name:AVAudioSessionInterruptionNotification
  84. object:[AVAudioSession sharedInstance]];
  85. mainViewController = viewController;
  86. int sessionCategorySetting = GLOBAL_GET("audio/general/ios/session_category");
  87. // Initialize with default Ambient category.
  88. AVAudioSessionCategory category = AVAudioSessionCategoryAmbient;
  89. AVAudioSessionCategoryOptions options = 0;
  90. if (GLOBAL_GET("audio/general/ios/mix_with_others")) {
  91. options |= AVAudioSessionCategoryOptionMixWithOthers;
  92. }
  93. if (sessionCategorySetting == SESSION_CATEGORY_MULTI_ROUTE) {
  94. category = AVAudioSessionCategoryMultiRoute;
  95. } else if (sessionCategorySetting == SESSION_CATEGORY_PLAY_AND_RECORD) {
  96. category = AVAudioSessionCategoryPlayAndRecord;
  97. options |= AVAudioSessionCategoryOptionDefaultToSpeaker;
  98. options |= AVAudioSessionCategoryOptionAllowBluetoothA2DP;
  99. options |= AVAudioSessionCategoryOptionAllowAirPlay;
  100. } else if (sessionCategorySetting == SESSION_CATEGORY_PLAYBACK) {
  101. category = AVAudioSessionCategoryPlayback;
  102. } else if (sessionCategorySetting == SESSION_CATEGORY_RECORD) {
  103. category = AVAudioSessionCategoryRecord;
  104. } else if (sessionCategorySetting == SESSION_CATEGORY_SOLO_AMBIENT) {
  105. category = AVAudioSessionCategorySoloAmbient;
  106. }
  107. [[AVAudioSession sharedInstance] setCategory:category withOptions:options error:nil];
  108. return YES;
  109. }
  110. - (void)onAudioInterruption:(NSNotification *)notification {
  111. if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
  112. if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
  113. NSLog(@"Audio interruption began");
  114. OS_AppleEmbedded::get_singleton()->on_focus_out();
  115. } else if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]) {
  116. NSLog(@"Audio interruption ended");
  117. OS_AppleEmbedded::get_singleton()->on_focus_in();
  118. }
  119. }
  120. }
  121. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  122. if (OS::get_singleton()->get_main_loop()) {
  123. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_MEMORY_WARNING);
  124. }
  125. }
  126. - (void)applicationWillTerminate:(UIApplication *)application {
  127. apple_embedded_finish();
  128. }
  129. // When application goes to background (e.g. user switches to another app or presses Home),
  130. // then applicationWillResignActive -> applicationDidEnterBackground are called.
  131. // When user opens the inactive app again,
  132. // applicationWillEnterForeground -> applicationDidBecomeActive are called.
  133. // There are cases when applicationWillResignActive -> applicationDidBecomeActive
  134. // sequence is called without the app going to background. For example, that happens
  135. // if you open the app list without switching to another app or open/close the
  136. // notification panel by swiping from the upper part of the screen.
  137. - (void)applicationWillResignActive:(UIApplication *)application {
  138. OS_AppleEmbedded::get_singleton()->on_focus_out();
  139. }
  140. - (void)applicationDidBecomeActive:(UIApplication *)application {
  141. OS_AppleEmbedded::get_singleton()->on_focus_in();
  142. }
  143. - (void)applicationDidEnterBackground:(UIApplication *)application {
  144. OS_AppleEmbedded::get_singleton()->on_enter_background();
  145. }
  146. - (void)applicationWillEnterForeground:(UIApplication *)application {
  147. OS_AppleEmbedded::get_singleton()->on_exit_background();
  148. }
  149. - (void)dealloc {
  150. self.window = nil;
  151. }
  152. @end