Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1896 from reicast/einsteinx2/macos-command-line-args
Browse files Browse the repository at this point in the history
macOS: Added arg parsing and updated help message
  • Loading branch information
einsteinx2 committed Apr 23, 2020
2 parents 3bccd0b + 8875273 commit d68f580
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 50 deletions.
23 changes: 16 additions & 7 deletions libswirl/cfg/cl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions reicast/apple/emulator-osx/emulator-osx/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

+ (AppDelegate *)sharedInstance;

- (instancetype)initWithArgc:(int)argc andArgv:(char**)argv;

- (void)showConsoleWindow:(NSMenuItem *)menuItem;
- (void)hideConsoleWindow:(NSMenuItem *)menuItem;

Expand Down
109 changes: 68 additions & 41 deletions reicast/apple/emulator-osx/emulator-osx/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import "MainMenu.h"
#import "libswirl.h"
#import "gui/gui_renderer.h"
#include <cstdio>

#if FEAT_HAS_SERIAL_TTY
#include <util.h>
Expand All @@ -24,6 +25,8 @@
static CGSize _glViewSize;
static void gl_resize();

static bool _isInitializing = true;

@interface AppDelegate()
@property (strong) NSTextStorage *consoleTextStorage;
@end
Expand All @@ -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 -
Expand All @@ -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];
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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:^{
Expand Down Expand Up @@ -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();
}
}
}

Expand All @@ -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 -
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions reicast/apple/emulator-osx/emulator-osx/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit d68f580

Please sign in to comment.