src/video/uikit/SDL_uikitappdelegate.m
author Alex Szpakowski <slime73@gmail.com>
Thu, 20 Nov 2014 18:45:04 -0400
branchiOS-improvements
changeset 9524 9de159f9cefd
parent 9518 4bd15ffec1ad
child 9531 b7ad394978b1
permissions -rw-r--r--
Minor objective-c code style improvements
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
slouken@6556
    31
#include "SDL_uikitappdelegate.h"
slouken@6556
    32
#include "SDL_uikitmodes.h"
slouken@6556
    33
#include "../../events/SDL_events_c.h"
slouken@2765
    34
slouken@2765
    35
#ifdef main
slouken@2765
    36
#undef main
slouken@2765
    37
#endif
slouken@2765
    38
slouken@2765
    39
static int forward_argc;
slouken@2765
    40
static char **forward_argv;
kees@6006
    41
static int exit_status;
slouken@2765
    42
kees@6003
    43
int main(int argc, char **argv)
kees@6003
    44
{
slouken@5131
    45
    int i;
kees@6001
    46
slouken@5131
    47
    /* store arguments */
slouken@5131
    48
    forward_argc = argc;
slouken@5131
    49
    forward_argv = (char **)malloc((argc+1) * sizeof(char *));
kees@6003
    50
    for (i = 0; i < argc; i++) {
slouken@5131
    51
        forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
slouken@5131
    52
        strcpy(forward_argv[i], argv[i]);
slouken@5131
    53
    }
slouken@5131
    54
    forward_argv[i] = NULL;
slouken@2765
    55
slouken@5131
    56
    /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
slime73@9487
    57
    @autoreleasepool {
slime73@9510
    58
        UIApplicationMain(argc, argv, nil, [SDLUIKitDelegate getAppDelegateClassName]);
slime73@9487
    59
    }
kees@6001
    60
kees@6006
    61
    /* free the memory we used to hold copies of argc and argv */
kees@6006
    62
    for (i = 0; i < forward_argc; i++) {
kees@6006
    63
        free(forward_argv[i]);
kees@6006
    64
    }
kees@6006
    65
    free(forward_argv);
kees@6006
    66
kees@6006
    67
    return exit_status;
slouken@2765
    68
}
slouken@2765
    69
slouken@7432
    70
static void
slouken@7432
    71
SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
kees@6003
    72
{
slouken@7432
    73
    BOOL disable = (hint && *hint != '0');
tim@5555
    74
    [UIApplication sharedApplication].idleTimerDisabled = disable;
tim@5555
    75
}
tim@5555
    76
slouken@2765
    77
@implementation SDLUIKitDelegate
slouken@2765
    78
slouken@2765
    79
/* convenience method */
icculus@6090
    80
+ (id) sharedAppDelegate
kees@6003
    81
{
slime73@9487
    82
    /* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */
icculus@6090
    83
    return [[UIApplication sharedApplication] delegate];
slouken@2765
    84
}
slouken@2765
    85
kees@6003
    86
+ (NSString *)getAppDelegateClassName
kees@6003
    87
{
slouken@5532
    88
    /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
slouken@5532
    89
       this method and return the actual name of the delegate */
slouken@5532
    90
    return @"SDLUIKitDelegate";
slouken@5532
    91
}
slouken@5532
    92
kees@6003
    93
- (id)init
kees@6003
    94
{
slouken@5131
    95
    self = [super init];
slouken@5131
    96
    return self;
slouken@2765
    97
}
slouken@2765
    98
kees@6003
    99
- (void)postFinishLaunch
kees@6003
   100
{
slouken@5131
   101
    /* run the user's application, passing argc and argv */
slouken@6346
   102
    SDL_iPhoneSetEventPump(SDL_TRUE);
kees@6006
   103
    exit_status = SDL_main(forward_argc, forward_argv);
slouken@6346
   104
    SDL_iPhoneSetEventPump(SDL_FALSE);
kees@6001
   105
slouken@5131
   106
    /* exit, passing the return status from the user's application */
slouken@7191
   107
    /* We don't actually exit to support applications that do setup in
slouken@7191
   108
     * their main function and then allow the Cocoa event loop to run.
slouken@7191
   109
     */
slouken@7191
   110
    /* exit(exit_status); */
slouken@3422
   111
}
slouken@3422
   112
kees@6003
   113
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
kees@6003
   114
{
slouken@5131
   115
    /* Set working directory to resource path */
slime73@9524
   116
    [[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
kees@6001
   117
slouken@6349
   118
    /* register a callback for the idletimer hint */
slouken@7432
   119
    SDL_AddHintCallback(SDL_HINT_IDLE_TIMER_DISABLED,
slouken@7432
   120
                        SDL_IdleTimerDisabledChanged, NULL);
slouken@6349
   121
slouken@7355
   122
    SDL_SetMainReady();
slouken@5532
   123
    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
slouken@5487
   124
slouken@5487
   125
    return YES;
slouken@2765
   126
}
slouken@2765
   127
kees@6003
   128
- (void)applicationWillTerminate:(UIApplication *)application
kees@6003
   129
{
slouken@7190
   130
    SDL_SendAppEvent(SDL_APP_TERMINATING);
slouken@7190
   131
}
slouken@7190
   132
slouken@7190
   133
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
slouken@7190
   134
{
slouken@7190
   135
    SDL_SendAppEvent(SDL_APP_LOWMEMORY);
slouken@2765
   136
}
slouken@2765
   137
slime73@9504
   138
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
slime73@9504
   139
{
slime73@9505
   140
    BOOL isLandscape = UIInterfaceOrientationIsLandscape(application.statusBarOrientation);
slime73@9504
   141
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slime73@9504
   142
slime73@9504
   143
    if (_this && _this->num_displays > 0) {
slime73@9505
   144
        SDL_DisplayMode *desktopmode = &_this->displays[0].desktop_mode;
slime73@9505
   145
        SDL_DisplayMode *currentmode = &_this->displays[0].current_mode;
slime73@9504
   146
slime73@9504
   147
        /* The desktop display mode should be kept in sync with the screen
slime73@9504
   148
         * orientation so that updating a window's fullscreen state to
slime73@9504
   149
         * SDL_WINDOW_FULLSCREEN_DESKTOP keeps the window dimensions in the
slime73@9504
   150
         * correct orientation.
slime73@9504
   151
         */
slime73@9505
   152
        if (isLandscape != (desktopmode->w > desktopmode->h)) {
slime73@9505
   153
            int height = desktopmode->w;
slime73@9505
   154
            desktopmode->w = desktopmode->h;
slime73@9505
   155
            desktopmode->h = height;
slime73@9505
   156
        }
slime73@9505
   157
slime73@9505
   158
        /* Same deal with the current mode + SDL_GetCurrentDisplayMode. */
slime73@9505
   159
        if (isLandscape != (currentmode->w > currentmode->h)) {
slime73@9505
   160
            int height = currentmode->w;
slime73@9505
   161
            currentmode->w = currentmode->h;
slime73@9505
   162
            currentmode->h = height;
slime73@9504
   163
        }
slime73@9504
   164
    }
slime73@9504
   165
}
slime73@9504
   166
slouken@3400
   167
- (void) applicationWillResignActive:(UIApplication*)application
slouken@3400
   168
{
slouken@7190
   169
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@7190
   170
    if (_this) {
slouken@7190
   171
        SDL_Window *window;
slouken@7190
   172
        for (window = _this->windows; window != nil; window = window->next) {
slouken@7190
   173
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
slouken@7190
   174
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@7190
   175
        }
slouken@7190
   176
    }
slouken@7190
   177
    SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
slouken@7190
   178
}
icculus@4446
   179
slouken@7190
   180
- (void) applicationDidEnterBackground:(UIApplication*)application
slouken@7190
   181
{
slouken@7190
   182
    SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
slouken@7190
   183
}
kees@6001
   184
slouken@7190
   185
- (void) applicationWillEnterForeground:(UIApplication*)application
slouken@7190
   186
{
slouken@7190
   187
    SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
slouken@3400
   188
}
slouken@3400
   189
slouken@3400
   190
- (void) applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   191
{
slouken@7190
   192
    SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
slouken@3400
   193
icculus@4446
   194
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@7190
   195
    if (_this) {
slouken@7190
   196
        SDL_Window *window;
slouken@7190
   197
        for (window = _this->windows; window != nil; window = window->next) {
slouken@7190
   198
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
slouken@7190
   199
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@7190
   200
        }
icculus@4446
   201
    }
slouken@2765
   202
}
slouken@2765
   203
slouken@6587
   204
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
slouken@6587
   205
{
slouken@6587
   206
    NSURL *fileURL = [url filePathURL];
slouken@6587
   207
    if (fileURL != nil) {
slouken@6587
   208
        SDL_SendDropFile([[fileURL path] UTF8String]);
slouken@6587
   209
    } else {
slouken@6587
   210
        SDL_SendDropFile([[url absoluteString] UTF8String]);
slouken@6587
   211
    }
slouken@6587
   212
    return YES;
slouken@6587
   213
}
slouken@6587
   214
slouken@2765
   215
@end
slouken@5132
   216
slouken@6044
   217
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   218
slouken@5132
   219
/* vi: set ts=4 sw=4 expandtab: */