src/video/uikit/SDL_uikitappdelegate.m
author Kees Bakker <kees@ijzerbout.nl>
Tue, 27 Sep 2011 21:02:26 +0200
changeset 6001 3ae3d1e58d59
parent 5614 50bf98210aae
child 6003 fddf81967e2d
permissions -rw-r--r--
Cleanup out trailing whitespace in src/video/uikit
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@2765
    21
icculus@4446
    22
#import "../SDL_sysvideo.h"
tim@5555
    23
#import "SDL_assert.h"
tim@5555
    24
#import "SDL_hints.h"
tim@5555
    25
#import "../../SDL_hints_c.h"
icculus@4446
    26
slouken@2765
    27
#import "SDL_uikitappdelegate.h"
slouken@2765
    28
#import "SDL_uikitopenglview.h"
slouken@2765
    29
#import "SDL_events_c.h"
slouken@2765
    30
#import "jumphack.h"
slouken@2765
    31
slouken@2765
    32
#ifdef main
slouken@2765
    33
#undef main
slouken@2765
    34
#endif
slouken@2765
    35
slouken@2765
    36
extern int SDL_main(int argc, char *argv[]);
slouken@2765
    37
static int forward_argc;
slouken@2765
    38
static char **forward_argv;
slouken@2765
    39
slouken@2765
    40
int main(int argc, char **argv) {
slouken@2765
    41
slouken@5131
    42
    int i;
slouken@5131
    43
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
kees@6001
    44
slouken@5131
    45
    /* store arguments */
slouken@5131
    46
    forward_argc = argc;
slouken@5131
    47
    forward_argv = (char **)malloc((argc+1) * sizeof(char *));
slouken@5131
    48
    for (i=0; i<argc; i++) {
slouken@5131
    49
        forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
slouken@5131
    50
        strcpy(forward_argv[i], argv[i]);
slouken@5131
    51
    }
slouken@5131
    52
    forward_argv[i] = NULL;
slouken@2765
    53
slouken@5131
    54
    /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
slouken@5532
    55
    UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
kees@6001
    56
slouken@5131
    57
    [pool release];
slouken@5332
    58
    return 0;
slouken@2765
    59
}
slouken@2765
    60
tim@5555
    61
static void SDL_IdleTimerDisabledChanged(const char *name, const char *oldValue, const char *newValue) {
tim@5555
    62
    SDL_assert(SDL_strcmp(name, SDL_HINT_IDLE_TIMER_DISABLED) == 0);
kees@6001
    63
tim@5555
    64
    BOOL disable = (*newValue != '0');
tim@5555
    65
    [UIApplication sharedApplication].idleTimerDisabled = disable;
tim@5555
    66
}
tim@5555
    67
slouken@2765
    68
@implementation SDLUIKitDelegate
slouken@2765
    69
slouken@2765
    70
/* convenience method */
slouken@2765
    71
+(SDLUIKitDelegate *)sharedAppDelegate {
slouken@5131
    72
    /* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
slouken@5131
    73
    return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
slouken@2765
    74
}
slouken@2765
    75
slouken@5532
    76
+(NSString *)getAppDelegateClassName {
slouken@5532
    77
    /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
slouken@5532
    78
       this method and return the actual name of the delegate */
slouken@5532
    79
    return @"SDLUIKitDelegate";
slouken@5532
    80
}
slouken@5532
    81
slouken@2765
    82
- (id)init {
slouken@5131
    83
    self = [super init];
slouken@5131
    84
    return self;
slouken@2765
    85
}
slouken@2765
    86
slouken@3422
    87
- (void)postFinishLaunch {
kees@6001
    88
tim@5555
    89
    /* register a callback for the idletimer hint */
tim@5555
    90
    SDL_SetHint(SDL_HINT_IDLE_TIMER_DISABLED, "0");
tim@5555
    91
    SDL_RegisterHintChangedCb(SDL_HINT_IDLE_TIMER_DISABLED, &SDL_IdleTimerDisabledChanged);
slouken@3422
    92
slouken@5131
    93
    /* run the user's application, passing argc and argv */
slouken@5131
    94
    int exit_status = SDL_main(forward_argc, forward_argv);
kees@6001
    95
slouken@5131
    96
    /* free the memory we used to hold copies of argc and argv */
slouken@5131
    97
    int i;
slouken@5131
    98
    for (i=0; i<forward_argc; i++) {
slouken@5131
    99
        free(forward_argv[i]);
slouken@5131
   100
    }
kees@6001
   101
    free(forward_argv);
kees@6001
   102
slouken@5131
   103
    /* exit, passing the return status from the user's application */
slouken@5131
   104
    exit(exit_status);
slouken@3422
   105
}
slouken@3422
   106
slouken@5487
   107
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
kees@6001
   108
slouken@5131
   109
    /* Set working directory to resource path */
slouken@5131
   110
    [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
kees@6001
   111
slouken@5532
   112
    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
slouken@5487
   113
slouken@5487
   114
    return YES;
slouken@2765
   115
}
slouken@2765
   116
slouken@2765
   117
- (void)applicationWillTerminate:(UIApplication *)application {
kees@6001
   118
slouken@5131
   119
    SDL_SendQuit();
slouken@5131
   120
     /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
slouken@5131
   121
    longjmp(*(jump_env()), 1);
slouken@2765
   122
}
slouken@2765
   123
slouken@3400
   124
- (void) applicationWillResignActive:(UIApplication*)application
slouken@3400
   125
{
icculus@4446
   126
    //NSLog(@"%@", NSStringFromSelector(_cmd));
icculus@4446
   127
icculus@4446
   128
    // Send every window on every screen a MINIMIZED event.
icculus@4446
   129
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   130
    if (!_this) {
icculus@4446
   131
        return;
icculus@4446
   132
    }
kees@6001
   133
slouken@5532
   134
    SDL_Window *window;
slouken@5251
   135
    for (window = _this->windows; window != nil; window = window->next) {
slouken@5251
   136
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
icculus@4446
   137
    }
slouken@3400
   138
}
slouken@3400
   139
slouken@3400
   140
- (void) applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   141
{
icculus@4446
   142
    //NSLog(@"%@", NSStringFromSelector(_cmd));
slouken@3400
   143
icculus@4446
   144
    // Send every window on every screen a RESTORED event.
icculus@4446
   145
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   146
    if (!_this) {
icculus@4446
   147
        return;
icculus@4446
   148
    }
kees@6001
   149
kees@5614
   150
    SDL_Window *window;
slouken@5251
   151
    for (window = _this->windows; window != nil; window = window->next) {
kees@5614
   152
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@4446
   153
    }
slouken@2765
   154
}
slouken@2765
   155
slouken@2765
   156
@end
slouken@5132
   157
slouken@5132
   158
/* vi: set ts=4 sw=4 expandtab: */