forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TermInput.m
1211 lines (1035 loc) · 37 KB
/
TermInput.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
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2019 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blink is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////
#import "BKDefaults.h"
#import "TermInput.h"
#import "SmartKeysController.h"
#import "SmartKeysView.h"
#import "BKSettingsNotifications.h"
#import "BKUserConfigurationManager.h"
#import "BKKeyboardModifierViewController.h"
#import "TermDevice.h"
#import "openurl.h"
static NSDictionary *bkModifierMaps = nil;
static NSDictionary *CTRLCodes = nil;
static NSDictionary *FModifiers = nil;
static NSDictionary *FKeys = nil;
static NSString *SS3 = nil;
static NSString *CSI = nil;
NSString *const TermViewCtrlSeq = @"ctrlSeq:";
NSString *const TermViewEscSeq = @"escSeq:";
NSString *const TermViewEscCtrlSeq = @"escCtrlSeq:";
NSString *const TermViewCursorFuncSeq = @"cursorSeq:";
NSString *const TermViewFFuncSeq = @"fkeySeq:";
NSString *const TermViewAutoRepeateSeq = @"autoRepeatSeq:";
@implementation CC
+ (void)initialize
{
CTRLCodes = @{
@" " : @"\x00",
@"[" : @"\x1B",
@"]" : @"\x1D",
@"\\" : @"\x1C",
@"^" : @"\x1E",
@"_" : @"\x1F",
@"/" : @"\x1F",
@"2" : @"\x00",
@"3" : @"\x1B",
@"4" : @"\x1C",
@"5" : @"\x1D",
@"6" : @"\x1E",
@"7" : @"\x1F",
@"8" : @"\x7F"
};
FModifiers = @{
@0 : @0,
@(UIKeyModifierShift) : @2,
@(UIKeyModifierAlternate) : @3,
@(UIKeyModifierShift | UIKeyModifierAlternate) : @4,
@(UIKeyModifierControl) : @5,
@(UIKeyModifierShift | UIKeyModifierControl) : @6,
@(UIKeyModifierAlternate | UIKeyModifierControl) : @7,
@(UIKeyModifierShift | UIKeyModifierAlternate | UIKeyModifierControl) : @8
};
FKeys = @{
UIKeyInputUpArrow : @"A",
UIKeyInputDownArrow : @"B",
UIKeyInputRightArrow : @"C",
UIKeyInputLeftArrow : @"D",
SpecialCursorKeyPgUp: @"5~",
SpecialCursorKeyPgDown: @"6~",
SpecialCursorKeyHome: @"H",
SpecialCursorKeyEnd: @"F"
};
SS3 = [self ESC:@"O"];
CSI = [self ESC:@"["];
}
+ (NSDictionary *)FModifiers
{
return FModifiers;
}
+ (NSString *)CTRL:(NSString *)c
{
NSString *code;
if ((code = [CTRLCodes objectForKey:c]) != nil) {
return code;
} else {
char x = [c characterAtIndex:0];
NSString *str = [NSString stringWithFormat:@"%c", x - 'a' + 1];
return str;
}
}
+ (NSString *)ESC:(NSString *)c
{
NSInteger len = [c length];
if (len == 0) {
return @"\x1B";
} else if (c == UIKeyInputEscape) {
return @"\x1B\x1B";
} else {
return [NSString stringWithFormat:@"\x1B%c", [c characterAtIndex:0]];
}
}
+ (NSString *)KEY:(NSString *)c
{
return [CC KEY:c MOD:0 RAW:NO];
}
+ (NSString *)KEY:(NSString *)c MOD:(NSInteger)m RAW:(BOOL)raw
{
NSArray *out;
BOOL isPageCursorKey = c == SpecialCursorKeyPgUp || c == SpecialCursorKeyPgDown;
if ([FKeys.allKeys containsObject:c]) {
if (m) {
out = @[ CSI, FKeys[c] ];
} else if (raw && !isPageCursorKey) {
return [NSString stringWithFormat:@"%@%@", SS3, FKeys[c]];
} else {
return [NSString stringWithFormat:@"%@%@", CSI, FKeys[c]];
}
} else if (c == UIKeyInputEscape) {
return @"\x1B";
} else if ([c isEqual:@"\n"]) {
// See raw mode: http://man7.org/linux/man-pages/man3/termios.3.html
// if (raw) {
// INLCR Translate NL to CR on input
return @"\r";
// }
// return c;
}
if (m) {
NSString *modSeq = [NSString stringWithFormat:@"1;%@", FModifiers[[NSNumber numberWithInteger:m]]];
return [out componentsJoinedByString:modSeq];
}
return c;
}
+ (NSString *)FKEY:(NSInteger)number
{
switch (number) {
case 1:
return [NSString stringWithFormat:@"%@P", SS3];
case 2:
return [NSString stringWithFormat:@"%@Q", SS3];
case 3:
return [NSString stringWithFormat:@"%@R", SS3];
case 4:
return [NSString stringWithFormat:@"%@S", SS3];
case 5:
return [NSString stringWithFormat:@"%@15~", CSI];
case 6:
case 7:
case 8:
return [NSString stringWithFormat:@"%@1%ld~", CSI, number + 1];
case 9:
case 10:
return [NSString stringWithFormat:@"%@2%ld~", CSI, number - 9];
case 11:
case 12:
return [NSString stringWithFormat:@"%@2%ld~", CSI, number - 8];
default:
return nil;
}
}
@end
@implementation UndoManager {
NSTimeInterval _startSkip;
}
- (BOOL)canRedo {
return [self _canOperate];
}
- (BOOL)canUndo {
return [self _canOperate];
}
-(void)undo
{
[_undoManagerDelegate undoWithManager:self];
}
- (void)redo
{
[_undoManagerDelegate redoWithManager:self];
}
- (void)skipNext {
_startSkip = [NSDate timeIntervalSinceReferenceDate];
}
- (BOOL)_canOperate {
return [NSDate timeIntervalSinceReferenceDate] - _startSkip > 0.5;
}
@end
@interface TermInput () <UndoManagerDelegate, UITextViewDelegate, NSTextStorageDelegate>
@end
@implementation TermInput {
NSMutableDictionary *_controlKeys;
NSMutableDictionary *_controlKeysWithoutAutoRepeat;
NSMutableDictionary *_functionKeys;
NSMutableDictionary *_functionTriggerKeys;
NSString *_specialFKeysRow;
NSSet<NSString *> *_imeLangSet;
NSMutableArray<UIKeyCommand *> *_rawAdditionalKbdCommands;
NSMutableArray<UIKeyCommand *> *_kbdCommands;
NSMutableArray<UIKeyCommand *> *_kbdCommandsWithoutAutoRepeat;
SmartKeysController *_smartKeys;
BOOL _inputEnabled;
NSString *_cmdModifierSequence;
UndoManager *_undoManager;
BOOL _skipTextStorageDelete;
NSString * _markedText;
}
+ (void)initialize
{
bkModifierMaps = @{
BKKeyboardModifierCtrl : [NSNumber numberWithInt:UIKeyModifierControl],
BKKeyboardModifierAlt : [NSNumber numberWithInt:UIKeyModifierAlternate],
BKKeyboardModifierCmd : [NSNumber numberWithInt:UIKeyModifierCommand],
BKKeyboardModifierCaps : [NSNumber numberWithInt:UIKeyModifierAlphaShift],
BKKeyboardModifierShift : [NSNumber numberWithInt:UIKeyModifierShift]
};
}
- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer {
self = [super initWithFrame:frame textContainer:textContainer];
if (self) {
self.smartDashesType = UITextSmartDashesTypeNo;
self.smartQuotesType = UITextSmartQuotesTypeNo;
self.smartInsertDeleteType = UITextSmartInsertDeleteTypeNo;
self.spellCheckingType = UITextSpellCheckingTypeNo;
self.delegate = self;
_undoManager = [[UndoManager alloc] init];
_undoManager.undoManagerDelegate = self;
[self _configureNotifications];
[self _configureShotcuts];
[self setHidden:YES];
self.textContainerInset = UIEdgeInsetsZero;
self.textContainer.lineFragmentPadding = 0;
self.font = [UIFont fontWithName:@"Menlo" size:0];
[self _configureLangSet];
_skipTextStorageDelete = NO;
self.textStorage.delegate = self;
self.keyboardAppearance = [self _keyboardAppearance];
}
return self;
}
// Autocorrection and autocapitalization should be implemented as overrides.
// Fixes #370
- (UITextAutocorrectionType)autocorrectionType
{
return UITextAutocorrectionTypeNo;
}
- (UITextAutocapitalizationType)autocapitalizationType
{
return UITextAutocapitalizationTypeNone;
}
- (void)_configureLangSet
{
_imeLangSet = [NSSet setWithObjects:
@"zh-Hans",
@"zh-Hant",
@"ja-JP",
nil];
}
- (void)deviceWrite:(NSString *) input {
[_device.view displayInput: input];
[_device write: input];
}
- (void)textStorage:(NSTextStorage *)textStorage
didProcessEditing:(NSTextStorageEditActions)editedMask
range:(NSRange)editedRange
changeInLength:(NSInteger)delta
{
if (delta == -1 && !_skipTextStorageDelete && !_markedText) {
[self deviceWrite:@"\x7f"];
}
}
- (BOOL)hasText {
return YES;
}
- (NSUndoManager *)undoManager {
return _undoManager;
}
- (UIKeyboardAppearance)_keyboardAppearance
{
BKKeyboardStyle style = [BKDefaults keyboardStyle];
switch (style) {
case BKKeyboardStyleLight: return UIKeyboardAppearanceLight;
case BKKeyboardStyleDark: return UIKeyboardAppearanceDark;
default: return UIKeyboardAppearanceDefault;
}
}
- (NSString *)textInputContextIdentifier
{
// Remember current input
return @"terminput";
}
- (void)_configureNotifications
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(_configureShotcuts)
name:BKKeyboardConfigChanged
object:nil];
[nc addObserver:self
selector:@selector(_configureShotcuts)
name:BKKeyboardFuncTriggerChanged
object:nil];
[nc addObserver:self selector:@selector(_setupAppearance) name:BKAppearanceChanged object:nil];
}
- (void)_setupAppearance {
self.keyboardAppearance = [self _keyboardAppearance];
[self reloadInputViews];
}
- (void)reset
{
_skipTextStorageDelete = YES;
self.text = @"";
self.alpha = 1;
[_device.view setIme: @"" completionHandler:nil];
_markedText = nil;
_skipTextStorageDelete = NO;
}
- (void)_insertText:(NSString *)text
{
// Discard CAPS on characters when caps are mapped and there is no SW keyboard.
BOOL capsWithoutSWKeyboard = !self.softwareKB && [self _capsMapped];
if (capsWithoutSWKeyboard && text.length == 1 && [text characterAtIndex:0] > 0x1F) {
if (!_markedText) {
text = [text lowercaseString];
}
}
if (_device.view.hasSelection) {
// If the key is a special key, we do not apply modifiers.
if (text.length > 1) {
// Check if we have a function key
NSRange range = [text rangeOfString:@"FKEY"];
if (range.location == NSNotFound) {
[self _changeSelectionWithInput:text andFlags: kNilOptions];
}
} else {
NSUInteger modifiers = [[_smartKeys view] modifiers];
[self _changeSelectionWithInput:text andFlags:modifiers];
}
return;
}
// If the key is a special key, we do not apply modifiers.
if (text.length > 1) {
// Check if we have a function key
NSRange range = [text rangeOfString:@"FKEY"];
if (range.location != NSNotFound) {
NSString *value = [text substringFromIndex:(range.length)];
[self deviceWrite:[CC FKEY:[value integerValue]]];
} else {
[self deviceWrite:[CC KEY:text MOD:0 RAW:_device.rawMode]];
}
} else {
NSUInteger modifiers = [[_smartKeys view] modifiers];
if (modifiers == KbdCtrlModifier) {
[self ctrlSeqWithInput:text];
} else if (modifiers == KbdAltModifier) {
[self escSeqWithInput:text];
} else if (modifiers == (KbdCtrlModifier | KbdAltModifier)) {
[self escCtrlSeqWithInput: text];
} else {
[self deviceWrite:[CC KEY:text MOD:0 RAW:_device.rawMode]];
}
}
}
- (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
{
[super setMarkedText:markedText selectedRange:selectedRange];
_markedText = markedText;
[_device.view setIme: _markedText
completionHandler:^(id data, NSError * _Nullable error) {
if (!data) {
return;
}
CGRect rect = CGRectFromString(data[@"markedRect"]);
CGFloat suggestionsHeight = 44;
CGFloat maxY = CGRectGetMaxY(rect);
CGFloat minY = CGRectGetMinY(rect);
if (maxY - suggestionsHeight < 0) {
rect.origin.y = maxY;
} else {
rect.origin.y = minY - suggestionsHeight;
}
rect.size.height = 0;
self.frame = rect;
}];
}
- (void)unmarkText
{
[super unmarkText];
if (_markedText) {
[self _insertText:_markedText];
}
[self reset];
}
- (void)insertText:(NSString *)text
{
[self _insertText:text];
if (_markedText) {
[self reset];
return;
}
[super insertText:text];
NSInteger wordsToKeepInLine = 3;
text = self.text;
NSArray *comps = [text componentsSeparatedByString:@" "];
if (comps.count > wordsToKeepInLine) {
comps = [comps subarrayWithRange:NSMakeRange(comps.count - wordsToKeepInLine, wordsToKeepInLine)];
_skipTextStorageDelete = YES;
self.text = [comps componentsJoinedByString:@" "];
_skipTextStorageDelete = NO;
}
}
// Delete forward. See https://github.com/blinksh/blink/issues/559
- (void)_deleteForwardAndNotify:(bool)notify {
[self deviceWrite:@"\x1b\x5b\x33\x7e"];
}
- (void)deleteBackward
{
if (_markedText.length == 1) {
_markedText = nil;
_skipTextStorageDelete = YES;
[self unmarkText];
_skipTextStorageDelete = NO;
return;
}
// Send a delete backward key to the buffer
[self deviceWrite:@"\x7f"];
_skipTextStorageDelete = YES;
[super deleteBackward];
_skipTextStorageDelete = NO;
}
// Alt+Backspace
- (void)_deleteByWord
{
if (![self _remapInput:@"\x7f" forModifier:BKKeyboardModifierAlt]) {
// Default to `^[^?`. See https://github.com/blinksh/blink/issues/117
[self deviceWrite:[CC ESC:@"\x7f"]];
}
}
- (void)escSeqWithInput:(NSString *)input
{
if (_device.view.hasSelection) {
if ([input isEqualToString:@""]) {
[self _changeSelectionWithInput:UIKeyInputEscape andFlags:kNilOptions];
} else {
[self _changeSelectionWithInput:input andFlags:UIKeyModifierAlternate];
}
} else {
[self deviceWrite:[CC ESC:input]];
}
}
- (void)escSeq:(UIKeyCommand *)cmd
{
[self escSeqWithInput:cmd.input];
}
- (void)escSeqNoInput:(UIKeyCommand *)cmd
{
[self escSeqWithInput:@""];
}
- (void)arrowSeq:(UIKeyCommand *)cmd
{
if (_device.view.hasSelection) {
[self _changeSelection:cmd];
} else {
[self deviceWrite:[CC KEY:cmd.input MOD:cmd.modifierFlags RAW:_device.rawMode]];
}
}
// Shift prints uppercase in the case CAPSLOCK is blocked
- (void)shiftSeq:(UIKeyCommand *)cmd
{
if ([cmd.input length] == 0) {
return;
} else {
[self deviceWrite:[cmd.input uppercaseString]];
}
}
- (void)ctrlSeqWithInput:(NSString *)input
{
if (_device.view.hasSelection) {
[self _changeSelectionWithInput:input andFlags:UIKeyModifierControl];
} else {
if ([_device.delegate handleControl:input]) {
return;
}
[self deviceWrite:[CC CTRL:input]];
}
}
- (void)ctrlSeq:(UIKeyCommand *)cmd
{
if ([cmd.input length]) {
[self ctrlSeqWithInput:cmd.input];
}
}
- (void)dummySeq:(UIKeyCommand *)cmd
{
}
- (void)escCtrlSeqWithInput:(NSString *)input
{
NSString *seq = [NSString stringWithFormat:@"%@%@", [CC ESC:nil], [CC CTRL:input]];
[self deviceWrite:seq];
}
- (void)escCtrlSeq:(UIKeyCommand *)cmd
{
if (_device.view.hasSelection) {
[self _changeSelectionWithInput:cmd.input andFlags:UIKeyModifierControl | UIKeyModifierAlternate];
} else {
[self escCtrlSeqWithInput:cmd.input];
}
}
- (void)cursorSeq:(UIKeyCommand *)cmd
{
if (_device.view.hasSelection) {
[self _changeSelection:cmd];
return;
}
if (cmd.input == UIKeyInputUpArrow) {
[self deviceWrite:[CC KEY:SpecialCursorKeyPgUp MOD:0 RAW:_device.rawMode]];
} else if (cmd.input == UIKeyInputDownArrow) {
[self deviceWrite:[CC KEY:SpecialCursorKeyPgDown MOD:0 RAW:_device.rawMode]];
} else if (cmd.input == UIKeyInputLeftArrow) {
[self deviceWrite:[CC KEY:SpecialCursorKeyHome MOD:0 RAW:_device.rawMode]];
} else if (cmd.input == UIKeyInputRightArrow) {
[self deviceWrite:[CC KEY:SpecialCursorKeyEnd MOD:0 RAW:_device.rawMode]];
}
}
- (void)fkeySeq:(UIKeyCommand *)cmd
{
NSInteger value = [cmd.input integerValue];
if (value == 0) {
[self deviceWrite:[CC FKEY:10]];
} else {
[self deviceWrite:[CC FKEY:value]];
}
}
- (void)autoRepeatSeq:(id)sender
{
UIKeyCommand *command = (UIKeyCommand*)sender;
if (_device.view.hasSelection) {
[self _changeSelection:command];
} else {
NSString *text = command.input;
NSUInteger modifiers = [[_smartKeys view] modifiers];
if (modifiers == KbdCtrlModifier) {
[self ctrlSeqWithInput:text];
} else if (modifiers == KbdAltModifier) {
[self escSeqWithInput:text];
} else if (modifiers == (KbdCtrlModifier | KbdAltModifier)) {
[self escCtrlSeqWithInput: text];
} else {
[self deviceWrite:text];
}
}
}
- (BOOL)_remapCmdSeqWithSender:(id)sender andInput:(NSString *)input
{
if (!_cmdModifierSequence || sender != nil) {
return NO;
}
if (_cmdModifierSequence == TermViewCtrlSeq) {
[self ctrlSeqWithInput:input];
} else if (_cmdModifierSequence == TermViewEscSeq) {
[self escSeqWithInput:input];
} else {
// return NO?
}
return YES;
}
// This are all key commands capture by UIKeyInput and triggered
// straight to the handler. A different firstresponder than UIKeyInput could
// capture them, but we would not capture normal keys. We remap them
// here as commands to the terminal.
// Cmd+c
- (void)copy:(id)sender
{
if (![self _remapCmdSeqWithSender:sender andInput:@"c"]) {
[_device.view copy:sender];
}
}
// Cmd+x
- (void)cut:(id)sender
{
[self _remapCmdSeqWithSender:sender andInput:@"x"];
}
// Cmd+v
- (void)paste:(id)sender
{
if (![self _remapCmdSeqWithSender:sender andInput:@"v"]) {
[self yank:sender];
}
}
// Cmd+z
- (void)undoWithManager:(UndoManager *)manager
{
[self _remapCmdSeqWithSender:manager andInput:@"z"];
}
// Cmd+Z
- (void)redoWithManager:(UndoManager *)manager
{
[self _remapCmdSeqWithSender:manager andInput:@"Z"];
}
// Cmd+a
- (void)selectAll:(id)sender
{
[self _remapCmdSeqWithSender:sender andInput:@"a"];
}
// Cmd+b
- (void)toggleBoldface:(id)sender
{
[self _remapCmdSeqWithSender:sender andInput:@"b"];
}
// Cmd+i
- (void)toggleItalics:(id)sender
{
[self _remapCmdSeqWithSender:sender andInput:@"i"];
}
// Cmd+u
- (void)toggleUnderline:(id)sender
{
[self _remapCmdSeqWithSender:sender andInput:@"u"];
}
- (void)pasteSelection:(id)sender
{
[_device.view pasteSelection: sender];
}
- (void)copyLink:(id)sender
{
UIPasteboard.generalPasteboard.URL = [_device.view detectedLink];
[_device.view cleanSelection];
}
- (void)openLink:(id)sender
{
NSURL * url = [_device.view detectedLink];
[_device.view cleanSelection];
if (url == nil) {
return;
}
blink_openurl(url);
}
- (void)unselect:(id)sender
{
[_device.view cleanSelection];
}
- (UIEditingInteractionConfiguration)editingInteractionConfiguration {
[_undoManager skipNext];
return UIEditingInteractionConfigurationDefault;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (self.window.windowScene.activationState != UISceneActivationStateForegroundActive ) {
return NO;
}
if ([sender isKindOfClass:[UIMenuController class]]) {
TermView *deviceView = _device.view;
// The menu can only perform paste methods
if (action == @selector(paste:) ||
(action == @selector(copy:) && deviceView.hasSelection) ||
(action == @selector(pasteSelection:) && deviceView.hasSelection) ||
(action == @selector(copyLink:) && deviceView.detectedLink) ||
(action == @selector(openLink:) && deviceView.detectedLink)
) {
return YES;
}
return NO;
} else if ([sender isEqual:@"UIEditingInteraction"]) {
return action != @selector(cut:);
}
// UIApplicationState appState = [[UIApplication sharedApplication] applicationState];
//
// if (appState != UIApplicationStateActive) {
// return NO;
// }
// super returns NO (No text?), so we check ourselves.
if (action == @selector(paste:) ||
action == @selector(cut:) ||
action == @selector(copy:) ||
action == @selector(select:) ||
action == @selector(selectAll:) ||
action == @selector(delete:) ||
action == @selector(toggleBoldface:) ||
action == @selector(toggleItalics:) ||
action == @selector(toggleUnderline:)
) {
return YES;
}
BOOL result = [super canPerformAction:action withSender:sender];
return result;
}
#pragma mark External Keyboard
- (void)_setKbdCommands
{
_kbdCommands = [NSMutableArray array];
_kbdCommandsWithoutAutoRepeat = [NSMutableArray array];
for (NSNumber *modifier in _functionKeys.allKeys) {
[_kbdCommands addObjectsFromArray:_functionKeys[modifier]];
if (modifier.integerValue == UIKeyModifierAlphaShift || modifier.integerValue == UIKeyModifierShift) {
continue;
}
[_kbdCommandsWithoutAutoRepeat addObjectsFromArray:_functionKeys[modifier]];
}
for (NSNumber *modifier in _functionTriggerKeys.allKeys) {
[_kbdCommands addObjectsFromArray:_functionTriggerKeys[modifier]];
[_kbdCommandsWithoutAutoRepeat addObjectsFromArray:_functionTriggerKeys[modifier]];
}
[_kbdCommands addObjectsFromArray:_rawAdditionalKbdCommands];
[_kbdCommandsWithoutAutoRepeat addObjectsFromArray:_rawAdditionalKbdCommands];
[_kbdCommands addObjectsFromArray:self._functionModifierKeys];
[_kbdCommandsWithoutAutoRepeat addObjectsFromArray:self._functionModifierKeys];
// This dummy command to hand stuck cmd key
UIKeyCommand *cmdCommand = [UIKeyCommand keyCommandWithInput:@""
modifierFlags:UIKeyModifierCommand
action:@selector(_kbCmd:)];
[_kbdCommands addObject:cmdCommand];
[_kbdCommandsWithoutAutoRepeat addObject:cmdCommand];
UIKeyCommand *altEnterCommand = [UIKeyCommand keyCommandWithInput:@"\r"
modifierFlags:UIKeyModifierAlternate
action:@selector(_altEnter:)];
[_kbdCommands addObject:altEnterCommand];
[_kbdCommandsWithoutAutoRepeat addObject:altEnterCommand];
UIKeyCommand *shiftTabCommand = [UIKeyCommand keyCommandWithInput:@"\t"
modifierFlags:UIKeyModifierShift
action:@selector(_shiftTab:)];
[_kbdCommands addObject:shiftTabCommand];
[_kbdCommandsWithoutAutoRepeat addObject:shiftTabCommand];
for (NSNumber *modifier in _controlKeysWithoutAutoRepeat.allKeys) {
[_kbdCommandsWithoutAutoRepeat addObjectsFromArray:_controlKeys[modifier]];
}
for (NSNumber *modifier in _controlKeys.allKeys) {
[_kbdCommands addObjectsFromArray:_controlKeys[modifier]];
}
}
- (void)_kbCmd:(UIKeyCommand *)cmd
{
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
[self resignFirstResponder];
}
}
- (void)_altEnter:(UIKeyCommand *)cmd
{
[self escSeq:cmd];
}
- (void)_shiftTab:(UIKeyCommand *)cmd
{
[self deviceWrite:@"\x1b\x5b\x5a"];
}
- (void)_assignSequence:(NSString *)seq toModifier:(UIKeyModifierFlags)modifier
{
if (!seq) {
if (modifier == UIKeyModifierCommand) {
_cmdModifierSequence = nil;
}
[_controlKeys setObject:@[] forKey:[NSNumber numberWithInteger:modifier]];
}
NSMutableArray *cmds = [NSMutableArray array];
NSString *charset;
NSString *shiftCharset = nil;
if (seq == TermViewCtrlSeq) {
charset = @"2345678qwertyuiopasdfghjklzxcvbnm[\\]^/_\t";
if (BKDefaults.grabCtrlSpace) {
charset = [charset stringByAppendingString:@" "];
}
} else if (seq == TermViewEscCtrlSeq) {
charset = @"2345678qwertyuiopasdfghjklzxcvbnm[\\]^/_\t ";
} else if (seq == TermViewEscSeq) {
shiftCharset = @"qwertyuiopasdfghjklzxcvbnm";
charset = [shiftCharset stringByAppendingString:@"1234567890`~!@#$%^&*()_=+[]{}\\|;':\",./<>?\t"];
} else if (seq == TermViewAutoRepeateSeq) {
charset = @"qwertyuiopasdfghjklzxcvbnm1234567890";
} else {
return;
}
// Cmd is default for iOS shortcuts, so we control whether or not we are re-mapping those ourselves.
if (modifier == UIKeyModifierCommand) {
_cmdModifierSequence = seq;
}
SEL action = NSSelectorFromString(seq);
[charset enumerateSubstringsInRange:NSMakeRange(0, charset.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[cmds addObject:[UIKeyCommand keyCommandWithInput:substring
modifierFlags:modifier
action:action]];
// Capture shift key presses to get transformed and not printed lowercase when CapsLock is Ctrl
if (modifier == UIKeyModifierAlphaShift) {
[cmds addObjectsFromArray:[self _shiftMaps]];
}
}];
if (shiftCharset) {
[[shiftCharset uppercaseString] enumerateSubstringsInRange:NSMakeRange(0, shiftCharset.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[cmds addObject:[UIKeyCommand keyCommandWithInput:substring
modifierFlags:modifier | UIKeyModifierShift
action:action]];
}];
}
[_controlKeys setObject:cmds forKey:@(modifier)];
}
- (void)_assignKey:(NSString *)key toModifier:(UIKeyModifierFlags)modifier
{
NSMutableArray *cmds = [[NSMutableArray alloc] init];
if (key == UIKeyInputEscape) {
[cmds addObject:[UIKeyCommand keyCommandWithInput:@""
modifierFlags:modifier action:@selector(escSeq:)]];
if (modifier == UIKeyModifierAlphaShift) {
[cmds addObjectsFromArray:[self _shiftMaps]];
}
[_functionKeys setObject:cmds forKey:[NSNumber numberWithInteger:modifier]];
} else {
[_functionKeys setObject:cmds forKey:[NSNumber numberWithInteger:modifier]];
}
}
- (NSArray *)_shiftMaps
{
NSMutableArray *cmds = [[NSMutableArray alloc] init];
NSString *charset = @"qwertyuiopasdfghjklzxcvbnm";
[charset enumerateSubstringsInRange:NSMakeRange(0, charset.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[cmds addObject:[UIKeyCommand keyCommandWithInput:substring
modifierFlags:UIKeyModifierShift
action:@selector(shiftSeq:)]];
}];
return cmds;
}
- (void)_assignFunction:(NSString *)function toTriggers:(UIKeyModifierFlags)triggers
{
// And Removing the Seq?
NSMutableArray *functions = [[NSMutableArray alloc] init];
SEL seq = NSSelectorFromString(function);
if (function == TermViewCursorFuncSeq) {
[functions addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:triggers action:seq]];
[functions addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:triggers action:seq]];
[functions addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:triggers action:seq]];
[functions addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:triggers action:seq]];
} else if (function == TermViewFFuncSeq) {
[_specialFKeysRow enumerateSubstringsInRange:NSMakeRange(0, [_specialFKeysRow length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[functions addObject:[UIKeyCommand keyCommandWithInput:substring
modifierFlags:triggers
action:@selector(fkeySeq:)]];