forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TermDevice.m
338 lines (281 loc) · 8.15 KB
/
TermDevice.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
////////////////////////////////////////////////////////////////////////////////
//
// 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>.
//
////////////////////////////////////////////////////////////////////////////////
#import "TermDevice.h"
static int __sizeOfIncompleteSequenceAtTheEnd(const char *buffer, size_t len) {
// Find the first UTF mark and compare with the iterator.
int i = 1;
size_t count = ((len >= 3) ? 3 : len);
for (; i <= count; i++) {
unsigned char c = buffer[len - i];
if (i == 1 && (c & 0x80) == 0) {
// Single simple character, all good
return 0;
}
// 10XXX XXXX
if (c >> 6 == 0x02) {
continue;
}
// Check if the character corresponds to the sequence by ORing with it
if ((i == 2 && ((c | 0xDF) == 0xDF)) || // 110X XXXX 1 1101 1111
(i == 3 && ((c | 0xEF) == 0xEF)) || // 1110 XXXX 2 1110 1111
(i == 4 && ((c | 0xF7) == 0xF7))) { // 1111 0XXX 3 1111 0111
// Complete sequence
return 0;
} else {
return i;
}
}
return 0;
}
@interface ViewStream: NSObject
@property TermView *view;
@end
@implementation ViewStream {
dispatch_data_t _splitChar;
dispatch_io_t _channel;
}
- (instancetype) initWithQueue:(dispatch_queue_t) queue fd:(dispatch_fd_t)fd
{
if (self = [super init]) {
_channel = dispatch_io_create(DISPATCH_IO_STREAM, fd, queue,
^(int error) {
if (error) {
printf("Error creating channel");
}
});
dispatch_io_set_low_water(_channel, 1);
dispatch_io_read(_channel, 0, SIZE_MAX, queue,
^(bool done, dispatch_data_t data, int error) {
[self _processStream:data];
});
}
return self;
}
- (void)_processStream:(dispatch_data_t)data
{
if (_splitChar) {
data = dispatch_data_create_concat(_splitChar, data);
_splitChar = nil;
}
NSData * nsData = (NSData *)data;
NSString *output = [[NSString alloc] initWithData:nsData encoding:NSUTF8StringEncoding];
// Best case. We got good utf8 seq.
if (output) {
[_view write:output];
return;
}
// May be we have incomplete utf8 seq at the end;
const char *buffer = [nsData bytes];
size_t len = nsData.length;
int incompleteSize = __sizeOfIncompleteSequenceAtTheEnd(buffer, len);
if (incompleteSize == 0) {
// No, we didn't find any incomplete seq at the end.
// We have wrong seq in the middle. Pass base64 data. JS will heal it.
[_view writeB64:nsData];
return;
}
// Save splitted sequences
_splitChar = dispatch_data_create_subrange(data, len - incompleteSize, incompleteSize);
// We stripped incomplete seq.
// Let's try to create string again with range
output = [[NSString alloc] initWithBytes:buffer length:len - incompleteSize encoding:NSUTF8StringEncoding];
if (output) {
// Good seq. Write it as string.
[_view write:output];
return;
}
// Nope, fallback to base64
[_view writeB64:[nsData subdataWithRange:NSMakeRange(0, len - incompleteSize)]];
}
- (void) close {
dispatch_io_close(_channel, DISPATCH_IO_STOP);
}
@end
@interface TermDevice () <TermViewDeviceProtocol>
@end
// The TermStream is the PTYDevice
// They might actually be different. The Device listens, the stream is lower level.
// The PTY never listens. The Device or Wigdget is a way to indicate that
@implementation TermDevice {
// Initialized from stream, and make the stream duplicate itself.
// The stream then has access to the "device" or "widget"
// The Widget then has functions to read from the stream and pass it.
int _pinput[2];
int _poutput[2];
int _perror[2];
dispatch_queue_t _queue;
ViewStream *_outStream;
ViewStream *_errStream;
}
- (id)init
{
self = [super init];
if (self) {
pipe(_pinput);
pipe(_poutput);
pipe(_perror);
// TODO: Change the interface
// Initialize on the stream
_stream = [[TermStream alloc] init];
_stream.in = fdopen(_pinput[0], "r");
_stream.out = fdopen(_poutput[1], "w");
_stream.err = fdopen(_perror[1], "w");
setvbuf(_stream.out, NULL, _IONBF, 0);
setvbuf(_stream.err, NULL, _IONBF, 0);
setvbuf(_stream.in, NULL, _IONBF, 0);
// Create channel with a callback
_queue = dispatch_queue_create("blink.TermDevice", NULL);
_outStream = [[ViewStream alloc] initWithQueue:_queue fd:_poutput[0]];
_errStream = [[ViewStream alloc] initWithQueue:_queue fd:_perror[0]];
}
return self;
}
- (void)write:(NSString *)input
{
write(_pinput[1], [input UTF8String], [input lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
}
- (void)close
{
// TODO: Closing the streams!! But they are duplicated!!!!
[_stream close];
[_outStream close];
[_errStream close];
}
- (void)attachView:(TermView *)termView
{
if (termView) {
_view = termView;
_view.device = self;
_outStream.view = termView;
_errStream.view = termView;
} else {
_outStream.view = nil;
_errStream.view = nil;
_view.device = nil;
_view = nil;
}
}
- (void)setRawMode:(BOOL)rawMode
{
if (_stream.out) {
if (rawMode) {
fprintf(_stream.out, "\x1b]1337;BlinkAutoCR=0\x07");
} else {
fprintf(_stream.out, "\x1b]1337;BlinkAutoCR=1\x07");
}
}
_rawMode = rawMode;
}
- (void)setSecureTextEntry:(BOOL)secureTextEntry
{
_secureTextEntry = secureTextEntry;
dispatch_async(dispatch_get_main_queue(), ^{
if (secureTextEntry == _input.secureTextEntry) {
return;
}
_input.secureTextEntry = secureTextEntry;
[_input reset];
[_input reloadInputViews];
});
}
- (void)dealloc
{
[self close];
_input = nil;
_view = nil;
}
- (void)attachInput:(TermInput *)termInput
{
_input = termInput;
if (!_input) {
[_view blur];
}
if (_input.device != self) {
[_input.device attachInput:nil];
[_input reset];
}
_input.device = self;
if (_secureTextEntry != _input.secureTextEntry) {
_input.secureTextEntry = _secureTextEntry;
[_input reset];
[_input reloadInputViews];
}
if ([_input isFirstResponder]) {
[_view focus];
[_delegate deviceFocused];
} else {
[_view blur];
}
}
- (void)focus {
[_view focus];
[_delegate deviceFocused];
if (![_view.window isKeyWindow]) {
[_view.window makeKeyWindow];
}
if (![_input isFirstResponder]) {
[_input becomeFirstResponder];
}
}
- (void)blur {
[_view blur];
}
#pragma mark - TermViewDeviceProtocol
- (void)viewIsReady
{
[_delegate deviceIsReady];
}
- (void)viewFontSizeChanged:(NSInteger)size
{
[_delegate viewFontSizeChanged:size];
}
- (void)viewWinSizeChanged:(struct winsize)newWinSize
{
if (win.ws_col == newWinSize.ws_col && win.ws_row == newWinSize.ws_row) {
return;
}
win.ws_col = newWinSize.ws_col;
win.ws_row = newWinSize.ws_row;
[_delegate deviceSizeChanged];
}
- (void)viewSendString:(NSString *)data
{
[self write:data];
}
- (void)viewCopyString:(NSString *)text
{
[[UIPasteboard generalPasteboard] setString:text];
}
- (BOOL)handleControl:(NSString *)control
{
return NO;
}
@end