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
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_UIKIT
    24 
    25 #import "../SDL_sysvideo.h"
    26 #import "SDL_assert.h"
    27 #import "SDL_hints.h"
    28 #import "../../SDL_hints_c.h"
    29 
    30 #import "SDL_uikitappdelegate.h"
    31 #import "SDL_uikitopenglview.h"
    32 #import "SDL_events_c.h"
    33 #import "jumphack.h"
    34 
    35 #ifdef main
    36 #undef main
    37 #endif
    38 
    39 extern int SDL_main(int argc, char *argv[]);
    40 static int forward_argc;
    41 static char **forward_argv;
    42 static int exit_status;
    43 
    44 int main(int argc, char **argv)
    45 {
    46     int i;
    47     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    48 
    49     /* store arguments */
    50     forward_argc = argc;
    51     forward_argv = (char **)malloc((argc+1) * sizeof(char *));
    52     for (i = 0; i < argc; i++) {
    53         forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
    54         strcpy(forward_argv[i], argv[i]);
    55     }
    56     forward_argv[i] = NULL;
    57 
    58     /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
    59     UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
    60 
    61     /* free the memory we used to hold copies of argc and argv */
    62     for (i = 0; i < forward_argc; i++) {
    63         free(forward_argv[i]);
    64     }
    65     free(forward_argv);
    66 
    67     [pool release];
    68     return exit_status;
    69 }
    70 
    71 static void SDL_IdleTimerDisabledChanged(const char *name, const char *oldValue, const char *newValue)
    72 {
    73     SDL_assert(SDL_strcmp(name, SDL_HINT_IDLE_TIMER_DISABLED) == 0);
    74 
    75     BOOL disable = (*newValue != '0');
    76     [UIApplication sharedApplication].idleTimerDisabled = disable;
    77 }
    78 
    79 @implementation SDLUIKitDelegate
    80 
    81 /* convenience method */
    82 + (SDLUIKitDelegate *)sharedAppDelegate
    83 {
    84     /* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
    85     return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
    86 }
    87 
    88 + (NSString *)getAppDelegateClassName
    89 {
    90     /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
    91        this method and return the actual name of the delegate */
    92     return @"SDLUIKitDelegate";
    93 }
    94 
    95 - (id)init
    96 {
    97     self = [super init];
    98     return self;
    99 }
   100 
   101 - (void)postFinishLaunch
   102 {
   103     /* register a callback for the idletimer hint */
   104     SDL_SetHint(SDL_HINT_IDLE_TIMER_DISABLED, "0");
   105     SDL_RegisterHintChangedCb(SDL_HINT_IDLE_TIMER_DISABLED, &SDL_IdleTimerDisabledChanged);
   106 
   107     /* run the user's application, passing argc and argv */
   108     exit_status = SDL_main(forward_argc, forward_argv);
   109 
   110     /* exit, passing the return status from the user's application */
   111     // exit(exit_status);
   112 }
   113 
   114 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   115 {
   116     /* Set working directory to resource path */
   117     [[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
   118 
   119     [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
   120 
   121     return YES;
   122 }
   123 
   124 - (void)applicationWillTerminate:(UIApplication *)application
   125 {
   126     SDL_SendQuit();
   127      /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
   128     longjmp(*(jump_env()), 1);
   129 }
   130 
   131 - (void) applicationWillResignActive:(UIApplication*)application
   132 {
   133     //NSLog(@"%@", NSStringFromSelector(_cmd));
   134 
   135     // Send every window on every screen a MINIMIZED event.
   136     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   137     if (!_this) {
   138         return;
   139     }
   140 
   141     SDL_Window *window;
   142     for (window = _this->windows; window != nil; window = window->next) {
   143         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   144     }
   145 }
   146 
   147 - (void) applicationDidBecomeActive:(UIApplication*)application
   148 {
   149     //NSLog(@"%@", NSStringFromSelector(_cmd));
   150 
   151     // Send every window on every screen a RESTORED event.
   152     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   153     if (!_this) {
   154         return;
   155     }
   156 
   157     SDL_Window *window;
   158     for (window = _this->windows; window != nil; window = window->next) {
   159         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   160     }
   161 }
   162 
   163 @end
   164 
   165 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   166 
   167 /* vi: set ts=4 sw=4 expandtab: */