Misc. iOS code improvements. iOS-improvements
authorAlex Szpakowski <slime73@gmail.com>
Mon, 14 Jul 2014 16:50:25 -0300
branchiOS-improvements
changeset 9487f9fbc2d1edb0
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
     1.1 --- a/.hgignore	Sun Jul 13 09:04:55 2014 -0700
     1.2 +++ b/.hgignore	Mon Jul 14 16:50:25 2014 -0300
     1.3 @@ -8,6 +8,9 @@
     1.4  sdl-config
     1.5  SDL2.spec
     1.6  build
     1.7 +Build
     1.8 +*xcuserdata*
     1.9 +*xcworkspacedata*
    1.10  
    1.11  # for Xcode
    1.12  *.orig
     2.1 --- a/include/SDL_syswm.h	Sun Jul 13 09:04:55 2014 -0700
     2.2 +++ b/include/SDL_syswm.h	Mon Jul 14 16:50:25 2014 -0300
     2.3 @@ -208,13 +208,21 @@
     2.4  #if defined(SDL_VIDEO_DRIVER_COCOA)
     2.5          struct
     2.6          {
     2.7 -            NSWindow *window;           /* The Cocoa window */
     2.8 +#if defined(__OBJC__) && __has_feature(objc_arc)
     2.9 +            NSWindow __unsafe_unretained *window; /* The Cocoa window */
    2.10 +#else
    2.11 +            NSWindow *window;                     /* The Cocoa window */
    2.12 +#endif
    2.13          } cocoa;
    2.14  #endif
    2.15  #if defined(SDL_VIDEO_DRIVER_UIKIT)
    2.16          struct
    2.17          {
    2.18 -            UIWindow *window;           /* The UIKit window */
    2.19 +#if defined(__OBJC__) && __has_feature(objc_arc)
    2.20 +            UIWindow __unsafe_unretained *window; /* The UIKit window */
    2.21 +#else
    2.22 +            UIWindow *window;                     /* The UIKit window */
    2.23 +#endif
    2.24          } uikit;
    2.25  #endif
    2.26  #if defined(SDL_VIDEO_DRIVER_WAYLAND)
     3.1 --- a/src/file/cocoa/SDL_rwopsbundlesupport.m	Sun Jul 13 09:04:55 2014 -0700
     3.2 +++ b/src/file/cocoa/SDL_rwopsbundlesupport.m	Mon Jul 14 16:50:25 2014 -0300
     3.3 @@ -41,27 +41,24 @@
     3.4          return fopen(file, mode);
     3.5      }
     3.6  
     3.7 -    NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
     3.8 +    @autoreleasepool {
     3.9 +        NSFileManager* file_manager = [NSFileManager defaultManager];
    3.10 +        NSString* resource_path = [[NSBundle mainBundle] resourcePath];
    3.11  
    3.12 +        NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    3.13  
    3.14 -    NSFileManager* file_manager = [NSFileManager defaultManager];
    3.15 -    NSString* resource_path = [[NSBundle mainBundle] resourcePath];
    3.16 -
    3.17 -    NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];
    3.18 -
    3.19 -    NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
    3.20 -    if([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
    3.21 -        fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    3.22 +        NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
    3.23 +        if([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
    3.24 +            fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
    3.25 +        }
    3.26 +        else {
    3.27 +            fp = fopen(file, mode);
    3.28 +        }
    3.29      }
    3.30 -    else {
    3.31 -        fp = fopen(file, mode);
    3.32 -    }
    3.33 -
    3.34 -    [autorelease_pool drain];
    3.35  
    3.36      return fp;
    3.37  }
    3.38  
    3.39 -#endif /* __MACOSX__ */
    3.40 +#endif /* __APPLE__ */
    3.41  
    3.42  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/filesystem/cocoa/SDL_sysfilesystem.m	Sun Jul 13 09:04:55 2014 -0700
     4.2 +++ b/src/filesystem/cocoa/SDL_sysfilesystem.m	Mon Jul 14 16:50:25 2014 -0300
     4.3 @@ -36,67 +36,69 @@
     4.4  char *
     4.5  SDL_GetBasePath(void)
     4.6  {
     4.7 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     4.8 -    NSBundle *bundle = [NSBundle mainBundle];
     4.9 -    const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
    4.10 -    const char *base = NULL;
    4.11      char *retval = NULL;
    4.12 -    if (baseType == NULL) {
    4.13 -        baseType = "resource";
    4.14 -    }
    4.15 -    if (SDL_strcasecmp(baseType, "bundle")==0) {
    4.16 -        base = [[bundle bundlePath] fileSystemRepresentation];
    4.17 -    } else if (SDL_strcasecmp(baseType, "parent")==0) {
    4.18 -        base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
    4.19 -    } else {
    4.20 -        /* this returns the exedir for non-bundled  and the resourceDir for bundled apps */
    4.21 -        base = [[bundle resourcePath] fileSystemRepresentation];
    4.22 -    }
    4.23 -    if (base) {
    4.24 -        const size_t len = SDL_strlen(base) + 2;
    4.25 -        retval = (char *) SDL_malloc(len);
    4.26 -        if (retval == NULL) {
    4.27 -            SDL_OutOfMemory();
    4.28 +
    4.29 +    @autoreleasepool {
    4.30 +        const char *base = NULL;
    4.31 +        NSBundle *bundle = [NSBundle mainBundle];
    4.32 +        const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
    4.33 +        if (baseType == NULL) {
    4.34 +            baseType = "resource";
    4.35 +        }
    4.36 +        if (SDL_strcasecmp(baseType, "bundle")==0) {
    4.37 +            base = [[bundle bundlePath] fileSystemRepresentation];
    4.38 +        } else if (SDL_strcasecmp(baseType, "parent")==0) {
    4.39 +            base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
    4.40          } else {
    4.41 -            SDL_snprintf(retval, len, "%s/", base);
    4.42 +            /* this returns the exedir for non-bundled  and the resourceDir for bundled apps */
    4.43 +            base = [[bundle resourcePath] fileSystemRepresentation];
    4.44 +        }
    4.45 +
    4.46 +        if (base) {
    4.47 +            const size_t len = SDL_strlen(base) + 2;
    4.48 +            retval = (char *) SDL_malloc(len);
    4.49 +            if (retval == NULL) {
    4.50 +                SDL_OutOfMemory();
    4.51 +            } else {
    4.52 +                SDL_snprintf(retval, len, "%s/", base);
    4.53 +            }
    4.54          }
    4.55      }
    4.56  
    4.57 -    [pool release];
    4.58      return retval;
    4.59  }
    4.60  
    4.61  char *
    4.62  SDL_GetPrefPath(const char *org, const char *app)
    4.63  {
    4.64 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    4.65 -    NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    4.66      char *retval = NULL;
    4.67  
    4.68 -    if ([array count] > 0) {  /* we only want the first item in the list. */
    4.69 -        NSString *str = [array objectAtIndex:0];
    4.70 -        const char *base = [str fileSystemRepresentation];
    4.71 -        if (base) {
    4.72 -            const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
    4.73 -            retval = (char *) SDL_malloc(len);
    4.74 -            if (retval == NULL) {
    4.75 -                SDL_OutOfMemory();
    4.76 -            } else {
    4.77 -                char *ptr;
    4.78 -                SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
    4.79 -                for (ptr = retval+1; *ptr; ptr++) {
    4.80 -                    if (*ptr == '/') {
    4.81 -                        *ptr = '\0';
    4.82 -                        mkdir(retval, 0700);
    4.83 -                        *ptr = '/';
    4.84 +    @autoreleasepool {
    4.85 +        NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    4.86 +
    4.87 +        if ([array count] > 0) {  /* we only want the first item in the list. */
    4.88 +            NSString *str = [array objectAtIndex:0];
    4.89 +            const char *base = [str fileSystemRepresentation];
    4.90 +            if (base) {
    4.91 +                const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
    4.92 +                retval = (char *) SDL_malloc(len);
    4.93 +                if (retval == NULL) {
    4.94 +                    SDL_OutOfMemory();
    4.95 +                } else {
    4.96 +                    char *ptr;
    4.97 +                    SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
    4.98 +                    for (ptr = retval+1; *ptr; ptr++) {
    4.99 +                        if (*ptr == '/') {
   4.100 +                            *ptr = '\0';
   4.101 +                            mkdir(retval, 0700);
   4.102 +                            *ptr = '/';
   4.103 +                        }
   4.104                      }
   4.105 +                    mkdir(retval, 0700);
   4.106                  }
   4.107 -                mkdir(retval, 0700);
   4.108              }
   4.109          }
   4.110      }
   4.111 -
   4.112 -    [pool release];
   4.113      return retval;
   4.114  }
   4.115  
     5.1 --- a/src/video/uikit/SDL_uikitappdelegate.m	Sun Jul 13 09:04:55 2014 -0700
     5.2 +++ b/src/video/uikit/SDL_uikitappdelegate.m	Mon Jul 14 16:50:25 2014 -0300
     5.3 @@ -44,7 +44,6 @@
     5.4  int main(int argc, char **argv)
     5.5  {
     5.6      int i;
     5.7 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     5.8  
     5.9      /* store arguments */
    5.10      forward_argc = argc;
    5.11 @@ -56,7 +55,9 @@
    5.12      forward_argv[i] = NULL;
    5.13  
    5.14      /* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
    5.15 -    UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
    5.16 +    @autoreleasepool {
    5.17 +        UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
    5.18 +    }
    5.19  
    5.20      /* free the memory we used to hold copies of argc and argv */
    5.21      for (i = 0; i < forward_argc; i++) {
    5.22 @@ -64,7 +65,6 @@
    5.23      }
    5.24      free(forward_argv);
    5.25  
    5.26 -    [pool release];
    5.27      return exit_status;
    5.28  }
    5.29  
    5.30 @@ -151,8 +151,8 @@
    5.31      } else {
    5.32          image = self->splashPortrait;
    5.33      }
    5.34 -    if (image)
    5.35 -    {
    5.36 +
    5.37 +    if (image) {
    5.38          splash.image = image;
    5.39      }
    5.40  }
    5.41 @@ -165,7 +165,7 @@
    5.42  /* convenience method */
    5.43  + (id) sharedAppDelegate
    5.44  {
    5.45 -    /* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
    5.46 +    /* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */
    5.47      return [[UIApplication sharedApplication] delegate];
    5.48  }
    5.49  
     6.1 --- a/src/video/uikit/SDL_uikitevents.m	Sun Jul 13 09:04:55 2014 -0700
     6.2 +++ b/src/video/uikit/SDL_uikitevents.m	Mon Jul 14 16:50:25 2014 -0300
     6.3 @@ -40,8 +40,9 @@
     6.4  void
     6.5  UIKit_PumpEvents(_THIS)
     6.6  {
     6.7 -    if (!UIKit_EventPumpEnabled)
     6.8 +    if (!UIKit_EventPumpEnabled) {
     6.9          return;
    6.10 +    }
    6.11  
    6.12      /* Let the run loop run for a short amount of time: long enough for
    6.13         touch events to get processed (which is important to get certain
     7.1 --- a/src/video/uikit/SDL_uikitmessagebox.m	Sun Jul 13 09:04:55 2014 -0700
     7.2 +++ b/src/video/uikit/SDL_uikitmessagebox.m	Mon Jul 14 16:50:25 2014 -0300
     7.3 @@ -71,41 +71,39 @@
     7.4  UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
     7.5  {
     7.6      int clicked;
     7.7 +    int i;
     7.8 +    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
     7.9  
    7.10 -    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    7.11 +    @autoreleasepool {
    7.12 +        UIAlertView* alert = [[UIAlertView alloc] init];
    7.13  
    7.14 -    UIAlertView* alert = [[UIAlertView alloc] init];
    7.15 +        alert.title = [NSString stringWithUTF8String:messageboxdata->title];
    7.16 +        alert.message = [NSString stringWithUTF8String:messageboxdata->message];
    7.17 +        alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
    7.18  
    7.19 -    alert.title = [NSString stringWithUTF8String:messageboxdata->title];
    7.20 -    alert.message = [NSString stringWithUTF8String:messageboxdata->message];
    7.21 -    alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
    7.22 +        for (i = 0; i < messageboxdata->numbuttons; ++i) {
    7.23 +            [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
    7.24 +        }
    7.25  
    7.26 -    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
    7.27 -    int i;
    7.28 -    for (i = 0; i < messageboxdata->numbuttons; ++i) {
    7.29 -        [alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
    7.30 +        /* Set up for showing the alert */
    7.31 +        clicked = messageboxdata->numbuttons;
    7.32 +
    7.33 +        [alert show];
    7.34 +
    7.35 +        /* Run the main event loop until the alert has finished */
    7.36 +        /* Note that this needs to be done on the main thread */
    7.37 +        s_showingMessageBox = SDL_TRUE;
    7.38 +        while (clicked == messageboxdata->numbuttons) {
    7.39 +            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    7.40 +        }
    7.41 +        s_showingMessageBox = SDL_FALSE;
    7.42 +
    7.43 +        *buttonid = messageboxdata->buttons[clicked].buttonid;
    7.44 +
    7.45 +        [alert.delegate release];
    7.46 +        [alert release];
    7.47      }
    7.48  
    7.49 -    /* Set up for showing the alert */
    7.50 -    clicked = messageboxdata->numbuttons;
    7.51 -
    7.52 -    [alert show];
    7.53 -
    7.54 -    /* Run the main event loop until the alert has finished */
    7.55 -    /* Note that this needs to be done on the main thread */
    7.56 -    s_showingMessageBox = SDL_TRUE;
    7.57 -    while (clicked == messageboxdata->numbuttons) {
    7.58 -        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    7.59 -    }
    7.60 -    s_showingMessageBox = SDL_FALSE;
    7.61 -
    7.62 -    *buttonid = messageboxdata->buttons[clicked].buttonid;
    7.63 -
    7.64 -    [alert.delegate release];
    7.65 -    [alert release];
    7.66 -
    7.67 -    [pool release];
    7.68 -
    7.69      return 0;
    7.70  }
    7.71  
     8.1 --- a/src/video/uikit/SDL_uikitview.m	Sun Jul 13 09:04:55 2014 -0700
     8.2 +++ b/src/video/uikit/SDL_uikitview.m	Mon Jul 14 16:50:25 2014 -0300
     8.3 @@ -35,7 +35,7 @@
     8.4  #include "SDL_uikitmodes.h"
     8.5  #include "SDL_uikitwindow.h"
     8.6  
     8.7 -void _uikit_keyboard_init() ;
     8.8 +void _uikit_keyboard_init();
     8.9  
    8.10  @implementation SDL_uikitview
    8.11  
    8.12 @@ -66,7 +66,7 @@
    8.13      CGPoint point = [touch locationInView: self];
    8.14  
    8.15      /* Get the display scale and apply that to the input coordinates */
    8.16 -    SDL_Window *window = self->viewcontroller.window;
    8.17 +    SDL_Window *window = viewcontroller.window;
    8.18      SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    8.19      SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
    8.20  
    8.21 @@ -83,18 +83,15 @@
    8.22  
    8.23  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    8.24  {
    8.25 -    NSEnumerator *enumerator = [touches objectEnumerator];
    8.26 -    UITouch *touch = (UITouch*)[enumerator nextObject];
    8.27 -
    8.28 -    while (touch) {
    8.29 +    for (UITouch *touch in touches) {
    8.30          if (!leftFingerDown) {
    8.31              CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
    8.32  
    8.33              /* send moved event */
    8.34 -            SDL_SendMouseMotion(self->viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
    8.35 +            SDL_SendMouseMotion(viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
    8.36  
    8.37              /* send mouse down event */
    8.38 -            SDL_SendMouseButton(self->viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    8.39 +            SDL_SendMouseButton(viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    8.40  
    8.41              leftFingerDown = touch;
    8.42          }
    8.43 @@ -118,19 +115,15 @@
    8.44              }
    8.45          }
    8.46  #endif
    8.47 -        touch = (UITouch*)[enumerator nextObject];
    8.48      }
    8.49  }
    8.50  
    8.51  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    8.52  {
    8.53 -    NSEnumerator *enumerator = [touches objectEnumerator];
    8.54 -    UITouch *touch = (UITouch*)[enumerator nextObject];
    8.55 -
    8.56 -    while(touch) {
    8.57 +    for (UITouch *touch in touches) {
    8.58          if (touch == leftFingerDown) {
    8.59              /* send mouse up */
    8.60 -            SDL_SendMouseButton(self->viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
    8.61 +            SDL_SendMouseButton(viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
    8.62              leftFingerDown = nil;
    8.63          }
    8.64  
    8.65 @@ -149,7 +142,6 @@
    8.66              }
    8.67          }
    8.68  #endif
    8.69 -        touch = (UITouch*)[enumerator nextObject];
    8.70      }
    8.71  }
    8.72  
    8.73 @@ -165,15 +157,12 @@
    8.74  
    8.75  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    8.76  {
    8.77 -    NSEnumerator *enumerator = [touches objectEnumerator];
    8.78 -    UITouch *touch = (UITouch*)[enumerator nextObject];
    8.79 -
    8.80 -    while (touch) {
    8.81 +    for (UITouch *touch in touches) {
    8.82          if (touch == leftFingerDown) {
    8.83              CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
    8.84  
    8.85              /* send moved event */
    8.86 -            SDL_SendMouseMotion(self->viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
    8.87 +            SDL_SendMouseMotion(viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
    8.88          }
    8.89  
    8.90          CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
    8.91 @@ -190,7 +179,6 @@
    8.92              }
    8.93          }
    8.94  #endif
    8.95 -        touch = (UITouch*)[enumerator nextObject];
    8.96      }
    8.97  }
    8.98