Misc. iOS code improvements.

- Use @autoreleasepool instead of NSAutoReleasePool.

- Code style fixups.
This commit is contained in:
Alex Szpakowski 2014-07-14 16:50:25 -03:00
parent 96b613eab3
commit 734b523302
8 changed files with 117 additions and 120 deletions

View File

@ -8,6 +8,9 @@ Makefile
sdl-config sdl-config
SDL2.spec SDL2.spec
build build
Build
*xcuserdata*
*xcworkspacedata*
# for Xcode # for Xcode
*.orig *.orig

View File

@ -208,13 +208,21 @@ struct SDL_SysWMinfo
#if defined(SDL_VIDEO_DRIVER_COCOA) #if defined(SDL_VIDEO_DRIVER_COCOA)
struct 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; } cocoa;
#endif #endif
#if defined(SDL_VIDEO_DRIVER_UIKIT) #if defined(SDL_VIDEO_DRIVER_UIKIT)
struct 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; } uikit;
#endif #endif
#if defined(SDL_VIDEO_DRIVER_WAYLAND) #if defined(SDL_VIDEO_DRIVER_WAYLAND)

View File

@ -41,27 +41,24 @@ FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
return fopen(file, 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* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
NSString* resource_path = [[NSBundle mainBundle] resourcePath]; if([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)]; }
else {
NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component]; fp = fopen(file, mode);
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);
}
[autorelease_pool drain];
return fp; return fp;
} }
#endif /* __MACOSX__ */ #endif /* __APPLE__ */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -36,67 +36,69 @@
char * char *
SDL_GetBasePath(void) 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; char *retval = NULL;
if (baseType == NULL) {
baseType = "resource"; @autoreleasepool {
} const char *base = NULL;
if (SDL_strcasecmp(baseType, "bundle")==0) { NSBundle *bundle = [NSBundle mainBundle];
base = [[bundle bundlePath] fileSystemRepresentation]; const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
} else if (SDL_strcasecmp(baseType, "parent")==0) { if (baseType == NULL) {
base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation]; baseType = "resource";
} else { }
/* this returns the exedir for non-bundled and the resourceDir for bundled apps */ if (SDL_strcasecmp(baseType, "bundle")==0) {
base = [[bundle resourcePath] fileSystemRepresentation]; base = [[bundle bundlePath] fileSystemRepresentation];
} } else if (SDL_strcasecmp(baseType, "parent")==0) {
if (base) { base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
const size_t len = SDL_strlen(base) + 2;
retval = (char *) SDL_malloc(len);
if (retval == NULL) {
SDL_OutOfMemory();
} else { } 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; return retval;
} }
char * char *
SDL_GetPrefPath(const char *org, const char *app) SDL_GetPrefPath(const char *org, const char *app)
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
char *retval = NULL; char *retval = NULL;
if ([array count] > 0) { /* we only want the first item in the list. */ @autoreleasepool {
NSString *str = [array objectAtIndex:0]; NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
const char *base = [str fileSystemRepresentation];
if (base) { if ([array count] > 0) { /* we only want the first item in the list. */
const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4; NSString *str = [array objectAtIndex:0];
retval = (char *) SDL_malloc(len); const char *base = [str fileSystemRepresentation];
if (retval == NULL) { if (base) {
SDL_OutOfMemory(); const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
} else { retval = (char *) SDL_malloc(len);
char *ptr; if (retval == NULL) {
SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app); SDL_OutOfMemory();
for (ptr = retval+1; *ptr; ptr++) { } else {
if (*ptr == '/') { char *ptr;
*ptr = '\0'; SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
mkdir(retval, 0700); for (ptr = retval+1; *ptr; ptr++) {
*ptr = '/'; if (*ptr == '/') {
*ptr = '\0';
mkdir(retval, 0700);
*ptr = '/';
}
} }
mkdir(retval, 0700);
} }
mkdir(retval, 0700);
} }
} }
} }
[pool release];
return retval; return retval;
} }

View File

@ -44,7 +44,6 @@ static UIWindow *launch_window;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i; int i;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/* store arguments */ /* store arguments */
forward_argc = argc; forward_argc = argc;
@ -56,7 +55,9 @@ int main(int argc, char **argv)
forward_argv[i] = NULL; forward_argv[i] = NULL;
/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */ /* 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 */ /* free the memory we used to hold copies of argc and argv */
for (i = 0; i < forward_argc; i++) { for (i = 0; i < forward_argc; i++) {
@ -64,7 +65,6 @@ int main(int argc, char **argv)
} }
free(forward_argv); free(forward_argv);
[pool release];
return exit_status; return exit_status;
} }
@ -151,8 +151,8 @@ SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldVa
} else { } else {
image = self->splashPortrait; image = self->splashPortrait;
} }
if (image)
{ if (image) {
splash.image = image; splash.image = image;
} }
} }
@ -165,7 +165,7 @@ SDL_IdleTimerDisabledChanged(void *userdata, const char *name, const char *oldVa
/* convenience method */ /* convenience method */
+ (id) sharedAppDelegate + (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]; return [[UIApplication sharedApplication] delegate];
} }

View File

@ -40,8 +40,9 @@ SDL_iPhoneSetEventPump(SDL_bool enabled)
void void
UIKit_PumpEvents(_THIS) UIKit_PumpEvents(_THIS)
{ {
if (!UIKit_EventPumpEnabled) if (!UIKit_EventPumpEnabled) {
return; return;
}
/* Let the run loop run for a short amount of time: long enough for /* 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 touch events to get processed (which is important to get certain

View File

@ -71,41 +71,39 @@ int
UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid) UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{ {
int clicked; 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; int i;
for (i = 0; i < messageboxdata->numbuttons; ++i) { const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
[alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
@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; return 0;
} }

View File

@ -35,7 +35,7 @@
#include "SDL_uikitmodes.h" #include "SDL_uikitmodes.h"
#include "SDL_uikitwindow.h" #include "SDL_uikitwindow.h"
void _uikit_keyboard_init() ; void _uikit_keyboard_init();
@implementation SDL_uikitview @implementation SDL_uikitview
@ -66,7 +66,7 @@ void _uikit_keyboard_init() ;
CGPoint point = [touch locationInView: self]; CGPoint point = [touch locationInView: self];
/* Get the display scale and apply that to the input coordinates */ /* 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_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
@ -83,18 +83,15 @@ void _uikit_keyboard_init() ;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ {
NSEnumerator *enumerator = [touches objectEnumerator]; for (UITouch *touch in touches) {
UITouch *touch = (UITouch*)[enumerator nextObject];
while (touch) {
if (!leftFingerDown) { if (!leftFingerDown) {
CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
/* send moved event */ /* 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 */ /* 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; leftFingerDown = touch;
} }
@ -118,19 +115,15 @@ void _uikit_keyboard_init() ;
} }
} }
#endif #endif
touch = (UITouch*)[enumerator nextObject];
} }
} }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ {
NSEnumerator *enumerator = [touches objectEnumerator]; for (UITouch *touch in touches) {
UITouch *touch = (UITouch*)[enumerator nextObject];
while(touch) {
if (touch == leftFingerDown) { if (touch == leftFingerDown) {
/* send mouse up */ /* 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; leftFingerDown = nil;
} }
@ -149,7 +142,6 @@ void _uikit_keyboard_init() ;
} }
} }
#endif #endif
touch = (UITouch*)[enumerator nextObject];
} }
} }
@ -165,15 +157,12 @@ void _uikit_keyboard_init() ;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{ {
NSEnumerator *enumerator = [touches objectEnumerator]; for (UITouch *touch in touches) {
UITouch *touch = (UITouch*)[enumerator nextObject];
while (touch) {
if (touch == leftFingerDown) { if (touch == leftFingerDown) {
CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
/* send moved event */ /* 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]; CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
@ -190,7 +179,6 @@ void _uikit_keyboard_init() ;
} }
} }
#endif #endif
touch = (UITouch*)[enumerator nextObject];
} }
} }