Re-enable SIGTERM handling
[geany-mirror.git] / src / vte.c
blob20492ea81e463a26f62f545fba99fa729b6778b2
1 /*
2 * vte.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Virtual Terminal Emulation setup and handling code, using the libvte plugin library.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #ifdef HAVE_VTE
32 #include "vte.h"
34 #include "callbacks.h"
35 #include "document.h"
36 #include "geanyobject.h"
37 #include "msgwindow.h"
38 #include "prefs.h"
39 #include "sciwrappers.h"
40 #include "support.h"
41 #include "ui_utils.h"
42 #include "utils.h"
43 #include "keybindings.h"
45 #include "gtkcompat.h"
47 /* include stdlib.h AND unistd.h, because on GNU/Linux pid_t seems to be
48 * in stdlib.h, on FreeBSD in unistd.h, sys/types.h is needed for C89 */
49 #include <stdlib.h>
50 #include <sys/types.h>
51 #include <unistd.h>
53 #include <gdk/gdkkeysyms.h>
54 #include <signal.h>
55 #include <string.h>
56 #include <errno.h>
59 VteInfo vte_info = { FALSE, FALSE, FALSE, NULL, NULL };
60 VteConfig *vc;
62 static GPid pid = 0;
63 static gboolean clean = TRUE;
64 static GModule *module = NULL;
65 static struct VteFunctions *vf;
66 static gchar *gtk_menu_key_accel = NULL;
67 static GtkWidget *terminal_label = NULL;
68 static guint terminal_label_update_source = 0;
70 /* use vte wordchars to select paths */
71 static const gchar VTE_WORDCHARS[] = "-A-Za-z0-9,./?%&#:_";
72 static const gchar VTE_ADDITIONAL_WORDCHARS[] = "-,./?%&#:_";
75 /* Incomplete VteTerminal struct from vte/vte.h. */
76 typedef struct _VteTerminal VteTerminal;
77 struct _VteTerminal
79 GtkWidget widget;
80 GtkAdjustment *adjustment;
83 #define VTE_TERMINAL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
84 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
86 typedef enum {
87 VTE_CURSOR_BLINK_SYSTEM,
88 VTE_CURSOR_BLINK_ON,
89 VTE_CURSOR_BLINK_OFF
90 } VteTerminalCursorBlinkMode;
92 typedef enum {
93 /* we don't care for the other possible values */
94 VTE_PTY_DEFAULT = 0
95 } VtePtyFlags;
98 /* Holds function pointers we need to access the VTE API. */
99 struct VteFunctions
101 guint (*vte_get_major_version) (void);
102 guint (*vte_get_minor_version) (void);
103 GtkWidget* (*vte_terminal_new) (void);
104 pid_t (*vte_terminal_fork_command) (VteTerminal *terminal, const char *command, char **argv,
105 char **envv, const char *directory, gboolean lastlog,
106 gboolean utmp, gboolean wtmp);
107 gboolean (*vte_terminal_spawn_sync) (VteTerminal *terminal, VtePtyFlags pty_flags,
108 const char *working_directory, char **argv, char **envv,
109 GSpawnFlags spawn_flags, GSpawnChildSetupFunc child_setup,
110 gpointer child_setup_data, GPid *child_pid,
111 GCancellable *cancellable, GError **error);
112 void (*vte_terminal_set_size) (VteTerminal *terminal, glong columns, glong rows);
113 void (*vte_terminal_set_word_chars) (VteTerminal *terminal, const char *spec);
114 void (*vte_terminal_set_word_char_exceptions) (VteTerminal *terminal, const char *exceptions);
115 void (*vte_terminal_set_mouse_autohide) (VteTerminal *terminal, gboolean setting);
116 void (*vte_terminal_reset) (VteTerminal *terminal, gboolean full, gboolean clear_history);
117 GType (*vte_terminal_get_type) (void);
118 void (*vte_terminal_set_scroll_on_output) (VteTerminal *terminal, gboolean scroll);
119 void (*vte_terminal_set_scroll_on_keystroke) (VteTerminal *terminal, gboolean scroll);
120 void (*vte_terminal_set_font) (VteTerminal *terminal, const PangoFontDescription *font_desc);
121 void (*vte_terminal_set_scrollback_lines) (VteTerminal *terminal, glong lines);
122 gboolean (*vte_terminal_get_has_selection) (VteTerminal *terminal);
123 void (*vte_terminal_copy_clipboard) (VteTerminal *terminal);
124 void (*vte_terminal_paste_clipboard) (VteTerminal *terminal);
125 void (*vte_terminal_set_color_foreground) (VteTerminal *terminal, const GdkColor *foreground);
126 void (*vte_terminal_set_color_bold) (VteTerminal *terminal, const GdkColor *foreground);
127 void (*vte_terminal_set_color_background) (VteTerminal *terminal, const GdkColor *background);
128 void (*vte_terminal_feed_child) (VteTerminal *terminal, const char *data, glong length);
129 void (*vte_terminal_im_append_menuitems) (VteTerminal *terminal, GtkMenuShell *menushell);
130 void (*vte_terminal_set_cursor_blink_mode) (VteTerminal *terminal,
131 VteTerminalCursorBlinkMode mode);
132 void (*vte_terminal_set_cursor_blinks) (VteTerminal *terminal, gboolean blink);
133 void (*vte_terminal_select_all) (VteTerminal *terminal);
134 void (*vte_terminal_set_audible_bell) (VteTerminal *terminal, gboolean is_audible);
135 GtkAdjustment* (*vte_terminal_get_adjustment) (VteTerminal *terminal);
136 #if GTK_CHECK_VERSION(3, 0, 0)
137 /* hack for the VTE 2.91 API using GdkRGBA: we wrap the API to keep using GdkColor on our side */
138 void (*vte_terminal_set_color_foreground_rgba) (VteTerminal *terminal, const GdkRGBA *foreground);
139 void (*vte_terminal_set_color_bold_rgba) (VteTerminal *terminal, const GdkRGBA *foreground);
140 void (*vte_terminal_set_color_background_rgba) (VteTerminal *terminal, const GdkRGBA *background);
141 #endif
145 static void create_vte(void);
146 static void vte_start(GtkWidget *widget);
147 static void vte_restart(GtkWidget *widget);
148 static gboolean vte_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
149 static gboolean vte_keyrelease_cb(GtkWidget *widget, GdkEventKey *event, gpointer data);
150 static gboolean vte_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data);
151 static gboolean vte_register_symbols(GModule *module);
152 static void vte_popup_menu_clicked(GtkMenuItem *menuitem, gpointer user_data);
153 static GtkWidget *vte_create_popup_menu(void);
154 static void vte_commit_cb(VteTerminal *vte, gchar *arg1, guint arg2, gpointer user_data);
155 static void vte_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
156 gint x, gint y, GtkSelectionData *data, guint info, guint ltime);
159 enum
161 POPUP_COPY,
162 POPUP_PASTE,
163 POPUP_SELECTALL,
164 POPUP_CHANGEPATH,
165 POPUP_RESTARTTERMINAL,
166 POPUP_PREFERENCES,
167 TARGET_UTF8_STRING = 0,
168 TARGET_TEXT,
169 TARGET_COMPOUND_TEXT,
170 TARGET_STRING,
171 TARGET_TEXT_PLAIN
174 static const GtkTargetEntry dnd_targets[] =
176 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
177 { "TEXT", 0, TARGET_TEXT },
178 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
179 { "STRING", 0, TARGET_STRING },
180 { "text/plain", 0, TARGET_TEXT_PLAIN },
184 /* replacement for vte_terminal_get_adjustment() when it's not available */
185 static GtkAdjustment *default_vte_terminal_get_adjustment(VteTerminal *vte)
187 #if GTK_CHECK_VERSION(3, 0, 0)
188 if (GTK_IS_SCROLLABLE(vte))
189 return gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte));
190 #endif
191 /* this is only valid in < 0.38, 0.38 broke ABI */
192 return vte->adjustment;
196 #if GTK_CHECK_VERSION(3, 0, 0)
197 /* Wrap VTE 2.91 API using GdkRGBA with GdkColor so we use a single API on our side */
199 static void rgba_from_color(GdkRGBA *rgba, const GdkColor *color)
201 rgba->red = color->red / 65535.0;
202 rgba->green = color->green / 65535.0;
203 rgba->blue = color->blue / 65535.0;
204 rgba->alpha = 1.0;
207 # define WRAP_RGBA_SETTER(name) \
208 static void wrap_##name(VteTerminal *terminal, const GdkColor *color) \
210 GdkRGBA rgba; \
211 rgba_from_color(&rgba, color); \
212 vf->name##_rgba(terminal, &rgba); \
215 WRAP_RGBA_SETTER(vte_terminal_set_color_background)
216 WRAP_RGBA_SETTER(vte_terminal_set_color_bold)
217 WRAP_RGBA_SETTER(vte_terminal_set_color_foreground)
219 # undef WRAP_RGBA_SETTER
220 #endif
223 static gchar **vte_get_child_environment(void)
225 const gchar *exclude_vars[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL};
227 return utils_copy_environment(exclude_vars, "TERM", "xterm", NULL);
231 static void override_menu_key(void)
233 if (gtk_menu_key_accel == NULL) /* for restoring the default value */
234 g_object_get(G_OBJECT(gtk_settings_get_default()),
235 "gtk-menu-bar-accel", &gtk_menu_key_accel, NULL);
237 if (vc->ignore_menu_bar_accel)
238 gtk_settings_set_string_property(gtk_settings_get_default(),
239 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
240 else
241 gtk_settings_set_string_property(gtk_settings_get_default(),
242 "gtk-menu-bar-accel", gtk_menu_key_accel, "Geany");
246 static void on_startup_complete(G_GNUC_UNUSED GObject *dummy)
248 GeanyDocument *doc = document_get_current();
250 if (doc)
251 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
255 void vte_init(void)
257 if (vte_info.have_vte == FALSE)
258 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
259 geany_debug("Disabling terminal support");
260 return;
263 if (!EMPTY(vte_info.lib_vte))
265 module = g_module_open(vte_info.lib_vte, G_MODULE_BIND_LAZY);
267 #ifdef VTE_MODULE_PATH
268 else
270 module = g_module_open(VTE_MODULE_PATH, G_MODULE_BIND_LAZY);
272 #endif
274 if (module == NULL)
276 gint i;
277 const gchar *sonames[] = {
278 #if GTK_CHECK_VERSION(3, 0, 0)
279 "libvte-2.91.so", "libvte-2.91.so.0", "libvte-2.91.dylib",
280 "libvte2_90.so", "libvte2_90.so.9", "libvte2_90.dylib",
281 #else
282 "libvte.so", "libvte.so.4", "libvte.so.8", "libvte.so.9", "libvte.dylib",
283 #endif
284 NULL
287 for (i = 0; sonames[i] != NULL && module == NULL; i++)
289 module = g_module_open(sonames[i], G_MODULE_BIND_LAZY);
293 if (module == NULL)
295 vte_info.have_vte = FALSE;
296 geany_debug("Could not load libvte.so, embedded terminal support disabled");
297 return;
299 else
301 geany_debug("Loaded libvte from %s", g_module_name(module));
302 vf = g_new0(struct VteFunctions, 1);
303 if (vte_register_symbols(module))
304 vte_info.have_vte = TRUE;
305 else
307 vte_info.have_vte = FALSE;
308 g_free(vf);
309 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
310 /*g_module_close(module);*/
311 module = NULL;
312 return;
316 create_vte();
318 /* setup the F10 menu override (so it works before the widget is first realised). */
319 override_menu_key();
321 g_signal_connect(geany_object, "geany-startup-complete", G_CALLBACK(on_startup_complete), NULL);
325 static void on_vte_realize(void)
327 /* the vte widget has to be realised before color changes take effect */
328 vte_apply_user_settings();
330 if (vf->vte_terminal_im_append_menuitems && vc->im_submenu)
331 vf->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc->vte), GTK_MENU_SHELL(vc->im_submenu));
335 static gboolean vte_start_idle(G_GNUC_UNUSED gpointer user_data)
337 vte_start(vc->vte);
338 return FALSE;
342 static void create_vte(void)
344 GtkWidget *vte, *scrollbar, *hbox;
346 vc->vte = vte = vf->vte_terminal_new();
347 scrollbar = gtk_vscrollbar_new(vf->vte_terminal_get_adjustment(VTE_TERMINAL(vte)));
348 gtk_widget_set_can_focus(scrollbar, FALSE);
350 /* create menu now so copy/paste shortcuts work */
351 vc->menu = vte_create_popup_menu();
352 g_object_ref_sink(vc->menu);
354 hbox = gtk_hbox_new(FALSE, 0);
355 gtk_box_pack_start(GTK_BOX(hbox), vte, TRUE, TRUE, 0);
356 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, FALSE, 0);
358 /* set the default widget size first to prevent VTE expanding too much,
359 * sometimes causing the hscrollbar to be too big or out of view. */
360 gtk_widget_set_size_request(GTK_WIDGET(vte), 10, 10);
361 vf->vte_terminal_set_size(VTE_TERMINAL(vte), 30, 1);
363 vf->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte), TRUE);
364 if (vf->vte_terminal_set_word_chars)
365 vf->vte_terminal_set_word_chars(VTE_TERMINAL(vte), VTE_WORDCHARS);
366 else if (vf->vte_terminal_set_word_char_exceptions)
367 vf->vte_terminal_set_word_char_exceptions(VTE_TERMINAL(vte), VTE_ADDITIONAL_WORDCHARS);
369 gtk_drag_dest_set(vte, GTK_DEST_DEFAULT_ALL,
370 dnd_targets, G_N_ELEMENTS(dnd_targets), GDK_ACTION_COPY);
372 g_signal_connect(vte, "child-exited", G_CALLBACK(vte_start), NULL);
373 g_signal_connect(vte, "button-press-event", G_CALLBACK(vte_button_pressed), NULL);
374 g_signal_connect(vte, "event", G_CALLBACK(vte_keypress_cb), NULL);
375 g_signal_connect(vte, "key-release-event", G_CALLBACK(vte_keyrelease_cb), NULL);
376 g_signal_connect(vte, "commit", G_CALLBACK(vte_commit_cb), NULL);
377 g_signal_connect(vte, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
378 g_signal_connect(vte, "drag-data-received", G_CALLBACK(vte_drag_data_received), NULL);
380 /* start shell on idle otherwise the initial prompt can get corrupted */
381 g_idle_add(vte_start_idle, NULL);
383 gtk_widget_show_all(hbox);
384 terminal_label = gtk_label_new(_("Terminal"));
385 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow.notebook), hbox, terminal_label, MSG_VTE);
387 g_signal_connect_after(vte, "realize", G_CALLBACK(on_vte_realize), NULL);
391 void vte_close(void)
393 g_free(vf);
394 /* free the vte widget before unloading vte module
395 * this prevents a segfault on X close window if the message window is hidden */
396 gtk_widget_destroy(vc->vte);
397 gtk_widget_destroy(vc->menu);
398 g_object_unref(vc->menu);
399 g_free(vc->shell);
400 g_free(vc->font);
401 g_free(vc->send_cmd_prefix);
402 g_free(vc);
403 g_free(gtk_menu_key_accel);
404 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
405 * happens when the app really exits, not directly on g_module_close(). This still needs to
406 * be investigated. */
407 /*g_module_close(module); */
411 static gboolean set_dirty_idle(gpointer user_data)
413 gtk_widget_set_name(terminal_label, "geany-terminal-dirty");
414 terminal_label_update_source = 0;
415 return FALSE;
419 static void set_clean(gboolean value)
421 if (clean != value)
423 if (terminal_label)
425 if (terminal_label_update_source > 0)
427 g_source_remove(terminal_label_update_source);
428 terminal_label_update_source = 0;
430 if (value)
431 gtk_widget_set_name(terminal_label, NULL);
432 else
433 terminal_label_update_source = g_timeout_add(150, set_dirty_idle, NULL);
435 clean = value;
440 static gboolean vte_keyrelease_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
442 if (ui_is_keyval_enter_or_return(event->keyval) ||
443 ((event->keyval == GDK_c) && (event->state & GDK_CONTROL_MASK)))
445 /* assume any text on the prompt has been executed when pressing Enter/Return */
446 set_clean(TRUE);
448 return FALSE;
452 static gboolean vte_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
454 if (vc->enable_bash_keys)
455 return FALSE; /* Ctrl-[CD] will be handled by the VTE itself */
457 if (event->type != GDK_KEY_RELEASE)
458 return FALSE;
460 if ((event->keyval == GDK_c ||
461 event->keyval == GDK_d ||
462 event->keyval == GDK_C ||
463 event->keyval == GDK_D) &&
464 event->state & GDK_CONTROL_MASK &&
465 ! (event->state & GDK_SHIFT_MASK) && ! (event->state & GDK_MOD1_MASK))
467 vte_restart(widget);
468 return TRUE;
470 return FALSE;
474 static void vte_commit_cb(VteTerminal *vte, gchar *arg1, guint arg2, gpointer user_data)
476 set_clean(FALSE);
480 static void vte_start(GtkWidget *widget)
482 /* split the shell command line, so arguments will work too */
483 gchar **argv = g_strsplit(vc->shell, " ", -1);
485 if (argv != NULL)
487 gchar **env = vte_get_child_environment();
489 if (vf->vte_terminal_spawn_sync)
491 if (! vf->vte_terminal_spawn_sync(VTE_TERMINAL(widget), VTE_PTY_DEFAULT,
492 vte_info.dir, argv, env, 0, NULL, NULL,
493 &pid, NULL, NULL))
495 pid = -1;
498 else
500 pid = vf->vte_terminal_fork_command(VTE_TERMINAL(widget), argv[0], argv, env,
501 vte_info.dir, TRUE, TRUE, TRUE);
503 g_strfreev(env);
504 g_strfreev(argv);
506 else
507 pid = 0; /* use 0 as invalid pid */
509 set_clean(TRUE);
513 static void vte_restart(GtkWidget *widget)
515 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
516 if (pid > 0)
518 kill(pid, SIGINT);
519 pid = 0;
521 vf->vte_terminal_reset(VTE_TERMINAL(widget), TRUE, TRUE);
522 set_clean(TRUE);
526 static gboolean vte_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
528 if (event->button == 3)
530 gtk_widget_grab_focus(vc->vte);
531 gtk_menu_popup(GTK_MENU(vc->menu), NULL, NULL, NULL, NULL, event->button, event->time);
533 else if (event->button == 2)
535 gtk_widget_grab_focus(widget);
537 return FALSE;
541 static void vte_set_cursor_blink_mode(void)
543 if (vf->vte_terminal_set_cursor_blink_mode != NULL)
544 /* vte >= 0.17.1 */
545 vf->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc->vte),
546 (vc->cursor_blinks) ? VTE_CURSOR_BLINK_ON : VTE_CURSOR_BLINK_OFF);
547 else
548 /* vte < 0.17.1 */
549 vf->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc->vte), vc->cursor_blinks);
553 #if GTK_CHECK_VERSION(3, 0, 0)
554 static gboolean vte_is_2_91(void)
556 guint major = vf->vte_get_major_version ? vf->vte_get_major_version() : 0;
557 guint minor = vf->vte_get_minor_version ? vf->vte_get_minor_version() : 0;
559 /* 2.91 API started at 0.38 */
560 return ((major > 0 || (major == 0 && minor >= 38)) ||
561 /* 0.38 doesn't have runtime version checks, so check a symbol that didn't exist before */
562 vf->vte_terminal_spawn_sync != NULL);
564 #endif
567 static gboolean vte_register_symbols(GModule *mod)
569 #define BIND_SYMBOL_FULL(name, dest) \
570 g_module_symbol(mod, name, (void*)(dest))
571 #define BIND_SYMBOL(field) \
572 BIND_SYMBOL_FULL(#field, &vf->field)
573 #define BIND_REQUIRED_SYMBOL_FULL(name, dest) \
574 G_STMT_START { \
575 if (! BIND_SYMBOL_FULL(name, dest)) \
577 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
578 g_module_name(mod), name); \
579 return FALSE; \
581 } G_STMT_END
582 #define BIND_REQUIRED_SYMBOL(field) \
583 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field)
584 #define BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(field) \
585 G_STMT_START { \
586 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field##_rgba); \
587 vf->field = wrap_##field; \
588 } G_STMT_END
590 BIND_SYMBOL(vte_get_major_version);
591 BIND_SYMBOL(vte_get_minor_version);
592 BIND_REQUIRED_SYMBOL(vte_terminal_new);
593 BIND_REQUIRED_SYMBOL(vte_terminal_set_size);
594 if (! BIND_SYMBOL(vte_terminal_spawn_sync))
595 /* vte_terminal_spawn_sync() is available only in 0.38 */
596 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command);
597 /* 0.38 removed vte_terminal_set_word_chars() */
598 BIND_SYMBOL(vte_terminal_set_word_chars);
599 /* 0.40 introduced it under a different API */
600 BIND_SYMBOL(vte_terminal_set_word_char_exceptions);
601 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide);
602 BIND_REQUIRED_SYMBOL(vte_terminal_reset);
603 BIND_REQUIRED_SYMBOL(vte_terminal_get_type);
604 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output);
605 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke);
606 BIND_REQUIRED_SYMBOL(vte_terminal_set_font);
607 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines);
608 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection);
609 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard);
610 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard);
611 #if GTK_CHECK_VERSION(3, 0, 0)
612 if (vte_is_2_91())
614 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_foreground);
615 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_bold);
616 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_background);
618 else
619 #endif
621 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground);
622 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold);
623 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background);
625 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child);
626 BIND_SYMBOL(vte_terminal_im_append_menuitems);
627 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode))
628 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
629 * this symbol, we are probably on an older version and use the old API instead */
630 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks);
631 BIND_REQUIRED_SYMBOL(vte_terminal_select_all);
632 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell);
633 if (! BIND_SYMBOL(vte_terminal_get_adjustment))
634 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
635 vf->vte_terminal_get_adjustment = default_vte_terminal_get_adjustment;
637 #undef BIND_REQUIRED_SYMBOL_RGBA_WRAPPED
638 #undef BIND_REQUIRED_SYMBOL
639 #undef BIND_REQUIRED_SYMBOL_FULL
640 #undef BIND_SYMBOL
641 #undef BIND_SYMBOL_FULL
643 return TRUE;
647 void vte_apply_user_settings(void)
649 PangoFontDescription *font_desc;
651 if (! ui_prefs.msgwindow_visible)
652 return;
654 vf->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte), vc->scrollback_lines);
655 vf->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc->vte), vc->scroll_on_key);
656 vf->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc->vte), vc->scroll_on_out);
657 font_desc = pango_font_description_from_string(vc->font);
658 vf->vte_terminal_set_font(VTE_TERMINAL(vc->vte), font_desc);
659 pango_font_description_free(font_desc);
660 vf->vte_terminal_set_color_foreground(VTE_TERMINAL(vc->vte), &vc->colour_fore);
661 vf->vte_terminal_set_color_bold(VTE_TERMINAL(vc->vte), &vc->colour_fore);
662 vf->vte_terminal_set_color_background(VTE_TERMINAL(vc->vte), &vc->colour_back);
663 vf->vte_terminal_set_audible_bell(VTE_TERMINAL(vc->vte), prefs.beep_on_errors);
664 vte_set_cursor_blink_mode();
666 override_menu_key();
670 static void vte_popup_menu_clicked(GtkMenuItem *menuitem, gpointer user_data)
672 switch (GPOINTER_TO_INT(user_data))
674 case POPUP_COPY:
676 if (vf->vte_terminal_get_has_selection(VTE_TERMINAL(vc->vte)))
677 vf->vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte));
678 break;
680 case POPUP_PASTE:
682 vf->vte_terminal_paste_clipboard(VTE_TERMINAL(vc->vte));
683 break;
685 case POPUP_SELECTALL:
687 vte_select_all();
688 break;
690 case POPUP_CHANGEPATH:
692 GeanyDocument *doc = document_get_current();
693 if (doc != NULL)
694 vte_cwd(doc->file_name, TRUE);
695 break;
697 case POPUP_RESTARTTERMINAL:
699 vte_restart(vc->vte);
700 break;
702 case POPUP_PREFERENCES:
704 GtkWidget *notebook, *tab_page;
706 prefs_show_dialog();
708 notebook = ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2");
709 tab_page = ui_lookup_widget(ui_widgets.prefs_dialog, "frame_term");
711 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook),
712 gtk_notebook_page_num(GTK_NOTEBOOK(notebook), GTK_WIDGET(tab_page)));
714 break;
720 static GtkWidget *vte_create_popup_menu(void)
722 GtkWidget *menu, *item;
723 GtkAccelGroup *accel_group;
724 gboolean show_im_menu = TRUE;
726 menu = gtk_menu_new();
728 accel_group = gtk_accel_group_new();
729 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), accel_group);
731 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY, NULL);
732 gtk_widget_add_accelerator(item, "activate", accel_group,
733 GDK_c, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
734 gtk_widget_show(item);
735 gtk_container_add(GTK_CONTAINER(menu), item);
736 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_COPY));
738 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE, NULL);
739 gtk_widget_add_accelerator(item, "activate", accel_group,
740 GDK_v, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
741 gtk_widget_show(item);
742 gtk_container_add(GTK_CONTAINER(menu), item);
743 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_PASTE));
745 item = gtk_separator_menu_item_new();
746 gtk_widget_show(item);
747 gtk_container_add(GTK_CONTAINER(menu), item);
749 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL, NULL);
750 gtk_widget_show(item);
751 gtk_container_add(GTK_CONTAINER(menu), item);
752 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_SELECTALL));
754 item = gtk_separator_menu_item_new();
755 gtk_widget_show(item);
756 gtk_container_add(GTK_CONTAINER(menu), item);
758 item = gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
759 gtk_widget_show(item);
760 gtk_container_add(GTK_CONTAINER(menu), item);
761 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_CHANGEPATH));
763 item = gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
764 gtk_widget_show(item);
765 gtk_container_add(GTK_CONTAINER(menu), item);
766 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_RESTARTTERMINAL));
768 item = gtk_separator_menu_item_new();
769 gtk_widget_show(item);
770 gtk_container_add(GTK_CONTAINER(menu), item);
772 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
773 gtk_widget_show(item);
774 gtk_container_add(GTK_CONTAINER(menu), item);
775 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_PREFERENCES));
777 msgwin_menu_add_common_items(GTK_MENU(menu));
779 /* VTE 2.91 doesn't have IM context items, and GTK >= 3.10 doesn't show them anyway */
780 if (! vf->vte_terminal_im_append_menuitems || gtk_check_version(3, 10, 0) == NULL)
781 show_im_menu = FALSE;
782 else /* otherwise, query the setting */
783 g_object_get(gtk_settings_get_default(), "gtk-show-input-method-menu", &show_im_menu, NULL);
785 if (! show_im_menu)
786 vc->im_submenu = NULL;
787 else
789 item = gtk_separator_menu_item_new();
790 gtk_widget_show(item);
791 gtk_container_add(GTK_CONTAINER(menu), item);
793 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
794 vc->im_submenu = gtk_menu_new();
796 item = gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
797 gtk_widget_show(item);
798 gtk_container_add(GTK_CONTAINER(menu), item);
800 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), vc->im_submenu);
801 /* submenu populated after vte realized */
804 return menu;
808 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
809 * on the prompt). */
810 gboolean vte_send_cmd(const gchar *cmd)
812 if (clean)
814 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), cmd, strlen(cmd));
815 set_clean(TRUE); /* vte_terminal_feed_child() also marks the vte as not clean */
816 return TRUE;
818 else
819 return FALSE;
823 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
824 * Determines the working directory using various OS-specific mechanisms and stores the determined
825 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
826 const gchar *vte_get_working_directory(void)
828 if (pid > 0)
830 gchar buffer[4096 + 1];
831 gchar *file = g_strdup_printf("/proc/%d/cwd", pid);
832 gint length = readlink(file, buffer, sizeof(buffer));
834 if (length > 0 && *buffer == '/')
836 buffer[length] = '\0';
837 g_free(vte_info.dir);
838 vte_info.dir = g_strdup(buffer);
840 else if (length == 0)
842 gchar *cwd = g_get_current_dir();
844 if (cwd != NULL)
846 if (chdir(file) == 0)
848 g_free(vte_info.dir);
849 vte_info.dir = g_get_current_dir();
850 if (chdir(cwd) != 0)
851 geany_debug("%s: %s", G_STRFUNC, g_strerror(errno));
853 g_free(cwd);
856 g_free(file);
859 return vte_info.dir;
863 /* Changes the current working directory of the VTE to the path of the given filename.
864 * filename is expected to be in UTF-8 encoding.
865 * filename can also be a path, then it is used directly.
866 * If force is set to TRUE, it will always change the cwd
868 void vte_cwd(const gchar *filename, gboolean force)
870 if (vte_info.have_vte && (vc->follow_path || force) &&
871 filename != NULL && g_path_is_absolute(filename))
873 gchar *path;
875 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
876 path = g_strdup(filename);
877 else
878 path = g_path_get_dirname(filename);
880 vte_get_working_directory(); /* refresh vte_info.dir */
881 if (! utils_str_equal(path, vte_info.dir))
883 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
884 gchar *quoted_path = g_shell_quote(path);
885 gchar *cmd = g_strconcat(vc->send_cmd_prefix, "cd ", quoted_path, "\n", NULL);
886 if (! vte_send_cmd(cmd))
888 const gchar *msg = _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
889 ui_set_statusbar(FALSE, "%s", msg);
890 geany_debug("%s", msg);
892 g_free(quoted_path);
893 g_free(cmd);
895 g_free(path);
900 static void vte_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
901 gint x, gint y, GtkSelectionData *data, guint info, guint ltime)
903 if (info == TARGET_TEXT_PLAIN)
905 if (gtk_selection_data_get_format(data) == 8 && gtk_selection_data_get_length(data) > 0)
906 vf->vte_terminal_feed_child(VTE_TERMINAL(widget),
907 (const gchar*) gtk_selection_data_get_data(data),
908 gtk_selection_data_get_length(data));
910 else
912 gchar *text = (gchar*) gtk_selection_data_get_text(data);
913 if (!EMPTY(text))
914 vf->vte_terminal_feed_child(VTE_TERMINAL(widget), text, strlen(text));
915 g_free(text);
917 gtk_drag_finish(drag_context, TRUE, FALSE, ltime);
921 static void on_check_run_in_vte_toggled(GtkToggleButton *togglebutton, GtkWidget *user_data)
923 g_return_if_fail(GTK_IS_WIDGET(user_data));
924 gtk_widget_set_sensitive(user_data, gtk_toggle_button_get_active(togglebutton));
928 static void on_term_font_set(GtkFontButton *widget, gpointer user_data)
930 const gchar *fontbtn = gtk_font_button_get_font_name(widget);
932 if (! utils_str_equal(fontbtn, vc->font))
934 SETPTR(vc->font, g_strdup(gtk_font_button_get_font_name(widget)));
935 vte_apply_user_settings();
940 static void on_term_fg_color_set(GtkColorButton *widget, gpointer user_data)
942 gtk_color_button_get_color(widget, &vc->colour_fore);
946 static void on_term_bg_color_set(GtkColorButton *widget, gpointer user_data)
948 gtk_color_button_get_color(widget, &vc->colour_back);
952 void vte_append_preferences_tab(void)
954 if (vte_info.have_vte)
956 GtkWidget *frame_term, *button_shell, *entry_shell;
957 GtkWidget *check_run_in_vte, *check_skip_script;
958 GtkWidget *font_button, *fg_color_button, *bg_color_button;
960 button_shell = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "button_term_shell"));
961 entry_shell = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "entry_shell"));
962 ui_setup_open_button_callback(button_shell, NULL,
963 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_ENTRY(entry_shell));
965 check_skip_script = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "check_skip_script"));
966 gtk_widget_set_sensitive(check_skip_script, vc->run_in_vte);
968 check_run_in_vte = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "check_run_in_vte"));
969 g_signal_connect(G_OBJECT(check_run_in_vte), "toggled",
970 G_CALLBACK(on_check_run_in_vte_toggled), check_skip_script);
972 font_button = ui_lookup_widget(ui_widgets.prefs_dialog, "font_term");
973 g_signal_connect(font_button, "font-set", G_CALLBACK(on_term_font_set), NULL);
975 fg_color_button = ui_lookup_widget(ui_widgets.prefs_dialog, "color_fore");
976 g_signal_connect(fg_color_button, "color-set", G_CALLBACK(on_term_fg_color_set), NULL);
978 bg_color_button = ui_lookup_widget(ui_widgets.prefs_dialog, "color_back");
979 g_signal_connect(bg_color_button, "color-set", G_CALLBACK(on_term_bg_color_set), NULL);
981 frame_term = ui_lookup_widget(ui_widgets.prefs_dialog, "frame_term");
982 gtk_widget_show_all(frame_term);
987 void vte_select_all(void)
989 if (vf->vte_terminal_select_all != NULL)
990 vf->vte_terminal_select_all(VTE_TERMINAL(vc->vte));
994 void vte_send_selection_to_vte(void)
996 GeanyDocument *doc;
997 gchar *text;
998 gsize len;
1000 doc = document_get_current();
1001 g_return_if_fail(doc != NULL);
1003 if (sci_has_selection(doc->editor->sci))
1005 text = sci_get_selection_contents(doc->editor->sci);
1007 else
1008 { /* Get the current line */
1009 gint line_num = sci_get_current_line(doc->editor->sci);
1010 text = sci_get_line(doc->editor->sci, line_num);
1013 len = strlen(text);
1015 if (vc->send_selection_unsafe)
1016 { /* Explicitly append a trailing newline character to get the command executed,
1017 this is disabled by default as it could cause all sorts of damage. */
1018 if (text[len-1] != '\n' && text[len-1] != '\r')
1020 SETPTR(text, g_strconcat(text, "\n", NULL));
1021 len++;
1024 else
1025 { /* Make sure there is no newline character at the end to prevent unwanted execution */
1026 while (text[len-1] == '\n' || text[len-1] == '\r')
1028 text[len-1] = '\0';
1029 len--;
1033 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), text, len);
1035 /* show the VTE */
1036 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
1037 gtk_widget_grab_focus(vc->vte);
1038 msgwin_show_hide(TRUE);
1040 g_free(text);
1044 #endif