Skip to content

Commit

Permalink
- updated Cocoa backend to follow single exit point workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-lysiuk committed Oct 12, 2019
1 parent bb5ca2c commit 29e3222
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
44 changes: 27 additions & 17 deletions src/posix/cocoa/i_main.mm
Expand Up @@ -140,25 +140,12 @@ void I_DetectOS()
FArgs* Args; // command line arguments


int OriginalMainTry(int argc, char** argv)
{
Args = new FArgs(argc, argv);

NSString* exePath = [[NSBundle mainBundle] executablePath];
progdir = [[exePath stringByDeletingLastPathComponent] UTF8String];
progdir += "/";

auto ret = D_DoomMain();
FConsoleWindow::DeleteInstance();
return ret;
}

namespace
{

TArray<FString> s_argv;

int OriginalMain(int argc, char** argv)
int DoMain(int argc, char** argv)
{
printf(GAMENAME" %s - %s - Cocoa version\nCompiled on %s\n\n",
GetVersionString(), GetGitTime(), __DATE__);
Expand All @@ -178,7 +165,15 @@ int OriginalMain(int argc, char** argv)
vid_defheight = static_cast<int>(screenSize.height);
vid_vsync = true;

return OriginalMainTry(argc, argv);
Args = new FArgs(argc, argv);

NSString* exePath = [[NSBundle mainBundle] executablePath];
progdir = [[exePath stringByDeletingLastPathComponent] UTF8String];
progdir += "/";

auto ret = D_DoomMain();
FConsoleWindow::DeleteInstance();
return ret;
}

} // unnamed namespace
Expand All @@ -203,6 +198,10 @@ - (BOOL)application:(NSApplication*)theApplication openFile:(NSString*)filename;

- (void)processEvents:(NSTimer*)timer;

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;

- (void)sendExitEvent:(id)sender;

@end


Expand Down Expand Up @@ -276,7 +275,7 @@ - (void)applicationDidFinishLaunching:(NSNotification*)aNotification

argv[argc] = nullptr;

exit(OriginalMain(argc, &argv[0]));
exit(DoMain(argc, &argv[0]));
}


Expand Down Expand Up @@ -341,6 +340,17 @@ - (void)processEvents:(NSTimer*)timer
[pool release];
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
[self sendExitEvent:sender];
return NSTerminateLater;
}

- (void)sendExitEvent:(id)sender
{
throw CExitEvent(0);
}

@end


Expand Down Expand Up @@ -370,7 +380,7 @@ - (void)processEvents:(NSTimer*)timer
keyEquivalent:@""];
[menu addItem:[NSMenuItem separatorItem]];
[menu addItemWithTitle:[@"Quit " stringByAppendingString:@GAMENAME]
action:@selector(terminate:)
action:@selector(sendExitEvent:)
keyEquivalent:@"q"];

NSMenuItem* menuItem = [NSMenuItem new];
Expand Down
4 changes: 2 additions & 2 deletions src/posix/cocoa/i_video.mm
Expand Up @@ -64,8 +64,8 @@ @implementation NSWindow(ExitAppOnClose)
- (void)exitAppOnClose
{
NSButton* closeButton = [self standardWindowButton:NSWindowCloseButton];
[closeButton setAction:@selector(terminate:)];
[closeButton setTarget:NSApp];
[closeButton setAction:@selector(sendExitEvent:)];
[closeButton setTarget:NSApp.delegate];
}

@end
Expand Down
6 changes: 3 additions & 3 deletions src/posix/cocoa/st_console.mm
Expand Up @@ -172,7 +172,7 @@
[quitButton setTitle:@"Quit"];
[quitButton setKeyEquivalent:@"\r"];
[quitButton setTarget:NSApp];
[quitButton setAction:@selector(terminate:)];
[quitButton setAction:@selector(stopModal)];

NSView* quitPanel = [[NSView alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, textViewWidth, 32.0f)];
[quitPanel setAutoresizingMask:NSViewWidthSizable];
Expand Down Expand Up @@ -461,8 +461,8 @@ static void UpdateTimed(const Function& function)
[m_netAbortButton setBezelStyle:NSRoundedBezelStyle];
[m_netAbortButton setTitle:@"Cancel"];
[m_netAbortButton setKeyEquivalent:@"\r"];
[m_netAbortButton setTarget:NSApp];
[m_netAbortButton setAction:@selector(terminate:)];
[m_netAbortButton setTarget:NSApp.delegate];
[m_netAbortButton setAction:@selector(sendExitEvent:)];

// Panel for controls above
m_netView = [[NSView alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, 512.0f, NET_VIEW_HEIGHT)];
Expand Down
3 changes: 2 additions & 1 deletion src/posix/osx/iwadpicker_cocoa.mm
Expand Up @@ -40,6 +40,7 @@
#include "m_argv.h"
#include "m_misc.h"
#include "gameconfigfile.h"
#include "doomerrors.h"

#include <Cocoa/Cocoa.h>
#include <wordexp.h>
Expand Down Expand Up @@ -360,7 +361,7 @@ - (void)menuActionSent:(NSNotification*)notification

if ( @selector(terminate:) == [menuItem action] )
{
exit(0);
throw CExitEvent(0);
}
}

Expand Down

0 comments on commit 29e3222

Please sign in to comment.