From 57a7e24d2171394f7ad1a4975b07ef6a61432bab Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Wed, 30 Sep 2020 21:49:06 -0400 Subject: [PATCH] added working directory option for custom mame config. also log path/working directory to the log window. --- Ample/Ample.h | 1 + Ample/Ample.m | 1 + Ample/Base.lproj/Preferences.xib | 36 +++++++++++++++++---- Ample/LaunchWindowController.m | 4 +++ Ample/LogWindowController.m | 49 ++++++++++++++++++----------- Ample/PreferencesWindowController.m | 29 +++++++++++++++-- 6 files changed, 94 insertions(+), 26 deletions(-) diff --git a/Ample/Ample.h b/Ample/Ample.h index 2060116..33ddacd 100644 --- a/Ample/Ample.h +++ b/Ample/Ample.h @@ -17,6 +17,7 @@ NSString *SupportDirectoryPath(void); /* NSUserDefaults keys */ extern NSString *kUseCustomMame; extern NSString *kMamePath; +extern NSString *kMameWorkingDirectory; extern NSString *kAutoCloseLogWindow; extern NSString *kMameComponentsDate; diff --git a/Ample/Ample.m b/Ample/Ample.m index 007dbaa..b3da4f7 100644 --- a/Ample/Ample.m +++ b/Ample/Ample.m @@ -37,5 +37,6 @@ NSString *SupportDirectoryPath(void) { NSString *kUseCustomMame = @"UseCustomMame"; NSString *kMamePath = @"MamePath"; +NSString *kMameWorkingDirectory = @"MameWorkingDirectory"; NSString *kAutoCloseLogWindow = @"AutoCloseLogWindow"; NSString *kMameComponentsDate = @"MameComponentsDate"; diff --git a/Ample/Base.lproj/Preferences.xib b/Ample/Base.lproj/Preferences.xib index 0f81232..6f7aca5 100644 --- a/Ample/Base.lproj/Preferences.xib +++ b/Ample/Base.lproj/Preferences.xib @@ -10,6 +10,7 @@ + @@ -25,7 +26,7 @@ - + @@ -38,17 +39,31 @@ - - + + + + + + + + + + + + + + + + - + + + + + + + + + + diff --git a/Ample/LaunchWindowController.m b/Ample/LaunchWindowController.m index d35ee33..327995b 100644 --- a/Ample/LaunchWindowController.m +++ b/Ample/LaunchWindowController.m @@ -374,6 +374,9 @@ static NSString * JoinArguments(NSArray *argv) { if (![_args count]) return; + [LogWindowController controllerForArgs: _args]; + +#if 0 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSURL *url = MameURL(); @@ -395,6 +398,7 @@ static NSString * JoinArguments(NSArray *argv) { } [LogWindowController controllerForTask: task]; +#endif } diff --git a/Ample/LogWindowController.m b/Ample/LogWindowController.m index f6ef0ef..7996332 100644 --- a/Ample/LogWindowController.m +++ b/Ample/LogWindowController.m @@ -47,13 +47,25 @@ static NSURL *MameURL(void) { } return [bundle URLForAuxiliaryExecutable: @"mame64"]; - - return nil; } +static NSURL *MameWorkingDirectory(void) { + + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + + if ([defaults boolForKey: kUseCustomMame]) { + NSString *path = [defaults stringForKey: kMameWorkingDirectory]; + if (![path length]) return [NSURL fileURLWithPath: path]; + } + + return SupportDirectory(); +} + + + +(id)controllerForArgs: (NSArray *)args { - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; +// NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSURL *url = MameURL(); @@ -65,19 +77,12 @@ static NSURL *MameURL(void) { return nil; } - NSTask *task = [NSTask new]; [task setExecutableURL: url]; [task setArguments: args]; - - if (![defaults boolForKey: kUseCustomMame]) { - // run in Application Support/Ample. - [task setCurrentDirectoryURL: SupportDirectory()]; - } + [task setCurrentDirectoryURL: MameWorkingDirectory()]; return [LogWindowController controllerForTask: task]; - - } - (void)windowDidLoad { @@ -103,25 +108,33 @@ static NSURL *MameURL(void) { NSError *error = nil; NSPipe *pipe = [NSPipe pipe]; - + + // window not yet loaded until [self window] called. + const char *path = [[task executableURL] fileSystemRepresentation]; +// if (cp) [self appendString: [NSString stringWithFormat: @"MAME path: %s", cp]]; + const char *wd = [[task currentDirectoryURL] fileSystemRepresentation]; +// if (cp) [self appendString: [NSString stringWithFormat: @"Working Directory: %s", cp]]; + [task setStandardError: pipe]; [task setStandardOutput: pipe]; [task launchAndReturnError: &error]; if (error) { - NSURL *url = [task executableURL]; - NSString *path = [NSString stringWithCString: [url fileSystemRepresentation] encoding: NSUTF8StringEncoding]; - NSLog(@"NSTask error. Path = %@ error = %@", path, error); - [self appendString: path]; - [self appendString: [error description]]; +// NSURL *url = [task executableURL]; +// NSString *path = [NSString stringWithCString: [url fileSystemRepresentation] encoding: NSUTF8StringEncoding]; + NSLog(@"NSTask error. Path = %s error = %@", path, error); +// [self appendString: path]; +// [self appendString: [error description]]; return error; } _task = task; NSString *title = [NSString stringWithFormat: @"Ample Log - %u", [task processIdentifier]]; [[self window] setTitle: title]; _handle = [pipe fileHandleForReading]; - + + if (path) [self appendString: [NSString stringWithFormat: @"MAME path: %s\n", path]]; + if (wd) [self appendString: [NSString stringWithFormat: @"Working Directory: %s\n", wd]]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; diff --git a/Ample/PreferencesWindowController.m b/Ample/PreferencesWindowController.m index f8e12ee..f9f689e 100644 --- a/Ample/PreferencesWindowController.m +++ b/Ample/PreferencesWindowController.m @@ -14,6 +14,7 @@ @interface PreferencesWindowController () @property (weak) IBOutlet NSTextField *pathField; +@property (weak) IBOutlet NSTextField *wdField; @property (weak) IBOutlet NSButton *fixButton; @end @@ -31,7 +32,8 @@ // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. [self validateMamePath: [defaults stringForKey: kMamePath]]; - + [self validateMameWD: [defaults stringForKey: kMameWorkingDirectory]]; + /* check vmnet_helper permissions */ int needs_fixin = [self checkHelperPermissions: nil]; @@ -48,6 +50,23 @@ } } +-(void)validateMameWD: (NSString *)path { + NSFileManager * fm = [NSFileManager defaultManager]; + BOOL directory = YES; + + if ([path length] == 0) { + [_wdField setTextColor: [NSColor blackColor]]; + return; + } + + if ([fm fileExistsAtPath: path isDirectory: &directory] && directory) { + [_wdField setTextColor: [NSColor blackColor]]; + return; + + } + [_wdField setTextColor: [NSColor redColor]]; +} + - (IBAction)pathChanged:(id)sender { NSString *path = [sender stringValue]; @@ -55,6 +74,12 @@ [self validateMamePath: path]; } +- (IBAction)wdChanged:(id)sender { + + NSString *path = [sender stringValue]; + + [self validateMameWD: path]; +} // -1 - error // 1 - needs help @@ -150,7 +175,7 @@ const char* args_chown[] = {"root", cp , NULL}; const char* args_chmod[] = {"+s", cp, NULL}; - // well ... the second command executes a lot more consistently when the (optional) fp is provided and the we fgets the buffer. + // well ... the second command executes a lot more consistently when the (optional) fp is provided and the we fgets the buffer. myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, "/usr/sbin/chown", kAuthorizationFlagDefaults, (char**)args_chown, &fp); fgets(buffer, sizeof(buffer), fp); fclose(fp);