Commit 734b523302a44fc7d63a551c400e404d80305f7c

Alex Szpakowski 2014-07-14T16:50:25

Misc. iOS code improvements. - Use @autoreleasepool instead of NSAutoReleasePool. - Code style fixups.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
diff --git a/.hgignore b/.hgignore
index e61b7d9..a0d89da 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 715f2c0..c81cd48 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 f6a65a9..fcab269 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];
-
-
-    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);
-    }
-    else {
-        fp = fopen(file, mode);
+    @autoreleasepool {
+        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);
+        }
+        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 adcebd6..d11739e 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 a9924fb..2d92807 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 94fba87..48bb1bd 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 c24f3ff..78f84e3 100644
--- a/src/video/uikit/SDL_uikitmessagebox.m
+++ b/src/video/uikit/SDL_uikitmessagebox.m
@@ -71,40 +71,38 @@ int
 UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
 {
     int clicked;
+    int i;
+    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
 
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    @autoreleasepool {
+        UIAlertView* alert = [[UIAlertView 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];
 
-    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]];
+        }
 
-    const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
-    int i;
-    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]];
+        }
+        s_showingMessageBox = SDL_FALSE;
 
-    /* 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;
+        *buttonid = messageboxdata->buttons[clicked].buttonid;
 
-    [alert.delegate release];
-    [alert release];
-
-    [pool release];
+        [alert.delegate release];
+        [alert release];
+    }
 
     return 0;
 }
diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m
index c51a3ba..a7fa3d5 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];
     }
 }