From 1fbe66cad0b6947c5b62dea2f9aae6d653d8bb59 Mon Sep 17 00:00:00 2001 From: meancoot Date: Wed, 13 Feb 2013 16:26:12 -0500 Subject: [PATCH] ios: Lifecycle fixes: Fix crash when pressing home button. Add a hack to allow exiting the running game by triple-tapping on the top center of the screen. (Less than 10% from the top, and within the middle 20%) --- ios/RetroArch/RetroArch_iOS.m | 58 +++++++++++++++++++++++++++++------ ios/RetroArch/game_view.m | 32 +++++++++++++++++-- ios/RetroArch/views.h | 5 ++- 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/ios/RetroArch/RetroArch_iOS.m b/ios/RetroArch/RetroArch_iOS.m index 8103fed59a..cdbd5b2159 100644 --- a/ios/RetroArch/RetroArch_iOS.m +++ b/ios/RetroArch/RetroArch_iOS.m @@ -21,6 +21,9 @@ extern bool ios_keys[256]; extern uint32_t ios_current_touch_count; @implementation RetroArch_iOS +{ + game_view* game; +} + (RetroArch_iOS*)get { @@ -29,12 +32,15 @@ extern uint32_t ios_current_touch_count; - (void)runGame:(NSString*)path { - self.window.rootViewController = [[game_view alloc] initWithGame:path]; + game = [[game_view alloc] initWithGame:path]; + self.window.rootViewController = game; self.navigator = nil; } - (void)gameHasExited { + game = nil; + self.navigator = [[UINavigationController alloc] init]; [self.navigator pushViewController: [[module_list alloc] init] animated:YES]; @@ -74,6 +80,22 @@ extern uint32_t ios_current_touch_count; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyReleased:) name: GSEventKeyUpNotification object: nil]; } +- (void)applicationWillEnterForeground:(UIApplication *)application +{ + if (game) + { + [game resume]; + } +} + +- (void)applicationDidEnterBackground:(UIApplication *)application +{ + if (game) + { + [game pause]; + } +} + -(void) keyPressed: (NSNotification*) notification { int keycode = [[notification.userInfo objectForKey:@"keycode"] intValue]; @@ -93,18 +115,34 @@ extern uint32_t ios_current_touch_count; - (void)processTouches:(NSArray*)touches { - ios_current_touch_count = [touches count]; - - for(int i = 0; i != [touches count]; i ++) + if (game) { - UITouch *touch = [touches objectAtIndex:i]; - CGPoint coord = [touch locationInView:self.window.rootViewController.view]; - float scale = [[UIScreen mainScreen] scale]; + ios_current_touch_count = [touches count]; + + for(int i = 0; i != [touches count]; i ++) + { + UITouch *touch = [touches objectAtIndex:i]; + CGPoint coord = [touch locationInView:game.view]; + float scale = [[UIScreen mainScreen] scale]; - ios_touches[i].is_down = (touch.phase != UITouchPhaseEnded) && (touch.phase != UITouchPhaseCancelled); + // Exit hack! + if (touch.tapCount == 3) + { + if (coord.y < game.view.bounds.size.height / 10.0f) + { + float tenpct = game.view.bounds.size.width / 10.0f; + if (coord.x >= tenpct * 4 && coord.x <= tenpct * 6) + { + [game exit]; + } + } + } - ios_touches[i].screen_x = coord.x * scale; - ios_touches[i].screen_y = coord.y * scale; + ios_touches[i].is_down = (touch.phase != UITouchPhaseEnded) && (touch.phase != UITouchPhaseCancelled); + + ios_touches[i].screen_x = coord.x * scale; + ios_touches[i].screen_y = coord.y * scale; + } } } diff --git a/ios/RetroArch/game_view.m b/ios/RetroArch/game_view.m index c97d993461..5bb8cf986a 100644 --- a/ios/RetroArch/game_view.m +++ b/ios/RetroArch/game_view.m @@ -21,10 +21,24 @@ static float screen_scale; static int frame_skips = 4; static bool is_syncing = true; +static bool active_iterate() +{ + while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) == kCFRunLoopRunHandledSource); + return rarch_main_iterate(); +} + +static bool idle_iterate() +{ + CFRunLoopRunInMode(kCFRunLoopDefaultMode, .5, false); + return true; +} + @implementation game_view { EAGLContext *gl_context; NSString* game; + bool paused; + bool exiting; } - (id)initWithGame:(NSString*)path @@ -36,6 +50,21 @@ static bool is_syncing = true; return self; } +- (void)pause +{ + paused = true; +} + +- (void)resume +{ + paused = false; +} + +- (void)exit +{ + exiting = true; +} + - (void)dealloc { if ([EAGLContext currentContext] == gl_context) [EAGLContext setCurrentContext:nil]; @@ -67,8 +96,7 @@ static bool is_syncing = true; if (rarch_main_init_wrap(&main_wrapper) == 0) { rarch_init_msg_queue(); - while (rarch_main_iterate()) - while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) == kCFRunLoopRunHandledSource); + while (!exiting && (paused ? idle_iterate() : active_iterate())); rarch_main_deinit(); rarch_deinit_msg_queue(); diff --git a/ios/RetroArch/views.h b/ios/RetroArch/views.h index aee66d63f8..c9132a2a31 100644 --- a/ios/RetroArch/views.h +++ b/ios/RetroArch/views.h @@ -4,7 +4,10 @@ #include "conf/config_file.h" @interface game_view : UIViewController -- (id)initWithGame:(NSString*)path; +- (id)initWithGame:(NSString*)path;\ +- (void)pause; +- (void)resume; +- (void)exit; @end @interface module_list : UITableViewController