2024-03-25 19:27:26 +00:00
|
|
|
//
|
|
|
|
// InterfaceController.m
|
|
|
|
// Mini vMac WatchKit Extension
|
|
|
|
//
|
|
|
|
// Created by Jesús A. Álvarez on 14/10/2018.
|
|
|
|
// Copyright © 2018 namedfork. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-03-27 17:10:05 +00:00
|
|
|
@import Foundation;
|
|
|
|
@import UIKit;
|
2024-03-25 19:27:26 +00:00
|
|
|
@import ObjectiveC.runtime;
|
|
|
|
@import WatchConnectivity;
|
2024-03-29 19:35:35 +00:00
|
|
|
@import AVFAudio;
|
2024-03-25 19:27:26 +00:00
|
|
|
|
2024-03-27 19:25:15 +00:00
|
|
|
#import "UIKit+Watch.h"
|
2024-03-27 17:10:05 +00:00
|
|
|
#import "InterfaceController.h"
|
|
|
|
#import "EmulatorProtocol.h"
|
2024-03-27 19:25:15 +00:00
|
|
|
#import "TrackPad.h"
|
2024-03-27 17:10:05 +00:00
|
|
|
|
2024-03-25 19:27:26 +00:00
|
|
|
@interface NSObject (fs_override)
|
|
|
|
+(id)sharedApplication;
|
|
|
|
-(id)keyWindow;
|
|
|
|
-(id)rootViewController;
|
|
|
|
-(NSArray *)viewControllers;
|
|
|
|
-(id)view;
|
|
|
|
-(NSArray *)subviews;
|
|
|
|
-(id)timeLabel;
|
|
|
|
-(id)layer;
|
|
|
|
-(void)addSubview:(id)subview;
|
|
|
|
-(CGPoint)center;
|
|
|
|
-(NSString*)timeText;
|
|
|
|
-(id)sharedPUICApplication;
|
|
|
|
-(void)_setStatusBarTimeHidden:(BOOL)hidden animated:(BOOL)animated completion:(void (^)(void))completion;
|
2024-03-27 17:10:05 +00:00
|
|
|
-(void)setAffineTransform:(CGAffineTransform)transform;
|
|
|
|
-(void)setContentsScale:(CGFloat)value;
|
|
|
|
-(void)setContentsGravity:(NSString*)gravity;
|
|
|
|
-(void)setMinificationFilter:(NSString*)filter;
|
2024-03-27 19:25:15 +00:00
|
|
|
-(void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(NSUInteger)controlEvents;
|
|
|
|
-(void)setIdleTimerDisabled:(BOOL)disabled;
|
2024-03-30 11:28:30 +00:00
|
|
|
-(BOOL)prefersStatusBarHidden;
|
2024-03-25 19:27:26 +00:00
|
|
|
@end
|
|
|
|
|
2024-03-29 19:35:35 +00:00
|
|
|
@interface InterfaceController () <WKExtendedRuntimeSessionDelegate>
|
2024-03-25 19:27:26 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2024-03-27 19:25:15 +00:00
|
|
|
static NSObject<Emulator> *sharedEmulator = nil;
|
|
|
|
|
2024-03-25 19:27:26 +00:00
|
|
|
@implementation InterfaceController
|
|
|
|
{
|
2024-03-29 19:35:35 +00:00
|
|
|
WKExtendedRuntimeSession *runtimeSession;
|
2024-03-25 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)load {
|
|
|
|
/* Hack to make the digital time overlay disappear (on watchOS 6) */
|
|
|
|
Class CLKTimeFormatter = NSClassFromString(@"CLKTimeFormatter");
|
|
|
|
if ([CLKTimeFormatter instancesRespondToSelector:@selector(timeText)]) {
|
|
|
|
Method m = class_getInstanceMethod(CLKTimeFormatter, @selector(timeText));
|
|
|
|
method_setImplementation(m, imp_implementationWithBlock(^NSString*(id self, SEL _cmd) { return @" "; }));
|
|
|
|
}
|
2024-03-30 11:28:30 +00:00
|
|
|
/* hide status bar on watchOS 10 */
|
|
|
|
Class clsUIViewController = NSClassFromString(@"UIViewController");
|
|
|
|
if ([clsUIViewController instancesRespondToSelector:@selector(prefersStatusBarHidden)]) {
|
|
|
|
Method m = class_getInstanceMethod(clsUIViewController, @selector(prefersStatusBarHidden));
|
|
|
|
method_setImplementation(m, imp_implementationWithBlock(^BOOL(id self, SEL _cmd) { return YES; }));
|
|
|
|
}
|
2024-03-25 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 19:25:15 +00:00
|
|
|
+ (id<Emulator>)sharedEmulator {
|
|
|
|
return sharedEmulator;
|
|
|
|
}
|
|
|
|
|
2024-03-25 19:27:26 +00:00
|
|
|
- (void)awakeWithContext:(id)context {
|
|
|
|
[super awakeWithContext:context];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)hideTimeLabel {
|
|
|
|
/* Hack to make the digital time overlay disappear (on watchOS 5) */
|
|
|
|
id fullScreenView = [self fullScreenView];
|
|
|
|
if ([fullScreenView respondsToSelector:@selector(timeLabel)]) {
|
|
|
|
[[[fullScreenView timeLabel] layer] setOpacity:0];
|
|
|
|
}
|
|
|
|
|
2024-03-27 17:10:05 +00:00
|
|
|
/* Hack to make the digital time overlay disappear (on watchOS 7 and 8) */
|
2024-03-25 19:27:26 +00:00
|
|
|
Class PUICApplication = NSClassFromString(@"PUICApplication");
|
|
|
|
if ([PUICApplication instancesRespondToSelector:@selector(_setStatusBarTimeHidden:animated:completion:)]) {
|
|
|
|
[[PUICApplication sharedApplication] _setStatusBarTimeHidden:YES animated:NO completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-27 19:25:15 +00:00
|
|
|
- (UIView*)fullScreenView {
|
2024-03-25 19:27:26 +00:00
|
|
|
id parentView = [[[[[[NSClassFromString(@"UIApplication") sharedApplication] keyWindow] rootViewController] viewControllers] firstObject] view];
|
|
|
|
id view = [self findDescendantViewOfClass:NSClassFromString(@"SPFullScreenView") inView:parentView]; // watchOS 5
|
|
|
|
if (view == nil) {
|
|
|
|
view = [self findDescendantViewOfClass:NSClassFromString(@"SPInterfaceRemoteView") inView:parentView]; // watchOS 6
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)findDescendantViewOfClass:(Class)viewClass inView:(id)parentView {
|
|
|
|
for (NSObject *view in [parentView subviews]) {
|
|
|
|
if ([view isKindOfClass:viewClass]) {
|
|
|
|
return view;
|
|
|
|
} else {
|
|
|
|
id foundView = [self findDescendantViewOfClass:viewClass inView:view];
|
|
|
|
if (foundView != nil) return foundView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didAppear {
|
|
|
|
[self hideTimeLabel];
|
2024-03-27 19:25:15 +00:00
|
|
|
if (sharedEmulator == nil) {
|
2024-03-27 17:10:05 +00:00
|
|
|
[self loadAndStartEmulator];
|
2024-03-27 19:25:15 +00:00
|
|
|
} else {
|
|
|
|
sharedEmulator.running = YES;
|
2024-03-27 17:10:05 +00:00
|
|
|
}
|
2024-03-25 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)willActivate {
|
|
|
|
if ([WKExtension sharedExtension].applicationState == WKApplicationStateActive) {
|
2024-03-27 19:25:15 +00:00
|
|
|
sharedEmulator.running = YES;
|
2024-03-25 19:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didDeactivate {
|
|
|
|
// This method is called when watch view controller is no longer visible
|
|
|
|
[super didDeactivate];
|
2024-03-27 19:25:15 +00:00
|
|
|
sharedEmulator.running = NO;
|
2024-03-25 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sessionReachabilityDidChange:(WCSession *)session {
|
2024-03-27 17:10:05 +00:00
|
|
|
//uint32_t connected = session.activationState == WCSessionActivationStateActivated && session.reachable;
|
2024-03-25 19:27:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 17:10:05 +00:00
|
|
|
- (void)loadAndStartEmulator {
|
2024-03-29 19:35:35 +00:00
|
|
|
#ifdef LTOVRTCP_SERVER
|
|
|
|
setenv("LTOVRTCP_SERVER", LTOVRTCP_SERVER, 1);
|
|
|
|
#endif
|
2024-03-27 19:25:15 +00:00
|
|
|
UIView *fullScreenView = [self fullScreenView];
|
2024-03-27 17:10:05 +00:00
|
|
|
Class emulatorClass = NSClassFromString(@"MacPlus4MEmulator");
|
2024-03-27 19:25:15 +00:00
|
|
|
sharedEmulator = [emulatorClass new];
|
2024-03-29 19:35:35 +00:00
|
|
|
sharedEmulator.rootViewController = self;
|
2024-03-27 19:25:15 +00:00
|
|
|
sharedEmulator.showAlert = ^(NSString *title, NSString *message) {
|
2024-03-28 17:51:49 +00:00
|
|
|
[self presentAlertControllerWithTitle:title message:message preferredStyle:WKAlertControllerStyleAlert actions:@[
|
|
|
|
[WKAlertAction actionWithTitle:@"OK" style:WKAlertActionStyleDefault handler:^{}]]];
|
2024-03-27 17:10:05 +00:00
|
|
|
};
|
2024-03-29 19:35:35 +00:00
|
|
|
|
|
|
|
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;
|
2024-03-27 19:25:15 +00:00
|
|
|
sharedEmulator.screenLayer = fullScreenView.layer;
|
|
|
|
sharedEmulator.speed = sharedEmulator.initialSpeed;
|
|
|
|
[sharedEmulator.screenLayer setContentsGravity:@"CAGravityResizeAspectFill"];
|
2024-03-30 12:15:52 +00:00
|
|
|
CGFloat scale = [self bestScaleForScreen];
|
|
|
|
[sharedEmulator.screenLayer setAffineTransform:CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), scale, scale)];
|
2024-03-27 19:25:15 +00:00
|
|
|
[sharedEmulator.screenLayer setMinificationFilter:@"CAFilterTrilinear"];
|
2024-03-29 19:35:35 +00:00
|
|
|
#if TARGET_OS_SIMULATOR
|
2024-03-27 19:25:15 +00:00
|
|
|
[sharedEmulator performSelector:@selector(run) withObject:nil afterDelay:0.1];
|
2024-03-29 19:35:35 +00:00
|
|
|
#endif
|
2024-03-27 19:25:15 +00:00
|
|
|
|
|
|
|
TrackPad *trackpad = [[TrackPad alloc] initWithFrame:fullScreenView.bounds];
|
|
|
|
[fullScreenView addSubview:trackpad];
|
2024-03-29 19:35:35 +00:00
|
|
|
|
|
|
|
runtimeSession = [WKExtendedRuntimeSession new];
|
|
|
|
runtimeSession.delegate = self;
|
|
|
|
[runtimeSession start];
|
|
|
|
}
|
|
|
|
|
2024-03-30 12:15:52 +00:00
|
|
|
- (CGFloat)bestScaleForScreen {
|
|
|
|
CGSize screenSize = [UIScreen mainScreen].bounds.size;
|
|
|
|
NSInteger screenWidthAndHeight = (NSInteger)(screenSize.width) * 1000 + (NSInteger)(screenSize.height);
|
|
|
|
// manually selected scales to account for non-square screens
|
|
|
|
switch (screenWidthAndHeight) {
|
|
|
|
// 38 40 41 42 44 45 49
|
|
|
|
case 136170: // 38mm
|
|
|
|
return 0.33;
|
|
|
|
case 176215: // 41mm
|
|
|
|
return 0.40;
|
|
|
|
case 184224: // 44mm
|
|
|
|
return 0.42;
|
|
|
|
case 198242: // 45mm
|
|
|
|
case 205251: // 49mm
|
|
|
|
return 0.455;
|
|
|
|
case 162197: // 40mm
|
|
|
|
case 156195: // 42mm
|
|
|
|
default:
|
|
|
|
return 0.375;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 19:35:35 +00:00
|
|
|
- (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");
|
2024-03-27 17:10:05 +00:00
|
|
|
}
|
2024-03-27 19:25:15 +00:00
|
|
|
|
2024-03-25 19:27:26 +00:00
|
|
|
@end
|