forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TermView.m
470 lines (368 loc) · 12.7 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
////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2018 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 "TermView.h"
#import "BKDefaults.h"
#import "BKSettingsNotifications.h"
#import "BKFont.h"
#import "BKTheme.h"
#import "TermJS.h"
@implementation BKWebView
- (BOOL)canResignFirstResponder
{
return NO;
}
- (BOOL)becomeFirstResponder
{
return NO;
}
- (void)_keyboardDidChangeFrame:(id)sender
{
}
- (void)_keyboardWillChangeFrame:(id)sender
{
}
- (void)_keyboardWillShow:(id)sender
{
}
- (void)_keyboardWillHide:(id)sender
{
}
@end
@interface TermView () <UIGestureRecognizerDelegate, WKScriptMessageHandler>
@end
@implementation TermView {
WKWebView *_webView;
BOOL _focused;
BOOL _jsIsBusy;
dispatch_queue_t _jsQueue;
NSMutableString *_jsBuffer;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_jsQueue = dispatch_queue_create(@"TermView.js".UTF8String, DISPATCH_QUEUE_SERIAL);
_jsBuffer = [[NSMutableString alloc] init];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self _addWebView];
}
return self;
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
}
- (BOOL)canBecomeFirstResponder {
return NO;
}
- (void)_addWebView
{
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.selectionGranularity = WKSelectionGranularityCharacter;
[configuration.userContentController addScriptMessageHandler:self name:@"interOp"];
_webView = [[BKWebView alloc] initWithFrame:self.bounds configuration:configuration];
_webView.scrollView.delaysContentTouches = NO;
_webView.scrollView.canCancelContentTouches = NO;
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.panGestureRecognizer.enabled = NO;
self.opaque = NO;
_webView.opaque = NO;
self.alpha = 0;
_webView.alpha = 0;
_webView.backgroundColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_webView];
}
- (NSString *)title
{
return _webView.title;
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
_webView.backgroundColor = backgroundColor;
_webView.alpha = 1;
self.opaque = YES;
_webView.opaque = YES;
}
- (void)loadWith:(MCPSessionParameters *)params;
{
[_webView.configuration.userContentController addUserScript:[self _termInitScriptWith:params]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"term" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
}
- (void)reloadWith:(MCPSessionParameters *)params;
{
[_webView.configuration.userContentController removeAllUserScripts];
[_webView.configuration.userContentController addUserScript:[self _termInitScriptWith:params]];
[_webView reload];
}
- (void)setWidth:(NSInteger)count
{
[_webView evaluateJavaScript:term_setWidth(count) completionHandler:nil];
}
- (void)setFontSize:(NSNumber *)newSize
{
[_webView evaluateJavaScript:term_setFontSize(newSize) completionHandler:nil];
}
- (void)clear
{
[_webView evaluateJavaScript:term_clear() completionHandler:nil];
}
- (void)cleanSelection
{
[_webView evaluateJavaScript:term_cleanSelection() completionHandler:nil];
}
- (void)setCursorBlink:(BOOL)state
{
[_webView evaluateJavaScript:term_setCursorBlink(state) completionHandler:nil];
}
- (void)setBoldAsBright:(BOOL)state
{
[_webView evaluateJavaScript:term_setBoldAsBright(state) completionHandler:nil];
}
- (void)setBoldEnabled:(NSUInteger)state
{
[_webView evaluateJavaScript:term_setBoldEnabled(state) completionHandler:nil];
}
- (void)reset
{
[_webView evaluateJavaScript:term_reset() completionHandler:nil];
}
- (void)increaseFontSize
{
[_webView evaluateJavaScript:term_increaseFontSize() completionHandler:nil];
}
- (void)decreaseFontSize
{
[_webView evaluateJavaScript:term_decreaseFontSize() completionHandler:nil];
}
- (void)resetFontSize
{
[_webView evaluateJavaScript:term_resetFontSize() completionHandler:nil];
}
- (void)focus {
_focused = YES;
[_webView evaluateJavaScript:term_focus() completionHandler:nil];
}
- (void)blur
{
_focused = NO;
[_webView evaluateJavaScript:term_blur() completionHandler:nil];
}
// Write data to terminal control
- (void)write:(NSString *)data
{
dispatch_async(_jsQueue, ^{
[_jsBuffer appendString:data];
if (_jsIsBusy) {
return;
}
_jsIsBusy = YES;
NSString * buffer = _jsBuffer;
_jsBuffer = [[NSMutableString alloc] init];
NSString *jsScript = term_write(buffer);
dispatch_async(dispatch_get_main_queue(), ^{
[_webView evaluateJavaScript: jsScript completionHandler:^(id result, NSError *error) {
dispatch_async(_jsQueue, ^{
_jsIsBusy = NO;
if (_jsBuffer.length > 0) {
[self write:@""];
}
});
}];
});
});
}
// 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:@"selectionchange"]) {
[self _handleSelectionChange:data];
} else if ([operation isEqualToString:@"sigwinch"]) {
if ([_termDelegate respondsToSelector:@selector(updateTermRows:Cols:)]) {
[_termDelegate updateTermRows:data[@"rows"] Cols:data[@"cols"]];
}
} else if ([operation isEqualToString:@"terminalReady"]) {
self.alpha = 1;
if ([_termDelegate respondsToSelector:@selector(terminalIsReady:)]) {
[_termDelegate terminalIsReady:data];
if (_focused) {
[self focus];
} else {
[self blur];
}
}
} else if ([operation isEqualToString:@"fontSizeChanged"]) {
if ([_termDelegate respondsToSelector:@selector(fontSizeChanged:)]) {
[_termDelegate fontSizeChanged:data[@"size"]];
}
} else if ([operation isEqualToString:@"copy"]) {
[[UIPasteboard generalPasteboard] setString:data[@"content"]];
} else if ([operation isEqualToString:@"sendString"]) {
[_termDelegate write:data[@"string"]];
}
}
- (void)_handleSelectionChange:(NSDictionary *)data
{
_selectedText = data[@"text"];
_hasSelection = _selectedText.length > 0;
if (!_hasSelection) {
return;
}
NSMutableArray *items = [[NSMutableArray alloc] init];
UIMenuController * menu = [UIMenuController sharedMenuController];
[items addObject:[[UIMenuItem alloc] initWithTitle:@"Paste selection"
action:@selector(pasteSelection:)]];
_detectedLink = [self _detectLinkInSelection:data];
if (_detectedLink) {
NSString *host = [_detectedLink host];
[items addObject:[[UIMenuItem alloc] initWithTitle:[@"Copy " stringByAppendingString:host]
action:@selector(copyLink:)]];
[items addObject:[[UIMenuItem alloc] initWithTitle:[@"Open " stringByAppendingString:host]
action:@selector(openLink:)]];
}
CGRect rect = CGRectFromString(data[@"rect"]);
[menu setMenuItems:items];
[menu setTargetRect:rect inView:self];
[menu setMenuVisible:YES animated:NO];
}
- (void)modifySideOfSelection
{
[_webView evaluateJavaScript:term_modifySideSelection() completionHandler:nil];
}
- (void)modifySelectionInDirection:(NSString *)direction granularity:(NSString *)granularity
{
[_webView evaluateJavaScript:term_modifySelection(direction, granularity) completionHandler:nil];
}
- (NSURL *)_detectLinkInSelection:(NSDictionary *)data
{
__block NSURL *result = nil;
NSString *text = data[@"base"];
NSInteger offset = [data[@"offset"] integerValue];
if (text == nil || [text length] == 0) {
return nil;
}
NSDataDetector * dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
[dataDetector enumerateMatchesInString:text options:kNilOptions range:NSMakeRange(0, [text length])
usingBlock:^(NSTextCheckingResult * _Nullable res, NSMatchingFlags flags, BOOL * _Nonnull stop) {
if (res == nil) {
return;
}
NSURL *url = res.URL;
if (url && res.range.location <= offset && res.range.location + res.range.length >= offset) {
result = url;
*stop = YES;
}
}];
return result;
}
// just to remove warning in selector
- (void)copyLink:(id)sender
{
}
- (void)openLink:(id)sender
{
}
- (void)yank:(id)sender
{
}
- (void)pasteSelection:(id)sender
{
}
- (void)copy:(id)sender
{
[_webView copy:sender];
}
- (NSString *)_detectFontFamilyFromContent:(NSString *)content
{
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"font-family:\\s*(.+);"
options:NSRegularExpressionCaseInsensitive
error:nil];
__block NSString *result = nil;
[regex enumerateMatchesInString:content
options:0
range:NSMakeRange(0, [content length])
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
{
if (match && match.numberOfRanges == 2) {
result = [content substringWithRange:[match rangeAtIndex:1]];
}
*stop = YES;
}];
return result;
}
- (WKUserScript *)_termInitScriptWith:(MCPSessionParameters *)params;
{
NSMutableArray *script = [[NSMutableArray alloc] init];
BKFont *font = [BKFont withName: params.fontName ?: [BKDefaults selectedFontName]];
NSString *fontFamily = font.name;
NSString *content = font.content;
if (font && font.isCustom && content) {
[script addObject:term_appendUserCss(content)];
fontFamily = [self _detectFontFamilyFromContent:content] ?: font.name;
}
[script addObject:@"function applyUserSettings() {"];
{
if (fontFamily) {
[script addObject: term_setFontFamily(fontFamily)];
}
[script addObject:term_setBoldEnabled(params.enableBold)];
[script addObject:term_setBoldAsBright(params.boldAsBright)];
BKTheme *theme = [BKTheme withName: params.themeName ?: [BKDefaults selectedThemeName]];
if (theme) {
[script addObject:theme.content];
}
[script addObject:term_setFontSize(params.fontSize == 0 ? [BKDefaults selectedFontSize] : @(params.fontSize))];
[script addObject: term_setCursorBlink([BKDefaults isCursorBlink])];
}
[script addObject:@"};"];
[script addObject:term_init()];
return [[WKUserScript alloc] initWithSource:
[script componentsJoinedByString:@"\n"]
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:YES];
}
- (void)terminate
{
// Disconnect message handler
[_webView.configuration.userContentController removeScriptMessageHandlerForName:@"interOp"];
}
@end