Misc. iOS code improvements. iOS-improvements
authorAlex Szpakowski <slime73@gmail.com>
Mon, 14 Jul 2014 16:50:25 -0300
branchiOS-improvements
changeset 9487 f9fbc2d1edb0
parent 9008 f061a86fbb08
child 9488 f15520430c94
Misc. iOS code improvements. - Use @autoreleasepool instead of NSAutoReleasePool. - Code style fixups.
.hgignore
include/SDL_syswm.h
src/file/cocoa/SDL_rwopsbundlesupport.m
src/filesystem/cocoa/SDL_sysfilesystem.m
src/video/uikit/SDL_uikitappdelegate.m
src/video/uikit/SDL_uikitevents.m
src/video/uikit/SDL_uikitmessagebox.m
src/video/uikit/SDL_uikitview.m
--- a/.hgignore	Sun Jul 13 09:04:55 2014 -0700
+++ b/.hgignore	Mon Jul 14 16:50:25 2014 -0300
@@ -8,6 +8,9 @@
 sdl-config
 SDL2.spec
 build
+Build
+*xcuserdata*
+*xcworkspacedata*
 
 # for Xcode
 *.orig
--- a/include/SDL_syswm.h	Sun Jul 13 09:04:55 2014 -0700
+++ b/include/SDL_syswm.h	Mon Jul 14 16:50:25 2014 -0300
@@ -208,13 +208,21 @@
 #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)
--- a/src/file/cocoa/SDL_rwopsbundlesupport.m	Sun Jul 13 09:04:55 2014 -0700
+++ b/src/file/cocoa/SDL_rwopsbundlesupport.m	Mon Jul 14 16:50:25 2014 -0300
@@ -41,27 +41,24 @@
         return fopen(file, mode);
     }
 
-    NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
-
+    @autoreleasepool {
+        NSFileManager* file_manager = [NSFileManager defaultManager];
+        NSString* resource_path = [[NSBundle mainBundle] resourcePath];
 
-    NSFileManager* file_manager = [NSFileManager defaultManager];
-    NSString* resource_path = [[NSBundle mainBundle] resourcePath];
-
-    NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
+        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: */
--- a/src/filesystem/cocoa/SDL_sysfilesystem.m	Sun Jul 13 09:04:55 2014 -0700
+++ b/src/filesystem/cocoa/SDL_sysfilesystem.m	Mon Jul 14 16:50:25 2014 -0300
@@ -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;
 }
 
--- a/src/video/uikit/SDL_uikitappdelegate.m	Sun Jul 13 09:04:55 2014 -0700
+++ b/src/video/uikit/SDL_uikitappdelegate.m	Mon Jul 14 16:50:25 2014 -0300
@@ -44,7 +44,6 @@
 int main(int argc, char **argv)
 {
     int i;
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
     /* store arguments */
     forward_argc = argc;
@@ -56,7 +55,9 @@
     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 @@
     }
     free(forward_argv);
 
-    [pool release];
     return exit_status;
 }
 
@@ -151,8 +151,8 @@
     } else {
         image = self->splashPortrait;
     }
-    if (image)
-    {
+
+    if (image) {
         splash.image = image;
     }
 }
@@ -165,7 +165,7 @@
 /* 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];
 }
 
--- a/src/video/uikit/SDL_uikitevents.m	Sun Jul 13 09:04:55 2014 -0700
+++ b/src/video/uikit/SDL_uikitevents.m	Mon Jul 14 16:50:25 2014 -0300
@@ -40,8 +40,9 @@
 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
--- a/src/video/uikit/SDL_uikitmessagebox.m	Sun Jul 13 09:04:55 2014 -0700
+++ b/src/video/uikit/SDL_uikitmessagebox.m	Mon Jul 14 16:50:25 2014 -0300
@@ -71,40 +71,38 @@
 UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
 {
     int clicked;
+    int i;
+    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
 
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
-    UIAlertView* alert = [[UIAlertView alloc] init];
+    @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];
+        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]];
-    }
+        for (i = 0; i < messageboxdata->numbuttons; ++i) {
+            [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
+        }
 
-    /* Set up for showing the alert */
-    clicked = messageboxdata->numbuttons;
+        /* Set up for showing the alert */
+        clicked = messageboxdata->numbuttons;
 
-    [alert show];
+        [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]];
+        /* 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];
     }
-    s_showingMessageBox = SDL_FALSE;
-
-    *buttonid = messageboxdata->buttons[clicked].buttonid;
-
-    [alert.delegate release];
-    [alert release];
-
-    [pool release];
 
     return 0;
 }
--- a/src/video/uikit/SDL_uikitview.m	Sun Jul 13 09:04:55 2014 -0700
+++ b/src/video/uikit/SDL_uikitview.m	Mon Jul 14 16:50:25 2014 -0300
@@ -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 @@
     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)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 @@
             }
         }
 #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 @@
             }
         }
 #endif
-        touch = (UITouch*)[enumerator nextObject];
     }
 }
 
@@ -165,15 +157,12 @@
 
 - (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 @@
             }
         }
 #endif
-        touch = (UITouch*)[enumerator nextObject];
     }
 }