--- src/platform/apple/GLFWCustomDelegate.h 2025-11-16 00:10:03.000000000 +0800 +++ src/platform/apple/GLFWCustomDelegate.h 2026-01-08 08:39:43.000000000 +0800 @@ -13,10 +13,20 @@ #import #import +#if !defined(__OBJC2__) && !defined(Method) +typedef struct objc_method *Method; +#endif + +#ifndef NS_ASSUME_NONNULL_BEGIN +#define NS_ASSUME_NONNULL_BEGIN +#define NS_ASSUME_NONNULL_END +#endif + NS_ASSUME_NONNULL_BEGIN @interface GLFWCustomDelegate : NSObject -+ (void)load; // load is called before even main() is run (as part of objc class registration) +/* load is called before even main() is run (as part of objc class registration) */ ++ (void)load; @end NS_ASSUME_NONNULL_END --- src/platform/apple/GLFWCustomDelegate.m 2025-11-16 00:10:03.000000000 +0800 +++ src/platform/apple/GLFWCustomDelegate.m 2026-01-08 08:38:48.000000000 +0800 @@ -9,6 +9,12 @@ \**********************************************/ #import "GLFWCustomDelegate.h" + +/* Need to declare Method before including runtime.h to avoid conflicts */ +#if !defined(__OBJC2__) && !defined(Method) +#define Method Method +#endif + #import extern void AddFile(const char* filename); @@ -17,23 +23,31 @@ + (void)load { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^ + static BOOL initialized = NO; + Class class; + + if (!initialized) { - Class class = objc_getClass("GLFWApplicationDelegate"); + initialized = YES; + class = objc_getClass("GLFWApplicationDelegate"); - [GLFWCustomDelegate swizzle: class src: @selector(application: openFile:) tgt: @selector(swz_application: openFile:)]; - [GLFWCustomDelegate swizzle: class src: @selector(application: openFiles:) tgt: @selector(swz_application: openFiles:)]; - }); + [GLFWCustomDelegate swizzle: class src: @selector(application: openFile:) tgt: @selector(swz_application:openFile:)]; + [GLFWCustomDelegate swizzle: class src: @selector(application: openFiles:) tgt: @selector(swz_application:openFiles:)]; + } } + (void) swizzle: (Class) original_c src: (SEL)original_s tgt: (SEL)target_s { - Class target_c = [GLFWCustomDelegate class]; - Method originalMethod = class_getInstanceMethod(original_c, original_s); - Method swizzledMethod = class_getInstanceMethod(target_c, target_s); + Class target_c; + struct objc_method *originalMethod; + struct objc_method *swizzledMethod; + BOOL didAddMethod; + + target_c = [GLFWCustomDelegate class]; + originalMethod = class_getInstanceMethod(original_c, original_s); + swizzledMethod = class_getInstanceMethod(target_c, target_s); - BOOL didAddMethod = + didAddMethod = class_addMethod(original_c, original_s, method_getImplementation(swizzledMethod), @@ -54,15 +68,22 @@ - (BOOL)swz_application: (NSApplication*)sender openFile: (NSString*)filename { - AddFile(filename.UTF8String); - return true; + (void)sender; /* Suppress unused parameter warning */ + AddFile([filename UTF8String]); + return YES; } -- (void)swz_application: (NSApplication*)sender openFiles: (NSArray*)filenames +- (void)swz_application: (NSApplication*)sender openFiles: (NSArray*)filenames { - for (NSString* filename in filenames) + NSString* filename; + NSEnumerator* enumerator; + + (void)sender; /* Suppress unused parameter warning */ + + enumerator = [filenames objectEnumerator]; + while ((filename = [enumerator nextObject])) { - AddFile(filename.UTF8String); + AddFile([filename UTF8String]); } }