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