1//
2// Copyright 2020 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ios_main.mm: Alternative entry point for iOS executables that initializes UIKit before calling
8// the default entry point.
9
10#import <UIKit/UIKit.h>
11
12#include <stdio.h>
13
14static int original_argc;
15static char **original_argv;
16
17extern "C" int main(int argc, char **argv);
18
19@interface AngleUtilAppDelegate : UIResponder <UIApplicationDelegate>
20
21@property(nullable, nonatomic, strong) UIWindow *window;
22
23@end
24
25@implementation AngleUtilAppDelegate
26
27@synthesize window;
28
29- (void)runMain
30{
31    exit(main(original_argc, original_argv));
32}
33
34- (BOOL)application:(UIApplication *)application
35    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
36{
37    self.window                    = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
38    self.window.rootViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
39    [self.window makeKeyAndVisible];
40    // We need to return from this function before the app finishes launching, so call main in a
41    // timer callback afterward.
42    [NSTimer scheduledTimerWithTimeInterval:0
43                                     target:self
44                                   selector:@selector(runMain)
45                                   userInfo:nil
46                                    repeats:NO];
47    return YES;
48}
49
50@end
51
52extern "C" int ios_main(int argc, char **argv)
53{
54    original_argc = argc;
55    original_argv = argv;
56    return UIApplicationMain(argc, argv, nullptr, NSStringFromClass([AngleUtilAppDelegate class]));
57}
58