-
Notifications
You must be signed in to change notification settings - Fork 53
/
AppDelegate.m
365 lines (323 loc) · 15 KB
/
AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// AppDelegate.m
// Mini vMac
//
// Created by Jesús A. Álvarez on 27/04/2016.
// Copyright © 2016-2018 namedfork. All rights reserved.
//
@import AVFoundation;
#import "AppDelegate.h"
#import "SettingsViewController.h"
#import "InsertDiskViewController.h"
#import "ViewController.h"
static AppDelegate *sharedAppDelegate = nil;
static NSObject<Emulator> *sharedEmulator = nil;
NSString *DocumentsChangedNotification = @"documentsChanged";
@interface AppDelegate () <BTCMouseDelegate>
@end
@implementation AppDelegate
{
}
+ (instancetype)sharedInstance {
return sharedAppDelegate;
}
+ (id<Emulator>)sharedEmulator {
return sharedEmulator;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
sharedAppDelegate = self;
[self initDefaults];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:NULL];
[self loadAndStartEmulator];
if ([application respondsToSelector:@selector(btcMouseSetRawMode:)]) {
[application btcMouseSetRawMode:YES];
[application btcMouseSetDelegate:self];
}
// populate documents directory so it shows up in Files
[[NSFileManager defaultManager] createDirectoryAtPath:self.userKeyboardLayoutsPath withIntermediateDirectories:YES attributes:nil error:nil];
return YES;
}
- (void)initDefaults {
// default settings
NSDictionary *layoutForLanguage = @{@"en": @"British.nfkeyboardlayout",
@"es": @"Spanish.nfkeyboardlayout",
@"en-US": @"US.nfkeyboardlayout"};
NSString *firstLanguage = [NSBundle preferredLocalizationsFromArray:layoutForLanguage.allKeys].firstObject;
NSDictionary *defaultValues = @{@"trackpad": @([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad),
@"keyboardLayout": layoutForLanguage[firstLanguage],
@"machine": @"MacPlus4M",
@"speedValue": @(sharedEmulator.initialSpeed),
@"runInBackground": @NO,
@"autoSlow": @(sharedEmulator.initialAutoSlow),
@"screenFilter": kCAFilterLinear,
@"autoShowGestureHelp": @YES,
@"recentDisks": @[]
};
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:defaultValues];
[defaults addObserver:self forKeyPath:@"speedValue" options:0 context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if (object == [NSUserDefaults standardUserDefaults]) {
NSUserDefaults *defaults = object;
if ([keyPath isEqualToString:@"speedValue"]) {
sharedEmulator.speed = [defaults integerForKey:@"speedValue"];
} else if ([keyPath isEqualToString:@"autoSlow"]) {
sharedEmulator.autoSlow = [defaults integerForKey:@"autoSlow"];
}
}
}
- (NSString*)emulatorBundlesPath {
return [NSBundle mainBundle].privateFrameworksPath;
}
- (NSArray<NSBundle*>*)emulatorBundles {
NSArray<NSString*> *names = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.emulatorBundlesPath error:NULL];
NSMutableArray *emulatorBundles = [NSMutableArray arrayWithCapacity:names.count];
for (NSString *name in [names pathsMatchingExtensions:@[@"mnvm"]]) {
NSBundle *bundle = [NSBundle bundleWithPath:[self.emulatorBundlesPath stringByAppendingPathComponent:name]];
[emulatorBundles addObject:bundle];
}
return emulatorBundles;
}
- (BOOL)loadEmulator:(NSString*)name {
NSString *emulatorBundleName = [name stringByAppendingPathExtension:@"mnvm"];
NSString *emulatorBundlePath = [self.emulatorBundlesPath stringByAppendingPathComponent:emulatorBundleName];
NSBundle *emulatorBundle = [NSBundle bundleWithPath:emulatorBundlePath];
[emulatorBundle load];
sharedEmulator = [[emulatorBundle principalClass] new];
sharedEmulator.rootViewController = self.window.rootViewController;
sharedEmulator.showAlert = ^(NSString *title, NSString *message) {
[self showAlertWithTitle:title message:message];
};
sharedEmulator.dataPath = self.documentsPath;
#if defined(TARGET_OS_VISION) && TARGET_OS_VISION == 1
[ViewController adjustToScreenSize];
#endif
return sharedEmulator != nil;
}
- (void)loadAndStartEmulator {
[self willChangeValueForKey:@"sharedEmulator"];
if (sharedEmulator) {
NSBundle *bundle = sharedEmulator.bundle;
id<Emulator> oldEmulator = sharedEmulator;
sharedEmulator = nil;
[oldEmulator shutdown];
[bundle unload];
}
if (![self loadEmulator:[[NSUserDefaults standardUserDefaults] stringForKey:@"machine"]]) {
[self loadEmulator:@"MacPlus4M"];
}
[self didChangeValueForKey:@"sharedEmulator"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults integerForKey:@"speedValue"] > sharedEmulator.initialSpeed) {
[defaults setValue:@(sharedEmulator.initialSpeed) forKey:@"speedValue"];
} else {
sharedEmulator.speed = [defaults integerForKey:@"speedValue"];
}
[sharedEmulator performSelector:@selector(run) withObject:nil afterDelay:0.1];
}
- (id<Emulator>)sharedEmulator {
return sharedEmulator;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
if ([defaults boolForKey:@"runInBackground"]) {
// slow down to 1x when in background
sharedEmulator.speed = EmulatorSpeed1x;
} else {
sharedEmulator.running = NO;
}
if (sharedEmulator.anyDiskInserted == NO) {
exit(0);
}
}
- (void)handleEventWithMove:(CGPoint)move andWheel:(float)wheel andPan:(float)pan andButtons:(int)buttons {
[sharedEmulator moveMouseX:move.x/2.0 Y:move.y/2.0];
[sharedEmulator setMouseButton:buttons == 1];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (sharedEmulator.running) {
sharedEmulator.speed = [defaults integerForKey:@"speedValue"];
} else {
sharedEmulator.running = YES;
}
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlertWithTitle:title message:message];
});
return;
}
BOOL wasRunning = sharedEmulator.isRunning;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[sharedEmulator setRunning:wasRunning];
}]];
UIViewController *controller = self.window.rootViewController;
while (controller.presentedViewController) {
controller = controller.presentedViewController;
}
[controller presentViewController:alert animated:YES completion:nil];
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
BOOL success = NO;
if ([shortcutItem.type isEqualToString:@"disk"]) {
NSString *fileName = (NSString*)shortcutItem.userInfo[@"disk"];
NSString *filePath = [self.documentsPath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] && ![sharedEmulator isDiskInserted:filePath]) {
success = YES;
[sharedEmulator performSelector:@selector(insertDisk:) withObject:filePath afterDelay:1.0];
}
}
completionHandler(success);
}
#pragma mark - Settings / Insert Disk / Help
- (void)showSettings:(id)sender {
[self.window.rootViewController performSelector:@selector(showSettings:) withObject:sender];
}
- (void)showInsertDisk:(id)sender {
[self.window.rootViewController performSelector:@selector(showInsertDisk:) withObject:sender];
}
- (void)showGestureHelp:(id)sender {
[self.window.rootViewController performSelector:@selector(showGestureHelp:) withObject:sender];
}
#pragma mark - Files
- (BOOL)isSandboxed {
#if TARGET_IPHONE_SIMULATOR
return YES;
#else
static dispatch_once_t onceToken;
static BOOL sandboxed;
dispatch_once(&onceToken, ^{
// not sandboxed if parent of documents directory is "mobile"
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject.stringByStandardizingPath;
sandboxed = ![documentsPath.stringByDeletingLastPathComponent.lastPathComponent isEqualToString:@"mobile"];
});
return sandboxed;
#endif
}
- (NSArray<NSString *> *)diskImageExtensions {
return @[@"dsk", @"img", @"dc42", @"diskcopy42", @"image"];
}
- (NSString *)documentsPath {
static dispatch_once_t onceToken;
static NSString *documentsPath;
dispatch_once(&onceToken, ^{
documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject.stringByStandardizingPath;
if (!self.sandboxed) {
documentsPath = [documentsPath stringByAppendingPathComponent:@"Mini vMac"].stringByStandardizingPath;
}
[[NSFileManager defaultManager] createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:NULL];
});
return documentsPath;
}
- (NSString *)userKeyboardLayoutsPath {
static dispatch_once_t onceToken;
static NSString *userKeyboardLayoutsPath;
dispatch_once(&onceToken, ^{
userKeyboardLayoutsPath = [self.documentsPath stringByAppendingPathComponent:@"Keyboard Layouts"];
});
return userKeyboardLayoutsPath;
}
- (NSArray<NSString *> *)keyboardLayoutPaths {
NSArray *keyboardLayouts = [[NSBundle mainBundle] pathsForResourcesOfType:@"nfkeyboardlayout" inDirectory:@"Keyboard Layouts"];
NSString *userKeyboardLayoutsPath = [AppDelegate sharedInstance].userKeyboardLayoutsPath;
NSArray *userKeyboardLayouts = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:userKeyboardLayoutsPath error:nil] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension.lowercaseString = %@", @"nfkeyboardlayout"]];
if (userKeyboardLayouts.count > 0) {
keyboardLayouts = [keyboardLayouts arrayByAddingObjectsFromArray:userKeyboardLayouts];
}
return keyboardLayouts;
}
- (BOOL)importFileToDocuments:(NSURL *)url copy:(BOOL)copy {
if (url.fileURL) {
// opening file
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fileName = url.path.lastPathComponent;
NSString *destinationPath = [self.documentsPath stringByAppendingPathComponent:fileName];
NSError *error = NULL;
NSInteger tries = 1;
while ([fileManager fileExistsAtPath:destinationPath]) {
NSString *newFileName;
if (fileName.pathExtension.length > 0) {
newFileName = [NSString stringWithFormat:@"%@ %d.%@", fileName.stringByDeletingPathExtension, (int)tries, fileName.pathExtension];
} else {
newFileName = [NSString stringWithFormat:@"%@ %d", fileName, (int)tries];
}
destinationPath = [self.documentsPath stringByAppendingPathComponent:newFileName];
tries++;
}
if (copy) {
[fileManager copyItemAtPath:url.path toPath:destinationPath error:&error];
} else {
[fileManager moveItemAtPath:url.path toPath:destinationPath error:&error];
}
if (error) {
[self showAlertWithTitle:fileName message:error.localizedFailureReason];
} else {
NSDictionary *userInfo = @{@"path": destinationPath};
[[NSNotificationCenter defaultCenter] postNotificationName:DocumentsChangedNotification object:self userInfo:userInfo];
[self showAlertWithTitle:@"File Import" message:[NSString stringWithFormat:@"%@ imported to Documents", destinationPath.lastPathComponent]];
}
}
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
if (url.fileURL) {
// opening file
NSString *inboxPath = [self.documentsPath stringByAppendingPathComponent:@"Inbox"];
if ([url.path.stringByStandardizingPath hasPrefix:inboxPath]) {
// pre-iOS 11 import through inbox
[url startAccessingSecurityScopedResource];
[self importFileToDocuments:url copy:NO];
[url stopAccessingSecurityScopedResource];
} else if ([url.path.stringByStandardizingPath hasPrefix:self.documentsPath]) {
// already in documents - mount
[sharedEmulator insertDisk:url.path];
} else if ([options[UIApplicationOpenURLOptionsOpenInPlaceKey] boolValue]) {
// not in documents - copy
[url startAccessingSecurityScopedResource];
[self importFileToDocuments:url copy:YES];
[url stopAccessingSecurityScopedResource];
} else {
return [self importFileToDocuments:url copy:NO];
}
}
return YES;
}
#pragma mark - Making a Scene
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
for (NSUserActivity *activity in options.userActivities) {
if ([activity.activityType isEqualToString:@"net.namedfork.keyboard"]) {
return [UISceneConfiguration configurationWithName:@"Keyboard" sessionRole:UIWindowSceneSessionRoleApplication];
}
}
if ([self sceneWithName:@"Default"] == nil) {
[[AppDelegate sharedEmulator] setRunning:YES];
return [UISceneConfiguration configurationWithName:@"Default" sessionRole:UIWindowSceneSessionRoleApplication];
}
return nil;
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
if ([self sceneWithName:@"Default"] == nil) {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"runInBackground"] == NO) {
[[AppDelegate sharedEmulator] setRunning:NO];
}
UIScene *keyboardScene = [self sceneWithName:@"Keyboard"];
if (keyboardScene != nil) {
// if only keyboard is left, close it too
[application requestSceneSessionDestruction:keyboardScene.session options:nil errorHandler:nil];
}
}
}
- (UIScene*)sceneWithName:(NSString*)name {
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
if ([scene.session.configuration.name isEqualToString:name]) {
return scene;
}
}
return nil;
}
@end