src/video/uikit/SDL_uikitappdelegate.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 6006 889d89332de4
child 6090 687400b595ab
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
*/
slouken@6044
    21
#include "SDL_config.h"
slouken@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2765
    24
icculus@4446
    25
#import "../SDL_sysvideo.h"
tim@5555
    26
#import "SDL_assert.h"
tim@5555
    27
#import "SDL_hints.h"
tim@5555
    28
#import "../../SDL_hints_c.h"
icculus@4446
    29
slouken@2765
    30
#import "SDL_uikitappdelegate.h"
slouken@2765
    31
#import "SDL_uikitopenglview.h"
slouken@2765
    32
#import "SDL_events_c.h"
slouken@2765
    33
#import "jumphack.h"
slouken@2765
    34
slouken@2765
    35
#ifdef main
slouken@2765
    36
#undef main
slouken@2765
    37
#endif
slouken@2765
    38
slouken@2765
    39
extern int SDL_main(int argc, char *argv[]);
slouken@2765
    40
static int forward_argc;
slouken@2765
    41
static char **forward_argv;
kees@6006
    42
static int exit_status;
slouken@2765
    43
kees@6003
    44
int main(int argc, char **argv)
kees@6003
    45
{
slouken@5131
    46
    int i;
slouken@5131
    47
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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 */
slouken@5532
    59
    UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
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
slouken@5131
    67
    [pool release];
kees@6006
    68
    return exit_status;
slouken@2765
    69
}
slouken@2765
    70
kees@6003
    71
static void SDL_IdleTimerDisabledChanged(const char *name, const char *oldValue, const char *newValue)
kees@6003
    72
{
tim@5555
    73
    SDL_assert(SDL_strcmp(name, SDL_HINT_IDLE_TIMER_DISABLED) == 0);
kees@6001
    74
tim@5555
    75
    BOOL disable = (*newValue != '0');
tim@5555
    76
    [UIApplication sharedApplication].idleTimerDisabled = disable;
tim@5555
    77
}
tim@5555
    78
slouken@2765
    79
@implementation SDLUIKitDelegate
slouken@2765
    80
slouken@2765
    81
/* convenience method */
kees@6003
    82
+ (SDLUIKitDelegate *)sharedAppDelegate
kees@6003
    83
{
slouken@5131
    84
    /* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
slouken@5131
    85
    return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
slouken@2765
    86
}
slouken@2765
    87
kees@6003
    88
+ (NSString *)getAppDelegateClassName
kees@6003
    89
{
slouken@5532
    90
    /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
slouken@5532
    91
       this method and return the actual name of the delegate */
slouken@5532
    92
    return @"SDLUIKitDelegate";
slouken@5532
    93
}
slouken@5532
    94
kees@6003
    95
- (id)init
kees@6003
    96
{
slouken@5131
    97
    self = [super init];
slouken@5131
    98
    return self;
slouken@2765
    99
}
slouken@2765
   100
kees@6003
   101
- (void)postFinishLaunch
kees@6003
   102
{
tim@5555
   103
    /* register a callback for the idletimer hint */
tim@5555
   104
    SDL_SetHint(SDL_HINT_IDLE_TIMER_DISABLED, "0");
tim@5555
   105
    SDL_RegisterHintChangedCb(SDL_HINT_IDLE_TIMER_DISABLED, &SDL_IdleTimerDisabledChanged);
slouken@3422
   106
slouken@5131
   107
    /* run the user's application, passing argc and argv */
kees@6006
   108
    exit_status = SDL_main(forward_argc, forward_argv);
kees@6001
   109
slouken@5131
   110
    /* exit, passing the return status from the user's application */
kees@6006
   111
    // exit(exit_status);
slouken@3422
   112
}
slouken@3422
   113
kees@6003
   114
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
kees@6003
   115
{
slouken@5131
   116
    /* Set working directory to resource path */
slouken@5131
   117
    [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
kees@6001
   118
slouken@5532
   119
    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
slouken@5487
   120
slouken@5487
   121
    return YES;
slouken@2765
   122
}
slouken@2765
   123
kees@6003
   124
- (void)applicationWillTerminate:(UIApplication *)application
kees@6003
   125
{
slouken@5131
   126
    SDL_SendQuit();
slouken@5131
   127
     /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
slouken@5131
   128
    longjmp(*(jump_env()), 1);
slouken@2765
   129
}
slouken@2765
   130
slouken@3400
   131
- (void) applicationWillResignActive:(UIApplication*)application
slouken@3400
   132
{
icculus@4446
   133
    //NSLog(@"%@", NSStringFromSelector(_cmd));
icculus@4446
   134
icculus@4446
   135
    // Send every window on every screen a MINIMIZED event.
icculus@4446
   136
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   137
    if (!_this) {
icculus@4446
   138
        return;
icculus@4446
   139
    }
kees@6001
   140
slouken@5532
   141
    SDL_Window *window;
slouken@5251
   142
    for (window = _this->windows; window != nil; window = window->next) {
slouken@5251
   143
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
icculus@4446
   144
    }
slouken@3400
   145
}
slouken@3400
   146
slouken@3400
   147
- (void) applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   148
{
icculus@4446
   149
    //NSLog(@"%@", NSStringFromSelector(_cmd));
slouken@3400
   150
icculus@4446
   151
    // Send every window on every screen a RESTORED event.
icculus@4446
   152
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   153
    if (!_this) {
icculus@4446
   154
        return;
icculus@4446
   155
    }
kees@6001
   156
kees@5614
   157
    SDL_Window *window;
slouken@5251
   158
    for (window = _this->windows; window != nil; window = window->next) {
kees@5614
   159
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@4446
   160
    }
slouken@2765
   161
}
slouken@2765
   162
slouken@2765
   163
@end
slouken@5132
   164
slouken@6044
   165
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   166
slouken@5132
   167
/* vi: set ts=4 sw=4 expandtab: */