| 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 |
|
| 31 |
#import "app_delegate_service.h" |
| 32 |
|
| 33 |
#import "godot_view_apple_embedded.h" |
| 34 |
#import "godot_view_controller.h" |
| 35 |
#import "os_apple_embedded.h" |
| 36 |
|
| 37 |
#include "core/config/project_settings.h" |
| 38 |
#include "core/os/main_loop.h" |
| 39 |
#import "drivers/coreaudio/audio_driver_coreaudio.h" |
| 40 |
#include "main/main.h" |
| 41 |
|
| 42 |
#import <AVFoundation/AVFoundation.h> |
| 43 |
#import <AudioToolbox/AudioServices.h> |
| 44 |
|
| 45 |
int gargc; |
| 46 |
char **gargv; |
| 47 |
|
| 48 |
extern int apple_embedded_main(int, char **); |
| 49 |
extern void apple_embedded_finish(); |
| 50 |
|
| 51 |
@implementation GDTAppDelegateService |
| 52 |
|
| 53 |
enum { |
| 54 |
SESSION_CATEGORY_AMBIENT, |
| 55 |
SESSION_CATEGORY_MULTI_ROUTE, |
| 56 |
SESSION_CATEGORY_PLAY_AND_RECORD, |
| 57 |
SESSION_CATEGORY_PLAYBACK, |
| 58 |
SESSION_CATEGORY_RECORD, |
| 59 |
SESSION_CATEGORY_SOLO_AMBIENT |
| 60 |
}; |
| 61 |
|
| 62 |
static GDTViewController *mainViewController = nil; |
| 63 |
|
| 64 |
+ (GDTViewController *)viewController { |
| 65 |
return mainViewController; |
| 66 |
} |
| 67 |
|
| 68 |
+ (void)setViewController:(GDTViewController *)viewController { |
| 69 |
mainViewController = viewController; |
| 70 |
} |
| 71 |
|
| 72 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
| 73 |
// TODO: might be required to make an early return, so app wouldn't crash because of timeout. |
| 74 |
// TODO: logo screen is not displayed while shaders are compiling |
| 75 |
// DummyViewController(Splash/LoadingViewController) -> setup -> GodotViewController |
| 76 |
|
| 77 |
// Fetch the command-line arguments from NSProcessInfo |
| 78 |
NSArray *arguments = [[NSProcessInfo processInfo] arguments]; |
| 79 |
gargc = (int)[arguments count]; |
| 80 |
gargv = (char **)malloc(sizeof(char *) * gargc); |
| 81 |
for (int i = 0; i < gargc; i++) { |
| 82 |
NSString *arg = arguments[i]; |
| 83 |
gargv[i] = strdup([arg UTF8String]); |
| 84 |
} |
| 85 |
|
| 86 |
int err = apple_embedded_main(gargc, gargv); |
| 87 |
|
| 88 |
if (err != 0) { |
| 89 |
// bail, things did not go very well for us, should probably output a message on screen with our error code... |
| 90 |
exit(0); |
| 91 |
return NO; |
| 92 |
} |
| 93 |
|
| 94 |
[[NSNotificationCenter defaultCenter] |
| 95 |
addObserver:self |
| 96 |
selector:@selector(onAudioInterruption:) |
| 97 |
name:AVAudioSessionInterruptionNotification |
| 98 |
object:[AVAudioSession sharedInstance]]; |
| 99 |
|
| 100 |
int sessionCategorySetting = GLOBAL_GET("audio/general/ios/session_category"); |
| 101 |
|
| 102 |
// Initialize with default Ambient category. |
| 103 |
AVAudioSessionCategory category = AVAudioSessionCategoryAmbient; |
| 104 |
AVAudioSessionCategoryOptions options = 0; |
| 105 |
|
| 106 |
if (GLOBAL_GET("audio/general/ios/mix_with_others")) { |
| 107 |
options |= AVAudioSessionCategoryOptionMixWithOthers; |
| 108 |
} |
| 109 |
|
| 110 |
if (sessionCategorySetting == SESSION_CATEGORY_MULTI_ROUTE) { |
| 111 |
category = AVAudioSessionCategoryMultiRoute; |
| 112 |
} else if (sessionCategorySetting == SESSION_CATEGORY_PLAY_AND_RECORD) { |
| 113 |
category = AVAudioSessionCategoryPlayAndRecord; |
| 114 |
options |= AVAudioSessionCategoryOptionDefaultToSpeaker; |
| 115 |
options |= AVAudioSessionCategoryOptionAllowBluetoothA2DP; |
| 116 |
options |= AVAudioSessionCategoryOptionAllowAirPlay; |
| 117 |
} else if (sessionCategorySetting == SESSION_CATEGORY_PLAYBACK) { |
| 118 |
category = AVAudioSessionCategoryPlayback; |
| 119 |
} else if (sessionCategorySetting == SESSION_CATEGORY_RECORD) { |
| 120 |
category = AVAudioSessionCategoryRecord; |
| 121 |
} else if (sessionCategorySetting == SESSION_CATEGORY_SOLO_AMBIENT) { |
| 122 |
category = AVAudioSessionCategorySoloAmbient; |
| 123 |
} |
| 124 |
|
| 125 |
[[AVAudioSession sharedInstance] setCategory:category withOptions:options error:nil]; |
| 126 |
|
| 127 |
return YES; |
| 128 |
} |
| 129 |
|
| 130 |
- (void)onAudioInterruption:(NSNotification *)notification { |
| 131 |
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) { |
| 132 |
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) { |
| 133 |
NSLog(@"Audio interruption began"); |
| 134 |
OS_AppleEmbedded::get_singleton()->on_focus_out(); |
| 135 |
} else if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]) { |
| 136 |
NSLog(@"Audio interruption ended"); |
| 137 |
OS_AppleEmbedded::get_singleton()->on_focus_in(); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { |
| 143 |
if (OS::get_singleton()->get_main_loop()) { |
| 144 |
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_MEMORY_WARNING); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
- (void)applicationWillTerminate:(UIApplication *)application { |
| 149 |
apple_embedded_finish(); |
| 150 |
} |
| 151 |
|
| 152 |
// When application goes to background (e.g. user switches to another app or presses Home), |
| 153 |
// then applicationWillResignActive -> applicationDidEnterBackground are called. |
| 154 |
// When user opens the inactive app again, |
| 155 |
// applicationWillEnterForeground -> applicationDidBecomeActive are called. |
| 156 |
|
| 157 |
// There are cases when applicationWillResignActive -> applicationDidBecomeActive |
| 158 |
// sequence is called without the app going to background. For example, that happens |
| 159 |
// if you open the app list without switching to another app or open/close the |
| 160 |
// notification panel by swiping from the upper part of the screen. |
| 161 |
|
| 162 |
- (void)sceneDidDisconnect:(UIScene *)scene API_AVAILABLE(ios(13.0), tvos(13.0), visionos(1.0)) { |
| 163 |
OS_AppleEmbedded::get_singleton()->on_focus_out(); |
| 164 |
} |
| 165 |
|
| 166 |
- (void)sceneWillResignActive:(UIScene *)scene API_AVAILABLE(ios(13.0), tvos(13.0), visionos(1.0)) { |
| 167 |
OS_AppleEmbedded::get_singleton()->on_focus_out(); |
| 168 |
} |
| 169 |
|
| 170 |
- (void)sceneDidBecomeActive:(UIScene *)scene API_AVAILABLE(ios(13.0), tvos(13.0), visionos(1.0)) { |
| 171 |
OS_AppleEmbedded::get_singleton()->on_focus_in(); |
| 172 |
} |
| 173 |
|
| 174 |
- (void)sceneDidEnterBackground:(UIScene *)scene API_AVAILABLE(ios(13.0), tvos(13.0), visionos(1.0)) { |
| 175 |
OS_AppleEmbedded::get_singleton()->on_enter_background(); |
| 176 |
} |
| 177 |
|
| 178 |
- (void)sceneWillEnterForeground:(UIScene *)scene API_AVAILABLE(ios(13.0), tvos(13.0), visionos(1.0)) { |
| 179 |
OS_AppleEmbedded::get_singleton()->on_exit_background(); |
| 180 |
} |
| 181 |
|
| 182 |
- (void)applicationWillResignActive:(UIApplication *)application { |
| 183 |
OS_AppleEmbedded::get_singleton()->on_focus_out(); |
| 184 |
} |
| 185 |
|
| 186 |
- (void)applicationDidBecomeActive:(UIApplication *)application { |
| 187 |
OS_AppleEmbedded::get_singleton()->on_focus_in(); |
| 188 |
} |
| 189 |
|
| 190 |
- (void)applicationDidEnterBackground:(UIApplication *)application { |
| 191 |
OS_AppleEmbedded::get_singleton()->on_enter_background(); |
| 192 |
} |
| 193 |
|
| 194 |
- (void)applicationWillEnterForeground:(UIApplication *)application { |
| 195 |
OS_AppleEmbedded::get_singleton()->on_exit_background(); |
| 196 |
} |
| 197 |
|
| 198 |
- (void)dealloc { |
| 199 |
self.window = nil; |
| 200 |
} |
| 201 |
|
| 202 |
@end |