~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/TerminalWindow.vala

  • Committer: Tony George
  • Date: 2016-03-28 15:36:17 UTC
  • Revision ID: tony.george.kol@gmail.com-20160328153617-fq5onsoz6dhyfsvt
Added embedded VTE terminal for restoring snapshots

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * TerminalWindow.vala
 
3
 *
 
4
 * Copyright 2015 Tony George <teejee2008@gmail.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
19
 * MA 02110-1301, USA.
 
20
 *
 
21
 *
 
22
 */
 
23
 
 
24
 
 
25
using Gtk;
 
26
using Gee;
 
27
 
 
28
using TeeJee.Logging;
 
29
using TeeJee.FileSystem;
 
30
using TeeJee.JSON;
 
31
using TeeJee.ProcessManagement;
 
32
using TeeJee.GtkHelper;
 
33
using TeeJee.Multimedia;
 
34
using TeeJee.System;
 
35
using TeeJee.Misc;
 
36
 
 
37
public class TerminalWindow : Gtk.Window {
 
38
        private Gtk.Box vbox_main;
 
39
        private Vte.Terminal term;
 
40
        
 
41
        private int def_width = 800;
 
42
        private int def_height = 600;
 
43
 
 
44
        private Pid child_pid;
 
45
        private Gtk.Window parent_win = null;
 
46
        public bool is_running = false;
 
47
        
 
48
        // init
 
49
        
 
50
        public TerminalWindow.with_parent(Gtk.Window? parent) {
 
51
                if (parent != null){
 
52
                        set_transient_for(parent);
 
53
                        parent_win = parent;
 
54
                }
 
55
                set_modal(true);
 
56
                window_position = WindowPosition.CENTER;
 
57
 
 
58
                this.delete_event.connect(()=>{
 
59
                        // do not allow window to close 
 
60
                        return true;
 
61
                });
 
62
                
 
63
                init_window();
 
64
        }
 
65
 
 
66
        public void init_window () {
 
67
                title = "";
 
68
                icon = get_app_icon(16);
 
69
                resizable = true;
 
70
                deletable = false;
 
71
                
 
72
                // vbox_main ---------------
 
73
                
 
74
                vbox_main = new Box (Orientation.VERTICAL, 6);
 
75
                vbox_main.set_size_request (def_width, def_height);
 
76
                add (vbox_main);
 
77
 
 
78
                // terminal ----------------------
 
79
                
 
80
                term = new Vte.Terminal();
 
81
                term.expand = true;
 
82
                vbox_main.add(term);
 
83
 
 
84
                #if VTE_291
 
85
                
 
86
                term.input_enabled = true;
 
87
                term.backspace_binding = Vte.EraseBinding.AUTO;
 
88
                term.cursor_blink_mode = Vte.CursorBlinkMode.SYSTEM;
 
89
                term.cursor_shape = Vte.CursorShape.UNDERLINE;
 
90
                term.rewrap_on_resize = true;
 
91
                
 
92
                #endif
 
93
                
 
94
                term.scroll_on_keystroke = true;
 
95
                term.scroll_on_output = true;
 
96
 
 
97
                // colors -----------------------------
 
98
                
 
99
                #if VTE_291
 
100
                
 
101
                var color = Gdk.RGBA();
 
102
                color.parse("#FFFFFF");
 
103
                term.set_color_foreground(color);
 
104
 
 
105
                color.parse("#404040");
 
106
                term.set_color_background(color);
 
107
                
 
108
                #else
 
109
                
 
110
                Gdk.Color color;
 
111
                Gdk.Color.parse("#FFFFFF", out color);
 
112
                term.set_color_foreground(color);
 
113
 
 
114
                Gdk.Color.parse("#404040", out color);
 
115
                term.set_color_background(color);
 
116
 
 
117
                #endif
 
118
                
 
119
                // grab focus ----------------
 
120
                
 
121
                term.grab_focus();
 
122
                
 
123
                show_all();
 
124
        }
 
125
 
 
126
        public void start_shell(){
 
127
                string[] argv = new string[1];
 
128
                argv[0] = "/bin/sh";
 
129
 
 
130
                string[] env = Environ.get();
 
131
                
 
132
                try{
 
133
 
 
134
                        is_running = true;
 
135
                        
 
136
                        #if VTE_291
 
137
                        
 
138
                        term.spawn_sync(
 
139
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
140
                                TEMP_DIR, //working_directory
 
141
                                argv, //argv
 
142
                                env, //env
 
143
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
144
                                null, //child_setup
 
145
                                out child_pid,
 
146
                                null
 
147
                        );
 
148
 
 
149
                        #else
 
150
 
 
151
                        term.fork_command_full(
 
152
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
153
                                App.temp_dir, //working_directory
 
154
                                argv, //argv
 
155
                                env, //env
 
156
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
157
                                null, //child_setup
 
158
                                out child_pid
 
159
                        );
 
160
 
 
161
                        #endif
 
162
                }
 
163
                catch (Error e) {
 
164
                        log_error (e.message);
 
165
                }
 
166
        }
 
167
 
 
168
        public void execute_command(string command){
 
169
                term.feed_child("%s\n".printf(command), -1);
 
170
        }
 
171
 
 
172
        public void execute_script(string script_path, bool wait = false){
 
173
                string[] argv = new string[1];
 
174
                argv[0] = script_path;
 
175
                
 
176
                string[] env = Environ.get();
 
177
 
 
178
                try{
 
179
 
 
180
                        is_running = true;
 
181
                        
 
182
                        #if VTE_291
 
183
                        
 
184
                        term.spawn_sync(
 
185
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
186
                                TEMP_DIR, //working_directory
 
187
                                argv, //argv
 
188
                                env, //env
 
189
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
190
                                null, //child_setup
 
191
                                out child_pid,
 
192
                                null
 
193
                        );
 
194
 
 
195
                        #else
 
196
 
 
197
                        term.fork_command_full(
 
198
                                Vte.PtyFlags.DEFAULT, //pty_flags
 
199
                                TEMP_DIR, //working_directory
 
200
                                argv, //argv
 
201
                                env, //env
 
202
                                GLib.SpawnFlags.SEARCH_PATH, //spawn_flags
 
203
                                null, //child_setup
 
204
                                out child_pid
 
205
                        );
 
206
 
 
207
                        #endif
 
208
 
 
209
                        term.watch_child(child_pid);
 
210
        
 
211
                        term.child_exited.connect(script_exit);
 
212
 
 
213
                        if (wait){
 
214
                                while (is_running){
 
215
                                        sleep(200);
 
216
                                        gtk_do_events();
 
217
                                }
 
218
                        }
 
219
                }
 
220
                catch (Error e) {
 
221
                        log_error (e.message);
 
222
                }
 
223
        }
 
224
 
 
225
        #if VTE_291
 
226
        public void script_exit(int status){
 
227
        #else
 
228
        public void script_exit(){
 
229
        #endif
 
230
 
 
231
                is_running = false;
 
232
                
 
233
                this.hide();
 
234
 
 
235
                //no need to check status again
 
236
                
 
237
                //destroying parent will display main window
 
238
                if (parent != null){
 
239
                        parent_win.destroy();
 
240
                }
 
241
        }
 
242
}
 
243
 
 
244