From ec03b24c71b3f779271e8808a35fab8063a21297 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sun, 13 Oct 2019 23:54:16 -0400 Subject: [PATCH] Add glfwGetCocoaOpenedFilenames() --- include/GLFW/glfw3native.h | 14 ++++++++++++++ src/cocoa_init.m | 33 +++++++++++++++++++++++++++++++++ src/cocoa_platform.h | 1 + 3 files changed, 48 insertions(+) diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index e680c1e34..8118e6e72 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -215,6 +215,20 @@ GLFWAPI CGDirectDisplayID glfwGetCocoaMonitor(GLFWmonitor* monitor); * @ingroup native */ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* window); + +/*! @brief Returns the list of filenames that opened the application, + * such as by dragging files to the app bundle or through file associations. + * + * @return A list of strings, null terminated. + * + * @thread_safety This function may be called from any thread. Access is not + * synchronized. + * + * @since Added in version 3.4. + * + * @ingroup native + */ +const char* const* glfwGetCocoaOpenedFilenames(void); #endif #if defined(GLFW_EXPOSE_NATIVE_NSGL) diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 01c1b319e..c698cdd04 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -461,6 +461,20 @@ static GLFWbool initializeTIS(void) _glfwRestoreVideoModeNS(_glfw.monitors[i]); } +- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames +{ + int len = [filenames count]; + // Last entry is nil + _glfw.ns.openedFilenames = calloc(len + 1, sizeof(char*)); + + for (int i = 0; i < len; i++) + { + NSString* filename = [filenames objectAtIndex:i]; + const char* filenameStr = [filename UTF8String]; + _glfw.ns.openedFilenames[i] = _glfw_strdup(filenameStr); + } +} + @end // GLFWApplicationDelegate @@ -577,6 +591,16 @@ void _glfwPlatformTerminate(void) if (_glfw.ns.keyUpMonitor) [NSEvent removeMonitor:_glfw.ns.keyUpMonitor]; + if (_glfw.ns.openedFilenames) + { + for (char** p = _glfw.ns.openedFilenames; *p; p++) + { + free(*p); + } + free(_glfw.ns.openedFilenames); + _glfw.ns.openedFilenames = nil; + } + free(_glfw.ns.clipboardString); _glfwTerminateNSGL(); @@ -594,3 +618,12 @@ const char* _glfwPlatformGetVersionString(void) ; } + +////////////////////////////////////////////////////////////////////////// +////// GLFW native API ////// +////////////////////////////////////////////////////////////////////////// + +const char* const* glfwGetCocoaOpenedFilenames(void) +{ + return _glfw.ns.openedFilenames; +} diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index f8bfe3e65..932303920 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -148,6 +148,7 @@ typedef struct _GLFWlibraryNS double restoreCursorPosX, restoreCursorPosY; // The window whose disabled cursor mode is active _GLFWwindow* disabledCursorWindow; + char** openedFilenames; struct { CFBundleRef bundle;