src/video/uikit/SDL_uikitappdelegate.m
author Kees Bakker <kees@ijzerbout.nl>
Tue, 27 Sep 2011 22:51:26 +0200
changeset 6003 fddf81967e2d
parent 6001 3ae3d1e58d59
child 6006 889d89332de4
permissions -rw-r--r--
Use a consistent source code format in src/video/uikit

For example:
* Opening bracket of a method/function on a new line at column 0
* space after "if", "while", etc
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
kees@6003
    40
int main(int argc, char **argv)
kees@6003
    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 *));
kees@6003
    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
kees@6003
    61
static void SDL_IdleTimerDisabledChanged(const char *name, const char *oldValue, const char *newValue)
kees@6003
    62
{
tim@5555
    63
    SDL_assert(SDL_strcmp(name, SDL_HINT_IDLE_TIMER_DISABLED) == 0);
kees@6001
    64
tim@5555
    65
    BOOL disable = (*newValue != '0');
tim@5555
    66
    [UIApplication sharedApplication].idleTimerDisabled = disable;
tim@5555
    67
}
tim@5555
    68
slouken@2765
    69
@implementation SDLUIKitDelegate
slouken@2765
    70
slouken@2765
    71
/* convenience method */
kees@6003
    72
+ (SDLUIKitDelegate *)sharedAppDelegate
kees@6003
    73
{
slouken@5131
    74
    /* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
slouken@5131
    75
    return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
slouken@2765
    76
}
slouken@2765
    77
kees@6003
    78
+ (NSString *)getAppDelegateClassName
kees@6003
    79
{
slouken@5532
    80
    /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
slouken@5532
    81
       this method and return the actual name of the delegate */
slouken@5532
    82
    return @"SDLUIKitDelegate";
slouken@5532
    83
}
slouken@5532
    84
kees@6003
    85
- (id)init
kees@6003
    86
{
slouken@5131
    87
    self = [super init];
slouken@5131
    88
    return self;
slouken@2765
    89
}
slouken@2765
    90
kees@6003
    91
- (void)postFinishLaunch
kees@6003
    92
{
tim@5555
    93
    /* register a callback for the idletimer hint */
tim@5555
    94
    SDL_SetHint(SDL_HINT_IDLE_TIMER_DISABLED, "0");
tim@5555
    95
    SDL_RegisterHintChangedCb(SDL_HINT_IDLE_TIMER_DISABLED, &SDL_IdleTimerDisabledChanged);
slouken@3422
    96
slouken@5131
    97
    /* run the user's application, passing argc and argv */
slouken@5131
    98
    int exit_status = SDL_main(forward_argc, forward_argv);
kees@6001
    99
slouken@5131
   100
    /* free the memory we used to hold copies of argc and argv */
slouken@5131
   101
    int i;
kees@6003
   102
    for (i = 0; i < forward_argc; i++) {
slouken@5131
   103
        free(forward_argv[i]);
slouken@5131
   104
    }
kees@6001
   105
    free(forward_argv);
kees@6001
   106
slouken@5131
   107
    /* exit, passing the return status from the user's application */
slouken@5131
   108
    exit(exit_status);
slouken@3422
   109
}
slouken@3422
   110
kees@6003
   111
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
kees@6003
   112
{
slouken@5131
   113
    /* Set working directory to resource path */
slouken@5131
   114
    [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
kees@6001
   115
slouken@5532
   116
    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
slouken@5487
   117
slouken@5487
   118
    return YES;
slouken@2765
   119
}
slouken@2765
   120
kees@6003
   121
- (void)applicationWillTerminate:(UIApplication *)application
kees@6003
   122
{
slouken@5131
   123
    SDL_SendQuit();
slouken@5131
   124
     /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
slouken@5131
   125
    longjmp(*(jump_env()), 1);
slouken@2765
   126
}
slouken@2765
   127
slouken@3400
   128
- (void) applicationWillResignActive:(UIApplication*)application
slouken@3400
   129
{
icculus@4446
   130
    //NSLog(@"%@", NSStringFromSelector(_cmd));
icculus@4446
   131
icculus@4446
   132
    // Send every window on every screen a MINIMIZED event.
icculus@4446
   133
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   134
    if (!_this) {
icculus@4446
   135
        return;
icculus@4446
   136
    }
kees@6001
   137
slouken@5532
   138
    SDL_Window *window;
slouken@5251
   139
    for (window = _this->windows; window != nil; window = window->next) {
slouken@5251
   140
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
icculus@4446
   141
    }
slouken@3400
   142
}
slouken@3400
   143
slouken@3400
   144
- (void) applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   145
{
icculus@4446
   146
    //NSLog(@"%@", NSStringFromSelector(_cmd));
slouken@3400
   147
icculus@4446
   148
    // Send every window on every screen a RESTORED event.
icculus@4446
   149
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   150
    if (!_this) {
icculus@4446
   151
        return;
icculus@4446
   152
    }
kees@6001
   153
kees@5614
   154
    SDL_Window *window;
slouken@5251
   155
    for (window = _this->windows; window != nil; window = window->next) {
kees@5614
   156
        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@4446
   157
    }
slouken@2765
   158
}
slouken@2765
   159
slouken@2765
   160
@end
slouken@5132
   161
slouken@5132
   162
/* vi: set ts=4 sw=4 expandtab: */