src/video/uikit/SDL_uikitappdelegate.m
author Alex Szpakowski <slime73@gmail.com>
Thu, 20 Nov 2014 18:45:04 -0400
branchiOS-improvements
changeset 9524 9de159f9cefd
parent 9518 4bd15ffec1ad
child 9531 b7ad394978b1
permissions -rw-r--r--
Minor objective-c code style improvements
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_UIKIT
    24 
    25 #include "../SDL_sysvideo.h"
    26 #include "SDL_assert.h"
    27 #include "SDL_hints.h"
    28 #include "SDL_system.h"
    29 #include "SDL_main.h"
    30 
    31 #include "SDL_uikitappdelegate.h"
    32 #include "SDL_uikitmodes.h"
    33 #include "../../events/SDL_events_c.h"
    34 
    35 #ifdef main
    36 #undef main
    37 #endif
    38 
    39 static int forward_argc;
    40 static char **forward_argv;
    41 static int exit_status;
    42 
    43 int main(int argc, char **argv)
    44 {
    45     int i;
    46 
    47     /* store arguments */
    48     forward_argc = argc;
    49     forward_argv = (char **)malloc((argc+1) * sizeof(char *));
    50     for (i = 0; i < argc; i++) {
    51         forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
    52         strcpy(forward_argv[i], argv[i]);
    53     }
    54     forward_argv[i] = NULL;
    55 
    56     /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
    57     @autoreleasepool {
    58         UIApplicationMain(argc, argv, nil, [SDLUIKitDelegate getAppDelegateClassName]);
    59     }
    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     return exit_status;
    68 }
    69 
    70 static void
    71 SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    72 {
    73     BOOL disable = (hint && *hint != '0');
    74     [UIApplication sharedApplication].idleTimerDisabled = disable;
    75 }
    76 
    77 @implementation SDLUIKitDelegate
    78 
    79 /* convenience method */
    80 + (id) sharedAppDelegate
    81 {
    82     /* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */
    83     return [[UIApplication sharedApplication] delegate];
    84 }
    85 
    86 + (NSString *)getAppDelegateClassName
    87 {
    88     /* subclassing notice: when you subclass this appdelegate, make sure to add a category to override
    89        this method and return the actual name of the delegate */
    90     return @"SDLUIKitDelegate";
    91 }
    92 
    93 - (id)init
    94 {
    95     self = [super init];
    96     return self;
    97 }
    98 
    99 - (void)postFinishLaunch
   100 {
   101     /* run the user's application, passing argc and argv */
   102     SDL_iPhoneSetEventPump(SDL_TRUE);
   103     exit_status = SDL_main(forward_argc, forward_argv);
   104     SDL_iPhoneSetEventPump(SDL_FALSE);
   105 
   106     /* exit, passing the return status from the user's application */
   107     /* We don't actually exit to support applications that do setup in
   108      * their main function and then allow the Cocoa event loop to run.
   109      */
   110     /* exit(exit_status); */
   111 }
   112 
   113 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   114 {
   115     /* Set working directory to resource path */
   116     [[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
   117 
   118     /* register a callback for the idletimer hint */
   119     SDL_AddHintCallback(SDL_HINT_IDLE_TIMER_DISABLED,
   120                         SDL_IdleTimerDisabledChanged, NULL);
   121 
   122     SDL_SetMainReady();
   123     [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
   124 
   125     return YES;
   126 }
   127 
   128 - (void)applicationWillTerminate:(UIApplication *)application
   129 {
   130     SDL_SendAppEvent(SDL_APP_TERMINATING);
   131 }
   132 
   133 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
   134 {
   135     SDL_SendAppEvent(SDL_APP_LOWMEMORY);
   136 }
   137 
   138 - (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
   139 {
   140     BOOL isLandscape = UIInterfaceOrientationIsLandscape(application.statusBarOrientation);
   141     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   142 
   143     if (_this && _this->num_displays > 0) {
   144         SDL_DisplayMode *desktopmode = &_this->displays[0].desktop_mode;
   145         SDL_DisplayMode *currentmode = &_this->displays[0].current_mode;
   146 
   147         /* The desktop display mode should be kept in sync with the screen
   148          * orientation so that updating a window's fullscreen state to
   149          * SDL_WINDOW_FULLSCREEN_DESKTOP keeps the window dimensions in the
   150          * correct orientation.
   151          */
   152         if (isLandscape != (desktopmode->w > desktopmode->h)) {
   153             int height = desktopmode->w;
   154             desktopmode->w = desktopmode->h;
   155             desktopmode->h = height;
   156         }
   157 
   158         /* Same deal with the current mode + SDL_GetCurrentDisplayMode. */
   159         if (isLandscape != (currentmode->w > currentmode->h)) {
   160             int height = currentmode->w;
   161             currentmode->w = currentmode->h;
   162             currentmode->h = height;
   163         }
   164     }
   165 }
   166 
   167 - (void) applicationWillResignActive:(UIApplication*)application
   168 {
   169     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   170     if (_this) {
   171         SDL_Window *window;
   172         for (window = _this->windows; window != nil; window = window->next) {
   173             SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
   174             SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   175         }
   176     }
   177     SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
   178 }
   179 
   180 - (void) applicationDidEnterBackground:(UIApplication*)application
   181 {
   182     SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
   183 }
   184 
   185 - (void) applicationWillEnterForeground:(UIApplication*)application
   186 {
   187     SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
   188 }
   189 
   190 - (void) applicationDidBecomeActive:(UIApplication*)application
   191 {
   192     SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
   193 
   194     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   195     if (_this) {
   196         SDL_Window *window;
   197         for (window = _this->windows; window != nil; window = window->next) {
   198             SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
   199             SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   200         }
   201     }
   202 }
   203 
   204 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
   205 {
   206     NSURL *fileURL = [url filePathURL];
   207     if (fileURL != nil) {
   208         SDL_SendDropFile([[fileURL path] UTF8String]);
   209     } else {
   210         SDL_SendDropFile([[url absoluteString] UTF8String]);
   211     }
   212     return YES;
   213 }
   214 
   215 @end
   216 
   217 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   218 
   219 /* vi: set ts=4 sw=4 expandtab: */