forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTerminal.m
230 lines (198 loc) · 7.75 KB
/
Terminal.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
//
// Terminal.m
// iSH
//
// Created by Theodore Dubois on 10/18/17.
//
#include <iconv.h>
#import "Terminal.h"
#import "DelayedUITask.h"
#import "UserPreferences.h"
#include "fs/tty.h"
@interface Terminal () <WKScriptMessageHandler>
@property WKWebView *webView;
@property (nonatomic) struct tty *tty;
@property NSMutableData *pendingData;
@property DelayedUITask *refreshTask;
@property DelayedUITask *scrollToBottomTask;
@end
@interface CustomWebView : WKWebView
@end
@implementation CustomWebView
- (BOOL)becomeFirstResponder {
return NO;
}
@end
@implementation Terminal
static Terminal *terminal = nil;
- (instancetype)init {
if (terminal)
return terminal;
if (self = [super init]) {
self.pendingData = [NSMutableData new];
self.refreshTask = [[DelayedUITask alloc] initWithTarget:self action:@selector(refresh)];
self.scrollToBottomTask = [[DelayedUITask alloc] initWithTarget:self action:@selector(scrollToBottom)];
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
[config.userContentController addScriptMessageHandler:self name:@"log"];
[config.userContentController addScriptMessageHandler:self name:@"resize"];
[config.userContentController addScriptMessageHandler:self name:@"selectionchange"];
self.webView = [[CustomWebView alloc] initWithFrame:CGRectZero configuration:config];
self.webView.scrollView.scrollEnabled = NO;
[self.webView loadRequest:
[NSURLRequest requestWithURL:
[NSBundle.mainBundle URLForResource:@"xterm-dist/term" withExtension:@"html"]]];
[self.webView addObserver:self forKeyPath:@"loading" options:0 context:NULL];
[self _addPreferenceObservers];
terminal = self;
}
return self;
}
- (void)setTty:(struct tty *)tty {
_tty = tty;
[self syncWindowSize];
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"log"]) {
NSLog(@"%@", message.body);
} else if ([message.name isEqualToString:@"resize"]) {
[self syncWindowSize];
}
}
- (void)syncWindowSize {
NSLog(@"syncing");
[self.webView evaluateJavaScript:@"[term.cols, term.rows]" completionHandler:^(NSArray<NSNumber *> *dimensions, NSError *error) {
if (self.tty == NULL) {
NSLog(@"gave up");
return;
}
int cols = dimensions[0].intValue;
int rows = dimensions[1].intValue;
NSLog(@"%dx%d", cols, rows);
lock(&self.tty->lock);
tty_set_winsize(self.tty, (struct winsize_) {.col = cols, .row = rows});
unlock(&self.tty->lock);
}];
}
- (size_t)write:(const void *)buf length:(size_t)len {
@synchronized (self) {
[self.pendingData appendData:[NSData dataWithBytes:buf length:len]];
[self.refreshTask schedule];
}
return len;
}
- (void)sendInput:(const char *)buf length:(size_t)len {
tty_input(self.tty, buf, len);
[self.scrollToBottomTask schedule];
}
- (void)scrollToBottom {
[self.webView evaluateJavaScript:@"term.scrollToBottom()" completionHandler:nil];
}
- (void)_addPreferenceObservers {
UserPreferences *prefs = [UserPreferences shared];
NSKeyValueObservingOptions opts = NSKeyValueObservingOptionNew;
[prefs addObserver:self forKeyPath:@"fontSize" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"theme" options:opts context:nil];
}
- (NSString *)cssColor:(UIColor *)color {
CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
return [NSString stringWithFormat:@"rgba(%ld, %ld, %ld, %ld)",
lround(red * 255), lround(green * 255), lround(blue * 255), lround(alpha * 255)];
}
- (void)_updateStyleFromPreferences {
UserPreferences *prefs = [UserPreferences shared];
id themeInfo = @{
@"fontSize": prefs.fontSize,
@"foregroundColor": [self cssColor:prefs.theme.foregroundColor],
@"backgroundColor": [self cssColor:prefs.theme.backgroundColor],
};
NSString *json = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:themeInfo options:0 error:nil] encoding:NSUTF8StringEncoding];
[self.webView evaluateJavaScript:[NSString stringWithFormat:@"updateStyle(%@)", json] completionHandler:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if (object == self.webView && [keyPath isEqualToString:@"loading"] && !self.webView.loading) {
[self _updateStyleFromPreferences];
[self.refreshTask schedule];
[self.webView removeObserver:self forKeyPath:@"loading"];
} else if (object == [UserPreferences shared]) {
[self _updateStyleFromPreferences];
}
}
NSData *removeInvalidUTF8(NSData *data) {
// character encoding hell
NSMutableData *cleanData = [NSMutableData dataWithLength:data.length];
iconv_t conv = iconv_open("UTF-8", "UTF-8");
BOOL yes = YES;
iconvctl(conv, ICONV_SET_DISCARD_ILSEQ, &yes);
const char *dataBytes = data.bytes;
char *cleanDataBytes = cleanData.mutableBytes;
size_t dataLength = data.length;
size_t cleanDataLength = cleanData.length;
iconv(conv, (char **) &dataBytes, &dataLength, &cleanDataBytes, &cleanDataLength);
iconv_close(conv);
cleanData.length -= cleanDataLength;
return cleanData;
}
- (void)refresh {
if (self.webView.loading)
return;
NSData *data;
@synchronized (self) {
data = self.pendingData;
self.pendingData = [NSMutableData new];
}
NSData *cleanData = removeInvalidUTF8(data);
NSString *str = [[NSString alloc] initWithData:cleanData encoding:NSUTF8StringEncoding];
if (str == nil) {
// dammit what to do.
str = @"[invalid utf8]";
}
NSError *err = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@[str] options:0 error:&err];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSAssert(err == nil, @"JSON serialization failed, wtf");
NSString *jsToEvaluate = [NSString stringWithFormat:@"term.write(%@[0])", json];
[self.webView evaluateJavaScript:jsToEvaluate completionHandler:nil];
}
+ (Terminal *)terminalWithType:(int)type number:(int)number {
return [Terminal new];
}
@end
static int ios_tty_open(struct tty *tty) {
Terminal *terminal = [Terminal terminalWithType:tty->type number:tty->num];
terminal.tty = tty;
tty->data = (void *) CFBridgingRetain(terminal);
// termios
tty->termios.lflags = ISIG_ | ICANON_ | ECHO_ | ECHOE_ | ECHOCTL_;
tty->termios.iflags = ICRNL_;
tty->termios.oflags = OPOST_ | ONLCR_;
tty->termios.cc[VINTR_] = '\x03';
tty->termios.cc[VQUIT_] = '\x1c';
tty->termios.cc[VERASE_] = '\x7f';
tty->termios.cc[VKILL_] = '\x15';
tty->termios.cc[VEOF_] = '\x04';
tty->termios.cc[VTIME_] = 0;
tty->termios.cc[VMIN_] = 1;
tty->termios.cc[VSTART_] = '\x11';
tty->termios.cc[VSTOP_] = '\x13';
tty->termios.cc[VSUSP_] = '\x1a';
tty->termios.cc[VEOL_] = 0;
tty->termios.cc[VREPRINT_] = '\x12';
tty->termios.cc[VDISCARD_] = '\x0f';
tty->termios.cc[VWERASE_] = '\x17';
tty->termios.cc[VLNEXT_] = '\x16';
tty->termios.cc[VEOL2_] = 0;
return 0;
}
static ssize_t ios_tty_write(struct tty *tty, const void *buf, size_t len) {
Terminal *terminal = (__bridge Terminal *) tty->data;
return [terminal write:buf length:len];
}
static void ios_tty_close(struct tty *tty) {
CFBridgingRelease(tty->data);
}
struct tty_driver ios_tty_driver = {
.open = ios_tty_open,
.write = ios_tty_write,
.close = ios_tty_close,
};