This repository was archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathTerminal.vala
More file actions
304 lines (248 loc) · 9.81 KB
/
Copy pathTerminal.vala
File metadata and controls
304 lines (248 loc) · 9.81 KB
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
/*
* Copyright © 2013–2014 Philipp Emanuel Weidmann <pew@worldwidemann.com>
*
* Nemo vir est qui mundum non reddat meliorem.
*
*
* This file is part of Final Term.
*
* Final Term 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.
*
* Final Term 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 Final Term. If not, see <http://www.gnu.org/licenses/>.
*/
// TODO: Rename to "TerminalController"?
public class Terminal : Object {
public int lines { get; set; }
public int columns { get; set; }
public TerminalStream terminal_stream { get; set; default = new TerminalStream(); }
public TerminalOutput terminal_output { get; set; }
public TerminalView terminal_view { get; set; }
private Posix.pid_t fork_pid;
private int command_file;
private IOChannel command_channel;
// Store class instances indexed by the shell process' PID
// – an ugly necessity because of Vala's closure limitations
private static Gee.Map<int, Terminal> terminals_by_pid = new Gee.HashMap<int, Terminal>();
public Terminal() {
lines = Settings.get_default().terminal_lines;
columns = Settings.get_default().terminal_columns;
terminal_stream.element_completed.connect(on_stream_element_completed);
terminal_stream.transient_text_updated.connect(on_stream_transient_text_updated);
terminal_output = new TerminalOutput(this);
terminal_output.line_added.connect(on_output_line_added);
terminal_output.line_updated.connect(on_output_line_updated);
terminal_output.command_updated.connect(on_output_command_updated);
terminal_output.command_executed.connect(on_output_command_executed);
#if HAS_NOTIFY
terminal_output.command_finished.connect(on_output_command_finished);
#endif
terminal_output.progress_updated.connect(on_output_progress_updated);
terminal_output.progress_finished.connect(on_output_progress_finished);
terminal_output.cursor_position_changed.connect(on_output_cursor_position_changed);
initialize_pty();
on_settings_changed(null);
Settings.get_default().changed.connect(on_settings_changed);
}
public void update_autocompletion_position() {
int x;
int y;
terminal_view.terminal_output_view.get_screen_position(
terminal_output.command_start_position, out x, out y);
// Move popup one character down so it doesn't occlude the input
y += Settings.get_default().character_height;
FinalTerm.autocompletion.move_popup(x, y);
}
public void clear_command() {
// TODO: Handle cases where cursor is not at the end of the line
for (int i = 0; i < terminal_output.get_command().char_count(); i++) {
// Delete last character (backspace)
send_text("\x7F");
}
}
public void set_command(string command) {
clear_command();
send_text(command);
}
public void run_command(string command) {
set_command(command);
send_text("\n");
}
private void on_stream_element_completed(TerminalStream.StreamElement stream_element) {
terminal_output.parse_stream_element(stream_element);
}
private void on_stream_transient_text_updated(string transient_text) {
terminal_output.parse_transient_text(transient_text);
}
private void on_output_line_added() {
terminal_view.terminal_output_view.add_line_views();
// Schedule autoscroll with low priority to ensure it is performed
// only after all layout changes triggered by adding the new line
// are complete
// TODO: Add information about instance to key
Utilities.schedule_execution(() => {
terminal_view.terminal_output_view.scroll_to_position();
}, "scroll_to_position", 0, Priority.DEFAULT_IDLE);
}
private void on_output_line_updated(int line_index) {
terminal_view.terminal_output_view.mark_line_as_updated(line_index);
// TODO: Add information about instance to key
Utilities.schedule_execution(terminal_view.terminal_output_view.render_terminal_output,
"render_terminal_output", Settings.get_default().render_interval);
}
private void on_output_command_updated(string command) {
message(_("Command updated: '%s'"), command);
var stripped_command = command.strip();
if (stripped_command == "") {
FinalTerm.autocompletion.hide_popup();
} else {
// TODO: This should be scheduled to avoid congestion
FinalTerm.autocompletion.show_popup(stripped_command);
update_autocompletion_position();
}
}
private void on_output_command_executed(string command) {
message(_("Command executed: '%s'"), command);
FinalTerm.autocompletion.hide_popup();
var stripped_command = command.strip();
if (stripped_command != "")
FinalTerm.autocompletion.add_command(stripped_command);
}
private void on_output_progress_updated(int percentage, string operation) {
#if HAS_UNITY
FinalTerm.launcher.progress_visible = true;
FinalTerm.launcher.progress = percentage / 100.0;
#endif
terminal_view.show_progress(percentage, operation);
}
private void on_output_progress_finished() {
#if HAS_UNITY
FinalTerm.launcher.progress_visible = false;
#endif
terminal_view.hide_progress();
}
#if HAS_NOTIFY
private void on_output_command_finished(string command, int return_code) {
if (terminal_view.window_has_focus())
return;
var message = _("Command finished") + ((return_code == 0) ? "" :
" (" + _("return code") + ": " + return_code.to_string() + ")");
var notification = new Notify.Notification(message, command, "final-term");
try {
notification.show();
} catch (Error e) { warning(_("Failed to show notification: %s"), e.message); }
}
#endif
private void on_output_cursor_position_changed(TerminalOutput.CursorPosition new_position) {
// TODO: Add information about instance to key
Utilities.schedule_execution(terminal_view.terminal_output_view.render_terminal_output,
"render_terminal_output", Settings.get_default().render_interval);
}
public void send_text(string text) {
size_t bytes_written;
try {
command_channel.write_chars((char[])text.data, out bytes_written);
command_channel.flush();
} catch (Error e) { warning(_("Sending text failed: %s"), e.message); }
}
// Makes the PTY aware that the size (lines and columns)
// of the terminal has been changed
public void update_size() {
Linux.winsize terminal_size = { (ushort)lines, (ushort)columns, 0, 0 };
Linux.ioctl(command_file, Linux.Termios.TIOCSWINSZ, terminal_size);
}
private void initialize_pty() {
int pty_master;
char[] slave_name = null;
Linux.winsize terminal_size = { (ushort)lines, (ushort)columns, 0, 0 };
fork_pid = Linux.forkpty(out pty_master, slave_name, null, terminal_size);
switch (fork_pid) {
case -1: // Error
critical(_("Fork failed"));
break;
case 0: // This is the child process
run_shell();
break;
default: // This is the parent process
command_file = pty_master;
initialize_read();
// Store instance reference to be retrieved from inside the closure
terminals_by_pid.set((int)fork_pid, this);
Posix.@signal(Posix.SIGCHLD, (@signal) => {
// Some child process terminated
Posix.pid_t child_pid;
// Do not let the shell process turn defunct
// Note that multiple child processes might have terminated simultaneously
// as noted in http://stackoverflow.com/questions/2595503/determine-pid-of-terminated-process
while ((child_pid = Posix.waitpid(-1, null, Posix.WNOHANG)) != -1) {
var this_terminal = terminals_by_pid.get((int)child_pid);
// Close channel to keep pending shell IO from triggering UI updates (and crashes)
// after the corresponding TerminalWidget has been removed
// TODO: handle error properly
try {
this_terminal.command_channel.shutdown(false);
} catch (IOChannelError e) {
stderr.printf("Error in command_channel.shutdown");
}
this_terminal.shell_terminated();
// TODO: If allowed to run, this loop turns into an infinite loop
// when multiple terminals are used. INVESTIGATE FURTHER!
break;
}
});
break;
}
}
private void run_shell() {
Environment.set_variable("TERM", Settings.get_default().emulated_terminal, true);
string[] arguments = { Settings.get_default().shell_path, "--rcfile",
Config.PKGDATADIR + "/Startup/bash_startup", "-i" };
// Add custom shell arguments
foreach (var argument in Settings.get_default().shell_arguments) {
arguments += argument;
}
// Replace child process with shell process
Posix.execvp(Settings.get_default().shell_path, arguments);
// If this line is reached, execvp() must have failed
critical(_("execvp failed"));
Posix.exit(Posix.EXIT_FAILURE);
}
public void terminate_shell() {
// SIGTERM does not reliably terminate the process
Posix.kill(fork_pid, Posix.SIGKILL);
}
private void initialize_read() {
command_channel = new IOChannel.unix_new(command_file);
command_channel.add_watch(IOCondition.IN, (source, condition) => {
if (condition == IOCondition.HUP) {
message(_("Connection broken"));
return false;
}
if (!(IOFlags.IS_READABLE in command_channel.get_flags())) {
message(_("Channel not readable"));
return false;
}
// TODO: Read all available characters rather than one
unichar character;
try {
command_channel.read_unichar(out character);
} catch (Error e) { warning(_("Reading unichar failed: %s"), e.message); }
terminal_stream.parse_character(character);
return true;
});
}
private void on_settings_changed(string? key) {
// TODO: This breaks if multiple terminal widgets are used
if (FinalTerm.autocompletion.is_popup_visible())
update_autocompletion_position();
}
public signal void shell_terminated();
}