diff --git a/libswirl/cfg/cl.cpp b/libswirl/cfg/cl.cpp index 84e4f1a1e6..c27ca7a182 100644 --- a/libswirl/cfg/cl.cpp +++ b/libswirl/cfg/cl.cpp @@ -102,13 +102,22 @@ int setconfig(wchar** arg,int cl) return rv; } -int showhelp(wchar** arg,int cl) +int showhelp(wchar** argv,int cl) { - printf("\nAvailable commands :\n"); - - printf("-config section:key=value [, ..]: add a virtual config value\n Virtual config values won't be saved to the .cfg file\n unless a different value is written to em\nNote :\n You can specify many settings in the xx:yy=zz , gg:hh=jj , ...\n format.The spaces between the values and ',' are needed.\n"); - printf("-portable: look for data and discs in the current directory\n"); - printf("\n-help: show help info\n"); + printf("\nUsage:\n"); + printf(" Load emulator GUI: %s\n", argv[0]); + printf(" Directly load disc image: %s image_path.[cdi/gdi]\n\n", argv[0]); + + printf("Available commands:\n"); + + printf(" -config section:key=value [, ..]\n"); + printf(" Add a virtual config value unless a different value is written to em\n"); + printf(" Note: You can specify many settings in the xx:yy=zz , gg:hh=jj , ... format.\n"); + printf(" The spaces between the values and ',' are needed.\n"); + printf(" -portable:\n"); + printf(" Look for data and discs in the current directory\n"); + printf(" -help:\n"); + printf(" Show the help info that you're reading now\n\n"); return 0; } @@ -122,7 +131,7 @@ bool ParseCommandLine(int argc,wchar* argv[]) { if (stricmp(*arg,"-help")==0 || stricmp(*arg,"--help")==0) { - showhelp(arg,cl); + showhelp(argv,cl); return true; } else if (stricmp(*arg,"-config")==0 || stricmp(*arg,"--config")==0) diff --git a/reicast/apple/emulator-osx/emulator-osx/AppDelegate.h b/reicast/apple/emulator-osx/emulator-osx/AppDelegate.h index 08c3224902..007ac16286 100644 --- a/reicast/apple/emulator-osx/emulator-osx/AppDelegate.h +++ b/reicast/apple/emulator-osx/emulator-osx/AppDelegate.h @@ -19,6 +19,8 @@ + (AppDelegate *)sharedInstance; +- (instancetype)initWithArgc:(int)argc andArgv:(char**)argv; + - (void)showConsoleWindow:(NSMenuItem *)menuItem; - (void)hideConsoleWindow:(NSMenuItem *)menuItem; diff --git a/reicast/apple/emulator-osx/emulator-osx/AppDelegate.mm b/reicast/apple/emulator-osx/emulator-osx/AppDelegate.mm index 2cb32868e9..61e24d53f2 100644 --- a/reicast/apple/emulator-osx/emulator-osx/AppDelegate.mm +++ b/reicast/apple/emulator-osx/emulator-osx/AppDelegate.mm @@ -7,6 +7,7 @@ #import "MainMenu.h" #import "libswirl.h" #import "gui/gui_renderer.h" +#include #if FEAT_HAS_SERIAL_TTY #include @@ -24,6 +25,8 @@ static CGSize _glViewSize; static void gl_resize(); +static bool _isInitializing = true; + @interface AppDelegate() @property (strong) NSTextStorage *consoleTextStorage; @end @@ -35,6 +38,10 @@ @implementation AppDelegate { // Console redirection dispatch_source_t _stdoutSource; dispatch_source_t _stderrSource; + + // Terminal arguments + int _argc; + char** _argv; } #pragma mark - Delegate Methods - @@ -43,6 +50,13 @@ + (AppDelegate *)sharedInstance { return _sharedInstance; } +- (instancetype)initWithArgc:(int)argc andArgv:(char **)argv { + self = [super init]; + _argc = argc; + _argv = argv; + return self; +} + - (void)applicationDidFinishLaunching:(NSNotification *)notification { _sharedInstance = self; [self mainSetup]; @@ -123,18 +137,22 @@ - (void)hideConsoleWindow:(NSMenuItem *)menuItem { #pragma mark - Setup Methods - - (void)mainSetup { - [self captureConsoleOutput]; - [self setupWindow]; - [self setupScreenDPI]; [self setupPaths]; common_linux_setup(); - if (reicast_init(0, NULL) != 0) { + int result = reicast_init(_argc, _argv); + if (result == 0) { + _isInitializing = false; + [self captureConsoleOutput]; + #if FEAT_HAS_SERIAL_TTY + _removePTYSymlink = common_serial_pty_setup(); + #endif + [self setupUIThread]; + } else if (result == 69) { + // We showed the usage text, so don't show an error, just exit + exit(0); + } else { [self alertAndTerminateWithMessage:@"Reicast initialization failed"]; } - #if FEAT_HAS_SERIAL_TTY - _removePTYSymlink = common_serial_pty_setup(); - #endif - [self setupUIThread]; } - (void)captureConsoleOutput { @@ -189,6 +207,33 @@ - (void)captureConsoleOutput { dispatch_resume(_stdoutSource); } +- (void)setupPaths { + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSString *configDirPath = [[NSURL fileURLWithPathComponents:@[NSHomeDirectory(), @".reicast"]] path]; + BOOL isDir = false; + BOOL exists = [fileManager fileExistsAtPath:configDirPath isDirectory:&isDir]; + if (exists && !isDir) { + NSString *formatString = @"Config directory exists, but is not a directory: %@"; + [self alertAndTerminateWithMessage:[NSString stringWithFormat:formatString, configDirPath]]; + } else if (!exists) { + NSError *error; + [fileManager createDirectoryAtPath:configDirPath withIntermediateDirectories:YES attributes:nil error:&error]; + if (error) { + NSString *formatString = @"Failed to create config directory at %@ with error: %@"; + [self alertAndTerminateWithMessage:[NSString stringWithFormat:formatString, configDirPath, error]]; + } + } + + // Set user config path + std::string config_dir([configDirPath UTF8String]); + set_user_config_dir(config_dir); + set_user_data_dir(config_dir); + + // Set system data path + std::string data_dir([[[NSBundle mainBundle] resourcePath] UTF8String]); + add_system_data_dir(data_dir); +} + - (void)setupWindow { // Create the OpenGL view and context // TODO: BEN See why background color is red when resizing window tall (it's likely due to default GL framebuffer color) @@ -207,6 +252,8 @@ - (void)setupWindow { // Set initial scale factor and view size _backingScaleFactor = _mainWindow.screen.backingScaleFactor; _glViewSize = _glView.bounds.size; + + [self setupScreenDPI]; } - (void)setupScreenDPI { @@ -222,33 +269,6 @@ - (void)setupScreenDPI { NSLog(@"displayPixelSize: %@ displayPhysicalSize: %@ screen_dpi: %d", NSStringFromSize(displayPixelSize), NSStringFromSize(displayPhysicalSize), screen_dpi); } -- (void)setupPaths { - NSFileManager *fileManager = [NSFileManager defaultManager]; - NSString *configDirPath = [[NSURL fileURLWithPathComponents:@[NSHomeDirectory(), @".reicast"]] path]; - BOOL isDir = false; - BOOL exists = [fileManager fileExistsAtPath:configDirPath isDirectory:&isDir]; - if (exists && !isDir) { - NSString *formatString = @"Config directory exists, but is not a directory: %@"; - [self alertAndTerminateWithMessage:[NSString stringWithFormat:formatString, configDirPath]]; - } else if (!exists) { - NSError *error; - [fileManager createDirectoryAtPath:configDirPath withIntermediateDirectories:YES attributes:nil error:&error]; - if (error) { - NSString *formatString = @"Failed to create config directory at %@ with error: %@"; - [self alertAndTerminateWithMessage:[NSString stringWithFormat:formatString, configDirPath, error]]; - } - } - - // Set user config path - std::string config_dir([configDirPath UTF8String]); - set_user_config_dir(config_dir); - set_user_data_dir(config_dir); - - // Set system data path - std::string data_dir([[[NSBundle mainBundle] resourcePath] UTF8String]); - add_system_data_dir(data_dir); -} - - (void)setupUIThread { // Run the rendering in a dedicated thread _uiThread = [[NSThread alloc] initWithBlock:^{ @@ -293,7 +313,9 @@ - (void)shutdownEmulator { }); } else { // Stop the UI thread (which will then complete the shutdown, otherwise doing it here will crash) - g_GUIRenderer->Stop(); + if (g_GUIRenderer) { + g_GUIRenderer->Stop(); + } } } @@ -303,7 +325,8 @@ - (void)alertAndTerminateWithMessage:(NSString *)message { alert.messageText = message; [alert addButtonWithTitle:@"Exit"]; [alert runModal]; - [NSApp terminate:nil]; + // NOTE: Using [NSApp terminate:] here will hang, so using exit() instead + exit(1); } #pragma mark - Reicast OS Functions - @@ -329,7 +352,7 @@ void UpdateInputState(u32 port) { // Called by libswirl to create the emulator window void os_CreateWindow() { - // Left empty on purpose as we have nothing to run here + [AppDelegate.sharedInstance setupWindow]; } // Called by libswirl when OpenGL is initialized @@ -381,9 +404,13 @@ int darw_printf(const wchar* text,...) { va_start(args, text); vsprintf(temp, text, args); va_end(args); - - NSLog(@"%s", temp); - + + if (_isInitializing) { + fprintf(stdout, "%s", temp); + } else { + NSLog(@"%s", temp); + } + return 0; } diff --git a/reicast/apple/emulator-osx/emulator-osx/main.m b/reicast/apple/emulator-osx/emulator-osx/main.m index 94248d409a..452fac10e5 100644 --- a/reicast/apple/emulator-osx/emulator-osx/main.m +++ b/reicast/apple/emulator-osx/emulator-osx/main.m @@ -12,11 +12,11 @@ int main(int argc, char *argv[]) { // Create Application and AppDelegate NSApplication *app = [NSApplication sharedApplication]; [app setActivationPolicy:NSApplicationActivationPolicyRegular]; - [app setDelegate:(id)[[AppDelegate alloc] init]]; + [app setDelegate:(id)[[AppDelegate alloc] initWithArgc:argc andArgv:argv]]; // Create the Main Menu [MainMenu create]; - + // Start the Application [app activateIgnoringOtherApps:YES]; [app run];