SDL: Fixed Xcode build

From 76920a230f31696245024c34f500fec244137ba1 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 24 Dec 2024 10:49:58 -0800
Subject: [PATCH] Fixed Xcode build

---
 Xcode/SDL/SDL.xcodeproj/project.pbxproj | 48 ++++++++++++++++++++-----
 src/tray/cocoa/SDL_tray.m               | 32 +++++++----------
 src/tray/dummy/SDL_tray.c               |  4 +++
 3 files changed, 56 insertions(+), 28 deletions(-)

diff --git a/Xcode/SDL/SDL.xcodeproj/project.pbxproj b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
index 0c30b1942afe2..08dfd070acd35 100644
--- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj
+++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj
@@ -386,6 +386,8 @@
 		F32DDAD12AB795A30041EAA5 /* SDL_audioqueue.c in Sources */ = {isa = PBXBuildFile; fileRef = F32DDACB2AB795A30041EAA5 /* SDL_audioqueue.c */; };
 		F32DDAD32AB795A30041EAA5 /* SDL_audioqueue.h in Headers */ = {isa = PBXBuildFile; fileRef = F32DDACD2AB795A30041EAA5 /* SDL_audioqueue.h */; };
 		F32DDAD42AB795A30041EAA5 /* SDL_audioresample.c in Sources */ = {isa = PBXBuildFile; fileRef = F32DDACE2AB795A30041EAA5 /* SDL_audioresample.c */; };
+		F338A1182D1B37D8007CDFDF /* SDL_tray.m in Sources */ = {isa = PBXBuildFile; fileRef = F338A1172D1B37D8007CDFDF /* SDL_tray.m */; };
+		F338A11A2D1B37E4007CDFDF /* SDL_tray.c in Sources */ = {isa = PBXBuildFile; fileRef = F338A1192D1B37E4007CDFDF /* SDL_tray.c */; };
 		F34B9895291DEFF500AAC96E /* SDL_hidapi_steam.c in Sources */ = {isa = PBXBuildFile; fileRef = A75FDAAC23E2795C00529352 /* SDL_hidapi_steam.c */; };
 		F35B79632D03A7B900C6D591 /* SDL_asyncio.h in Headers */ = {isa = PBXBuildFile; fileRef = 00004945A946DF5B1AED0000 /* SDL_asyncio.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		F362B9192B3349E200D30B94 /* controller_list.h in Headers */ = {isa = PBXBuildFile; fileRef = F362B9152B3349E200D30B94 /* controller_list.h */; };
@@ -927,6 +929,8 @@
 		F32DDACB2AB795A30041EAA5 /* SDL_audioqueue.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_audioqueue.c; sourceTree = "<group>"; };
 		F32DDACD2AB795A30041EAA5 /* SDL_audioqueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_audioqueue.h; sourceTree = "<group>"; };
 		F32DDACE2AB795A30041EAA5 /* SDL_audioresample.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_audioresample.c; sourceTree = "<group>"; };
+		F338A1172D1B37D8007CDFDF /* SDL_tray.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDL_tray.m; sourceTree = "<group>"; };
+		F338A1192D1B37E4007CDFDF /* SDL_tray.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SDL_tray.c; sourceTree = "<group>"; };
 		F362B9152B3349E200D30B94 /* controller_list.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = controller_list.h; sourceTree = "<group>"; };
 		F362B9162B3349E200D30B94 /* SDL_gamepad_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_gamepad_c.h; sourceTree = "<group>"; };
 		F362B9172B3349E200D30B94 /* SDL_steam_virtual_gamepad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_steam_virtual_gamepad.h; sourceTree = "<group>"; };
@@ -1337,25 +1341,26 @@
 				A7D8A77623E2513E00DCD162 /* thread */,
 				0000F5E7419220E3A8AB0000 /* time */,
 				A7D8A5DE23E2513D00DCD162 /* timer */,
+				F338A1142D1B3735007CDFDF /* tray */,
 				A7D8A5EB23E2513D00DCD162 /* video */,
-				A7D8A7F523E2513F00DCD162 /* SDL_assert_c.h */,
+				A7D8A57123E2513D00DCD162 /* SDL.c */,
 				A7D8A94423E2514000DCD162 /* SDL_assert.c */,
-				A7D8A57523E2513D00DCD162 /* SDL_error_c.h */,
+				A7D8A7F523E2513F00DCD162 /* SDL_assert_c.h */,
 				A7D8A8BF23E2513F00DCD162 /* SDL_error.c */,
+				A7D8A57523E2513D00DCD162 /* SDL_error_c.h */,
 				F382071C284F362F004DD584 /* SDL_guid.c */,
-				000078E1881E857EBB6C0000 /* SDL_hashtable.c */,
 				0000B6ADCD88CAD6610F0000 /* SDL_hashtable.h */,
-				A7D8A8D123E2514000DCD162 /* SDL_hints_c.h */,
+				000078E1881E857EBB6C0000 /* SDL_hashtable.c */,
 				A7D8A5AB23E2513D00DCD162 /* SDL_hints.c */,
+				A7D8A8D123E2514000DCD162 /* SDL_hints_c.h */,
 				A7D8A58323E2513D00DCD162 /* SDL_internal.h */,
-				A1BB8B6127F6CF320057CFA8 /* SDL_list.c */,
 				A1BB8B6227F6CF330057CFA8 /* SDL_list.h */,
-				F386F6E42884663E001840AA /* SDL_log_c.h */,
+				A1BB8B6127F6CF320057CFA8 /* SDL_list.c */,
 				A7D8A5DD23E2513D00DCD162 /* SDL_log.c */,
+				F386F6E42884663E001840AA /* SDL_log_c.h */,
 				F3E5A6EA2AD5E0E600293D83 /* SDL_properties.c */,
-				F386F6E52884663E001840AA /* SDL_utils_c.h */,
 				F386F6E62884663E001840AA /* SDL_utils.c */,
-				A7D8A57123E2513D00DCD162 /* SDL.c */,
+				F386F6E52884663E001840AA /* SDL_utils_c.h */,
 			);
 			name = "Library Source";
 			path = ../../src;
@@ -2268,6 +2273,31 @@
 			path = gpu;
 			sourceTree = "<group>";
 		};
+		F338A1142D1B3735007CDFDF /* tray */ = {
+			isa = PBXGroup;
+			children = (
+				F338A1162D1B378D007CDFDF /* cocoa */,
+				F338A1152D1B3786007CDFDF /* dummy */,
+			);
+			path = tray;
+			sourceTree = "<group>";
+		};
+		F338A1152D1B3786007CDFDF /* dummy */ = {
+			isa = PBXGroup;
+			children = (
+				F338A1192D1B37E4007CDFDF /* SDL_tray.c */,
+			);
+			path = dummy;
+			sourceTree = "<group>";
+		};
+		F338A1162D1B378D007CDFDF /* cocoa */ = {
+			isa = PBXGroup;
+			children = (
+				F338A1172D1B37D8007CDFDF /* SDL_tray.m */,
+			);
+			path = cocoa;
+			sourceTree = "<group>";
+		};
 		F36C7ACF294B9F5E004D61C3 /* core */ = {
 			isa = PBXGroup;
 			children = (
@@ -2784,6 +2814,7 @@
 				A7D8B86623E2514400DCD162 /* SDL_audiocvt.c in Sources */,
 				A7D8B9F523E2514400DCD162 /* SDL_rotate.c in Sources */,
 				A7D8BBE323E2574800DCD162 /* SDL_uikitvideo.m in Sources */,
+				F338A1182D1B37D8007CDFDF /* SDL_tray.m in Sources */,
 				5616CA4E252BB2A6005D5928 /* SDL_sysurl.m in Sources */,
 				F3B439562C937DAB00792030 /* SDL_process.c in Sources */,
 				A7D8A97523E2514000DCD162 /* SDL_coremotionsensor.m in Sources */,
@@ -2822,6 +2853,7 @@
 				A7D8BA3723E2514400DCD162 /* SDL_d3dmath.c in Sources */,
 				F3A9AE9C2C8A13C100AAC390 /* SDL_pipeline_gpu.c in Sources */,
 				75E0915A241EA924004729E1 /* SDL_virtualjoystick.c in Sources */,
+				F338A11A2D1B37E4007CDFDF /* SDL_tray.c in Sources */,
 				A7D8ABEB23E2514100DCD162 /* SDL_nullvideo.c in Sources */,
 				F3990E072A78833C000D8759 /* hid.m in Sources */,
 				A7D8AB6723E2514100DCD162 /* SDL_offscreenevents.c in Sources */,
diff --git a/src/tray/cocoa/SDL_tray.m b/src/tray/cocoa/SDL_tray.m
index 515ee6527cb5c..5197b7dab893c 100644
--- a/src/tray/cocoa/SDL_tray.m
+++ b/src/tray/cocoa/SDL_tray.m
@@ -21,6 +21,8 @@
 
 #include "SDL_internal.h"
 
+#ifdef SDL_PLATFORM_MACOS
+
 #include <Cocoa/Cocoa.h>
 
 #include "../../video/SDL_surface_c.h"
@@ -30,7 +32,7 @@
 struct SDL_TrayMenu {
     NSMenu *nsmenu;
 
-    size_t nEntries;
+    int nEntries;
     SDL_TrayEntry **entries;
 
     SDL_Tray *parent_tray;
@@ -102,7 +104,7 @@ static void DestroySDLMenu(SDL_TrayMenu *menu)
 
 SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
 {
-    SDL_Tray *tray = (SDL_Tray *) SDL_malloc(sizeof(SDL_Tray));
+    SDL_Tray *tray = (SDL_Tray *)SDL_calloc(1, sizeof(*tray));
 
     AppDelegate *delegate = [[AppDelegate alloc] init];
     app = [NSApplication sharedApplication];
@@ -112,8 +114,6 @@ static void DestroySDLMenu(SDL_TrayMenu *menu)
         return NULL;
     }
 
-    SDL_memset((void *) tray, 0, sizeof(*tray));
-
     tray->statusItem = nil;
     tray->statusBar = [NSStatusBar systemStatusBar];
     tray->statusItem = [tray->statusBar statusItemWithLength:NSVariableStatusItemLength];
@@ -211,14 +211,11 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
 
 SDL_TrayMenu *SDL_CreateTrayMenu(SDL_Tray *tray)
 {
-    SDL_TrayMenu *menu = SDL_malloc(sizeof(SDL_TrayMenu));
-
+    SDL_TrayMenu *menu = (SDL_TrayMenu *)SDL_calloc(1, sizeof(*menu));
     if (!menu) {
         return NULL;
     }
 
-    SDL_memset((void *) menu, 0, sizeof(*menu));
-
     NSMenu *nsmenu = [[NSMenu alloc] init];
     [nsmenu setAutoenablesItems:FALSE];
 
@@ -251,14 +248,11 @@ void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
         return NULL;
     }
 
-    SDL_TrayMenu *menu = SDL_malloc(sizeof(SDL_TrayMenu));
-
+    SDL_TrayMenu *menu = (SDL_TrayMenu *)SDL_calloc(1, sizeof(*menu));
     if (!menu) {
         return NULL;
     }
 
-    SDL_memset((void *) menu, 0, sizeof(*menu));
-
     NSMenu *nsmenu = [[NSMenu alloc] init];
     [nsmenu setAutoenablesItems:FALSE];
 
@@ -312,7 +306,7 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
     }
 
     menu->nEntries--;
-    SDL_TrayEntry ** new_entries = SDL_realloc(menu->entries, menu->nEntries * sizeof(SDL_TrayEntry *));
+    SDL_TrayEntry **new_entries = (SDL_TrayEntry **)SDL_realloc(menu->entries, menu->nEntries * sizeof(*new_entries));
 
     /* Not sure why shrinking would fail, but even if it does, we can live with a "too big" array */
     if (new_entries) {
@@ -326,7 +320,7 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
 
 SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *label, SDL_TrayEntryFlags flags)
 {
-    if (pos < -1 || pos > (int) menu->nEntries) {
+    if (pos < -1 || pos > menu->nEntries) {
         SDL_InvalidParamError("pos");
         return NULL;
     }
@@ -335,16 +329,12 @@ void SDL_RemoveTrayEntry(SDL_TrayEntry *entry)
         pos = menu->nEntries;
     }
 
-    SDL_TrayEntry *entry = SDL_malloc(sizeof(SDL_TrayEntry));
-
+    SDL_TrayEntry *entry = (SDL_TrayEntry *)SDL_calloc(1, sizeof(*entry));
     if (!entry) {
         return NULL;
     }
 
-    SDL_memset((void *) entry, 0, sizeof(*entry));
-
-    SDL_TrayEntry **new_entries = (SDL_TrayEntry **) SDL_realloc(menu->entries, (menu->nEntries + 1) * sizeof(SDL_TrayEntry *));
-
+    SDL_TrayEntry **new_entries = (SDL_TrayEntry **)SDL_realloc(menu->entries, (menu->nEntries + 1) * sizeof(*new_entries));
     if (!new_entries) {
         SDL_free(entry);
         return NULL;
@@ -456,3 +446,5 @@ void SDL_DestroyTray(SDL_Tray *tray)
 
     SDL_free(tray);
 }
+
+#endif // SDL_PLATFORM_MACOS
diff --git a/src/tray/dummy/SDL_tray.c b/src/tray/dummy/SDL_tray.c
index 3a105ad46a5e0..e8eaebd32640d 100644
--- a/src/tray/dummy/SDL_tray.c
+++ b/src/tray/dummy/SDL_tray.c
@@ -21,6 +21,8 @@
 
 #include "SDL_internal.h"
 
+#ifndef SDL_PLATFORM_MACOS
+
 SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
 {
     SDL_Unsupported();
@@ -137,3 +139,5 @@ void SDL_DestroyTray(SDL_Tray *tray)
 {
     SDL_Unsupported();
 }
+
+#endif // !SDL_PLATFORM_MACOS