-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.cpp
284 lines (249 loc) · 10.7 KB
/
terminal.cpp
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
#define PCRE2_CODE_UNIT_WIDTH 0
#include <pcre2.h>
#include "svanterm.h"
long last_notification_timestamp;
extern FindWindow *find_window;
extern Terminal *dock_from;
void Terminal::kill_vte() {
kill(child_pid, 9);
}
bool Terminal::header_button_press(GdkEventButton* event) {
focus_vte();
if (event->button == 2) {
kill_vte();
}
return TRUE;
}
gboolean Terminal::vte_child_exited(GtkWidget *widget, GdkEventExpose *event, gpointer user_data) {
Terminal *_this = static_cast<Terminal *>(user_data);
static_cast<Frame *>(_this->get_parent())->destroy();
}
gboolean Terminal::vte_click(GtkWidget *vte, GdkEvent *event, gpointer user_data) {
GdkEventButton *button_event = (GdkEventButton *)event;
if (button_event->button != 1 || !(button_event->state & GDK_CONTROL_MASK))
return FALSE;
const char *url = vte_terminal_match_check_event(VTE_TERMINAL(vte), event, 0);
if (!url)
return FALSE;
if (strcasestr(url, "http") != url)
url = std::string(url).insert(0, "http://").c_str();
if (vfork() == 0) {
execlp("xdg-open", "xdg-open", url, NULL);
_exit(0);
}
}
gboolean Terminal::vte_got_focus(GtkWidget *vte, GdkEvent *event, gpointer user_data) {
// To make sure this terminal is selected next time this tab is selected
Terminal *_this = static_cast<Terminal *>(user_data);
get_tab_frame(_this)->set_focus_chain(std::vector<Gtk::Widget *>(1, _this));
update_active_terminals();
return FALSE;
}
gboolean Terminal::vte_lost_focus(GtkWidget *vte, GdkEvent *event, gpointer user_data) {
update_active_terminals();
return FALSE;
}
void Terminal::vte_selection_changed(VteTerminal *vte, gpointer user_data) {
if (vte_terminal_get_has_selection(vte)) {
vte_terminal_copy_clipboard(vte);
}
}
void Terminal::vte_beep(VteTerminal *vte, gpointer user_data) {
Terminal *_this = static_cast<Terminal *>(user_data);
if (time(NULL) < last_notification_timestamp + 10)
return;
last_notification_timestamp = time(NULL);
if (_this->notifications_enabled) {
auto notification = notify_notification_new("Terminal Bell", vte_terminal_get_window_title(vte), "dialog-information");
notify_notification_show(notification, NULL);
g_object_unref(G_OBJECT(notification));
}
}
void Terminal::update_title() {
find_label.set_label(vte_terminal_get_window_title((VteTerminal *)vte));
find_window->list_box.invalidate_filter();
auto title = std::string(vte_terminal_get_window_title((VteTerminal *)vte));
if (notifications_enabled)
title.append(" (notif. enabled)");
label.set_label(title.c_str());
get_tab_frame(this)->update_title();
auto window = static_cast<TerminalWindow *>(get_tab_frame(this)->get_parent()->get_parent());
window->update_title();
}
void Terminal::vte_title_changed(VteTerminal *widget, gpointer user_data) {
Terminal *_this = static_cast<Terminal *>(user_data);
_this->update_title();
}
Terminal::Terminal() {
dock_hint = GdkRectangle{0, 0, 0, 0};
vte = vte_terminal_new();
char *argv[] = { vte_get_user_shell(), NULL };
vte_terminal_spawn_sync(VTE_TERMINAL(vte), VTE_PTY_DEFAULT, NULL, argv, NULL,
(GSpawnFlags)0, NULL,
NULL, &child_pid, NULL, NULL);
set_orientation(Gtk::ORIENTATION_VERTICAL);
scrollbox.set_orientation(Gtk::ORIENTATION_HORIZONTAL);
scrollbar.set_orientation(Gtk::ORIENTATION_VERTICAL);
eventbox.add(label);
eventbox.signal_button_press_event().connect(mem_fun(this, &Terminal::header_button_press));
VteRegex *regex = vte_regex_new_for_match("(https?://|www\\.)[^\\s]*", -1, PCRE2_MULTILINE, NULL);
vte_terminal_match_add_regex(VTE_TERMINAL(vte), regex, 0);
vte_terminal_set_scrollback_lines(VTE_TERMINAL(vte), 10000);
g_signal_connect(vte, "bell", G_CALLBACK(Terminal::vte_beep), this);
g_signal_connect(vte, "child-exited", G_CALLBACK(Terminal::vte_child_exited), this);
g_signal_connect(vte, "button-press-event", G_CALLBACK(Terminal::vte_click), this);
g_signal_connect(vte, "focus-in-event", G_CALLBACK(Terminal::vte_got_focus), this);
g_signal_connect(vte, "focus-out-event", G_CALLBACK(Terminal::vte_lost_focus), this);
g_signal_connect(vte, "selection-changed", G_CALLBACK(Terminal::vte_selection_changed), this);
g_signal_connect(vte, "window-title-changed", G_CALLBACK(Terminal::vte_title_changed), this);
searchbar.add(searchentry);
searchbar.connect_entry(searchentry);
searchentry.signal_focus_out_event().connect(mem_fun(this, &Terminal::searchentry_lost_focus));
searchentry.signal_key_release_event().connect(mem_fun(this, &Terminal::searchentry_keypress));
pack_start(eventbox, false, false, 0);
pack_start(scrollbox, true, true, 0);
pack_start(searchbar, false, false, 0);
gtk_box_pack_start(GTK_BOX(scrollbox.gobj()), vte, true, true, 0);
scrollbox.pack_start(scrollbar, false, false, 0);
gtk_range_set_adjustment(GTK_RANGE(scrollbar.gobj()), gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte)));
show_all_children();
find_label.terminal = this;
find_label.set_alignment(0.0, 0.5);
find_window->list_box.prepend(find_label);
std::vector<Gtk::TargetEntry> listTargets;
listTargets.push_back(Gtk::TargetEntry("SvanTerminal", Gtk::TARGET_SAME_APP, 0));
eventbox.drag_source_set(listTargets);
drag_dest_set(listTargets);
eventbox.signal_drag_begin().connect(sigc::mem_fun(this, &Terminal::on_my_drag_begin));
eventbox.signal_drag_failed().connect(sigc::mem_fun(this, &Terminal::on_my_drag_failed));
eventbox.signal_drag_end().connect(sigc::mem_fun(this, &Terminal::on_my_drag_end));
signal_drag_motion().connect(sigc::mem_fun(this, &Terminal::on_my_drag_motion));
signal_drag_drop().connect(sigc::mem_fun(this, &Terminal::on_my_drag_drop));
signal_drag_leave().connect(sigc::mem_fun(this, &Terminal::on_my_drag_leave));
}
Terminal::~Terminal() {
find_window->list_box.remove(*find_label.get_parent());
gtk_widget_destroy(vte);
}
bool Terminal::searchentry_keypress(GdkEventKey* event) {
if (event->keyval == GDK_KEY_Shift_L || event->keyval == GDK_KEY_Shift_R)
return false;
if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) {
if (event->state & GDK_SHIFT_MASK) {
if (!vte_terminal_search_find_next((VteTerminal *)vte)) {
vte_terminal_unselect_all((VteTerminal *)vte);
vte_terminal_search_find_next((VteTerminal *)vte);
}
} else {
if (!vte_terminal_search_find_previous((VteTerminal *)vte)) {
vte_terminal_unselect_all((VteTerminal *)vte);
vte_terminal_search_find_previous((VteTerminal *)vte);
}
}
return false;
}
VteRegex *regex = vte_regex_new_for_search(searchentry.get_text().c_str(), -1, PCRE2_CASELESS, NULL);
vte_terminal_unselect_all((VteTerminal *)vte);
vte_terminal_search_set_regex((VteTerminal *)vte, regex, 0);
vte_terminal_search_find_previous((VteTerminal *)vte);
return false;
}
void Terminal::vte_set_active(gboolean active) {
GtkWidget *terminal = gtk_widget_get_parent(gtk_widget_get_parent(vte));
GdkRGBA terminal_color;
if (active) {
set_name("terminal_active");
terminal_color = (GdkRGBA){.85, .85, .85, 1};
} else {
set_name("terminal_inactive");
terminal_color = (GdkRGBA){.6, .6, .6, 1};
}
vte_terminal_set_color_foreground((VteTerminal *)vte, &terminal_color);
}
void Terminal::focus_vte() {
gtk_window_set_focus(GTK_WINDOW(get_toplevel()->gobj()), vte);
}
bool Terminal::searchentry_lost_focus(GdkEventFocus *event) {
if (!get_tab_frame(this)->get_focus_child())
focus_vte();
}
bool Terminal::on_draw(const Cairo::RefPtr<Cairo::Context>& cr) {
Box::on_draw(cr);
cr->set_source_rgb(1, 0, 0);
cr->rectangle(dock_hint.x, dock_hint.y, dock_hint.x+dock_hint.width, dock_hint.y+dock_hint.height);
cr->fill();
}
bool Terminal::on_my_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
int width = get_width();
int height = get_height();
int from_center_x = x - width/2;
int from_center_y = y - height/2;
if (ABS(from_center_x) > ABS(from_center_y))
if (from_center_x > 0)
dock_pos = Gtk::POS_RIGHT;
else
dock_pos = Gtk::POS_LEFT;
else
if (from_center_y > 0)
dock_pos = Gtk::POS_BOTTOM;
else
dock_pos = Gtk::POS_TOP;
switch (dock_pos) {
case Gtk::POS_TOP:
dock_hint = GdkRectangle{0, 0, width, height/2};
break;
case Gtk::POS_LEFT:
dock_hint = GdkRectangle{0, 0, width/2, height};
break;
case Gtk::POS_RIGHT:
dock_hint = GdkRectangle{width/2, 0, width/2, height};
break;
case Gtk::POS_BOTTOM:
dock_hint = GdkRectangle{0, height/2, width, height/2};
}
queue_draw();
}
bool Terminal::on_my_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
dock_hint = GdkRectangle{0, 0, 0, 0};
queue_draw();
if (dock_from == this) {
return FALSE;
}
Frame *old_frame = static_cast<Frame *>(dock_from->get_parent());
switch (dock_pos) {
case GTK_POS_TOP:
manage(new Splitter(get_parent(), dock_from, this, Gtk::ORIENTATION_VERTICAL));
break;
case GTK_POS_BOTTOM:
manage(new Splitter(get_parent(), this, dock_from, Gtk::ORIENTATION_VERTICAL));
break;
case GTK_POS_LEFT:
manage(new Splitter(get_parent(), dock_from, this, Gtk::ORIENTATION_HORIZONTAL));
break;
case GTK_POS_RIGHT:
manage(new Splitter(get_parent(), this, dock_from, Gtk::ORIENTATION_HORIZONTAL));
}
old_frame->destroy();
static_cast<TerminalWindow *>(this->get_toplevel())->present();
dock_from->focus_vte();
}
void Terminal::on_my_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context) {
dock_from = this;
}
void Terminal::on_my_drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time) {
dock_hint = GdkRectangle{0, 0, 0, 0};
queue_draw();
}
bool Terminal::on_my_drag_failed(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::DragResult result) {
Frame *old_frame = static_cast<Frame *>(dock_from->get_parent());
if (result == Gtk::DRAG_RESULT_NO_TARGET) {
TerminalWindow *window = new TerminalWindow;
window->show();
window->tabcontrol.add_tab(manage(new TabFrame(dock_from)));
window->show_all();
old_frame->destroy();
}
}
void Terminal::on_my_drag_end(const Glib::RefPtr<Gdk::DragContext>& context) {
dock_from = NULL;
}