-
Notifications
You must be signed in to change notification settings - Fork 18
/
MobileTerminalViewController.m
338 lines (288 loc) · 13.4 KB
/
MobileTerminalViewController.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
// MobileTerminalViewController.m
// MobileTerminal
#import "MobileTerminalViewController.h"
#import "Terminal/TerminalKeyboard.h"
#import "Terminal/TerminalGroupView.h"
#import "Terminal/TerminalView.h"
#import "Preferences/Settings.h"
#import "Preferences/TerminalSettings.h"
#import "VT100/ColorMap.h"
#import "MenuView.h"
#import "GestureResponder.h"
#import "GestureActionRegistry.h"
@implementation MobileTerminalViewController
@synthesize contentView;
@synthesize terminalGroupView;
@synthesize terminalSelector;
@synthesize preferencesButton;
@synthesize menuButton;
@synthesize interfaceDelegate;
@synthesize menuView;
@synthesize gestureResponder;
@synthesize gestureActionRegistry;
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (void)awakeFromNib
{
terminalKeyboard = [[TerminalKeyboard alloc] init];
keyboardShown = NO;
// Copy and paste is off by default
copyPasteEnabled = NO;
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)unregisterForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
// TODO(allen): Fix the deprecation of UIKeyboardBoundsUserInfoKey
// below -- it requires more of a change because the replacement
// is not available in 3.1.3
- (void)keyboardWillShow:(NSNotification*)aNotification
{
if (keyboardShown) {
return;
}
keyboardShown = YES;
[UIView animateWithDuration:[[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] animations:^{
// Get the size of the keyboard.
NSValue* aValue = [aNotification.userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Resize to the original height of the screen with the keyboard
CGRect viewFrame = contentView.frame;
viewFrame.size.height -= keyboardSize.height;
contentView.frame = viewFrame;
} completion:NULL];
}
-(IBAction)esc:(id)sender {
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c",0x1b] dataUsingEncoding:NSASCIIStringEncoding]];
}
-(IBAction)tab:(id)sender {
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c",0x9] dataUsingEncoding:NSASCIIStringEncoding]];
}
-(IBAction)up:(id)sender {
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c%c%c",(char)0x1B,(char)0x5B,(char)0x41] dataUsingEncoding:NSASCIIStringEncoding]];
}
-(IBAction)down:(id)sender {
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c%c%c",(char)0x1B,(char)0x5B,(char)0x42] dataUsingEncoding:NSASCIIStringEncoding]];
}
-(IBAction)left:(id)sender {
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c%c%c",(char)0x1B,(char)0x5B,(char)0x44] dataUsingEncoding:NSASCIIStringEncoding]];
}
-(IBAction)right:(id)sender {
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c%c%c",(char)0x1B,(char)0x5B,(char)0x43] dataUsingEncoding:NSASCIIStringEncoding]];
}
-(IBAction)showfn:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
[specialscroller setAlpha:0];
[UIView commitAnimations];
}
-(IBAction)hidefn:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
[specialscroller setAlpha:1];
[UIView commitAnimations];
}
-(IBAction)fn:(UIBarButtonItem *)sender {
if ([sender.title isEqualToString:@"F1"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%cOP",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F2"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%cOQ",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F3"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%cOR",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F4"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%cOS",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F5"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[15~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F6"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[17~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F7"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[18~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F8"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[19~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F9"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[20~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F10"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[21~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F11"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[22~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
} else if ([sender.title isEqualToString:@"F12"]){
[[terminalGroupView terminalAtIndex:[terminalSelector currentPage]] receiveKeyboardInput:[[NSString stringWithFormat:@"%c[23~",0x1B] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
- (void)keyboardWillHide:(NSNotification*)aNotification
{
if (!keyboardShown) {
return;
}
keyboardShown = NO;
[UIView animateWithDuration:[[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] animations:^{
// Get the size of the keyboard.
NSValue* aValue = [aNotification.userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Resize to the original height of the screen without the keyboard
CGRect viewFrame = contentView.frame;
viewFrame.size.height += keyboardSize.height;
contentView.frame = viewFrame;
} completion:NULL];
}
- (void)setShowKeyboard:(BOOL)showKeyboard
{
if (showKeyboard) {
[terminalKeyboard becomeFirstResponder];
} else {
[terminalKeyboard resignFirstResponder];
}
}
- (void)toggleKeyboard:(id)sender
{
BOOL isShown = keyboardShown;
[self setShowKeyboard:!isShown];
}
- (void)toggleCopyPaste:(id)sender;
{
copyPasteEnabled = !copyPasteEnabled;
[gestureResponder setSwipesEnabled:!copyPasteEnabled];
for (int i = 0; i < [terminalGroupView terminalCount]; ++i) {
TerminalView* terminal = [terminalGroupView terminalAtIndex:i];
[terminal setCopyPasteEnabled:copyPasteEnabled];
}
}
// Invoked when the page control is clicked to make a new terminal active. The
// keyboard events are forwarded to the new active terminal and it is made the
// front-most terminal view.
- (void)terminalSelectionDidChange:(id)sender
{
TerminalView* terminalView =
[terminalGroupView terminalAtIndex:[terminalSelector currentPage]];
terminalKeyboard.inputDelegate = terminalView;
gestureActionRegistry.terminalInput = terminalView;
[terminalGroupView bringTerminalToFront:terminalView];
}
// Invoked when the preferences button is pressed
- (void)preferencesButtonPressed:(id)sender
{
// Remember the keyboard state for the next reload and don't listen for
// keyboard hide/show events
shouldShowKeyboard = keyboardShown;
[self unregisterForKeyboardNotifications];
[interfaceDelegate preferencesButtonPressed:sender];
}
// Invoked when the menu button is pressed
- (void)menuButtonPressed:(id)sender
{
[menuView setHidden:![menuView isHidden]];
}
// Invoked when a menu item is clicked, to run the specified command.
- (void)selectedCommand:(NSString*)command
{
TerminalView* terminalView = [terminalGroupView frontTerminal];
[terminalView receiveKeyboardInput:[command dataUsingEncoding:NSUTF8StringEncoding]];
// Make the menu disappear
[menuView setHidden:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// User clicked the Exit button below
exit(0);
}
- (void)viewDidLoad {
[super viewDidLoad];
@try {
[terminalGroupView startSubProcess];
} @catch (NSException* e) {
NSLog(@"Caught %@: %@", [e name], [e reason]);
if ([[e name] isEqualToString:@"ForkException"]) {
// This happens if we fail to fork for some reason.
// TODO(allen): Provide a helpful hint -- a kernel patch?
UIAlertView* view =
[[UIAlertView alloc] initWithTitle:[e name]
message:[e reason]
delegate:self
cancelButtonTitle:@"Exit"
otherButtonTitles:NULL];
[view show];
return;
}
[e raise];
return;
}
// TODO(allen): This should be configurable
shouldShowKeyboard = YES;
// Adding the keyboard to the view has no effect, except that it is will
// later allow us to make it the first responder so we can show the keyboard
// on the screen.
[[self view] addSubview:terminalKeyboard];
// The menu button points to the right, but for this context it should point
// up, since the menu moves that way.
menuButton.transform = CGAffineTransformMakeRotation(-90.0f * M_PI / 180.0f);
[menuButton setNeedsLayout];
// Setup the page control that selects the active terminal
[terminalSelector setNumberOfPages:[terminalGroupView terminalCount]];
[terminalSelector setCurrentPage:0];
// Make the first terminal active
[self terminalSelectionDidChange:self];
CGRect frame = terminalGroupView.frame;
frame.origin.y += 49;
frame.size.height -= 49;
frame.size.height -= 19;
terminalGroupView.frame = frame;
//specialkeys.layer.zPosition = 999999;
fnscroller.contentSize = CGSizeMake(600, 43);
specialscroller.contentSize = CGSizeMake(467, 43);
//[((UIToolbar *)[specialscroller.subviews objectAtIndex:0]) items];
}
- (void)viewDidAppear:(BOOL)animated
{
[interfaceDelegate rootViewDidAppear];
[self registerForKeyboardNotifications];
[self setShowKeyboard:shouldShowKeyboard];
// Reset the font in case it changed in the preferenes view
TerminalSettings* settings = [[Settings sharedInstance] terminalSettings];
UIFont* font = [settings font];
for (int i = 0; i < [terminalGroupView terminalCount]; ++i) {
TerminalView* terminalView = [terminalGroupView terminalAtIndex:i];
[terminalView setFont:font];
[terminalView setNeedsLayout];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// This supports everything except for upside down, since upside down is most
// likely accidental.
switch (interfaceOrientation) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
return YES;
default:
return NO;
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// We rotated, and almost certainly changed the frame size of the text view.
[[self view] layoutSubviews];
}
- (void)didReceiveMemoryWarning {
// TODO(allen): Should clear scrollback buffers to save memory?
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[terminalKeyboard release];
[super dealloc];
}
@end