From 734b523302a44fc7d63a551c400e404d80305f7c Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 14 Jul 2014 16:50:25 -0300 Subject: [PATCH] Misc. iOS code improvements. - Use @autoreleasepool instead of NSAutoReleasePool. - Code style fixups. --- .hgignore | 3 + include/SDL_syswm.h | 12 +++- src/file/cocoa/SDL_rwopsbundlesupport.m | 27 ++++---- src/filesystem/cocoa/SDL_sysfilesystem.m | 88 ++++++++++++------------ src/video/uikit/SDL_uikitappdelegate.m | 12 ++-- src/video/uikit/SDL_uikitevents.m | 3 +- src/video/uikit/SDL_uikitmessagebox.m | 62 ++++++++--------- src/video/uikit/SDL_uikitview.m | 30 +++----- 8 files changed, 117 insertions(+), 120 deletions(-) diff --git a/.hgignore b/.hgignore index e61b7d96a..a0d89da14 100644 --- a/.hgignore +++ b/.hgignore @@ -8,6 +8,9 @@ Makefile sdl-config SDL2.spec build +Build +*xcuserdata* +*xcworkspacedata* # for Xcode *.orig diff --git a/include/SDL_syswm.h b/include/SDL_syswm.h index 715f2c058..c81cd48de 100644 --- a/include/SDL_syswm.h +++ b/include/SDL_syswm.h @@ -208,13 +208,21 @@ struct SDL_SysWMinfo #if defined(SDL_VIDEO_DRIVER_COCOA) struct { - NSWindow *window; /* The Cocoa window */ +#if defined(__OBJC__) && __has_feature(objc_arc) + NSWindow __unsafe_unretained *window; /* The Cocoa window */ +#else + NSWindow *window; /* The Cocoa window */ +#endif } cocoa; #endif #if defined(SDL_VIDEO_DRIVER_UIKIT) struct { - UIWindow *window; /* The UIKit window */ +#if defined(__OBJC__) && __has_feature(objc_arc) + UIWindow __unsafe_unretained *window; /* The UIKit window */ +#else + UIWindow *window; /* The UIKit window */ +#endif } uikit; #endif #if defined(SDL_VIDEO_DRIVER_WAYLAND) diff --git a/src/file/cocoa/SDL_rwopsbundlesupport.m b/src/file/cocoa/SDL_rwopsbundlesupport.m index f6a65a99a..fcab269c9 100644 --- a/src/file/cocoa/SDL_rwopsbundlesupport.m +++ b/src/file/cocoa/SDL_rwopsbundlesupport.m @@ -41,27 +41,24 @@ FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode) return fopen(file, mode); } - NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init]; + @autoreleasepool { + NSFileManager* file_manager = [NSFileManager defaultManager]; + NSString* resource_path = [[NSBundle mainBundle] resourcePath]; + NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)]; - NSFileManager* file_manager = [NSFileManager defaultManager]; - NSString* resource_path = [[NSBundle mainBundle] resourcePath]; - - NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)]; - - NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component]; - if([file_manager fileExistsAtPath:full_path_with_file_to_try]) { - fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode); + NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component]; + if([file_manager fileExistsAtPath:full_path_with_file_to_try]) { + fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode); + } + else { + fp = fopen(file, mode); + } } - else { - fp = fopen(file, mode); - } - - [autorelease_pool drain]; return fp; } -#endif /* __MACOSX__ */ +#endif /* __APPLE__ */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/filesystem/cocoa/SDL_sysfilesystem.m b/src/filesystem/cocoa/SDL_sysfilesystem.m index adcebd68b..d11739eb9 100644 --- a/src/filesystem/cocoa/SDL_sysfilesystem.m +++ b/src/filesystem/cocoa/SDL_sysfilesystem.m @@ -36,67 +36,69 @@ char * SDL_GetBasePath(void) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSBundle *bundle = [NSBundle mainBundle]; - const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String]; - const char *base = NULL; char *retval = NULL; - if (baseType == NULL) { - baseType = "resource"; - } - if (SDL_strcasecmp(baseType, "bundle")==0) { - base = [[bundle bundlePath] fileSystemRepresentation]; - } else if (SDL_strcasecmp(baseType, "parent")==0) { - base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation]; - } else { - /* this returns the exedir for non-bundled and the resourceDir for bundled apps */ - base = [[bundle resourcePath] fileSystemRepresentation]; - } - if (base) { - const size_t len = SDL_strlen(base) + 2; - retval = (char *) SDL_malloc(len); - if (retval == NULL) { - SDL_OutOfMemory(); + + @autoreleasepool { + const char *base = NULL; + NSBundle *bundle = [NSBundle mainBundle]; + const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String]; + if (baseType == NULL) { + baseType = "resource"; + } + if (SDL_strcasecmp(baseType, "bundle")==0) { + base = [[bundle bundlePath] fileSystemRepresentation]; + } else if (SDL_strcasecmp(baseType, "parent")==0) { + base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation]; } else { - SDL_snprintf(retval, len, "%s/", base); + /* this returns the exedir for non-bundled and the resourceDir for bundled apps */ + base = [[bundle resourcePath] fileSystemRepresentation]; + } + + if (base) { + const size_t len = SDL_strlen(base) + 2; + retval = (char *) SDL_malloc(len); + if (retval == NULL) { + SDL_OutOfMemory(); + } else { + SDL_snprintf(retval, len, "%s/", base); + } } } - [pool release]; return retval; } char * SDL_GetPrefPath(const char *org, const char *app) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); char *retval = NULL; - if ([array count] > 0) { /* we only want the first item in the list. */ - NSString *str = [array objectAtIndex:0]; - const char *base = [str fileSystemRepresentation]; - if (base) { - const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4; - retval = (char *) SDL_malloc(len); - if (retval == NULL) { - SDL_OutOfMemory(); - } else { - char *ptr; - SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app); - for (ptr = retval+1; *ptr; ptr++) { - if (*ptr == '/') { - *ptr = '\0'; - mkdir(retval, 0700); - *ptr = '/'; + @autoreleasepool { + NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); + + if ([array count] > 0) { /* we only want the first item in the list. */ + NSString *str = [array objectAtIndex:0]; + const char *base = [str fileSystemRepresentation]; + if (base) { + const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4; + retval = (char *) SDL_malloc(len); + if (retval == NULL) { + SDL_OutOfMemory(); + } else { + char *ptr; + SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app); + for (ptr = retval+1; *ptr; ptr++) { + if (*ptr == '/') { + *ptr = '\0'; + mkdir(retval, 0700); + *ptr = '/'; + } } + mkdir(retval, 0700); } - mkdir(retval, 0700); } } } - - [pool release]; return retval; } diff --git a/src/video/uikit/SDL_uikitappdelegate.m b/src/video/uikit/SDL_uikitappdelegate.m index a9924fbca..2d9280782 100644 --- a/src/video/uikit/SDL_uikitappdelegate.m +++ b/src/video/uikit/SDL_uikitappdelegate.m @@ -44,7 +44,6 @@ static UIWindow *launch_window; int main(int argc, char **argv) { int i; - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; /* store arguments */ forward_argc = argc; @@ -56,7 +55,9 @@ int main(int argc, char **argv) forward_argv[i] = NULL; /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */ - UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]); + @autoreleasepool { + UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]); + } /* free the memory we used to hold copies of argc and argv */ for (i = 0; i < forward_argc; i++) { @@ -64,7 +65,6 @@ int main(int argc, char **argv) } free(forward_argv); - [pool release]; return exit_status; } @@ -151,8 +151,8 @@ SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldVa } else { image = self->splashPortrait; } - if (image) - { + + if (image) { splash.image = image; } } @@ -165,7 +165,7 @@ SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldVa /* convenience method */ + (id) sharedAppDelegate { - /* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */ + /* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */ return [[UIApplication sharedApplication] delegate]; } diff --git a/src/video/uikit/SDL_uikitevents.m b/src/video/uikit/SDL_uikitevents.m index 94fba878b..48bb1bd56 100644 --- a/src/video/uikit/SDL_uikitevents.m +++ b/src/video/uikit/SDL_uikitevents.m @@ -40,8 +40,9 @@ SDL_iPhoneSetEventPump(SDL_bool enabled) void UIKit_PumpEvents(_THIS) { - if (!UIKit_EventPumpEnabled) + if (!UIKit_EventPumpEnabled) { return; + } /* Let the run loop run for a short amount of time: long enough for touch events to get processed (which is important to get certain diff --git a/src/video/uikit/SDL_uikitmessagebox.m b/src/video/uikit/SDL_uikitmessagebox.m index c24f3ff8b..78f84e3eb 100644 --- a/src/video/uikit/SDL_uikitmessagebox.m +++ b/src/video/uikit/SDL_uikitmessagebox.m @@ -71,41 +71,39 @@ int UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid) { int clicked; - - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - UIAlertView* alert = [[UIAlertView alloc] init]; - - alert.title = [NSString stringWithUTF8String:messageboxdata->title]; - alert.message = [NSString stringWithUTF8String:messageboxdata->message]; - alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked]; - - const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons; int i; - for (i = 0; i < messageboxdata->numbuttons; ++i) { - [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]]; + const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons; + + @autoreleasepool { + UIAlertView* alert = [[UIAlertView alloc] init]; + + alert.title = [NSString stringWithUTF8String:messageboxdata->title]; + alert.message = [NSString stringWithUTF8String:messageboxdata->message]; + alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked]; + + for (i = 0; i < messageboxdata->numbuttons; ++i) { + [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]]; + } + + /* Set up for showing the alert */ + clicked = messageboxdata->numbuttons; + + [alert show]; + + /* Run the main event loop until the alert has finished */ + /* Note that this needs to be done on the main thread */ + s_showingMessageBox = SDL_TRUE; + while (clicked == messageboxdata->numbuttons) { + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; + } + s_showingMessageBox = SDL_FALSE; + + *buttonid = messageboxdata->buttons[clicked].buttonid; + + [alert.delegate release]; + [alert release]; } - /* Set up for showing the alert */ - clicked = messageboxdata->numbuttons; - - [alert show]; - - /* Run the main event loop until the alert has finished */ - /* Note that this needs to be done on the main thread */ - s_showingMessageBox = SDL_TRUE; - while (clicked == messageboxdata->numbuttons) { - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; - } - s_showingMessageBox = SDL_FALSE; - - *buttonid = messageboxdata->buttons[clicked].buttonid; - - [alert.delegate release]; - [alert release]; - - [pool release]; - return 0; } diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m index c51a3ba71..a7fa3d511 100644 --- a/src/video/uikit/SDL_uikitview.m +++ b/src/video/uikit/SDL_uikitview.m @@ -35,7 +35,7 @@ #include "SDL_uikitmodes.h" #include "SDL_uikitwindow.h" -void _uikit_keyboard_init() ; +void _uikit_keyboard_init(); @implementation SDL_uikitview @@ -66,7 +66,7 @@ void _uikit_keyboard_init() ; CGPoint point = [touch locationInView: self]; /* Get the display scale and apply that to the input coordinates */ - SDL_Window *window = self->viewcontroller.window; + SDL_Window *window = viewcontroller.window; SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; @@ -83,18 +83,15 @@ void _uikit_keyboard_init() ; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - NSEnumerator *enumerator = [touches objectEnumerator]; - UITouch *touch = (UITouch*)[enumerator nextObject]; - - while (touch) { + for (UITouch *touch in touches) { if (!leftFingerDown) { CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; /* send moved event */ - SDL_SendMouseMotion(self->viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y); + SDL_SendMouseMotion(viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y); /* send mouse down event */ - SDL_SendMouseButton(self->viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT); + SDL_SendMouseButton(viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT); leftFingerDown = touch; } @@ -118,19 +115,15 @@ void _uikit_keyboard_init() ; } } #endif - touch = (UITouch*)[enumerator nextObject]; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { - NSEnumerator *enumerator = [touches objectEnumerator]; - UITouch *touch = (UITouch*)[enumerator nextObject]; - - while(touch) { + for (UITouch *touch in touches) { if (touch == leftFingerDown) { /* send mouse up */ - SDL_SendMouseButton(self->viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT); + SDL_SendMouseButton(viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT); leftFingerDown = nil; } @@ -149,7 +142,6 @@ void _uikit_keyboard_init() ; } } #endif - touch = (UITouch*)[enumerator nextObject]; } } @@ -165,15 +157,12 @@ void _uikit_keyboard_init() ; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - NSEnumerator *enumerator = [touches objectEnumerator]; - UITouch *touch = (UITouch*)[enumerator nextObject]; - - while (touch) { + for (UITouch *touch in touches) { if (touch == leftFingerDown) { CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; /* send moved event */ - SDL_SendMouseMotion(self->viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y); + SDL_SendMouseMotion(viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y); } CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES]; @@ -190,7 +179,6 @@ void _uikit_keyboard_init() ; } } #endif - touch = (UITouch*)[enumerator nextObject]; } }