ample/Ample/LogWindowController.m

266 lines
6.7 KiB
Mathematica
Raw Permalink Normal View History

//
// LogWindowController.m
2020-08-30 03:24:49 +00:00
// Ample
//
// Created by Kelvin Sherlock on 8/29/2020.
// Copyright © 2020 Kelvin Sherlock. All rights reserved.
//
2020-09-02 03:52:11 +00:00
#import "Ample.h"
#import "LogWindowController.h"
static NSMutableSet *LogWindows;
@interface LogWindowController ()
@property (unsafe_unretained) IBOutlet NSTextView *textView;
@end
@implementation LogWindowController {
NSTask *_task;
NSFileHandle *_handle;
NSFont *_font;
BOOL _close;
BOOL _eof;
}
+(void)initialize {
LogWindows = [NSMutableSet set];
}
-(NSString *)windowNibName {
return @"LogWindow";
}
+(id)controllerForTask: (NSTask *)task close: (BOOL)close{
LogWindowController *controller = [[LogWindowController alloc] initWithWindowNibName: @"LogWindow"];
[controller runTask: task close: close];
return controller;
}
+(id)controllerForArgs: (NSArray *)args close: (BOOL)close {
NSURL *url = MameURL();
if (!url) {
NSAlert *alert = [NSAlert new];
[alert setMessageText: @"Unable to find MAME executable path"];
[alert runModal];
return nil;
}
NSTask *task = [NSTask new];
Updates to work with 10.11 (El Capitan) and newer. There are still a couple minor UI degradations eg: mame cheat sheet, recent disk images window, auto complete window. Squashed commit of the following: commit fb115024f805da38b747fd1905573fabb7bff24e Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 18:00:13 2021 -0400 ? commit 8405f4df95ae8f5bc5dc33598dc0b3f8972b32cd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:43 2021 -0400 tintColor not present before 10.14 commit 86a6102cee4f30cac2972259ef55fda3c32b8f9a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:19 2021 -0400 [menu setItemArray:] apparently has problems before 10.14 commit 65227a00743fbf143c623af8a76698d57bdc4573 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:58:15 2021 -0400 NSCache doesn't like null objects. commit 8a4c3a4662647d5ee0b609bde9f3aaa5c0d99776 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 16:00:05 2021 -0400 organize copy bundle resources. multiple-sized png images are converted to a single tiff (with multiple sizes). This is fine BUT EL capitan grabs the first image so if the @3x image is first, it will be used. commit 44b94cdfd47117864b6529214b1c91a0c2e41327 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 15:57:37 2021 -0400 add NewSlotViewController.m to Ample Lite. commit 2fa1b7418783e48ca625ab3490e43cbff9e5619a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:57:19 2021 -0400 Ample Lite was missing reference to New Slot View nib. commit 65f268e77a65f8c57019702d8f0c2b66577c5ee7 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:56:04 2021 -0400 based on testing, 10.11 still has public.file-url, which is kUTTypeFileURL. 10.13 added NSURLPBoardType as a synonym. commit cde123b16eb336b7695b444931b9e9bb9802d772 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:54:02 2021 -0400 set everything to 10.11 commit e7a670efd3298e8ce1409cde1cce559ba46879f4 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 23:33:31 2021 -0400 script to download SDL2.Framework commit 9f38f2972ab0e38fbdaebb3e3f35281978303519 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:15:32 2021 -0400 WKWebView apparently has a bug prior to 10.12 so it can't be created via a nib. commit e18aea724978b86cb0c04853649aeb613ac225ce Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:14:38 2021 -0400 version checks for features not present in 10.11 commit 07dcf30f5225564ec19d1aad3856828fdeaeb0f0 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri May 28 21:30:53 2021 -0400 lower target to 10.11
2021-05-30 22:00:34 +00:00
if (@available(macOS 10.13, *)) {
[task setExecutableURL: url];
[task setCurrentDirectoryURL: MameWorkingDirectory()];
} else {
[task setLaunchPath: MamePath()];
[task setCurrentDirectoryPath: MameWorkingDirectoryPath()];
}
[task setArguments: args];
return [LogWindowController controllerForTask: task close: close];
}
+(id)controllerForArgs: (NSArray *)args {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL close = [defaults boolForKey: kAutoCloseLogWindow];
return [self controllerForArgs: args close: close];
}
- (void)windowDidLoad {
[super windowDidLoad];
[LogWindows addObject: self];
_font = [NSFont userFixedPitchFontOfSize: 0];
}
-(void)appendString: (NSString *)string
{
if ([string length])
{
// needs explicit color attribute for proper dark mode support.
NSDictionary *attr = @{
NSForegroundColorAttributeName: [NSColor textColor],
NSFontAttributeName: _font,
};
NSAttributedString *astr = [[NSAttributedString alloc] initWithString: string attributes: attr];
[[_textView textStorage] appendAttributedString: astr];
}
}
2021-05-31 14:39:24 +00:00
-(void)appendAttributedString: (NSAttributedString *)string {
if ([string length]) {
[[_textView textStorage] appendAttributedString: string];
}
}
-(NSError *)runTask: (NSTask *)task close: (BOOL)close {
if (_task) return nil;
_close = close;
_eof = NO;
NSPipe *pipe = [NSPipe pipe];
// window not yet loaded until [self window] called.
Updates to work with 10.11 (El Capitan) and newer. There are still a couple minor UI degradations eg: mame cheat sheet, recent disk images window, auto complete window. Squashed commit of the following: commit fb115024f805da38b747fd1905573fabb7bff24e Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 18:00:13 2021 -0400 ? commit 8405f4df95ae8f5bc5dc33598dc0b3f8972b32cd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:43 2021 -0400 tintColor not present before 10.14 commit 86a6102cee4f30cac2972259ef55fda3c32b8f9a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:19 2021 -0400 [menu setItemArray:] apparently has problems before 10.14 commit 65227a00743fbf143c623af8a76698d57bdc4573 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:58:15 2021 -0400 NSCache doesn't like null objects. commit 8a4c3a4662647d5ee0b609bde9f3aaa5c0d99776 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 16:00:05 2021 -0400 organize copy bundle resources. multiple-sized png images are converted to a single tiff (with multiple sizes). This is fine BUT EL capitan grabs the first image so if the @3x image is first, it will be used. commit 44b94cdfd47117864b6529214b1c91a0c2e41327 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 15:57:37 2021 -0400 add NewSlotViewController.m to Ample Lite. commit 2fa1b7418783e48ca625ab3490e43cbff9e5619a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:57:19 2021 -0400 Ample Lite was missing reference to New Slot View nib. commit 65f268e77a65f8c57019702d8f0c2b66577c5ee7 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:56:04 2021 -0400 based on testing, 10.11 still has public.file-url, which is kUTTypeFileURL. 10.13 added NSURLPBoardType as a synonym. commit cde123b16eb336b7695b444931b9e9bb9802d772 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:54:02 2021 -0400 set everything to 10.11 commit e7a670efd3298e8ce1409cde1cce559ba46879f4 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 23:33:31 2021 -0400 script to download SDL2.Framework commit 9f38f2972ab0e38fbdaebb3e3f35281978303519 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:15:32 2021 -0400 WKWebView apparently has a bug prior to 10.12 so it can't be created via a nib. commit e18aea724978b86cb0c04853649aeb613ac225ce Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:14:38 2021 -0400 version checks for features not present in 10.11 commit 07dcf30f5225564ec19d1aad3856828fdeaeb0f0 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri May 28 21:30:53 2021 -0400 lower target to 10.11
2021-05-30 22:00:34 +00:00
const char *path = nil;
const char *wd = nil;
[task setStandardError: pipe];
[task setStandardOutput: pipe];
Updates to work with 10.11 (El Capitan) and newer. There are still a couple minor UI degradations eg: mame cheat sheet, recent disk images window, auto complete window. Squashed commit of the following: commit fb115024f805da38b747fd1905573fabb7bff24e Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 18:00:13 2021 -0400 ? commit 8405f4df95ae8f5bc5dc33598dc0b3f8972b32cd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:43 2021 -0400 tintColor not present before 10.14 commit 86a6102cee4f30cac2972259ef55fda3c32b8f9a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:19 2021 -0400 [menu setItemArray:] apparently has problems before 10.14 commit 65227a00743fbf143c623af8a76698d57bdc4573 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:58:15 2021 -0400 NSCache doesn't like null objects. commit 8a4c3a4662647d5ee0b609bde9f3aaa5c0d99776 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 16:00:05 2021 -0400 organize copy bundle resources. multiple-sized png images are converted to a single tiff (with multiple sizes). This is fine BUT EL capitan grabs the first image so if the @3x image is first, it will be used. commit 44b94cdfd47117864b6529214b1c91a0c2e41327 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 15:57:37 2021 -0400 add NewSlotViewController.m to Ample Lite. commit 2fa1b7418783e48ca625ab3490e43cbff9e5619a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:57:19 2021 -0400 Ample Lite was missing reference to New Slot View nib. commit 65f268e77a65f8c57019702d8f0c2b66577c5ee7 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:56:04 2021 -0400 based on testing, 10.11 still has public.file-url, which is kUTTypeFileURL. 10.13 added NSURLPBoardType as a synonym. commit cde123b16eb336b7695b444931b9e9bb9802d772 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:54:02 2021 -0400 set everything to 10.11 commit e7a670efd3298e8ce1409cde1cce559ba46879f4 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 23:33:31 2021 -0400 script to download SDL2.Framework commit 9f38f2972ab0e38fbdaebb3e3f35281978303519 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:15:32 2021 -0400 WKWebView apparently has a bug prior to 10.12 so it can't be created via a nib. commit e18aea724978b86cb0c04853649aeb613ac225ce Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:14:38 2021 -0400 version checks for features not present in 10.11 commit 07dcf30f5225564ec19d1aad3856828fdeaeb0f0 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri May 28 21:30:53 2021 -0400 lower target to 10.11
2021-05-30 22:00:34 +00:00
if (@available(macOS 10.13, *)) {
NSError *error = nil;
path = [[task executableURL] fileSystemRepresentation];
wd = [[task currentDirectoryURL] fileSystemRepresentation];
[task launchAndReturnError: &error];
if (error) {
NSLog(@"NSTask error. Path = %s error = %@", path, error);
2021-05-31 14:39:24 +00:00
return error;
Updates to work with 10.11 (El Capitan) and newer. There are still a couple minor UI degradations eg: mame cheat sheet, recent disk images window, auto complete window. Squashed commit of the following: commit fb115024f805da38b747fd1905573fabb7bff24e Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 18:00:13 2021 -0400 ? commit 8405f4df95ae8f5bc5dc33598dc0b3f8972b32cd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:43 2021 -0400 tintColor not present before 10.14 commit 86a6102cee4f30cac2972259ef55fda3c32b8f9a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:59:19 2021 -0400 [menu setItemArray:] apparently has problems before 10.14 commit 65227a00743fbf143c623af8a76698d57bdc4573 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 17:58:15 2021 -0400 NSCache doesn't like null objects. commit 8a4c3a4662647d5ee0b609bde9f3aaa5c0d99776 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 16:00:05 2021 -0400 organize copy bundle resources. multiple-sized png images are converted to a single tiff (with multiple sizes). This is fine BUT EL capitan grabs the first image so if the @3x image is first, it will be used. commit 44b94cdfd47117864b6529214b1c91a0c2e41327 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 15:57:37 2021 -0400 add NewSlotViewController.m to Ample Lite. commit 2fa1b7418783e48ca625ab3490e43cbff9e5619a Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:57:19 2021 -0400 Ample Lite was missing reference to New Slot View nib. commit 65f268e77a65f8c57019702d8f0c2b66577c5ee7 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:56:04 2021 -0400 based on testing, 10.11 still has public.file-url, which is kUTTypeFileURL. 10.13 added NSURLPBoardType as a synonym. commit cde123b16eb336b7695b444931b9e9bb9802d772 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun May 30 13:54:02 2021 -0400 set everything to 10.11 commit e7a670efd3298e8ce1409cde1cce559ba46879f4 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 23:33:31 2021 -0400 script to download SDL2.Framework commit 9f38f2972ab0e38fbdaebb3e3f35281978303519 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:15:32 2021 -0400 WKWebView apparently has a bug prior to 10.12 so it can't be created via a nib. commit e18aea724978b86cb0c04853649aeb613ac225ce Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sat May 29 22:14:38 2021 -0400 version checks for features not present in 10.11 commit 07dcf30f5225564ec19d1aad3856828fdeaeb0f0 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri May 28 21:30:53 2021 -0400 lower target to 10.11
2021-05-30 22:00:34 +00:00
}
} else {
path = [[task launchPath] fileSystemRepresentation];
wd = [[task currentDirectoryPath] fileSystemRepresentation];
@try {
[task launch];
} @catch (NSException *exception) {
NSLog(@"NSTask exception. Path = %s exception = %@", path, exception);
return nil; // ?
}
}
_task = task;
2020-09-01 03:25:37 +00:00
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];
[nc addObserver: self
selector: @selector(taskComplete:)
name: NSTaskDidTerminateNotification
object: _task];
[nc addObserver: self
selector: @selector(readComplete:)
name: NSFileHandleReadCompletionNotification
object: _handle];
[_handle readInBackgroundAndNotify];
[[self window] setDocumentEdited: YES];
return nil;
}
#pragma mark -
#pragma mark Notifications
-(void)readComplete:(NSNotification *)notification
{
// read complete, queue up another.
NSDictionary *dict = [notification userInfo];
NSData *data = [dict objectForKey: NSFileHandleNotificationDataItem];
if ([data length])
{
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[self appendString: string];
[_handle readInBackgroundAndNotify];
} else {
[self appendString: @"\n"]; // -listmedia sometimes causes display issues.
_eof = YES;
//[_textView setNeedsDisplay: YES]; // -listmedia sometimes weird.
}
}
-(void)taskCompleteHack {
}
/* hask! task complete may occur while output still being processed. add a delay to compensate. */
-(void)taskComplete: (NSNotification *)notification
{
if (!_eof) {
[self performSelector: @selector(taskComplete:) withObject: notification afterDelay: 0.5];
return;
}
BOOL ok = NO;
NSTaskTerminationReason reason;
int status;
NSString *string = nil;
reason = [_task terminationReason];
status = [_task terminationStatus];
if (reason == NSTaskTerminationReasonExit)
{
if (status == 0)
{
//string = @"\n\n[Success]\n\n";
ok = YES;
}
else string = @"\n\n[An error occurred]\n\n";
}
else
{
string = [NSString stringWithFormat: @"\n\n[Caught signal %d (%s)]\n\n", status, strsignal(status)];
}
if (string) {
NSDictionary *attr = @{
NSForegroundColorAttributeName: [NSColor systemRedColor],
NSFontAttributeName: _font,
};
NSAttributedString *astr = [[NSAttributedString alloc] initWithString: string attributes: attr];
[self appendAttributedString: astr];
}
_handle = nil;
_task = nil;
[[self window] setDocumentEdited: NO];
if (ok && _close) {
[[self window] close];
}
}
#pragma mark - NSWindowDelegate
-(void)windowWillClose:(NSNotification *)notification {
[LogWindows removeObject: self];
}
#pragma mark - IBActions
- (IBAction)clearLog:(id)sender {
NSAttributedString *empty = [NSAttributedString new];
[[_textView textStorage] setAttributedString: empty];
}
@end