SDLPal_AppDelegate.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../src/SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_UIKIT
  20. #include "../src/video/SDL_sysvideo.h"
  21. #include "SDL_assert.h"
  22. #include "SDL_hints.h"
  23. #include "SDL_system.h"
  24. #include "SDL_main.h"
  25. #import "SDLPal_AppDelegate.h"
  26. #import "../src/video/uikit/SDL_uikitmodes.h"
  27. #import "../src/video/uikit/SDL_uikitwindow.h"
  28. #include "../src/events/SDL_events_c.h"
  29. #ifdef main
  30. #undef main
  31. #endif
  32. #include "palcfg.h"
  33. static int forward_argc;
  34. static char **forward_argv;
  35. static int exit_status;
  36. int main(int argc, char **argv)
  37. {
  38. int i;
  39. /* store arguments */
  40. forward_argc = argc;
  41. forward_argv = (char **)malloc((argc+1) * sizeof(char *));
  42. for (i = 0; i < argc; i++) {
  43. forward_argv[i] = malloc( (strlen(argv[i])+1) * sizeof(char));
  44. strcpy(forward_argv[i], argv[i]);
  45. }
  46. forward_argv[i] = NULL;
  47. /* Give over control to run loop, SDLPalAppDelegate will handle most things from here */
  48. @autoreleasepool {
  49. UIApplicationMain(argc, argv, nil, [SDLPalAppDelegate getAppDelegateClassName]);
  50. }
  51. /* free the memory we used to hold copies of argc and argv */
  52. for (i = 0; i < forward_argc; i++) {
  53. free(forward_argv[i]);
  54. }
  55. free(forward_argv);
  56. return exit_status;
  57. }
  58. @implementation SDLPalAppDelegate
  59. /* convenience method */
  60. + (id)sharedAppDelegate
  61. {
  62. /* the delegate is set in UIApplicationMain(), which is guaranteed to be
  63. * called before this method */
  64. return [UIApplication sharedApplication].delegate;
  65. }
  66. + (NSString *)getAppDelegateClassName
  67. {
  68. /* subclassing notice: when you subclass this appdelegate, make sure to add
  69. * a category to override this method and return the actual name of the
  70. * delegate */
  71. return @"SDLPalAppDelegate";
  72. }
  73. - (void)postFinishLaunch
  74. {
  75. /* run the user's application, passing argc and argv */
  76. SDL_iPhoneSetEventPump(SDL_TRUE);
  77. exit_status = SDL_main(forward_argc, forward_argv);
  78. SDL_iPhoneSetEventPump(SDL_FALSE);
  79. /* exit, passing the return status from the user's application */
  80. /* We don't actually exit to support applications that do setup in their
  81. * main function and then allow the Cocoa event loop to run. */
  82. /* exit(exit_status); */
  83. [self restart];
  84. }
  85. #undef SDL_IPHONE_LAUNCHSCREEN
  86. - (void)launchGame {
  87. SDL_SetMainReady();
  88. [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];
  89. [self.window setBackgroundColor:[UIColor blackColor]];
  90. }
  91. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  92. {
  93. [self restart];
  94. return YES;
  95. }
  96. - (void)restart {
  97. PAL_LoadConfig(YES);
  98. if( gConfig.fLaunchSetting ) {
  99. self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
  100. UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Settings" bundle:nil];
  101. UIViewController *vc = [sb instantiateInitialViewController];
  102. self.window.rootViewController = vc;
  103. [self.window makeKeyAndVisible];
  104. }else{
  105. [self launchGame];
  106. }
  107. }
  108. - (void)applicationWillTerminate:(UIApplication *)application
  109. {
  110. SDL_SendAppEvent(SDL_APP_TERMINATING);
  111. }
  112. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
  113. {
  114. SDL_SendAppEvent(SDL_APP_LOWMEMORY);
  115. }
  116. #if !TARGET_OS_TV
  117. - (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
  118. {
  119. BOOL isLandscape = UIInterfaceOrientationIsLandscape(application.statusBarOrientation);
  120. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  121. if (_this && _this->num_displays > 0) {
  122. SDL_DisplayMode *desktopmode = &_this->displays[0].desktop_mode;
  123. SDL_DisplayMode *currentmode = &_this->displays[0].current_mode;
  124. /* The desktop display mode should be kept in sync with the screen
  125. * orientation so that updating a window's fullscreen state to
  126. * SDL_WINDOW_FULLSCREEN_DESKTOP keeps the window dimensions in the
  127. * correct orientation. */
  128. if (isLandscape != (desktopmode->w > desktopmode->h)) {
  129. int height = desktopmode->w;
  130. desktopmode->w = desktopmode->h;
  131. desktopmode->h = height;
  132. }
  133. /* Same deal with the current mode + SDL_GetCurrentDisplayMode. */
  134. if (isLandscape != (currentmode->w > currentmode->h)) {
  135. int height = currentmode->w;
  136. currentmode->w = currentmode->h;
  137. currentmode->h = height;
  138. }
  139. }
  140. }
  141. #endif
  142. - (void)applicationWillResignActive:(UIApplication*)application
  143. {
  144. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  145. if (_this) {
  146. SDL_Window *window;
  147. for (window = _this->windows; window != nil; window = window->next) {
  148. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
  149. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  150. }
  151. }
  152. SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
  153. }
  154. - (void)applicationDidEnterBackground:(UIApplication*)application
  155. {
  156. SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
  157. }
  158. - (void)applicationWillEnterForeground:(UIApplication*)application
  159. {
  160. SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
  161. }
  162. - (void)applicationDidBecomeActive:(UIApplication*)application
  163. {
  164. SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
  165. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  166. if (_this) {
  167. SDL_Window *window;
  168. for (window = _this->windows; window != nil; window = window->next) {
  169. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
  170. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
  171. }
  172. }
  173. }
  174. - (void)sendDropFileForURL:(NSURL *)url
  175. {
  176. NSURL *fileURL = url.filePathURL;
  177. if (fileURL != nil) {
  178. SDL_SendDropFile(NULL, fileURL.path.UTF8String);
  179. } else {
  180. SDL_SendDropFile(NULL, url.absoluteString.UTF8String);
  181. }
  182. SDL_SendDropComplete(NULL);
  183. }
  184. #if TARGET_OS_TV
  185. /* TODO: Use this on iOS 9+ as well? */
  186. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
  187. {
  188. /* TODO: Handle options */
  189. [self sendDropFileForURL:url];
  190. return YES;
  191. }
  192. #endif /* TARGET_OS_TV */
  193. #if !TARGET_OS_TV
  194. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  195. {
  196. [self sendDropFileForURL:url];
  197. return YES;
  198. }
  199. #endif /* !TARGET_OS_TV */
  200. @end
  201. #endif /* SDL_VIDEO_DRIVER_UIKIT */
  202. /* vi: set ts=4 sw=4 expandtab: */