From a5e8dd4e2eefdffdb27a4a3116644fa27e67adf7 Mon Sep 17 00:00:00 2001 From: Nicolas Guillemot Date: Tue, 24 Jan 2017 12:18:06 -0800 Subject: [PATCH] fix close window notif and unused vars (#10) It wasn't detecting that the window was closed when I closed it by clicking the red X button. As a result, the window would close, but mfb_update() didn't return -1, so the program kept running. This was fixed by listening to the willClose event and using it to set a flag. I'm not sure if this is the best way to do it, I really know nothing about OSX/Cocoa. Also, I fixed a few unused varible warnings. And warnings related to NSUInteger not matching the expected signature with NSWindowStyleMask. --- src/macosx/MacMiniFB.m | 7 +++++-- src/macosx/OSXWindow.h | 1 + src/macosx/OSXWindow.m | 21 +++++++++++++++++++-- src/macosx/OSXWindowFrameView.m | 2 ++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/macosx/MacMiniFB.m b/src/macosx/MacMiniFB.m index 9cefa56..a0fbb68 100644 --- a/src/macosx/MacMiniFB.m +++ b/src/macosx/MacMiniFB.m @@ -2,14 +2,14 @@ #include "OSXWindow.h" #include #include -#include +#include "MiniFB.h" /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void* g_updateBuffer = 0; int g_width = 0; int g_height = 0; -static NSWindow* window_; +static OSXWindow* window_; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -83,6 +83,9 @@ static int updateEvents() } [pool release]; + if (window_->closed) + state = -1; + return state; } diff --git a/src/macosx/OSXWindow.h b/src/macosx/OSXWindow.h index 5314f9c..05eb51e 100644 --- a/src/macosx/OSXWindow.h +++ b/src/macosx/OSXWindow.h @@ -5,6 +5,7 @@ @interface OSXWindow : NSWindow { NSView* childContentView; + @public bool closed; } @end diff --git a/src/macosx/OSXWindow.m b/src/macosx/OSXWindow.m index a910e10..e1107d1 100644 --- a/src/macosx/OSXWindow.m +++ b/src/macosx/OSXWindow.m @@ -6,7 +6,7 @@ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - (id)initWithContentRect:(NSRect)contentRect - styleMask:(NSUInteger)windowStyle + styleMask:(NSWindowStyleMask)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation { @@ -31,6 +31,14 @@ selector:@selector(mainWindowChanged:) name:NSWindowDidResignMainNotification object:self]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(willClose) + name:NSWindowWillCloseNotification + object:self]; + + closed = false; } return self; } @@ -65,6 +73,7 @@ - (void)mainWindowChanged:(NSNotification *)aNotification { + (void)aNotification; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -128,9 +137,17 @@ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -+ (NSRect)frameRectForContentRect:(NSRect)windowContentRect styleMask:(NSUInteger)windowStyle ++ (NSRect)frameRectForContentRect:(NSRect)windowContentRect styleMask:(NSWindowStyleMask)windowStyle { + (void)windowStyle; return NSInsetRect(windowContentRect, 0, 0); } +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (void)willClose +{ + closed = true; +} + @end diff --git a/src/macosx/OSXWindowFrameView.m b/src/macosx/OSXWindowFrameView.m index 1f01a41..0961d41 100644 --- a/src/macosx/OSXWindowFrameView.m +++ b/src/macosx/OSXWindowFrameView.m @@ -27,6 +27,8 @@ extern int g_height; - (void)drawRect:(NSRect)rect { + (void)rect; + if (!g_updateBuffer) return;