summaryrefslogtreecommitdiff
path: root/src/ios.m
blob: c9e843e4682027fb55554ed235fb8f5171e2739a (plain)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/* Copyright 2021 Jaakko Keränen <jaakko.keranen@iki.fi>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#include "ios.h"
#include "app.h"
#include "audio/player.h"
#include "ui/command.h"
#include "ui/window.h"

#include <the_Foundation/file.h>
#include <the_Foundation/fileinfo.h>
#include <the_Foundation/path.h>
#include <SDL_events.h>
#include <SDL_syswm.h>
#include <SDL_timer.h>

#import <AVFAudio/AVFAudio.h>
#import <CoreHaptics/CoreHaptics.h>
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

static iBool isSystemDarkMode_ = iFalse;
static iBool isPhone_          = iFalse;

static UIWindow *uiWindow_(iWindow *window) {
    SDL_SysWMinfo wm;
    SDL_VERSION(&wm.version);
    if (SDL_GetWindowWMInfo(window->win, &wm)) {
        return wm.info.uikit.window;
    }
    iAssert(false);
    return NULL;
}

static UIViewController *viewController_(iWindow *window) {
    UIWindow *uiWin = uiWindow_(window);
    if (uiWin) {
        return uiWin.rootViewController;
    }
    iAssert(false);
    return NULL;
}

static void notifyChange_SystemTextInput_(iSystemTextInput *);
static BOOL isNewlineAllowed_SystemTextInput_(const iSystemTextInput *);

/*----------------------------------------------------------------------------------------------*/

API_AVAILABLE(ios(13.0))
@interface HapticState : NSObject
@property (nonatomic, strong) CHHapticEngine *engine;
@property (nonatomic, strong) NSDictionary   *tapDef;
@property (nonatomic, strong) NSDictionary   *gentleTapDef;
@end

@implementation HapticState

-(void)setup {
    NSError *error;
    self.engine = [[CHHapticEngine alloc] initAndReturnError:&error];
    __weak HapticState *hs = self;
    [self.engine setResetHandler:^{
        NSLog(@"Haptic engine reset");
        NSError *startupError;
        [hs.engine startAndReturnError:&startupError];
        if (startupError) {
            NSLog(@"Engine couldn't restart");
        }
        else {
            // TODO: Create pattern players.
        }
    }];
    [self.engine setStoppedHandler:^(CHHapticEngineStoppedReason reason){
        NSLog(@"Haptic engine stopped");
        switch (reason) {
            case CHHapticEngineStoppedReasonAudioSessionInterrupt:
                break;
            case CHHapticEngineStoppedReasonApplicationSuspended:
                break;
            case CHHapticEngineStoppedReasonIdleTimeout:
                break;
            case CHHapticEngineStoppedReasonSystemError:
                break;
            default:
                break;
        }
    }];
    self.tapDef = @{
        CHHapticPatternKeyPattern:
            @[
                @{
                    CHHapticPatternKeyEvent: @{
                        CHHapticPatternKeyEventType:    CHHapticEventTypeHapticTransient,
                        CHHapticPatternKeyTime:         @0.0,
                        CHHapticPatternKeyEventDuration:@0.1,
                        CHHapticPatternKeyEventParameters: @[
                            @{
                                CHHapticPatternKeyParameterID: CHHapticEventParameterIDHapticIntensity,
                                CHHapticPatternKeyParameterValue: @1.0
                            }
                        ]
                    },
                },
            ]
    };
    self.gentleTapDef = @{
        CHHapticPatternKeyPattern:
            @[
                @{
                    CHHapticPatternKeyEvent: @{
                        CHHapticPatternKeyEventType:    CHHapticEventTypeHapticTransient,
                        CHHapticPatternKeyTime:         @0.0,
                        CHHapticPatternKeyEventDuration:@0.1,
                        CHHapticPatternKeyEventParameters: @[
                            @{
                                CHHapticPatternKeyParameterID: CHHapticEventParameterIDHapticIntensity,
                                CHHapticPatternKeyParameterValue: @0.33
                            }
                        ]
                    },
                },
            ]
    };
}


-(void)playHapticEffect:(NSDictionary *)def {
    NSError *error = nil;
    CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:def
                                                                     error:&error];
    // TODO: Check the error.
    id<CHHapticPatternPlayer> player = [self.engine createPlayerWithPattern:pattern error:&error];
    // TODO: Check the error.
    [self.engine startWithCompletionHandler:^(NSError *err){
        if (err == nil) {
            NSError *startError = nil;
            [player startAtTime:0.0 error:&startError];
        }
    }];
}

@end

/*----------------------------------------------------------------------------------------------*/

@interface AppState : NSObject<UIDocumentPickerDelegate, UITextFieldDelegate, UITextViewDelegate> {
    iString *fileBeingSaved;
    iString *pickFileCommand;
    iSystemTextInput *sysCtrl;
}
@property (nonatomic, assign) BOOL isHapticsAvailable;
@property (nonatomic, strong) NSObject *haptic;
@end

static AppState *appState_;

@implementation AppState

-(instancetype)init {
    self = [super init];
    fileBeingSaved = NULL;
    pickFileCommand = NULL;
    sysCtrl = NULL;
    return self;
}

-(void)setSystemTextInput:(iSystemTextInput *)sys {
    sysCtrl = sys;
}

-(iSystemTextInput *)systemTextInput {
    return sysCtrl;
}

-(void)setPickFileCommand:(const char *)command {
    if (!pickFileCommand) {
        pickFileCommand = new_String();
    }
    setCStr_String(pickFileCommand, command);
}

-(void)setFileBeingSaved:(const iString *)path {
    fileBeingSaved = copy_String(path);
}

-(void)removeSavedFile {
    /* The file was copied to an external location, so the cached copy is not needed. */
    remove(cstr_String(fileBeingSaved));
    delete_String(fileBeingSaved);
    fileBeingSaved = NULL;
}

-(void)setupHaptics {
    if (@available(iOS 13.0, *)) {
        self.isHapticsAvailable = CHHapticEngine.capabilitiesForHardware.supportsHaptics;
        if (self.isHapticsAvailable) {
            HapticState *hs = [[HapticState alloc] init];
            [hs setup];
            self.haptic = hs;
            /* We start the engine and keep it running. */
            NSError *err;
            [hs.engine startAndReturnError:&err];
        }
    } else {
        self.isHapticsAvailable = NO;
    }
}

- (void)documentPicker:(UIDocumentPickerViewController *)controller
didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
    if (fileBeingSaved) {
        [self removeSavedFile];
    }
    else {
        /* A file is being opened. */
        NSURL *url = [urls firstObject];
        iString *path = localFilePathFromUrl_String(collectNewCStr_String([[url absoluteString]
                                                                           UTF8String]));
        postCommandf_App("%s temp:1 path:%s",
                         cstr_String(pickFileCommand),
                         cstrCollect_String(path));
        delete_String(pickFileCommand);
        pickFileCommand = NULL;
    }
}

- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
    if (fileBeingSaved) {
        [self removeSavedFile];
    }
    if (pickFileCommand) {
        delete_String(pickFileCommand);
        pickFileCommand = NULL;
    }
}

-(void)keyboardOnScreen:(NSNotification *)notification {
    NSDictionary *info   = notification.userInfo;
    NSValue      *value  = info[UIKeyboardFrameEndUserInfoKey];
    CGRect rawFrame      = [value CGRectValue];
    UIView *view         = [viewController_(get_Window()) view];
    CGRect keyboardFrame = [view convertRect:rawFrame fromView:nil];
//    NSLog(@"keyboardFrame: %@", NSStringFromCGRect(keyboardFrame));
    iMainWindow *window = get_MainWindow();
    const iInt2 rootSize = size_Root(window->base.roots[0]);
    const int keyTop = keyboardFrame.origin.y * window->base.pixelRatio;
    setKeyboardHeight_MainWindow(window, rootSize.y - keyTop);
}

-(void)keyboardOffScreen:(NSNotification *)notification {
    setKeyboardHeight_MainWindow(get_MainWindow(), 0);
}

static void sendReturnKeyPress_(void) {
    SDL_Event ev = { .type = SDL_KEYDOWN };
    ev.key.timestamp = SDL_GetTicks();
    ev.key.keysym.sym = SDLK_RETURN;
    ev.key.state = SDL_PRESSED;
    SDL_PushEvent(&ev);
    ev.type = SDL_KEYUP;
    ev.key.state = SDL_RELEASED;
    SDL_PushEvent(&ev);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    sendReturnKeyPress_();
    return NO;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
    iSystemTextInput *sysCtrl = [appState_ systemTextInput];
    notifyChange_SystemTextInput_(sysCtrl);
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        if (!isNewlineAllowed_SystemTextInput_([appState_ systemTextInput])) {
            sendReturnKeyPress_();
            return NO;
        }
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    iSystemTextInput *sysCtrl = [appState_ systemTextInput];
    notifyChange_SystemTextInput_(sysCtrl);
}

@end

static void enableMouse_(iBool yes) {
    SDL_EventState(SDL_MOUSEBUTTONDOWN, yes);
    SDL_EventState(SDL_MOUSEMOTION, yes);
    SDL_EventState(SDL_MOUSEBUTTONUP, yes);
}

void setupApplication_iOS(void) {
    enableMouse_(iFalse);
    NSString *deviceModel = [[UIDevice currentDevice] model];
    if ([deviceModel isEqualToString:@"iPhone"]) {
        isPhone_ = iTrue;
    }
    appState_ = [[AppState alloc] init];
    [appState_ setupHaptics];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:appState_
               selector:@selector(keyboardOnScreen:)
                   name:UIKeyboardWillShowNotification
                 object:nil];
    [center addObserver:appState_
               selector:@selector(keyboardOffScreen:)
                   name:UIKeyboardWillHideNotification
                 object:nil];
    /* Media player remote controls. */
    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    [[commandCenter pauseCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        iPlayer *player = active_Player();
        if (player) {
            setPaused_Player(player, iTrue);
            return MPRemoteCommandHandlerStatusSuccess;
        }
        return MPRemoteCommandHandlerStatusCommandFailed;
    }];
    [[commandCenter playCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        iPlayer *player = active_Player();
        if (player) {
            if (isPaused_Player(player)) {
                setPaused_Player(player, iFalse);
            }
            else {
                start_Player(player);
            }
            return MPRemoteCommandHandlerStatusSuccess;
        }
        return MPRemoteCommandHandlerStatusCommandFailed;
    }];
    [[commandCenter stopCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        iPlayer *player = active_Player();
        if (player) {
            stop_Player(player);
            return MPRemoteCommandHandlerStatusSuccess;
        }
        return MPRemoteCommandHandlerStatusCommandFailed;
    }];
    [[commandCenter togglePlayPauseCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        iPlayer *player = active_Player();
        if (player) {
            setPaused_Player(player, !isPaused_Player(player));
            return MPRemoteCommandHandlerStatusSuccess;
        }
        return MPRemoteCommandHandlerStatusCommandFailed;
    }];
    [[commandCenter nextTrackCommand] setEnabled:NO];
    [[commandCenter previousTrackCommand] setEnabled:NO];
    [[commandCenter changeRepeatModeCommand] setEnabled:NO];
    [[commandCenter changeShuffleModeCommand] setEnabled:NO];
    [[commandCenter changePlaybackRateCommand] setEnabled:NO];
    [[commandCenter seekForwardCommand] setEnabled:NO];
    [[commandCenter seekBackwardCommand] setEnabled:NO];
    [[commandCenter skipForwardCommand] setEnabled:NO];
    [[commandCenter skipBackwardCommand] setEnabled:NO];
    [[commandCenter changePlaybackPositionCommand] setEnabled:NO];
}

static iBool isDarkMode_(iWindow *window) {
    UIViewController *ctl = viewController_(window);
    if (ctl) {
        UITraitCollection *traits = ctl.traitCollection;
        if (@available(iOS 12.0, *)) {
            return (traits.userInterfaceStyle == UIUserInterfaceStyleDark);
        }
    }
    return iFalse;
}

void safeAreaInsets_iOS(float *left, float *top, float *right, float *bottom) {
    iWindow *window = get_Window();
    UIViewController *ctl = viewController_(window);
    if (@available(iOS 11.0, *)) {
        const UIEdgeInsets safe = ctl.view.safeAreaInsets;
        if (left) *left = safe.left * window->pixelRatio;
        if (top) *top = safe.top * window->pixelRatio;
        if (right) *right = safe.right * window->pixelRatio;
        if (bottom) *bottom = safe.bottom * window->pixelRatio;
    } else {
        if (left) *left = 0.0f;
        if (top) *top = 0.0f;
        if (right) *right = 0.0f;
        if (bottom) *bottom = 0.0f;
    }
}

iBool isPhone_iOS(void) {
    return isPhone_;
}

int displayRefreshRate_iOS(void) {
    return (int) uiWindow_(get_Window()).screen.maximumFramesPerSecond;
}

void setupWindow_iOS(iWindow *window) {
    UIViewController *ctl = viewController_(window);
    isSystemDarkMode_ = isDarkMode_(window);
    postCommandf_App("~os.theme.changed dark:%d contrast:1", isSystemDarkMode_ ? 1 : 0);
}

void playHapticEffect_iOS(enum iHapticEffect effect) {
    if (@available(iOS 13.0, *)) {
        HapticState *hs = (HapticState *) appState_.haptic;
        switch(effect) {
            case tap_HapticEffect:
                [hs playHapticEffect:hs.tapDef];
                break;
            case gentleTap_HapticEffect:
                [hs playHapticEffect:hs.gentleTapDef];
                break;
        }
    }
}

iBool processEvent_iOS(const SDL_Event *ev) {
    if (ev->type == SDL_WINDOWEVENT) {
        if (ev->window.event == SDL_WINDOWEVENT_RESTORED) {
            const iBool isDark = isDarkMode_(get_Window());
            if (isDark != isSystemDarkMode_) {
                isSystemDarkMode_ = isDark;
                postCommandf_App("~os.theme.changed dark:%d contrast:1", isSystemDarkMode_ ? 1 : 0);
            }
        }
    }
    else if (ev->type == SDL_USEREVENT && ev->user.code == command_UserEventCode) {
        const char *cmd = command_UserEvent(ev);
        if (equal_Command(cmd, "ostheme")) {
            if (arg_Command(cmd)) {
                postCommandf_App("os.theme.changed dark:%d contrast:1", isSystemDarkMode_ ? 1 : 0);
            }
        }
        else if (equal_Command(cmd, "theme.changed")) {
            if (@available(iOS 13.0, *)) {
                /* SDL doesn't expose this as a setting, so we'll rely on a hack.
                   Adding an SDL hint for this would be a cleaner solution than calling
                   a private method. */
                UIViewController *vc = viewController_(get_Window());
                SEL sel = NSSelectorFromString(@"setStatusStyle:"); /* custom method */
                if ([vc respondsToSelector:sel]) {
                    NSInvocation *call = [NSInvocation invocationWithMethodSignature:
                                          [NSMethodSignature signatureWithObjCTypes:"v@:i"]];
                    [call setSelector:sel];
                    int style = isDark_ColorTheme(colorTheme_App()) ?
                        UIStatusBarStyleLightContent : UIStatusBarStyleDarkContent;
                    [call setArgument:&style atIndex:2];
                    [call invokeWithTarget:vc];
                }
            }
        }
    }
    return iFalse; /* allow normal processing */
}

void updateNowPlayingInfo_iOS(void) {
    const iPlayer *player = active_Player();
    if (!player) {
        clearNowPlayingInfo_iOS();
        return;
    }
    NSMutableDictionary<NSString *, id> *info = [[NSMutableDictionary<NSString *, id> alloc] init];
    [info setObject:[NSNumber numberWithDouble:time_Player(player)]
            forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    [info setObject:[NSNumber numberWithInt:MPNowPlayingInfoMediaTypeAudio]
            forKey:MPNowPlayingInfoPropertyMediaType];
    [info setObject:[NSNumber numberWithDouble:duration_Player(player)]
             forKey:MPMediaItemPropertyPlaybackDuration];
    const iString *title  = tag_Player(player, title_PlayerTag);
    const iString *artist = tag_Player(player, artist_PlayerTag);
    if (isEmpty_String(title)) {
        title = collectNewCStr_String("Audio"); /* TODO: Use link label or URL file name */
    }
    if (isEmpty_String(artist)) {
        artist = collectNewCStr_String("Lagrange"); /* TODO: Use domain or base URL */
    }
    [info setObject:[NSString stringWithUTF8String:cstr_String(title)]
             forKey:MPMediaItemPropertyTitle];
    [info setObject:[NSString stringWithUTF8String:cstr_String(artist)]
             forKey:MPMediaItemPropertyArtist];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
}

void clearNowPlayingInfo_iOS(void) {
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
}

void exportDownloadedFile_iOS(const iString *path) {
    NSURL *url = [NSURL fileURLWithPath:[[NSString alloc] initWithCString:cstr_String(path)
                                                                 encoding:NSUTF8StringEncoding]];
    UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc]
                                              initWithURL:url
                                              inMode:UIDocumentPickerModeExportToService];
    picker.delegate = appState_;
    [appState_ setFileBeingSaved:path];
    [viewController_(get_Window()) presentViewController:picker animated:YES completion:nil];
}

void pickFileForOpening_iOS(void) {
    pickFile_iOS("file.open");
}

void pickFile_iOS(const char *command) {
    [appState_ setPickFileCommand:command];
    UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc]
                                              initWithDocumentTypes:@[@"public.data"]
                                              inMode:UIDocumentPickerModeImport];
    picker.delegate = appState_;
    [viewController_(get_Window()) presentViewController:picker animated:YES completion:nil];
}

void openTextActivityView_iOS(const iString *text) {
    UIActivityViewController *actView =
        [[UIActivityViewController alloc]
         initWithActivityItems:@[
            [NSString stringWithUTF8String:cstr_String(text)]]
         applicationActivities:nil];
    [viewController_(get_Window()) presentViewController:actView animated:YES completion:nil];
}

/*----------------------------------------------------------------------------------------------*/

enum iAVFAudioPlayerState {
    initialized_AVFAudioPlayerState,
    playing_AVFAudioPlayerState,
    paused_AVFAudioPlayerState
};

struct Impl_AVFAudioPlayer {
    iString cacheFilePath;
    void *player; /* AVAudioPlayer *, no ARC */
    float volume;
    enum iAVFAudioPlayerState state;
};

iDefineTypeConstruction(AVFAudioPlayer)

void init_AVFAudioPlayer(iAVFAudioPlayer *d) {
    init_String(&d->cacheFilePath);
    d->player = NULL;
    d->volume = 1.0f;
    d->state = initialized_AVFAudioPlayerState;
    /* Playback is imminent. */
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}

void deinit_AVFAudioPlayer(iAVFAudioPlayer *d) {
    setInput_AVFAudioPlayer(d, NULL, NULL);
    if (d->player) {
        CFBridgingRelease(d->player);
        d->player = nil;
    }
}

static const char *cacheDir_ = "~/Library/Caches/Audio";

static const char *fileExt_(const iString *mimeType) {
    /* Media types that AVFAudioPlayer will try to play. */
    if (startsWithCase_String(mimeType, "audio/aiff") ||
        startsWithCase_String(mimeType, "audio/x-aiff")) {
        return ".aiff";
    }
    if (startsWithCase_String(mimeType, "audio/3gpp"))  return ".3gpp";
    if (startsWithCase_String(mimeType, "audio/mpeg"))  return ".mp3";
    if (startsWithCase_String(mimeType, "audio/mp3"))   return ".mp3";
    if (startsWithCase_String(mimeType, "audio/mp4"))   return ".mp4";
    if (startsWithCase_String(mimeType, "audio/mpeg4")) return ".mp4";
    if (startsWithCase_String(mimeType, "audio/aac"))   return ".aac";
    return "";
}

#define REF_d_player    (__bridge AVAudioPlayer *)d->player

iBool setInput_AVFAudioPlayer(iAVFAudioPlayer *d, const iString *mimeType, const iBlock *audioFileData) {
    if (!isEmpty_String(&d->cacheFilePath)) {
        remove(cstr_String(&d->cacheFilePath));
        clear_String(&d->cacheFilePath);
    }
    if (d->player) {
        CFBridgingRelease(d->player);
        d->player = nil;
    }
    if (mimeType && audioFileData && iCmpStr(fileExt_(mimeType), "")) {
        makeDirs_Path(collectNewCStr_String(cacheDir_));
        iFile *f = new_File(collectNewFormat_String("%s/%u%s", cacheDir_, SDL_GetTicks(), fileExt_(mimeType)));
        if (open_File(f, writeOnly_FileMode)) {
            write_File(f, audioFileData);
            set_String(&d->cacheFilePath, path_File(f));
            NSError *error = nil;
            d->player = (void *) CFBridgingRetain([[AVAudioPlayer alloc]
                         initWithContentsOfURL:[NSURL fileURLWithPath:
                                                [NSString stringWithUTF8String:cstr_String(&d->cacheFilePath)]]
                         error:&error]);
            if (error) {
                d->player = nil;
            }
            [REF_d_player setVolume:d->volume];
        }
        iRelease(f);
    }
    return d->player != nil;
}

void play_AVFAudioPlayer(iAVFAudioPlayer *d) {
    if (d->state != playing_AVFAudioPlayerState) {
        [REF_d_player play];
        d->state = playing_AVFAudioPlayerState;
    }
}

void stop_AVFAudioPlayer(iAVFAudioPlayer *d) {
    [REF_d_player stop];
    d->state = initialized_AVFAudioPlayerState;
}

void setPaused_AVFAudioPlayer(iAVFAudioPlayer *d, iBool paused) {
    if (paused && d->state != paused_AVFAudioPlayerState) {
        [REF_d_player pause];
        d->state = paused_AVFAudioPlayerState;
    }
    else if (!paused && d->state != playing_AVFAudioPlayerState) {
        [REF_d_player play];
        d->state = playing_AVFAudioPlayerState;
    }
}

void setVolume_AVFAudioPlayer(iAVFAudioPlayer *d, float volume) {
    d->volume = volume;
    if (d->player) {
        [REF_d_player setVolume:volume];
    }
}

double currentTime_AVFAudioPlayer(const iAVFAudioPlayer *d) {
    return [REF_d_player currentTime];
}

double duration_AVFAudioPlayer(const iAVFAudioPlayer *d) {
    return [REF_d_player duration];
}

iBool isStarted_AVFAudioPlayer(const iAVFAudioPlayer *d) {
    return d->state != initialized_AVFAudioPlayerState;
}

iBool isPaused_AVFAudioPlayer(const iAVFAudioPlayer *d) {
    return d->state == paused_AVFAudioPlayerState;
}

/*----------------------------------------------------------------------------------------------*/

struct Impl_SystemTextInput {
    int flags;
    void *field; /* single-line text field */
    void *view;  /* multi-line text view */
    void (*textChangedFunc)(iSystemTextInput *, void *);
    void *textChangedContext;
};

iDefineTypeConstructionArgs(SystemTextInput, (iRect rect, int flags), rect, flags)

#define REF_d_field  (__bridge UITextField *)d->field
#define REF_d_view   (__bridge UITextView *)d->view

static CGRect convertToCGRect_(const iRect *rect, iBool expanded) {
    const iWindow *win = get_Window();
    CGRect frame;
    // TODO: Convert coordinates properly!
    frame.origin.x = rect->pos.x / win->pixelRatio;
    frame.origin.y = (rect->pos.y - gap_UI + 1) / win->pixelRatio;
    frame.size.width = rect->size.x / win->pixelRatio;
    frame.size.height = rect->size.y / win->pixelRatio;
    /* Some padding to account for insets. If we just zero out the insets, the insertion point
       may be clipped at the edges. */
    if (expanded) {
        const float inset = gap_UI / get_Window()->pixelRatio;
        frame.origin.x -= inset + 1;
        frame.origin.y -= inset + 1;
        frame.size.width += 2 * inset + 2;
        frame.size.height += inset + 1 + inset;
    }
    return frame;
}

static UIColor *makeUIColor_(enum iColorId colorId) {
    iColor color = get_Color(colorId);
    return [UIColor colorWithRed:color.r / 255.0
                           green:color.g / 255.0
                            blue:color.b / 255.0
                           alpha:color.a / 255.0];
}

void init_SystemTextInput(iSystemTextInput *d, iRect rect, int flags) {
    d->flags = flags;
    d->field = NULL;
    d->view  = NULL;
    CGRect frame = convertToCGRect_(&rect, (flags & multiLine_SystemTextInputFlags) != 0);
    if (flags & multiLine_SystemTextInputFlags) {
        d->view = (void *) CFBridgingRetain([[UITextView alloc] initWithFrame:frame textContainer:nil]);
        [[viewController_(get_Window()) view] addSubview:REF_d_view];
    }
    else {
        d->field = (void *) CFBridgingRetain([[UITextField alloc] initWithFrame:frame]);
        [[viewController_(get_Window()) view] addSubview:REF_d_field];
    }
    UIControl<UITextInputTraits> *traits = (UIControl<UITextInputTraits> *) (d->view ? REF_d_view : REF_d_field);
    if (~flags & insertNewlines_SystemTextInputFlag) {
        [traits setReturnKeyType:UIReturnKeyDone];
    }
    if (flags & returnGo_SystemTextInputFlags) {
        [traits setReturnKeyType:UIReturnKeyGo];
    }
    if (flags & returnSend_SystemTextInputFlags) {
        [traits setReturnKeyType:UIReturnKeySend];
    }
    if (flags & disableAutocorrect_SystemTextInputFlag) {
        [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
        [traits setSpellCheckingType:UITextSpellCheckingTypeNo];
    }
    if (flags & disableAutocapitalize_SystemTextInputFlag) {
        [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    }
    if (flags & alignRight_SystemTextInputFlag) {
        if (d->field) {
            [REF_d_field setTextAlignment:NSTextAlignmentRight];
        }
        if (d->view) {
            [REF_d_view setTextAlignment:NSTextAlignmentRight];
        }
    }
    UIColor *textColor = makeUIColor_(uiInputTextFocused_ColorId);
    UIColor *backgroundColor = makeUIColor_(uiInputBackgroundFocused_ColorId);
    UIColor *tintColor = makeUIColor_(uiInputFrameHover_ColorId); /* use the accent color */ //uiInputCursor_ColorId);
    [appState_ setSystemTextInput:d];
    if (d->field) {
        UITextField *field = REF_d_field;
        [field setTextColor:textColor];
        [field setTintColor:tintColor];
        [field setDelegate:appState_];
        [field becomeFirstResponder];
    }
    else {
        UITextView *view = REF_d_view;
        [view setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.0f]];
        [view setTextColor:textColor];
        [view setTintColor:tintColor];
        if (flags & extraPadding_SystemTextInputFlag) {
            [view setContentInset:(UIEdgeInsets){ 0, 0, 3 * gap_UI / get_Window()->pixelRatio, 0}];
        }
        [view setEditable:YES];
        [view setDelegate:appState_];
        [view becomeFirstResponder];
    }
    d->textChangedFunc = NULL;
    d->textChangedContext = NULL;
}

void deinit_SystemTextInput(iSystemTextInput *d) {
    [appState_ setSystemTextInput:nil];
    if (d->field) {
        [REF_d_field removeFromSuperview];
        CFBridgingRelease(d->field);
        d->field = nil;
    }
    if (d->view) {
        [REF_d_view removeFromSuperview];
        CFBridgingRelease(d->view);
        d->view = nil;
    }
}

void selectAll_SystemTextInput(iSystemTextInput *d) {
    if (d->field) {
        [REF_d_field selectAll:nil];
    }
    if (d->view) {
        [REF_d_view selectAll:nil];
    }
}

void setText_SystemTextInput(iSystemTextInput *d, const iString *text, iBool allowUndo) {
    NSString *str = [NSString stringWithUTF8String:cstr_String(text)];
    if (d->field) {
        [REF_d_field setText:str];
        if (d->flags & selectAll_SystemTextInputFlags) {
            [REF_d_field selectAll:nil];
        }
    }
    else {
        UITextView *view = REF_d_view;
//        if (allowUndo) {
//            [view selectAll:nil];
//            if ([view shouldChangeTextInRange:[view selectedTextRange] replacementText:@""]) {
//                [[view textStorage] beginEditing];
//                [[view textStorage] replaceCharactersInRange:[view selectedRange] withString:@""];
//                [[view textStorage] endEditing];
//            }
//        }
//        else {
        // TODO: How to implement `allowUndo`, given that UITextView does not exist when unfocused?
        // Maybe keep the UITextStorage (if it has the undo?)?
        [view setText:str];
//        }
        if (d->flags & selectAll_SystemTextInputFlags) {
            [view selectAll:nil];
        }
    }
}

int preferredHeight_SystemTextInput(const iSystemTextInput *d) {
    if (d->view) {
        CGRect usedRect = [[REF_d_view layoutManager] usedRectForTextContainer:[REF_d_view textContainer]];
        return usedRect.size.height * get_Window()->pixelRatio;
    }
    return 0;
}

void setFont_SystemTextInput(iSystemTextInput *d, int fontId) {
    float height = lineHeight_Text(fontId) / get_Window()->pixelRatio;
    UIFont *font;
    //        for (NSString *name in [UIFont familyNames]) {
    //            printf("family: %s\n", [name cStringUsingEncoding:NSUTF8StringEncoding]);
    //        }
    if (fontId / maxVariants_Fonts * maxVariants_Fonts == monospace_FontId) {
//        font = [UIFont monospacedSystemFontOfSize:0.8f * height weight:UIFontWeightRegular];
//        for (NSString *name in [UIFont fontNamesForFamilyName:@"Iosevka Term"]) {
//            printf("fontname: %s\n", [name cStringUsingEncoding:NSUTF8StringEncoding]);
//        }
        font = [UIFont fontWithName:@"Iosevka-Term-Extended" size:height * 0.82f];
    }
    else {
//        font = [UIFont systemFontOfSize:0.65f * height];
        font = [UIFont fontWithName:@"SourceSans3-Regular" size:height * 0.7f];
    }
    if (d->field) {
        [REF_d_field setFont:font];
    }
    if (d->view) {
        [REF_d_view setFont:font];
    }
}

const iString *text_SystemTextInput(const iSystemTextInput *d) {
    if (d->field) {
        return collectNewCStr_String([[REF_d_field text] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    if (d->view) {
        return collectNewCStr_String([[REF_d_view text] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    return NULL;
}

void setRect_SystemTextInput(iSystemTextInput *d, iRect rect) {
    CGRect frame = convertToCGRect_(&rect, (d->flags & multiLine_SystemTextInputFlags) != 0);
    if (d->field) {
        [REF_d_field setFrame:frame];
    }
    else {
        [REF_d_view setFrame:frame];
    }
}

void setTextChangedFunc_SystemTextInput(iSystemTextInput *d,
                                        void (*textChangedFunc)(iSystemTextInput *, void *),
                                        void *context) {
    d->textChangedFunc = textChangedFunc;
    d->textChangedContext = context;
}

static void notifyChange_SystemTextInput_(iSystemTextInput *d) {
    if (d && d->textChangedFunc) {
        d->textChangedFunc(d, d->textChangedContext);
    }
}

static BOOL isNewlineAllowed_SystemTextInput_(const iSystemTextInput *d) {
    return (d->flags & insertNewlines_SystemTextInputFlag) != 0;
}