diff --git a/src/cocoa_init.m b/src/cocoa_init.m index a496cd3e..1bcee9d9 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -228,9 +228,12 @@ void _glfwPlatformTerminate(void) _glfw.ns.eventSource = NULL; } - [NSApp setDelegate:nil]; - [_glfw.ns.delegate release]; - _glfw.ns.delegate = nil; + id delegate = [NSApp delegate]; + if (delegate) + { + [delegate release]; + [NSApp setDelegate:nil]; + } [_glfw.ns.autoreleasePool release]; _glfw.ns.autoreleasePool = nil; diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index f977f220..368fb10f 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -72,7 +72,6 @@ typedef struct _GLFWwindowNS typedef struct _GLFWlibraryNS { CGEventSourceRef eventSource; - id delegate; id autoreleasePool; id cursor; diff --git a/src/cocoa_window.m b/src/cocoa_window.m index ba4ac059..60c00fe5 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -262,6 +262,13 @@ static NSRect convertRectToBacking(_GLFWwindow* window, NSRect contentRect) _glfwInputMonitorChange(); } +- (void)applicationDidFinishLaunching:(NSNotification *)notification +{ + [NSApp stop:nil]; + + _glfwPlatformPostEmptyEvent(); +} + @end // Translates OS X key modifiers into GLFW ones @@ -802,7 +809,18 @@ static GLboolean initializeAppKit(void) createMenuBar(); #endif - [NSApp finishLaunching]; + // There can only be one application delegate, but we allocate it the + // first time a window is created to keep all window code in this file + id delegate = [[GLFWApplicationDelegate alloc] init]; + if (delegate == nil) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "Cocoa: Failed to create application delegate"); + return GL_FALSE; + } + + [NSApp setDelegate:delegate]; + [NSApp run]; return GL_TRUE; } @@ -905,21 +923,6 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, if (!initializeAppKit()) return GL_FALSE; - // There can only be one application delegate, but we allocate it the - // first time a window is created to keep all window code in this file - if (_glfw.ns.delegate == nil) - { - _glfw.ns.delegate = [[GLFWApplicationDelegate alloc] init]; - if (_glfw.ns.delegate == nil) - { - _glfwInputError(GLFW_PLATFORM_ERROR, - "Cocoa: Failed to create application delegate"); - return GL_FALSE; - } - - [NSApp setDelegate:_glfw.ns.delegate]; - } - if (!createWindow(window, wndconfig)) return GL_FALSE;