src/video/uikit/SDL_uikitappdelegate.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 02 May 2010 05:08:12 -0400
changeset 4446 8b03a20b320f
parent 3685 64ce267332c6
child 4489 2bb1bfeee9e2
permissions -rw-r--r--
Much improved multi-display support for iPad.

Fixes most issues and limitations, I think.
slouken@2765
     1
/*
slouken@2765
     2
 SDL - Simple DirectMedia Layer
slouken@2859
     3
 Copyright (C) 1997-2009 Sam Lantinga
slouken@2765
     4
 
slouken@2765
     5
 This library is free software; you can redistribute it and/or
slouken@2765
     6
 modify it under the terms of the GNU Lesser General Public
slouken@2765
     7
 License as published by the Free Software Foundation; either
slouken@2765
     8
 version 2.1 of the License, or (at your option) any later version.
slouken@2765
     9
 
slouken@2765
    10
 This library is distributed in the hope that it will be useful,
slouken@2765
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2765
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2765
    13
 Lesser General Public License for more details.
slouken@2765
    14
 
slouken@2765
    15
 You should have received a copy of the GNU Lesser General Public
slouken@2765
    16
 License along with this library; if not, write to the Free Software
slouken@2765
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2765
    18
 
slouken@2765
    19
 Sam Lantinga
slouken@2765
    20
 slouken@libsdl.org
slouken@2765
    21
*/
slouken@2765
    22
icculus@4446
    23
#import "../SDL_sysvideo.h"
icculus@4446
    24
slouken@2765
    25
#import "SDL_uikitappdelegate.h"
slouken@2765
    26
#import "SDL_uikitopenglview.h"
slouken@2765
    27
#import "SDL_events_c.h"
slouken@2765
    28
#import "jumphack.h"
slouken@2765
    29
slouken@2765
    30
#ifdef main
slouken@2765
    31
#undef main
slouken@2765
    32
#endif
slouken@2765
    33
slouken@2765
    34
extern int SDL_main(int argc, char *argv[]);
slouken@2765
    35
static int forward_argc;
slouken@2765
    36
static char **forward_argv;
slouken@2765
    37
slouken@2765
    38
int main(int argc, char **argv) {
slouken@2765
    39
slouken@2765
    40
	int i;
slouken@2765
    41
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@2765
    42
	
slouken@2765
    43
	/* store arguments */
slouken@2765
    44
	forward_argc = argc;
slouken@2765
    45
	forward_argv = (char **)malloc(argc * sizeof(char *));
slouken@2765
    46
	for (i=0; i<argc; i++) {
slouken@2765
    47
		forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
slouken@2765
    48
		strcpy(forward_argv[i], argv[i]);
slouken@2765
    49
	}
slouken@2765
    50
slouken@2765
    51
	/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
slouken@2765
    52
	UIApplicationMain(argc, argv, NULL, @"SDLUIKitDelegate");
slouken@2765
    53
	
slouken@2765
    54
	[pool release];
slouken@2765
    55
	
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@2765
    62
	/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
slouken@2765
    63
	return (SDLUIKitDelegate *)[[UIApplication sharedApplication] delegate];
slouken@2765
    64
}
slouken@2765
    65
slouken@2765
    66
- (id)init {
slouken@2765
    67
	self = [super init];
slouken@2765
    68
	return self;
slouken@2765
    69
}
slouken@2765
    70
slouken@3422
    71
- (void)postFinishLaunch {
slouken@3422
    72
slouken@2765
    73
	/* run the user's application, passing argc and argv */
slouken@2765
    74
	int exit_status = SDL_main(forward_argc, forward_argv);
slouken@2765
    75
	
slouken@2765
    76
	/* free the memory we used to hold copies of argc and argv */
slouken@2765
    77
	int i;
slouken@2765
    78
	for (i=0; i<forward_argc; i++) {
slouken@2765
    79
		free(forward_argv[i]);
slouken@2765
    80
	}
slouken@2765
    81
	free(forward_argv);	
slouken@2765
    82
		
slouken@2765
    83
	/* exit, passing the return status from the user's application */
slouken@2765
    84
	exit(exit_status);
slouken@3422
    85
}
slouken@3422
    86
slouken@3422
    87
- (void)applicationDidFinishLaunching:(UIApplication *)application {
slouken@3422
    88
			
slouken@3422
    89
	/* Set working directory to resource path */
slouken@3422
    90
	[[NSFileManager defaultManager] changeCurrentDirectoryPath: [[NSBundle mainBundle] resourcePath]];
slouken@3422
    91
	
slouken@3422
    92
	[self performSelector:@selector(postFinishLaunch) withObject:nil
slouken@3422
    93
afterDelay:0.0];
slouken@2765
    94
}
slouken@2765
    95
slouken@2765
    96
- (void)applicationWillTerminate:(UIApplication *)application {
slouken@2765
    97
	
slouken@2765
    98
	SDL_SendQuit();
slouken@2765
    99
	 /* hack to prevent automatic termination.  See SDL_uikitevents.m for details */
slouken@2765
   100
	longjmp(*(jump_env()), 1);
slouken@2765
   101
	
slouken@2765
   102
}
slouken@2765
   103
slouken@3400
   104
- (void) applicationWillResignActive:(UIApplication*)application
slouken@3400
   105
{
icculus@4446
   106
    //NSLog(@"%@", NSStringFromSelector(_cmd));
icculus@4446
   107
icculus@4446
   108
    // Send every window on every screen a MINIMIZED event.
icculus@4446
   109
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   110
    if (!_this) {
icculus@4446
   111
        return;
icculus@4446
   112
    }
icculus@4446
   113
icculus@4446
   114
    int i;
icculus@4446
   115
    for (i = 0; i < _this->num_displays; i++) {
icculus@4446
   116
        const SDL_VideoDisplay *display = &_this->displays[i];
icculus@4446
   117
        SDL_Window *window;
icculus@4446
   118
        for (window = display->windows; window != nil; window = window->next) {
icculus@4446
   119
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
icculus@4446
   120
        }
icculus@4446
   121
    }
slouken@3400
   122
}
slouken@3400
   123
slouken@3400
   124
- (void) applicationDidBecomeActive:(UIApplication*)application
slouken@3400
   125
{
icculus@4446
   126
    //NSLog(@"%@", NSStringFromSelector(_cmd));
slouken@3400
   127
icculus@4446
   128
    // Send every window on every screen a RESTORED event.
icculus@4446
   129
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
icculus@4446
   130
    if (!_this) {
icculus@4446
   131
        return;
icculus@4446
   132
    }
slouken@3400
   133
icculus@4446
   134
    int i;
icculus@4446
   135
    for (i = 0; i < _this->num_displays; i++) {
icculus@4446
   136
        const SDL_VideoDisplay *display = &_this->displays[i];
icculus@4446
   137
        SDL_Window *window;
icculus@4446
   138
        for (window = display->windows; window != nil; window = window->next) {
icculus@4446
   139
            SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@4446
   140
        }
icculus@4446
   141
    }
slouken@2765
   142
}
slouken@2765
   143
slouken@2765
   144
@end