From 4684df5fc6f7c98bfe45857f34285aeaf9b31794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesu=CC=81s=20A=2E=20A=CC=81lvarez?= Date: Fri, 29 Mar 2024 20:35:35 +0100 Subject: [PATCH] dreadful abominations for networking to work on watchOS --- Mini vMac WatchKit Extension/Info.plist | 9 ++++ .../InterfaceController.m | 54 ++++++++++++++++--- Mini vMac.xcodeproj/project.pbxproj | 12 +++-- Mini vMac/EmulatorProtocol.h | 3 +- 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/Mini vMac WatchKit Extension/Info.plist b/Mini vMac WatchKit Extension/Info.plist index d8fef85..1f61da2 100644 --- a/Mini vMac WatchKit Extension/Info.plist +++ b/Mini vMac WatchKit Extension/Info.plist @@ -30,6 +30,15 @@ NSExtensionPointIdentifier com.apple.watchkit + UIBackgroundModes + + audio + fetch + + WKBackgroundModes + + mindfulness + WKExtensionDelegateClassName ExtensionDelegate diff --git a/Mini vMac WatchKit Extension/InterfaceController.m b/Mini vMac WatchKit Extension/InterfaceController.m index a44b89d..7622d7d 100644 --- a/Mini vMac WatchKit Extension/InterfaceController.m +++ b/Mini vMac WatchKit Extension/InterfaceController.m @@ -10,6 +10,7 @@ @import UIKit; @import ObjectiveC.runtime; @import WatchConnectivity; +@import AVFAudio; #import "UIKit+Watch.h" #import "InterfaceController.h" @@ -38,7 +39,7 @@ -(void)setIdleTimerDisabled:(BOOL)disabled; @end -@interface InterfaceController () +@interface InterfaceController () @end @@ -46,7 +47,7 @@ static NSObject *sharedEmulator = nil; @implementation InterfaceController { - + WKExtendedRuntimeSession *runtimeSession; } + (void)load { @@ -128,27 +129,66 @@ static NSObject *sharedEmulator = nil; } - (void)loadAndStartEmulator { +#ifdef LTOVRTCP_SERVER + setenv("LTOVRTCP_SERVER", LTOVRTCP_SERVER, 1); +#endif UIView *fullScreenView = [self fullScreenView]; Class emulatorClass = NSClassFromString(@"MacPlus4MEmulator"); sharedEmulator = [emulatorClass new]; - sharedEmulator.rootViewController = nil; + sharedEmulator.rootViewController = self; sharedEmulator.showAlert = ^(NSString *title, NSString *message) { [self presentAlertControllerWithTitle:title message:message preferredStyle:WKAlertControllerStyleAlert actions:@[ [WKAlertAction actionWithTitle:@"OK" style:WKAlertActionStyleDefault handler:^{}]]]; }; - sharedEmulator.dataPath = [NSBundle mainBundle].resourcePath; + + NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject.stringByStandardizingPath; + NSString *resourcePath = [NSBundle mainBundle].resourcePath; + NSFileManager *fm = [NSFileManager defaultManager]; + for (NSString *fileName in @[@"vMac.rom", @"disk1.dsk"]) { + NSString *srcPath = [resourcePath stringByAppendingPathComponent:fileName]; + NSString *dstPath = [documentsPath stringByAppendingPathComponent:fileName]; + if ([fm fileExistsAtPath:srcPath] && ![fm fileExistsAtPath:dstPath]) { + [fm copyItemAtPath:srcPath toPath:dstPath error:NULL]; + } + } + sharedEmulator.dataPath = documentsPath; sharedEmulator.screenLayer = fullScreenView.layer; sharedEmulator.speed = sharedEmulator.initialSpeed; [sharedEmulator.screenLayer setContentsGravity:@"CAGravityResizeAspectFill"]; [sharedEmulator.screenLayer setAffineTransform:CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), 0.375, 0.375)]; [sharedEmulator.screenLayer setMinificationFilter:@"CAFilterTrilinear"]; +#if TARGET_OS_SIMULATOR [sharedEmulator performSelector:@selector(run) withObject:nil afterDelay:0.1]; - - id app = [NSClassFromString(@"UIApplication") sharedApplication]; - [app setIdleTimerDisabled:YES]; +#endif TrackPad *trackpad = [[TrackPad alloc] initWithFrame:fullScreenView.bounds]; [fullScreenView addSubview:trackpad]; + + runtimeSession = [WKExtendedRuntimeSession new]; + runtimeSession.delegate = self; + [runtimeSession start]; +} + +- (void)extendedRuntimeSessionDidStart:(WKExtendedRuntimeSession *)extendedRuntimeSession { +#if TARGET_OS_SIMULATOR == 0 + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback + mode:AVAudioSessionModeDefault + routeSharingPolicy:AVAudioSessionRouteSharingPolicyLongFormAudio + options:0 + error:NULL]; + [[AVAudioSession sharedInstance] activateWithOptions:0 completionHandler:^(BOOL activated, NSError * _Nullable error) { + // network only works on watchOS when there's an active audio session + [sharedEmulator performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:NO]; + }]; +#endif +} + +- (void)extendedRuntimeSession:(WKExtendedRuntimeSession *)extendedRuntimeSession didInvalidateWithReason:(WKExtendedRuntimeSessionInvalidationReason)reason error:(NSError *)error { + NSLog(@"Runtime session invalidated: %@", error); +} + +- (void)extendedRuntimeSessionWillExpire:(WKExtendedRuntimeSession *)extendedRuntimeSession { + NSLog(@"Extended runtime session will expire"); } @end diff --git a/Mini vMac.xcodeproj/project.pbxproj b/Mini vMac.xcodeproj/project.pbxproj index 527ffbf..b1c4fb4 100644 --- a/Mini vMac.xcodeproj/project.pbxproj +++ b/Mini vMac.xcodeproj/project.pbxproj @@ -332,6 +332,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 2810D4722BB7441600A00C47 /* CoreAudioTypes.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreAudioTypes.framework; path = Platforms/WatchOS.platform/Developer/SDKs/WatchOS10.4.sdk/System/Library/Frameworks/CoreAudioTypes.framework; sourceTree = DEVELOPER_DIR; }; + 2810D4732BB7441600A00C47 /* CoreAudio.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreAudio.framework; path = Platforms/WatchOS.platform/Developer/SDKs/WatchOS10.4.sdk/System/Library/Frameworks/CoreAudio.framework; sourceTree = DEVELOPER_DIR; }; 2834204B2B96558700D7DB68 /* LTOVRUDP.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LTOVRUDP.h; sourceTree = ""; }; 283422CA1CF8EF8C0088B634 /* MacPlus4M.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MacPlus4M.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 283422D71CF8EFD80088B634 /* EmulatorProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EmulatorProtocol.h; sourceTree = ""; }; @@ -675,6 +677,8 @@ 2896738F2BB4947A0014D8E7 /* Frameworks */ = { isa = PBXGroup; children = ( + 2810D4732BB7441600A00C47 /* CoreAudio.framework */, + 2810D4722BB7441600A00C47 /* CoreAudioTypes.framework */, ); name = Frameworks; sourceTree = ""; @@ -2268,7 +2272,7 @@ SUPPORTED_PLATFORMS = "watchsimulator watchos"; TARGETED_DEVICE_FAMILY = 4; USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../Mini vMac\""; - WATCHOS_DEPLOYMENT_TARGET = 5.0; + WATCHOS_DEPLOYMENT_TARGET = 8.5; }; name = Debug; }; @@ -2293,7 +2297,7 @@ SUPPORTED_PLATFORMS = "watchsimulator watchos"; TARGETED_DEVICE_FAMILY = 4; USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../Mini vMac\""; - WATCHOS_DEPLOYMENT_TARGET = 5.0; + WATCHOS_DEPLOYMENT_TARGET = 8.5; }; name = Release; }; @@ -2311,7 +2315,7 @@ SKIP_INSTALL = YES; SUPPORTED_PLATFORMS = "watchsimulator watchos"; TARGETED_DEVICE_FAMILY = 4; - WATCHOS_DEPLOYMENT_TARGET = 5.0; + WATCHOS_DEPLOYMENT_TARGET = 8.5; }; name = Debug; }; @@ -2329,7 +2333,7 @@ SKIP_INSTALL = YES; SUPPORTED_PLATFORMS = "watchsimulator watchos"; TARGETED_DEVICE_FAMILY = 4; - WATCHOS_DEPLOYMENT_TARGET = 5.0; + WATCHOS_DEPLOYMENT_TARGET = 8.5; }; name = Release; }; diff --git a/Mini vMac/EmulatorProtocol.h b/Mini vMac/EmulatorProtocol.h index 0d8a4d0..4541afd 100644 --- a/Mini vMac/EmulatorProtocol.h +++ b/Mini vMac/EmulatorProtocol.h @@ -10,7 +10,8 @@ @import CoreGraphics; #if TARGET_OS_WATCH #define CALayer NSObject -#define UIViewController NSObject +@class InterfaceController; +#define UIViewController InterfaceController @interface NSObject (CALayer) - (void)setContents:(id)contents; @end