src/video/uikit/SDL_uikitappdelegate.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Sep 2018 11:49:25 -0700
changeset 12201 8bdc4d340419
parent 12144 c48e1ae67968
child 12503 806492103856
permissions -rw-r--r--
Fixed whitespace
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@5262
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5262
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5262
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2765
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2765
    24
slouken@6556
    25
#include "../SDL_sysvideo.h"
slouken@6556
    26
#include "SDL_assert.h"
slouken@6556
    27
#include "SDL_hints.h"
slouken@6556
    28
#include "SDL_system.h"
slouken@7283
    29
#include "SDL_main.h"
icculus@4446
    30
slime73@9532
    31
#import "SDL_uikitappdelegate.h"
slime73@9532
    32
#import "SDL_uikitmodes.h"
slime73@9532
    33
#import "SDL_uikitwindow.h"
slime73@9532
    34
slouken@6556
    35
#include "../../events/SDL_events_c.h"
slouken@2765
    36
slouken@2765
    37
#ifdef main
slouken@2765
    38
#undef main
slouken@2765
    39
#endif
slouken@2765
    40
slouken@2765
    41
static int forward_argc;
slouken@2765
    42
static char **forward_argv;
kees@6006
    43
static int exit_status;
slouken@2765
    44
kees@6003
    45
int main(int argc, char **argv)
kees@6003
    46
{
slouken@5131
    47
    int i;
kees@6001
    48
slouken@5131
    49
    /* store arguments */
slouken@5131
    50
    forward_argc = argc;
slouken@5131
    51
    forward_argv = (char **)malloc((argc+1) * sizeof(char *));
kees@6003
    52
    for (i = 0; i < argc; i++) {
slouken@5131
    53
        forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
slouken@5131
    54
        strcpy(forward_argv[i], argv[i]);
slouken@5131
    55
    }
slouken@5131
    56
    forward_argv[i] = NULL;
slouken@2765
    57
slouken@5131
    58
    /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
slime73@9487
    59
    @autoreleasepool {
slime73@9510
    60
        UIApplicationMain(argc, argv, nil, [SDLUIKitDelegate getAppDelegateClassName]);
slime73@9487
    61
    }
kees@6001
    62
kees@6006
    63
    /* free the memory we used to hold copies of argc and argv */
kees@6006
    64
    for (i = 0; i < forward_argc; i++) {
kees@6006
    65
        free(forward_argv[i]);
kees@6006
    66
    }
kees@6006
    67
    free(forward_argv);
kees@6006
    68
kees@6006
    69
    return exit_status;
slouken@2765
    70
}
slouken@2765
    71
slouken@11284
    72
static void SDLCALL
slouken@7432
    73
SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
kees@6003
    74
{
slouken@7432
    75
    BOOL disable = (hint && *hint != '0');
tim@5555
    76
    [UIApplication sharedApplication].idleTimerDisabled = disable;
tim@5555
    77
}
tim@5555
    78
slime73@10340
    79
#if !TARGET_OS_TV
slime73@9532
    80
/* Load a launch image using the old UILaunchImageFile-era naming rules. */
slime73@9532
    81
static UIImage *
slime73@9532
    82
SDL_LoadLaunchImageNamed(NSString *name, int screenh)
slime73@9532
    83
{
slime73@9532
    84
    UIInterfaceOrientation curorient = [UIApplication sharedApplication].statusBarOrientation;
slime73@9532
    85
    UIUserInterfaceIdiom idiom = [UIDevice currentDevice].userInterfaceIdiom;
slime73@9532
    86
    UIImage *image = nil;
slime73@9532
    87
slime73@9532
    88
    if (idiom == UIUserInterfaceIdiomPhone && screenh == 568) {
slime73@9532
    89
        /* The image name for the iPhone 5 uses its height as a suffix. */
slime73@9532
    90
        image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-568h", name]];
slime73@9532
    91
    } else if (idiom == UIUserInterfaceIdiomPad) {
slime73@9532
    92
        /* iPad apps can launch in any orientation. */
slime73@9532
    93
        if (UIInterfaceOrientationIsLandscape(curorient)) {
slime73@9532
    94
            if (curorient == UIInterfaceOrientationLandscapeLeft) {
slime73@9532
    95
                image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-LandscapeLeft", name]];
slime73@9532
    96
            } else {
slime73@9532
    97
                image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-LandscapeRight", name]];
slime73@9532
    98
            }
slime73@9532
    99
            if (!image) {
slime73@9532
   100
                image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-Landscape", name]];
slime73@9532
   101
            }
slime73@9532
   102
        } else {
slime73@9532
   103
            if (curorient == UIInterfaceOrientationPortraitUpsideDown) {
slime73@9532
   104
                image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-PortraitUpsideDown", name]];
slime73@9532
   105
            }
slime73@9532
   106
            if (!image) {
slime73@9532
   107
                image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-Portrait", name]];
slime73@9532
   108
            }
slime73@9532
   109
        }
slime73@9532
   110
    }
slime73@9532
   111
slime73@9532
   112
    if (!image) {
slime73@9532
   113
        image = [UIImage imageNamed:name];
slime73@9532
   114
    }
slime73@9532
   115
slime73@9532
   116
    return image;
slime73@9532
   117
}
slime73@10340
   118
#endif /* !TARGET_OS_TV */
slime73@10340
   119
slime73@10340
   120
@interface SDLLaunchScreenController ()
slime73@10340
   121
slime73@10340
   122
#if !TARGET_OS_TV
slime73@10340
   123
- (NSUInteger)supportedInterfaceOrientations;
slime73@10340
   124
#endif
slime73@10340
   125
slime73@10340
   126
@end
slime73@9532
   127
slime73@9552
   128
@implementation SDLLaunchScreenController
slime73@9532
   129
slime73@9532
   130
- (instancetype)init
slime73@9532
   131
{
slime73@10368
   132
    return [self initWithNibName:nil bundle:[NSBundle mainBundle]];
slime73@10368
   133
}
slime73@10368
   134
slime73@10368
   135
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
slime73@10368
   136
{
slime73@9532
   137
    if (!(self = [super initWithNibName:nil bundle:nil])) {
slime73@9532
   138
        return nil;
slime73@9532
   139
    }
slime73@9532
   140
slime73@10368
   141
    NSString *screenname = nibNameOrNil;
slime73@10368
   142
    NSBundle *bundle = nibBundleOrNil;
slime73@9552
   143
    BOOL atleastiOS8 = UIKit_IsSystemVersionAtLeast(8.0);
slime73@9534
   144
slime73@9532
   145
    /* Launch screens were added in iOS 8. Otherwise we use launch images. */
slime73@9552
   146
    if (screenname && atleastiOS8) {
slime73@9532
   147
        @try {
slime73@9532
   148
            self.view = [bundle loadNibNamed:screenname owner:self options:nil][0];
slime73@9532
   149
        }
slime73@9532
   150
        @catch (NSException *exception) {
slime73@9552
   151
            /* If a launch screen name is specified but it fails to load, iOS
slime73@9552
   152
             * displays a blank screen rather than falling back to an image. */
slime73@9532
   153
            return nil;
slime73@9532
   154
        }
slime73@9532
   155
    }
slime73@9532
   156
slime73@9532
   157
    if (!self.view) {
slime73@9532
   158
        NSArray *launchimages = [bundle objectForInfoDictionaryKey:@"UILaunchImages"];
slime73@9532
   159
        NSString *imagename = nil;
slime73@9532
   160
        UIImage *image = nil;
slime73@9532
   161
slime73@9532
   162
        int screenw = (int)([UIScreen mainScreen].bounds.size.width + 0.5);
slime73@9532
   163
        int screenh = (int)([UIScreen mainScreen].bounds.size.height + 0.5);
slime73@9532
   164
slime73@10371
   165
#if !TARGET_OS_TV
slime73@10371
   166
        UIInterfaceOrientation curorient = [UIApplication sharedApplication].statusBarOrientation;
slime73@10371
   167
slime73@9532
   168
        /* We always want portrait-oriented size, to match UILaunchImageSize. */
slime73@9532
   169
        if (screenw > screenh) {
slime73@9532
   170
            int width = screenw;
slime73@9532
   171
            screenw = screenh;
slime73@9532
   172
            screenh = width;
slime73@9532
   173
        }
slime73@10371
   174
#endif
slime73@9532
   175
slime73@9532
   176
        /* Xcode 5 introduced a dictionary of launch images in Info.plist. */
slime73@9532
   177
        if (launchimages) {
slime73@9532
   178
            for (NSDictionary *dict in launchimages) {
slime73@10371
   179
                NSString *minversion = dict[@"UILaunchImageMinimumOSVersion"];
slime73@10371
   180
                NSString *sizestring = dict[@"UILaunchImageSize"];
slime73@9532
   181
slime73@9532
   182
                /* Ignore this image if the current version is too low. */
slime73@9532
   183
                if (minversion && !UIKit_IsSystemVersionAtLeast(minversion.doubleValue)) {
slime73@9532
   184
                    continue;
slime73@9532
   185
                }
slime73@9532
   186
slime73@9532
   187
                /* Ignore this image if the size doesn't match. */
slime73@9532
   188
                if (sizestring) {
slime73@9532
   189
                    CGSize size = CGSizeFromString(sizestring);
slime73@9532
   190
                    if ((int)(size.width + 0.5) != screenw || (int)(size.height + 0.5) != screenh) {
slime73@9532
   191
                        continue;
slime73@9532
   192
                    }
slime73@9532
   193
                }
slime73@9532
   194
slime73@10371
   195
#if !TARGET_OS_TV
slime73@10371
   196
                UIInterfaceOrientationMask orientmask = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
slime73@10371
   197
                NSString *orientstring = dict[@"UILaunchImageOrientation"];
slime73@10371
   198
slime73@9532
   199
                if (orientstring) {
slime73@9532
   200
                    if ([orientstring isEqualToString:@"PortraitUpsideDown"]) {
slime73@9532
   201
                        orientmask = UIInterfaceOrientationMaskPortraitUpsideDown;
slime73@9532
   202
                    } else if ([orientstring isEqualToString:@"Landscape"]) {
slime73@9532
   203
                        orientmask = UIInterfaceOrientationMaskLandscape;
slime73@9532
   204
                    } else if ([orientstring isEqualToString:@"LandscapeLeft"]) {
slime73@9532
   205
                        orientmask = UIInterfaceOrientationMaskLandscapeLeft;
slime73@9532
   206
                    } else if ([orientstring isEqualToString:@"LandscapeRight"]) {
slime73@9532
   207
                        orientmask = UIInterfaceOrientationMaskLandscapeRight;
slime73@9532
   208
                    }
slime73@9532
   209
                }
slime73@9532
   210
slime73@9532
   211
                /* Ignore this image if the orientation doesn't match. */
slime73@9532
   212
                if ((orientmask & (1 << curorient)) == 0) {
slime73@9532
   213
                    continue;
slime73@9532
   214
                }
slime73@10371
   215
#endif
slime73@9532
   216
slime73@9532
   217
                imagename = dict[@"UILaunchImageName"];
slime73@9532
   218
            }
slime73@9532
   219
slime73@9532
   220
            if (imagename) {
slime73@9532
   221
                image = [UIImage imageNamed:imagename];
slime73@9532
   222
            }
slime73@10371
   223
        }
slime73@10371
   224
#if !TARGET_OS_TV
slime73@10371
   225
        else {
slime73@9532
   226
            imagename = [bundle objectForInfoDictionaryKey:@"UILaunchImageFile"];
slime73@9532
   227
slime73@9532
   228
            if (imagename) {
slime73@9532
   229
                image = SDL_LoadLaunchImageNamed(imagename, screenh);
slime73@9532
   230
            }
slime73@9532
   231
slime73@9532
   232
            if (!image) {
slime73@9532
   233
                image = SDL_LoadLaunchImageNamed(@"Default", screenh);
slime73@9532
   234
            }
slime73@9532
   235
        }
slime73@10371
   236
#endif
slime73@9532
   237
slime73@9532
   238
        if (image) {
slime73@9552
   239
            UIImageView *view = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
slime73@9552
   240
            UIImageOrientation imageorient = UIImageOrientationUp;
slime73@9552
   241
slime73@10371
   242
#if !TARGET_OS_TV
slime73@9552
   243
            /* Bugs observed / workaround tested in iOS 8.3, 7.1, and 6.1. */
slime73@9552
   244
            if (UIInterfaceOrientationIsLandscape(curorient)) {
slime73@9552
   245
                if (atleastiOS8 && image.size.width < image.size.height) {
slime73@9552
   246
                    /* On iOS 8, portrait launch images displayed in forced-
slime73@9552
   247
                     * landscape mode (e.g. a standard Default.png on an iPhone
slime73@9552
   248
                     * when Info.plist only supports landscape orientations) need
slime73@9552
   249
                     * to be rotated to display in the expected orientation. */
slime73@9552
   250
                    if (curorient == UIInterfaceOrientationLandscapeLeft) {
slime73@9552
   251
                        imageorient = UIImageOrientationRight;
slime73@9552
   252
                    } else if (curorient == UIInterfaceOrientationLandscapeRight) {
slime73@9552
   253
                        imageorient = UIImageOrientationLeft;
slime73@9552
   254
                    }
slime73@9552
   255
                } else if (!atleastiOS8 && image.size.width > image.size.height) {
slime73@9552
   256
                    /* On iOS 7 and below, landscape launch images displayed in
slime73@9552
   257
                     * landscape mode (e.g. landscape iPad launch images) need
slime73@9552
   258
                     * to be rotated to display in the expected orientation. */
slime73@9552
   259
                    if (curorient == UIInterfaceOrientationLandscapeLeft) {
slime73@9552
   260
                        imageorient = UIImageOrientationLeft;
slime73@9552
   261
                    } else if (curorient == UIInterfaceOrientationLandscapeRight) {
slime73@9552
   262
                        imageorient = UIImageOrientationRight;
slime73@9552
   263
                    }
slime73@9552
   264
                }
slime73@9534
   265
            }
slime73@10371
   266
#endif
slime73@9534
   267
slime73@9552
   268
            /* Create the properly oriented image. */
slime73@9552
   269
            view.image = [[UIImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:imageorient];
slime73@9552
   270
slime73@9552
   271
            self.view = view;
slime73@9532
   272
        }
slime73@9532
   273
    }
slime73@9532
   274
slime73@9532
   275
    return self;
slime73@9532
   276
}
slime73@9532
   277
slime73@9532
   278
- (void)loadView
slime73@9532
   279
{
slime73@9532
   280
    /* Do nothing. */
slime73@9532
   281
}
slime73@9532
   282
slime73@10340
   283
#if !TARGET_OS_TV
slime73@9552
   284
- (BOOL)shouldAutorotate
slime73@9552
   285
{
slime73@9552
   286
    /* If YES, the launch image will be incorrectly rotated in some cases. */
slime73@9552
   287
    return NO;
slime73@9552
   288
}
slime73@9552
   289
slime73@9532
   290
- (NSUInteger)supportedInterfaceOrientations
slime73@9532
   291
{
slime73@9552
   292
    /* We keep the supported orientations unrestricted to avoid the case where
slime73@9552
   293
     * there are no common orientations between the ones set in Info.plist and
slime73@9552
   294
     * the ones set here (it will cause an exception in that case.) */
slime73@9552
   295
    return UIInterfaceOrientationMaskAll;
slime73@9532
   296
}
slime73@10340
   297
#endif /* !TARGET_OS_TV */
slime73@9532
   298
slime73@9532
   299
@end
slime73@9532
   300
slime73@9532
   301
@implementation SDLUIKitDelegate {
slime73@9532
   302
    UIWindow *launchWindow;
slime73@9532
   303
}
slouken@2765
   304
slouken@2765
   305
/* convenience method */
slime73@9532
   306
+ (id)sharedAppDelegate
kees@6003
   307
{
slime73@9532
   308
    /* the delegate is set in UIApplicationMain(), which is guaranteed to be
slime73@9532
   309
     * called before this method */
slime73@9532
   310
    return [UIApplication sharedApplication].delegate;
slouken@2765
   311
}
slouken@2765
   312
kees@6003
   313
+ (NSString *)getAppDelegateClassName
kees@6003
   314
{
slime73@9532
   315
    /* subclassing notice: when you subclass this appdelegate, make sure to add
slime73@9532
   316
     * a category to override this method and return the actual name of the
slime73@9532
   317
     * delegate */
slouken@5532
   318
    return @"SDLUIKitDelegate";
slouken@5532
   319
}
slouken@5532
   320
slime73@9532
   321
- (void)hideLaunchScreen
kees@6003
   322
{
slime73@9532
   323
    UIWindow *window = launchWindow;
slime73@9532
   324
slime73@9532
   325
    if (!window || window.hidden) {
slime73@9532
   326
        return;
slime73@9532
   327
    }
slime73@9532
   328
slime73@9532
   329
    launchWindow = nil;
slime73@9532
   330
slime73@9532
   331
    /* Do a nice animated fade-out (roughly matches the real launch behavior.) */
slime73@9532
   332
    [UIView animateWithDuration:0.2 animations:^{
slime73@9532
   333
        window.alpha = 0.0;
slime73@9532
   334
    } completion:^(BOOL finished) {
slime73@9532
   335
        window.hidden = YES;
slime73@9532
   336
    }];
slouken@2765
   337
}
slouken@2765
   338
kees@6003
   339
- (void)postFinishLaunch
kees@6003
   340
{
slime73@9532
   341
    /* Hide the launch screen the next time the run loop is run. SDL apps will
slime73@9532
   342
     * have a chance to load resources while the launch screen is still up. */
slime73@9532
   343
    [self performSelector:@selector(hideLaunchScreen) withObject:nil afterDelay:0.0];
slime73@9532
   344
slouken@5131
   345
    /* run the user's application, passing argc and argv */
slouken@6346
   346
    SDL_iPhoneSetEventPump(SDL_TRUE);
kees@6006
   347
    exit_status = SDL_main(forward_argc, forward_argv);
slouken@6346
   348
    SDL_iPhoneSetEventPump(SDL_FALSE);
kees@6001
   349
slime73@9532
   350
    if (launchWindow) {
slime73@9532
   351
        launchWindow.hidden = YES;
slime73@9532
   352
        launchWindow = nil;
slime73@9532
   353
    }
slime73@9532
   354
slouken@5131
   355
    /* exit, passing the return status from the user's application */
slime73@9532
   356
    /* We don't actually exit to support applications that do setup in their
slime73@9532
   357
     * main function and then allow the Cocoa event loop to run. */
slouken@7191
   358
    /* exit(exit_status); */
slouken@3422
   359
}
slouken@3422
   360
kees@6003
   361
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
kees@6003
   362
{
slime73@9532
   363
    NSBundle *bundle = [NSBundle mainBundle];
slime73@9532
   364
slime73@9532
   365
#if SDL_IPHONE_LAUNCHSCREEN
slime73@9532
   366
    /* The normal launch screen is displayed until didFinishLaunching returns,
slime73@9532
   367
     * but SDL_main is called after that happens and there may be a noticeable
slime73@9532
   368
     * delay between the start of SDL_main and when the first real frame is
slime73@9532
   369
     * displayed (e.g. if resources are loaded before SDL_GL_SwapWindow is
slime73@9532
   370
     * called), so we show the launch screen programmatically until the first
slime73@9532
   371
     * time events are pumped. */
slime73@10368
   372
    UIViewController *vc = nil;
slime73@10371
   373
    NSString *screenname = nil;
slime73@9532
   374
slime73@10371
   375
    /* tvOS only uses a plain launch image. */
slime73@10369
   376
#if !TARGET_OS_TV
slime73@10371
   377
    screenname = [bundle objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
slime73@10368
   378
slime73@10368
   379
    if (screenname && UIKit_IsSystemVersionAtLeast(8.0)) {
slime73@10368
   380
        @try {
slime73@10368
   381
            /* The launch storyboard is actually a nib in some older versions of
slime73@10368
   382
             * Xcode. We'll try to load it as a storyboard first, as it's more
slime73@10368
   383
             * modern. */
slime73@10368
   384
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:screenname bundle:bundle];
slime73@10368
   385
            vc = [storyboard instantiateInitialViewController];
slime73@10368
   386
        }
slime73@10368
   387
        @catch (NSException *exception) {
slime73@10368
   388
            /* Do nothing (there's more code to execute below). */
slime73@10368
   389
        }
slime73@10368
   390
    }
slime73@10371
   391
#endif
slime73@10368
   392
slime73@10368
   393
    if (vc == nil) {
slime73@10368
   394
        vc = [[SDLLaunchScreenController alloc] initWithNibName:screenname bundle:bundle];
slime73@10368
   395
    }
slime73@10368
   396
slime73@10368
   397
    if (vc.view) {
slime73@9532
   398
        launchWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
slime73@9532
   399
slime73@9532
   400
        /* We don't want the launch window immediately hidden when a real SDL
slime73@9532
   401
         * window is shown - we fade it out ourselves when we're ready. */
slime73@9532
   402
        launchWindow.windowLevel = UIWindowLevelNormal + 1.0;
slime73@9532
   403
slime73@9532
   404
        /* Show the window but don't make it key. Events should always go to
slime73@9532
   405
         * other windows when possible. */
slime73@9532
   406
        launchWindow.hidden = NO;
slime73@9532
   407
slime73@10368
   408
        launchWindow.rootViewController = vc;
slime73@9532
   409
    }
slime73@9532
   410
#endif
slime73@9532
   411
slouken@5131
   412
    /* Set working directory to resource path */
slime73@9532
   413
    [[NSFileManager defaultManager] changeCurrentDirectoryPath:[bundle resourcePath]];
kees@6001
   414
slouken@6349
   415
    /* register a callback for the idletimer hint */
slouken@7432
   416
    SDL_AddHintCallback(SDL_HINT_IDLE_TIMER_DISABLED,
slouken@7432
   417
                        SDL_IdleTimerDisabledChanged, NULL);
slouken@6349
   418
slouken@7355
   419
    SDL_SetMainReady();
slouken@5532
   420
    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
slime73@9534
   421
slouken@5487
   422
    return YES;
slouken@2765
   423
}
slouken@2765
   424
slime73@10709
   425
- (UIWindow *)window
slime73@10709
   426
{
slime73@10709
   427
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slime73@10709
   428
    if (_this) {
slime73@10709
   429
        SDL_Window *window = NULL;
slime73@10709
   430
        for (window = _this->windows; window != NULL; window = window->next) {
slime73@10709
   431
            SDL_WindowData *data = (__bridge SDL_WindowData *) window->driverdata;
slime73@10709
   432
            if (data != nil) {
slime73@10709
   433
                return data.uiwindow;
slime73@10709
   434
            }
slime73@10709
   435
        }
slime73@10709
   436
    }
slime73@10709
   437
    return nil;
slime73@10709
   438
}
slime73@10709
   439
slime73@10709
   440
- (void)setWindow:(UIWindow *)window
slime73@10709
   441
{
slime73@10709
   442
    /* Do nothing. */
slime73@10709
   443
}
slime73@10709
   444
slime73@10340
   445
#if !TARGET_OS_TV
slime73@9504
   446
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
slime73@9504
   447
{
slouken@12201
   448
    SDL_OnApplicationDidChangeStatusBarOrientation();
slime73@9504
   449
}
slime73@10340
   450
#endif
slime73@9504
   451
slouken@10995
   452
- (void)applicationWillTerminate:(UIApplication *)application
slouken@10995
   453
{
slouken@10995
   454
    SDL_OnApplicationWillTerminate();
slouken@10995
   455
}
slouken@10995
   456
slouken@10995
   457
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
slouken@10995
   458
{
slouken@10995
   459
    SDL_OnApplicationDidReceiveMemoryWarning();
slouken@10995
   460
}
slouken@10995
   461
slime73@9532
   462
- (void)applicationWillResignActive:(UIApplication*)application
slouken@3400
   463
{
slouken@10995
   464
    SDL_OnApplicationWillResignActive();
slouken@7190
   465
}
icculus@4446
   466
slime73@9532
   467
- (void)applicationDidEnterBackground:(UIApplication*)application
slouken@7190
   468
{
slouken@10995
   469
    SDL_OnApplicationDidEnterBackground();
slouken@7190
   470
}
kees@6001
   471
slime73@9532
   472
- (void)applicationWillEnterForeground:(UIApplication*)application
slouken@7190
   473
{
slouken@10995
   474
    SDL_OnApplicationWillEnterForeground();
slouken@3400
   475
}
slouken@3400
   476
slime73@9532
   477
- (void)applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   478
{
slouken@10995
   479
    SDL_OnApplicationDidBecomeActive();
slouken@2765
   480
}
slouken@2765
   481
slime73@10350
   482
- (void)sendDropFileForURL:(NSURL *)url
slouken@6587
   483
{
slime73@9532
   484
    NSURL *fileURL = url.filePathURL;
slouken@6587
   485
    if (fileURL != nil) {
slime73@10350
   486
        SDL_SendDropFile(NULL, fileURL.path.UTF8String);
slouken@6587
   487
    } else {
slime73@10350
   488
        SDL_SendDropFile(NULL, url.absoluteString.UTF8String);
slouken@6587
   489
    }
icculus@10031
   490
    SDL_SendDropComplete(NULL);
slime73@10350
   491
}
slime73@10350
   492
slime73@11147
   493
#if TARGET_OS_TV || (defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0)
slime73@11147
   494
slime73@10350
   495
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
slime73@10350
   496
{
slime73@10350
   497
    /* TODO: Handle options */
slime73@10350
   498
    [self sendDropFileForURL:url];
slouken@6587
   499
    return YES;
slouken@6587
   500
}
slime73@10350
   501
slime73@11147
   502
#else
slime73@11147
   503
slime73@10350
   504
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
slime73@10350
   505
{
slime73@10350
   506
    [self sendDropFileForURL:url];
slime73@10350
   507
    return YES;
slime73@10350
   508
}
slime73@11147
   509
slime73@11147
   510
#endif
slouken@6587
   511
slouken@2765
   512
@end
slouken@5132
   513
slouken@6044
   514
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   515
slouken@5132
   516
/* vi: set ts=4 sw=4 expandtab: */