dreadful abominations for networking to work on watchOS

This commit is contained in:
Jesús A. Álvarez 2024-03-29 20:35:35 +01:00
parent 54f6496031
commit 4684df5fc6
4 changed files with 66 additions and 12 deletions

View File

@ -30,6 +30,15 @@
<key>NSExtensionPointIdentifier</key> <key>NSExtensionPointIdentifier</key>
<string>com.apple.watchkit</string> <string>com.apple.watchkit</string>
</dict> </dict>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
</array>
<key>WKBackgroundModes</key>
<array>
<string>mindfulness</string>
</array>
<key>WKExtensionDelegateClassName</key> <key>WKExtensionDelegateClassName</key>
<string>ExtensionDelegate</string> <string>ExtensionDelegate</string>
</dict> </dict>

View File

@ -10,6 +10,7 @@
@import UIKit; @import UIKit;
@import ObjectiveC.runtime; @import ObjectiveC.runtime;
@import WatchConnectivity; @import WatchConnectivity;
@import AVFAudio;
#import "UIKit+Watch.h" #import "UIKit+Watch.h"
#import "InterfaceController.h" #import "InterfaceController.h"
@ -38,7 +39,7 @@
-(void)setIdleTimerDisabled:(BOOL)disabled; -(void)setIdleTimerDisabled:(BOOL)disabled;
@end @end
@interface InterfaceController () @interface InterfaceController () <WKExtendedRuntimeSessionDelegate>
@end @end
@ -46,7 +47,7 @@ static NSObject<Emulator> *sharedEmulator = nil;
@implementation InterfaceController @implementation InterfaceController
{ {
WKExtendedRuntimeSession *runtimeSession;
} }
+ (void)load { + (void)load {
@ -128,27 +129,66 @@ static NSObject<Emulator> *sharedEmulator = nil;
} }
- (void)loadAndStartEmulator { - (void)loadAndStartEmulator {
#ifdef LTOVRTCP_SERVER
setenv("LTOVRTCP_SERVER", LTOVRTCP_SERVER, 1);
#endif
UIView *fullScreenView = [self fullScreenView]; UIView *fullScreenView = [self fullScreenView];
Class emulatorClass = NSClassFromString(@"MacPlus4MEmulator"); Class emulatorClass = NSClassFromString(@"MacPlus4MEmulator");
sharedEmulator = [emulatorClass new]; sharedEmulator = [emulatorClass new];
sharedEmulator.rootViewController = nil; sharedEmulator.rootViewController = self;
sharedEmulator.showAlert = ^(NSString *title, NSString *message) { sharedEmulator.showAlert = ^(NSString *title, NSString *message) {
[self presentAlertControllerWithTitle:title message:message preferredStyle:WKAlertControllerStyleAlert actions:@[ [self presentAlertControllerWithTitle:title message:message preferredStyle:WKAlertControllerStyleAlert actions:@[
[WKAlertAction actionWithTitle:@"OK" style:WKAlertActionStyleDefault handler:^{}]]]; [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.screenLayer = fullScreenView.layer;
sharedEmulator.speed = sharedEmulator.initialSpeed; sharedEmulator.speed = sharedEmulator.initialSpeed;
[sharedEmulator.screenLayer setContentsGravity:@"CAGravityResizeAspectFill"]; [sharedEmulator.screenLayer setContentsGravity:@"CAGravityResizeAspectFill"];
[sharedEmulator.screenLayer setAffineTransform:CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), 0.375, 0.375)]; [sharedEmulator.screenLayer setAffineTransform:CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), 0.375, 0.375)];
[sharedEmulator.screenLayer setMinificationFilter:@"CAFilterTrilinear"]; [sharedEmulator.screenLayer setMinificationFilter:@"CAFilterTrilinear"];
#if TARGET_OS_SIMULATOR
[sharedEmulator performSelector:@selector(run) withObject:nil afterDelay:0.1]; [sharedEmulator performSelector:@selector(run) withObject:nil afterDelay:0.1];
#endif
id app = [NSClassFromString(@"UIApplication") sharedApplication];
[app setIdleTimerDisabled:YES];
TrackPad *trackpad = [[TrackPad alloc] initWithFrame:fullScreenView.bounds]; TrackPad *trackpad = [[TrackPad alloc] initWithFrame:fullScreenView.bounds];
[fullScreenView addSubview:trackpad]; [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 @end

View File

@ -332,6 +332,8 @@
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference 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 = "<group>"; }; 2834204B2B96558700D7DB68 /* LTOVRUDP.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LTOVRUDP.h; sourceTree = "<group>"; };
283422CA1CF8EF8C0088B634 /* MacPlus4M.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MacPlus4M.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 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 = "<group>"; }; 283422D71CF8EFD80088B634 /* EmulatorProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EmulatorProtocol.h; sourceTree = "<group>"; };
@ -675,6 +677,8 @@
2896738F2BB4947A0014D8E7 /* Frameworks */ = { 2896738F2BB4947A0014D8E7 /* Frameworks */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
2810D4732BB7441600A00C47 /* CoreAudio.framework */,
2810D4722BB7441600A00C47 /* CoreAudioTypes.framework */,
); );
name = Frameworks; name = Frameworks;
sourceTree = "<group>"; sourceTree = "<group>";
@ -2268,7 +2272,7 @@
SUPPORTED_PLATFORMS = "watchsimulator watchos"; SUPPORTED_PLATFORMS = "watchsimulator watchos";
TARGETED_DEVICE_FAMILY = 4; TARGETED_DEVICE_FAMILY = 4;
USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../Mini vMac\""; USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../Mini vMac\"";
WATCHOS_DEPLOYMENT_TARGET = 5.0; WATCHOS_DEPLOYMENT_TARGET = 8.5;
}; };
name = Debug; name = Debug;
}; };
@ -2293,7 +2297,7 @@
SUPPORTED_PLATFORMS = "watchsimulator watchos"; SUPPORTED_PLATFORMS = "watchsimulator watchos";
TARGETED_DEVICE_FAMILY = 4; TARGETED_DEVICE_FAMILY = 4;
USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../Mini vMac\""; USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../Mini vMac\"";
WATCHOS_DEPLOYMENT_TARGET = 5.0; WATCHOS_DEPLOYMENT_TARGET = 8.5;
}; };
name = Release; name = Release;
}; };
@ -2311,7 +2315,7 @@
SKIP_INSTALL = YES; SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "watchsimulator watchos"; SUPPORTED_PLATFORMS = "watchsimulator watchos";
TARGETED_DEVICE_FAMILY = 4; TARGETED_DEVICE_FAMILY = 4;
WATCHOS_DEPLOYMENT_TARGET = 5.0; WATCHOS_DEPLOYMENT_TARGET = 8.5;
}; };
name = Debug; name = Debug;
}; };
@ -2329,7 +2333,7 @@
SKIP_INSTALL = YES; SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "watchsimulator watchos"; SUPPORTED_PLATFORMS = "watchsimulator watchos";
TARGETED_DEVICE_FAMILY = 4; TARGETED_DEVICE_FAMILY = 4;
WATCHOS_DEPLOYMENT_TARGET = 5.0; WATCHOS_DEPLOYMENT_TARGET = 8.5;
}; };
name = Release; name = Release;
}; };

View File

@ -10,7 +10,8 @@
@import CoreGraphics; @import CoreGraphics;
#if TARGET_OS_WATCH #if TARGET_OS_WATCH
#define CALayer NSObject #define CALayer NSObject
#define UIViewController NSObject @class InterfaceController;
#define UIViewController InterfaceController
@interface NSObject (CALayer) @interface NSObject (CALayer)
- (void)setContents:(id)contents; - (void)setContents:(id)contents;
@end @end