From f0029fd3c6dad5a07592d853c99567f9e8515eca Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Fri, 12 Jan 2018 16:38:19 +0200 Subject: [PATCH] [macOS] Adds ability to open files with "Open With" or double-click from Finder --- platform/osx/godot_main_osx.mm | 11 ++++++++++- platform/osx/os_osx.h | 2 ++ platform/osx/os_osx.mm | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/platform/osx/godot_main_osx.mm b/platform/osx/godot_main_osx.mm index 9d1a5566c9c..6ccbaf896b7 100644 --- a/platform/osx/godot_main_osx.mm +++ b/platform/osx/godot_main_osx.mm @@ -82,8 +82,17 @@ int main(int argc, char **argv) { #endif OS_OSX os; + Error err; + + if (os.open_with_filename != "") { + char *argv_c = (char *)malloc(os.open_with_filename.utf8().size()); + memcpy(argv_c, os.open_with_filename.utf8().get_data(), os.open_with_filename.utf8().size()); + err = Main::setup(argv[0], 1, &argv_c); + free(argv_c); + } else { + err = Main::setup(argv[0], argc - first_arg, &argv[first_arg]); + } - Error err = Main::setup(argv[0], argc - first_arg, &argv[first_arg]); if (err != OK) return 255; diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index a1869497ef2..c422eb9223d 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -115,6 +115,8 @@ public: Size2 window_size; Rect2 restore_rect; + String open_with_filename; + Point2 im_position; ImeCallback im_callback; void *im_target; diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index d0cc115f0e4..8369adbb5fd 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -144,6 +144,13 @@ static Vector2 get_mouse_pos(NSEvent *event) { @implementation GodotApplicationDelegate +- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename { + // Note: called before main loop init! + char *utfs = strdup([filename UTF8String]); + OS_OSX::singleton->open_with_filename.parse_utf8(utfs); + return YES; +} + - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { if (OS_OSX::singleton->get_main_loop()) OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST); @@ -2322,6 +2329,20 @@ OS_OSX::OS_OSX() { Vector loggers; loggers.push_back(memnew(OSXTerminalLogger)); _set_logger(memnew(CompositeLogger(loggers))); + + //process application:openFile: event + while (true) { + NSEvent *event = [NSApp + nextEventMatchingMask:NSEventMaskAny + untilDate:[NSDate distantPast] + inMode:NSDefaultRunLoopMode + dequeue:YES]; + + if (event == nil) + break; + + [NSApp sendEvent:event]; + } } bool OS_OSX::_check_internal_feature_support(const String &p_feature) {