forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTermView.m
942 lines (807 loc) · 28.9 KB
/
TermView.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
////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016 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>.
//
////////////////////////////////////////////////////////////////////////////////
#include <sys/ioctl.h>
#import "SmartKeysController.h"
#import "SmartKeysView.h"
#import "TermView.h"
#import "BKUserConfigurationManager.h"
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 TermViewCursorFuncSeq = @"cursorSeq:";
NSString *const TermViewFFuncSeq = @"fkeySeq:";
NSString *const TermViewAutoRepeateSeq = @"autoRepeatSeq:";
@interface CC : NSObject
+ (void)initialize;
+ (NSString *)CTRL:(NSString *)c;
+ (NSString *)ESC:(NSString *)c;
+ (NSString *)KEY:(NSString *)c;
@end
@implementation CC
+ (void)initialize
{
CTRLCodes = @{
@" " : @"\x00",
@"[" : @"\x1B",
@"]" : @"\x1D",
@"\\" : @"\x1C",
@"^" : @"\x1E",
@"_" : @"\x1F"
};
FModifiers = @{
@0 : @0,
[NSNumber numberWithInt:UIKeyModifierShift] : @2,
[NSNumber numberWithInt:UIKeyModifierAlternate] : @3,
[NSNumber numberWithInt:UIKeyModifierShift | UIKeyModifierAlternate] : @4,
[NSNumber numberWithInt:UIKeyModifierControl] : @5,
[NSNumber numberWithInt:UIKeyModifierShift | UIKeyModifierControl] : @6,
[NSNumber numberWithInt:UIKeyModifierAlternate | UIKeyModifierControl] : @7,
[NSNumber numberWithInt: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];
return [NSString stringWithFormat:@"%c", x - 'a' + 1];
}
}
+ (NSString *)ESC:(NSString *)c
{
if (c == nil || [c length] == 0 || c == UIKeyInputEscape) {
return @"\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"]) {
return @"\r";
}
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:
case 11:
case 12:
return [NSString stringWithFormat:@"%@2%ld~", CSI, number - 9];
default:
return nil;
}
}
@end
@interface TermView () <UIKeyInput, UIGestureRecognizerDelegate, WKScriptMessageHandler>
@property UITapGestureRecognizer *tapBackground;
@property UILongPressGestureRecognizer *longPressBackground;
@property UIPinchGestureRecognizer *pinchGesture;
@end
@implementation TermView {
WKWebView *_webView;
// option + e on iOS lets introduce an accented character, that we override
BOOL _disableAccents;
BOOL _dismissInput;
BOOL _pasteMenu;
NSMutableArray<UIKeyCommand *> *_kbdCommands;
SmartKeysController *_smartKeys;
UIView *cover;
NSTimer *_pinchSamplingTimer;
BOOL _raw;
BOOL _inputEnabled;
BOOL _cmdAsModifier;
NSMutableDictionary *_controlKeys;
NSMutableDictionary *_functionKeys;
NSMutableDictionary *_functionTriggerKeys;
NSString *_specialFKeysRow;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_inputEnabled = YES;
self.inputAssistantItem.leadingBarButtonGroups = @[];
self.inputAssistantItem.trailingBarButtonGroups = @[];
[self addWebView];
[self resetDefaultControlKeys];
}
return self;
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
if (self.window && self.window.screen == [UIScreen mainScreen]) {
[self addGestures];
[self configureNotifications];
}
}
- (void)addWebView
{
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration.userContentController addScriptMessageHandler:self name:@"interOp"];
_webView = [[WKWebView alloc] initWithFrame:self.frame configuration:configuration];
[self addSubview:_webView];
_webView.opaque = NO;
_webView.translatesAutoresizingMaskIntoConstraints = NO;
[_webView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
[_webView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
[_webView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
[_webView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
}
- (void)addGestures
{
if (!_tapBackground) {
_tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(activeControl:)];
[_tapBackground setNumberOfTapsRequired:1];
_tapBackground.delegate = self;
[self addGestureRecognizer:_tapBackground];
}
if (!_longPressBackground) {
_longPressBackground = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
_longPressBackground.delegate = self;
[self addGestureRecognizer:_longPressBackground];
}
if (!_pinchGesture) {
_pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
_pinchGesture.delegate = self;
[self addGestureRecognizer:_pinchGesture];
}
}
- (void)configureNotifications
{
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver:self];
[defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)resetDefaultControlKeys
{
_controlKeys = [[NSMutableDictionary alloc] init];
_functionKeys = [[NSMutableDictionary alloc] init];
_functionTriggerKeys = [[NSMutableDictionary alloc] init];
_specialFKeysRow = @"1234567890";
[self setKbdCommands];
}
#pragma mark Terminal Control
- (void)setScrollEnabled:(BOOL)scroll
{
[_webView.scrollView setScrollEnabled:NO];
}
- (void)setRawMode:(BOOL)raw
{
_raw = raw;
}
- (BOOL)rawMode
{
return _raw;
}
- (void)setInputEnabled:(BOOL)enabled
{
_inputEnabled = enabled;
if (!enabled && self.isFirstResponder) {
[self resignFirstResponder];
}
}
- (void)setColumnNumber:(NSInteger)count
{
[_webView evaluateJavaScript:[NSString stringWithFormat:@"setWidth(\"%ld\");", (long)count] completionHandler:nil];
}
- (void)setFontSize:(NSNumber *)newSize
{
[_webView evaluateJavaScript:[NSString stringWithFormat:@"setFontSize(\"%@\");", newSize] completionHandler:nil];
}
- (void)clear
{
[_webView evaluateJavaScript:[NSString stringWithFormat:@"clear();"] completionHandler:nil];
}
- (void)loadTerminal
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"term" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
// NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
}
// Write data to terminal control
- (void)write:(NSString *)data
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@[ data ] options:0 error:nil];
NSString *jsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *jsScript = [NSString stringWithFormat:@"write_to_term(%@[0])", jsString];
[_webView evaluateJavaScript:jsScript completionHandler:nil];
}
- (NSString *)title
{
return _webView.title;
}
// Since TermView is a WKScriptMessageHandler, it must implement the userContentController:didReceiveScriptMessage method. This is the method that is triggered each time 'interOp' is sent a message from the JavaScript code.
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
NSDictionary *sentData = (NSDictionary *)message.body;
NSString *operation = sentData[@"op"];
NSDictionary *data = sentData[@"data"];
if ([operation isEqualToString:@"sigwinch"]) {
if ([self.delegate respondsToSelector:@selector(updateTermRows:Cols:)]) {
self.rowCount = (int)[data[@"rows"]integerValue];
self.columnCount = (int)[data[@"columns"]integerValue];
[self.delegate updateTermRows:data[@"rows"] Cols:data[@"columns"]];
}
} else if ([operation isEqualToString:@"terminalReady"]) {
if ([self.delegate respondsToSelector:@selector(terminalIsReady)]) {
[self.delegate terminalIsReady];
}
} else if ([operation isEqualToString:@"fontSizeChanged"]) {
if ([self.delegate respondsToSelector:@selector(fontSizeChanged:)]) {
[self.delegate fontSizeChanged:data[@"size"]];
}
} else if ([operation isEqualToString:@"copy"]) {
[[UIPasteboard generalPasteboard] setString:data[@"content"]];
}
}
#pragma mark On-Screen keyboard - UIKeyInput
- (UIKeyboardAppearance)keyboardAppearance
{
return UIKeyboardAppearanceDark;
}
- (UITextAutocorrectionType)autocorrectionType
{
return UITextAutocorrectionTypeNo;
}
- (UIView *)inputAccessoryView
{
return [_smartKeys view];
}
- (void)keyboardWillHide:(NSNotification *)sender
{
// Always hide the AccessoryView.
self.inputAccessoryView.hidden = YES;
//_capsMapped = YES;
// If keyboard hides, then become first responder. This ensures there is a responder for the long focus events.
//[self becomeFirstResponder];
}
- (void)keyboardWillShow:(NSNotification *)sender
{
UIView *iaView = self.inputAccessoryView;
CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// Not needed, since iOS 8.0 respects the coordinates.
//CGRect inViewFrame = [self.view convertRect:frame fromView:nil];
CGRect bounds = self.bounds;
CGRect intersection = CGRectIntersection(frame, bounds);
// If the intersection is only the accesoryView, we have a external keyboard
if (intersection.size.height == [iaView frame].size.height) {
if ([BKUserConfigurationManager userSettingsValueForKey:BKUserConfigShowSmartKeysWithXKeyBoard]) {
iaView.hidden = NO;
} else {
iaView.hidden = YES;
}
} else {
//_capsMapped = NO;
iaView.hidden = NO;
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer == self.pinchGesture && [otherGestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
return YES;
}
return NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer == self.tapBackground && [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
// We cancel the one from the WebView from executing, as it will wait for this one to fail.
// We return yes, to make sure that is understood.
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return YES;
}
if (gestureRecognizer == self.longPressBackground && [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
return YES;
}
return NO;
}
- (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
[_webView becomeFirstResponder];
}
}
- (void)activeControl:(UITapGestureRecognizer *)gestureRecognizer
{
if (![self isFirstResponder]) {
[self becomeFirstResponder];
} else {
if (_pasteMenu) {
[[UIMenuController sharedMenuController]
setMenuVisible:NO
animated:YES];
} else {
[[UIMenuController sharedMenuController] setTargetRect:self.frame
inView:self];
UIMenuItem *pasteItem = [[UIMenuItem alloc] initWithTitle:@"Paste"
action:@selector(yank:)];
[[UIMenuController sharedMenuController]
setMenuItems:@[ pasteItem ]];
[[UIMenuController sharedMenuController]
setMenuVisible:YES
animated:YES];
}
_pasteMenu = !_pasteMenu;
}
}
- (void)handlePinch:(UIPinchGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
//_lastPinchScale = _webView.scrollView.zoomScale;
[_webView evaluateJavaScript:@"scaleTermStart();" completionHandler:nil];
if (_pinchSamplingTimer)
[_pinchSamplingTimer invalidate];
_pinchSamplingTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(pinchSampling:) userInfo:nil repeats:YES];
[_pinchSamplingTimer fire];
}
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
[_pinchSamplingTimer invalidate];
}
}
- (void)pinchSampling:(NSTimer *)timer
{
[_webView evaluateJavaScript:[NSString stringWithFormat:@"scaleTerm(%f);", _pinchGesture.scale] completionHandler:nil];
}
- (BOOL)canBecomeFirstResponder
{
if (!_inputEnabled) {
return NO;
}
return YES;
}
- (BOOL)canResignFirstResponder
{
return YES;
}
- (BOOL)becomeFirstResponder
{
if (!_smartKeys) {
_smartKeys = [[SmartKeysController alloc] init];
}
_smartKeys.textInputDelegate = self;
cover.hidden = YES;
[_webView evaluateJavaScript:@"focusTerm();" completionHandler:nil];
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder
{
[_webView evaluateJavaScript:@"blurTerm();" completionHandler:nil];
return [super resignFirstResponder];
}
- (BOOL)hasText
{
return YES;
}
- (void)deleteBackward
{
// Send a delete backward key to the buffer
[_delegate write:@"\x7f"];
}
- (void)insertText:(NSString *)text
{
if (_disableAccents) {
// If the accent switch is on, the next character should remove them.
//CFStringTransform((__bridge CFMutableStringRef)mtext, nil, kCFStringTransformStripCombiningMarks, NO);
text = [[NSString alloc] initWithData:[text dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding];
_disableAccents = NO;
}
// Discard CAPS on characters when caps are mapped and there is no SW keyboard.
BOOL capsWithoutSWKeyboard = [self capsMapped] & self.inputAccessoryView.hidden;
if (capsWithoutSWKeyboard && text.length == 1 && [text characterAtIndex:0] > 0x1F) {
text = [text lowercaseString];
}
// 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)];
[_delegate write:[CC FKEY:[value integerValue]]];
} else {
[_delegate write:[CC KEY:text MOD:0 RAW:_raw]];
}
} else {
NSUInteger modifiers = [[_smartKeys view] modifiers];
if (modifiers & KbdCtrlModifier) {
[_delegate write:[CC CTRL:text]];
} else if (modifiers & KbdAltModifier) {
[_delegate write:[CC ESC:text]];
} else {
[_delegate write:[CC KEY:text MOD:0 RAW:_raw]];
}
}
}
- (void)loadTerminalThemeJS:(NSString *)themeContent
{
[_webView evaluateJavaScript:themeContent completionHandler:nil];
}
- (void)loadTerminalFont:(NSString *)familyName fromCSS:(NSString *)cssPath
{
[_webView evaluateJavaScript:[NSString stringWithFormat:@"loadFontFromCSS(\"%@\", \"%@\");", cssPath, familyName] completionHandler:nil];
}
- (void)loadTerminalFont:(NSString *)familyName cssFontContent:(NSString *)cssContent
{
cssContent = [NSString stringWithFormat:@"data:text/css;utf-8,%@", cssContent];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@[ cssContent ] options:0 error:nil];
NSString *jsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *jsScript = [NSString stringWithFormat:@"loadFontFromCSS(%@[0], \"%@\")", jsString, familyName];
[_webView evaluateJavaScript:jsScript completionHandler:nil];
}
- (void)setCursorBlink:(BOOL)state
{
NSString *jsScript = [NSString stringWithFormat:@"setCursorBlink(%@)", state ? @"true" : @"false"];
[_webView evaluateJavaScript:jsScript completionHandler:nil];
}
- (void)reset
{
[_webView evaluateJavaScript:@"reset" completionHandler:nil];
}
#pragma mark External Keyboard
- (void)setKbdCommands
{
_kbdCommands = [NSMutableArray array];
[_kbdCommands addObjectsFromArray:self.presetShortcuts];
for (NSNumber *modifier in _controlKeys.allKeys) {
[_kbdCommands addObjectsFromArray:_controlKeys[modifier]];
}
for (NSNumber *modifier in _functionKeys.allKeys) {
[_kbdCommands addObjectsFromArray:_functionKeys[modifier]];
}
for (NSNumber *modifier in _functionTriggerKeys.allKeys) {
[_kbdCommands addObjectsFromArray:_functionTriggerKeys[modifier]];
}
[_kbdCommands addObjectsFromArray:self.functionModifierKeys];
}
- (void)assignSequence:(NSString *)seq toModifier:(UIKeyModifierFlags)modifier
{
if (seq) {
NSMutableArray *cmds = [NSMutableArray array];
NSString *charset;
if (seq == TermViewCtrlSeq) {
charset = @"qwertyuiopasdfghjklzxcvbnm[\\]^_ ";
} else if (seq == TermViewEscSeq) {
charset = @"qwertyuiopasdfghjklzxcvbnm1234567890`~-=_+[]\{}|;':\",./<>?/";
} 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) {
_cmdAsModifier = YES;
}
NSUInteger length = charset.length;
unichar buffer[length + 1];
[charset getCharacters:buffer range:NSMakeRange(0, length)];
[charset enumerateSubstringsInRange:NSMakeRange(0, length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[cmds addObject:[UIKeyCommand keyCommandWithInput:substring
modifierFlags:modifier
action:NSSelectorFromString(seq)]];
// Capture shift key presses to get transformed and not printed lowercase when CapsLock is Ctrl
if (modifier == UIKeyModifierAlphaShift) {
[cmds addObjectsFromArray:[self shiftMaps]];
}
}];
[_controlKeys setObject:cmds forKey:[NSNumber numberWithInteger:modifier]];
} else {
if (modifier == UIKeyModifierCommand) {
_cmdAsModifier = NO;
}
[_controlKeys setObject:@[] forKey:[NSNumber numberWithInteger:modifier]];
}
[self setKbdCommands];
}
- (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]];
}
[self setKbdCommands];
}
- (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:)]];
}];
}
[_functionTriggerKeys setObject:functions forKey:function];
[self setKbdCommands];
}
- (NSArray *)presetShortcuts
{
return @[ [UIKeyCommand keyCommandWithInput:@"+"
modifierFlags:[BKUserConfigurationManager shortCutModifierFlags]
action:@selector(increaseFontSize:)
discoverabilityTitle:@"Zoom In"],
[UIKeyCommand keyCommandWithInput:@"-"
modifierFlags:[BKUserConfigurationManager shortCutModifierFlags]
action:@selector(decreaseFontSize:)
discoverabilityTitle:@"Zoom Out"],
[UIKeyCommand keyCommandWithInput:@"="
modifierFlags:[BKUserConfigurationManager shortCutModifierFlags]
action:@selector(resetFontSize:)
discoverabilityTitle:@"Reset Zoom"],
[UIKeyCommand keyCommandWithInput: @"v" modifierFlags: [BKUserConfigurationManager shortCutModifierFlags]
action: @selector(yank:)
discoverabilityTitle: @"Paste"]];
}
- (NSArray *)functionModifierKeys
{
NSMutableArray *f = [NSMutableArray array];
for (NSNumber *modifier in [CC FModifiers]) {
[f addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:modifier.intValue action:@selector(arrowSeq:)]];
[f addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:modifier.intValue action:@selector(arrowSeq:)]];
[f addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:modifier.intValue action:@selector(arrowSeq:)]];
[f addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:modifier.intValue action:@selector(arrowSeq:)]];
}
[f addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputEscape modifierFlags:0 action:@selector(escSeq:)]];
return f;
}
- (NSArray<UIKeyCommand *> *)keyCommands
{
return _kbdCommands;
}
- (BOOL)capsMapped
{
return ([[_controlKeys objectForKey:[NSNumber numberWithInteger:UIKeyModifierAlphaShift]] count] ||
[[_functionKeys objectForKey:[NSNumber numberWithInteger:UIKeyModifierAlphaShift]] count]);
}
- (void)yank:(id)sender
{
NSString *str = [UIPasteboard generalPasteboard].string;
if (str) {
[_delegate write:str];
}
}
- (void)increaseFontSize:(UIKeyCommand *)cmd
{
[_webView evaluateJavaScript:@"increaseTermFontSize();" completionHandler:nil];
}
- (void)decreaseFontSize:(UIKeyCommand *)cmd
{
[_webView evaluateJavaScript:@"decreaseTermFontSize();" completionHandler:nil];
}
- (void)resetFontSize:(UIKeyCommand *)cmd
{
[_webView evaluateJavaScript:@"resetTermFontSize();" completionHandler:nil];
}
- (void)escSeq:(UIKeyCommand *)cmd
{
[_delegate write:[CC ESC:cmd.input]];
}
- (void)arrowSeq:(UIKeyCommand *)cmd
{
[_delegate write:[CC KEY:cmd.input MOD:cmd.modifierFlags RAW:_raw]];
}
// Shift prints uppercase in the case CAPSLOCK is blocked
- (void)shiftSeq:(UIKeyCommand *)cmd
{
if ([cmd.input length] == 0) {
return;
} else {
[_delegate write:[cmd.input uppercaseString]];
}
}
- (void)ctrlSeq:(UIKeyCommand *)cmd
{
[_delegate write:[CC CTRL:cmd.input]];
}
- (void)metaSeq:(UIKeyCommand *)cmd
{
if ([cmd.input isEqual:@"e"]) {
//_disableAccents = YES;
}
[_delegate write:[CC ESC:cmd.input]];
}
- (void)cursorSeq:(UIKeyCommand *)cmd
{
if (cmd.input == UIKeyInputUpArrow) {
[_delegate write:[CC KEY:SpecialCursorKeyPgUp MOD:0 RAW:_raw]];
}
if (cmd.input == UIKeyInputDownArrow) {
[_delegate write:[CC KEY:SpecialCursorKeyPgDown MOD:0 RAW:_raw]];
}
if (cmd.input == UIKeyInputLeftArrow) {
[_delegate write:[CC KEY:SpecialCursorKeyHome MOD:0 RAW:_raw]];
}
if (cmd.input == UIKeyInputRightArrow) {
[_delegate write:[CC KEY:SpecialCursorKeyEnd MOD:0 RAW:_raw]];
}
}
- (void)fkeySeq:(UIKeyCommand *)cmd
{
NSInteger value = [cmd.input integerValue];
if (value == 0) {
[_delegate write:[CC FKEY:10]];
} else {
[_delegate write:[CC FKEY:value]];
}
}
- (void)autoRepeatSeq:(id)sender
{
UIKeyCommand *command = (UIKeyCommand*)sender;
[_delegate write:command.input];
}
// 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 ([sender isKindOfClass:[UIMenuController class]]) {
// [_webView copy:sender];
// } else {
[_delegate write:[CC CTRL:@"c"]];
// }
}
// Cmd+x
- (void)cut:(id)sender
{
[_delegate write:[CC CTRL:@"x"]];
}
// Cmd+v
- (void)paste:(id)sender
{
if ([sender isKindOfClass:[UIMenuController class]] || !_cmdAsModifier) {
[self yank:sender];
} else {
[_delegate write:[CC CTRL:@"v"]];
}
}
// Cmd+a
- (void)selectAll:(id)sender
{
[_delegate write:[CC CTRL:@"a"]];
}
// Cmd+b
- (void)toggleBoldface:(id)sender
{
[_delegate write:[CC CTRL:@"b"]];
}
// Cmd+i
- (void)toggleItalics:(id)sender
{
[_delegate write:[CC CTRL:@"i"]];
}
// Cmd+u
- (void)toggleUnderline:(id)sender
{
[_delegate write:[CC CTRL:@"u"]];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ([sender isKindOfClass:[UIMenuController class]]) {
// The menu can only perform paste methods
if (action == @selector(paste:)) {
return YES;
}
return NO;
}
return [super canPerformAction:action withSender:sender];
}
@end