2 * vte.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
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.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Virtual Terminal Emulation setup and handling code, using the libvte plugin library.
33 #include "callbacks.h"
35 #include "geanyobject.h"
36 #include "msgwindow.h"
38 #include "sciwrappers.h"
42 #include "keybindings.h"
44 /* include stdlib.h AND unistd.h, because on GNU/Linux pid_t seems to be
45 * in stdlib.h, on FreeBSD in unistd.h, sys/types.h is needed for C89 */
47 #include <sys/types.h>
51 #include <gdk/gdkkeysyms.h>
57 VteInfo vte_info
= { FALSE
, FALSE
, FALSE
, NULL
, NULL
};
61 static gboolean clean
= TRUE
;
62 static GModule
*module
= NULL
;
63 static struct VteFunctions
*vf
;
64 static gchar
*gtk_menu_key_accel
= NULL
;
65 static GtkWidget
*terminal_label
= NULL
;
66 static guint terminal_label_update_source
= 0;
68 /* use vte wordchars to select paths */
69 static const gchar VTE_WORDCHARS
[] = "-A-Za-z0-9,./?%&#:_";
70 static const gchar VTE_ADDITIONAL_WORDCHARS
[] = "-,./?%&#:_";
73 /* Incomplete VteTerminal struct from vte/vte.h. */
74 typedef struct _VteTerminal VteTerminal
;
78 GtkAdjustment
*adjustment
;
81 #define VTE_TERMINAL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), VTE_TYPE_TERMINAL, VteTerminal))
82 #define VTE_TYPE_TERMINAL (vf->vte_terminal_get_type())
85 VTE_CURSOR_BLINK_SYSTEM
,
88 } VteTerminalCursorBlinkMode
;
91 /* we don't care for the other possible values */
96 /* Holds function pointers we need to access the VTE API. */
99 guint (*vte_get_major_version
) (void);
100 guint (*vte_get_minor_version
) (void);
101 GtkWidget
* (*vte_terminal_new
) (void);
102 pid_t (*vte_terminal_fork_command
) (VteTerminal
*terminal
, const char *command
, char **argv
,
103 char **envv
, const char *directory
, gboolean lastlog
,
104 gboolean utmp
, gboolean wtmp
);
105 gboolean (*vte_terminal_spawn_sync
) (VteTerminal
*terminal
, VtePtyFlags pty_flags
,
106 const char *working_directory
, char **argv
, char **envv
,
107 GSpawnFlags spawn_flags
, GSpawnChildSetupFunc child_setup
,
108 gpointer child_setup_data
, GPid
*child_pid
,
109 GCancellable
*cancellable
, GError
**error
);
110 void (*vte_terminal_set_size
) (VteTerminal
*terminal
, glong columns
, glong rows
);
111 void (*vte_terminal_set_word_chars
) (VteTerminal
*terminal
, const char *spec
);
112 void (*vte_terminal_set_word_char_exceptions
) (VteTerminal
*terminal
, const char *exceptions
);
113 void (*vte_terminal_set_mouse_autohide
) (VteTerminal
*terminal
, gboolean setting
);
114 void (*vte_terminal_reset
) (VteTerminal
*terminal
, gboolean full
, gboolean clear_history
);
115 GType (*vte_terminal_get_type
) (void);
116 void (*vte_terminal_set_scroll_on_output
) (VteTerminal
*terminal
, gboolean scroll
);
117 void (*vte_terminal_set_scroll_on_keystroke
) (VteTerminal
*terminal
, gboolean scroll
);
118 void (*vte_terminal_set_font
) (VteTerminal
*terminal
, const PangoFontDescription
*font_desc
);
119 void (*vte_terminal_set_scrollback_lines
) (VteTerminal
*terminal
, glong lines
);
120 gboolean (*vte_terminal_get_has_selection
) (VteTerminal
*terminal
);
121 void (*vte_terminal_copy_clipboard
) (VteTerminal
*terminal
);
122 void (*vte_terminal_paste_clipboard
) (VteTerminal
*terminal
);
123 void (*vte_terminal_set_color_foreground
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
124 void (*vte_terminal_set_color_bold
) (VteTerminal
*terminal
, const GdkColor
*foreground
);
125 void (*vte_terminal_set_color_background
) (VteTerminal
*terminal
, const GdkColor
*background
);
126 void (*vte_terminal_feed_child
) (VteTerminal
*terminal
, const char *data
, glong length
);
127 void (*vte_terminal_im_append_menuitems
) (VteTerminal
*terminal
, GtkMenuShell
*menushell
);
128 void (*vte_terminal_set_cursor_blink_mode
) (VteTerminal
*terminal
,
129 VteTerminalCursorBlinkMode mode
);
130 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
131 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
132 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
133 GtkAdjustment
* (*vte_terminal_get_adjustment
) (VteTerminal
*terminal
);
135 /* hack for the VTE 2.91 API using GdkRGBA: we wrap the API to keep using GdkColor on our side */
136 void (*vte_terminal_set_color_foreground_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
137 void (*vte_terminal_set_color_bold_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
138 void (*vte_terminal_set_color_background_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*background
);
142 static void create_vte(void);
143 static void vte_start(GtkWidget
*widget
);
144 static void vte_restart(GtkWidget
*widget
);
145 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
146 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
147 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
148 static gboolean
vte_register_symbols(GModule
*module
);
149 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
150 static GtkWidget
*vte_create_popup_menu(void);
151 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
152 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
153 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
162 POPUP_RESTARTTERMINAL
,
164 TARGET_UTF8_STRING
= 0,
166 TARGET_COMPOUND_TEXT
,
171 static const GtkTargetEntry dnd_targets
[] =
173 { "UTF8_STRING", 0, TARGET_UTF8_STRING
},
174 { "TEXT", 0, TARGET_TEXT
},
175 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
176 { "STRING", 0, TARGET_STRING
},
177 { "text/plain", 0, TARGET_TEXT_PLAIN
},
181 /* replacement for vte_terminal_get_adjustment() when it's not available */
182 static GtkAdjustment
*default_vte_terminal_get_adjustment(VteTerminal
*vte
)
184 if (GTK_IS_SCROLLABLE(vte
))
185 return gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte
));
186 /* this is only valid in < 0.38, 0.38 broke ABI */
187 return vte
->adjustment
;
191 /* Wrap VTE 2.91 API using GdkRGBA with GdkColor so we use a single API on our side */
193 static void rgba_from_color(GdkRGBA
*rgba
, const GdkColor
*color
)
195 rgba
->red
= color
->red
/ 65535.0;
196 rgba
->green
= color
->green
/ 65535.0;
197 rgba
->blue
= color
->blue
/ 65535.0;
201 #define WRAP_RGBA_SETTER(name) \
202 static void wrap_##name(VteTerminal *terminal, const GdkColor *color) \
205 rgba_from_color(&rgba, color); \
206 vf->name##_rgba(terminal, &rgba); \
209 WRAP_RGBA_SETTER(vte_terminal_set_color_background
)
210 WRAP_RGBA_SETTER(vte_terminal_set_color_bold
)
211 WRAP_RGBA_SETTER(vte_terminal_set_color_foreground
)
213 #undef WRAP_RGBA_SETTER
216 static gchar
**vte_get_child_environment(void)
218 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL
};
220 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
224 static void override_menu_key(void)
226 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
227 g_object_get(G_OBJECT(gtk_settings_get_default()),
228 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
230 if (vte_config
.ignore_menu_bar_accel
)
231 gtk_settings_set_string_property(gtk_settings_get_default(),
232 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
234 gtk_settings_set_string_property(gtk_settings_get_default(),
235 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
239 static void on_startup_complete(G_GNUC_UNUSED GObject
*dummy
)
241 GeanyDocument
*doc
= document_get_current();
243 /* ensure the widget is mapped and fully initialized, so actions like pasting text work
244 * (see https://github.com/geany/geany/issues/2813 for details) */
245 gtk_widget_realize(vte_config
.vte
);
248 vte_cwd((doc
->real_path
!= NULL
) ? doc
->real_path
: doc
->file_name
, FALSE
);
254 if (vte_info
.have_vte
== FALSE
)
255 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
256 geany_debug("Disabling terminal support");
260 if (!EMPTY(vte_info
.lib_vte
))
262 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
264 #ifdef VTE_MODULE_PATH
267 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
274 const gchar
*sonames
[] = {
276 "libvte-2.91.0.dylib", "libvte-2.91.dylib",
277 "libvte2_90.9.dylib", "libvte2_90.dylib",
279 "libvte-2.91.so", "libvte-2.91.so.0",
280 "libvte2_90.so", "libvte2_90.so.9",
284 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
286 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
292 vte_info
.have_vte
= FALSE
;
293 geany_debug("Could not load libvte.so, embedded terminal support disabled");
298 geany_debug("Loaded libvte from %s", g_module_name(module
));
299 vf
= g_new0(struct VteFunctions
, 1);
300 if (vte_register_symbols(module
))
301 vte_info
.have_vte
= TRUE
;
304 vte_info
.have_vte
= FALSE
;
306 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
307 /*g_module_close(module);*/
315 /* setup the F10 menu override (so it works before the widget is first realised). */
318 g_signal_connect(geany_object
, "geany-startup-complete", G_CALLBACK(on_startup_complete
), NULL
);
322 static void on_vte_realize(void)
324 /* the vte widget has to be realised before color changes take effect */
325 vte_apply_user_settings();
327 if (vf
->vte_terminal_im_append_menuitems
&& vte_config
.im_submenu
)
328 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vte_config
.vte
), GTK_MENU_SHELL(vte_config
.im_submenu
));
332 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
334 vte_start(vte_config
.vte
);
339 static void create_vte(void)
341 GtkWidget
*vte
, *scrollbar
, *hbox
;
343 vte_config
.vte
= vte
= vf
->vte_terminal_new();
344 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vf
->vte_terminal_get_adjustment(VTE_TERMINAL(vte
)));
345 gtk_widget_set_can_focus(scrollbar
, FALSE
);
347 /* create menu now so copy/paste shortcuts work */
348 vte_config
.menu
= vte_create_popup_menu();
349 g_object_ref_sink(vte_config
.menu
);
351 hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
352 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
353 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
355 /* set the default widget size first to prevent VTE expanding too much,
356 * sometimes causing the hscrollbar to be too big or out of view. */
357 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
358 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
360 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
361 if (vf
->vte_terminal_set_word_chars
)
362 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
363 else if (vf
->vte_terminal_set_word_char_exceptions
)
364 vf
->vte_terminal_set_word_char_exceptions(VTE_TERMINAL(vte
), VTE_ADDITIONAL_WORDCHARS
);
366 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
367 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
369 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
370 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
371 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
372 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
373 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
374 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
375 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
377 /* start shell on idle otherwise the initial prompt can get corrupted */
378 g_idle_add(vte_start_idle
, NULL
);
380 gtk_widget_show_all(hbox
);
381 terminal_label
= gtk_label_new(_("Terminal"));
382 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
384 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
390 /* free the vte widget before unloading vte module
391 * this prevents a segfault on X close window if the message window is hidden */
392 g_signal_handlers_disconnect_by_func(vte_config
.vte
, G_CALLBACK(vte_start
), NULL
);
393 gtk_widget_destroy(vte_config
.vte
);
394 gtk_widget_destroy(vte_config
.menu
);
395 g_object_unref(vte_config
.menu
);
396 g_free(vte_config
.shell
);
397 g_free(vte_config
.font
);
398 g_free(vte_config
.send_cmd_prefix
);
400 g_free(gtk_menu_key_accel
);
401 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
402 * happens when the app really exits, not directly on g_module_close(). This still needs to
403 * be investigated. */
404 /*g_module_close(module); */
408 static gboolean
set_dirty_idle(gpointer user_data
)
410 gtk_widget_set_name(terminal_label
, "geany-terminal-dirty");
411 terminal_label_update_source
= 0;
416 static void set_clean(gboolean value
)
422 if (terminal_label_update_source
> 0)
424 g_source_remove(terminal_label_update_source
);
425 terminal_label_update_source
= 0;
428 gtk_widget_set_name(terminal_label
, NULL
);
430 terminal_label_update_source
= g_timeout_add(150, set_dirty_idle
, NULL
);
437 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
439 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
440 ((event
->keyval
== GDK_KEY_c
) && (event
->state
& GDK_CONTROL_MASK
)))
442 /* assume any text on the prompt has been executed when pressing Enter/Return */
449 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
451 if (vte_config
.enable_bash_keys
)
452 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
454 if (event
->type
!= GDK_KEY_RELEASE
)
457 if ((event
->keyval
== GDK_KEY_c
||
458 event
->keyval
== GDK_KEY_d
||
459 event
->keyval
== GDK_KEY_C
||
460 event
->keyval
== GDK_KEY_D
) &&
461 event
->state
& GDK_CONTROL_MASK
&&
462 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
471 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
477 static void vte_start(GtkWidget
*widget
)
479 /* split the shell command line, so arguments will work too */
480 gchar
**argv
= g_strsplit(vte_config
.shell
, " ", -1);
484 gchar
**env
= vte_get_child_environment();
486 if (vf
->vte_terminal_spawn_sync
)
488 if (! vf
->vte_terminal_spawn_sync(VTE_TERMINAL(widget
), VTE_PTY_DEFAULT
,
489 vte_info
.dir
, argv
, env
, 0, NULL
, NULL
,
497 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
498 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
504 pid
= 0; /* use 0 as invalid pid */
510 static void vte_restart(GtkWidget
*widget
)
512 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
518 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
524 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
526 if (event
->button
== 3)
528 gtk_widget_grab_focus(vte_config
.vte
);
529 ui_menu_popup(GTK_MENU(vte_config
.menu
), NULL
, NULL
, event
->button
, event
->time
);
532 else if (event
->button
== 2)
534 gtk_widget_grab_focus(widget
);
540 static void vte_set_cursor_blink_mode(void)
542 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
544 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vte_config
.vte
),
545 (vte_config
.cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
548 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vte_config
.vte
), vte_config
.cursor_blinks
);
552 static gboolean
vte_is_2_91(void)
554 guint major
= vf
->vte_get_major_version
? vf
->vte_get_major_version() : 0;
555 guint minor
= vf
->vte_get_minor_version
? vf
->vte_get_minor_version() : 0;
557 /* 2.91 API started at 0.38 */
558 return ((major
> 0 || (major
== 0 && minor
>= 38)) ||
559 /* 0.38 doesn't have runtime version checks, so check a symbol that didn't exist before */
560 vf
->vte_terminal_spawn_sync
!= NULL
);
564 static gboolean
vte_register_symbols(GModule
*mod
)
566 #define BIND_SYMBOL_FULL(name, dest) \
567 g_module_symbol(mod, name, (void*)(dest))
568 #define BIND_SYMBOL(field) \
569 BIND_SYMBOL_FULL(#field, &vf->field)
570 #define BIND_REQUIRED_SYMBOL_FULL(name, dest) \
572 if (! BIND_SYMBOL_FULL(name, dest)) \
574 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
575 g_module_name(mod), name); \
579 #define BIND_REQUIRED_SYMBOL(field) \
580 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field)
581 #define BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(field) \
583 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field##_rgba); \
584 vf->field = wrap_##field; \
587 BIND_SYMBOL(vte_get_major_version
);
588 BIND_SYMBOL(vte_get_minor_version
);
589 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
590 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
591 if (! BIND_SYMBOL(vte_terminal_spawn_sync
))
592 /* vte_terminal_spawn_sync() is available only in 0.38 */
593 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
594 /* 0.38 removed vte_terminal_set_word_chars() */
595 BIND_SYMBOL(vte_terminal_set_word_chars
);
596 /* 0.40 introduced it under a different API */
597 BIND_SYMBOL(vte_terminal_set_word_char_exceptions
);
598 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
599 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
600 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
601 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
602 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
603 BIND_REQUIRED_SYMBOL(vte_terminal_set_font
);
604 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
605 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
606 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
607 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
611 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_foreground
);
612 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_bold
);
613 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_background
);
617 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
618 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
619 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
621 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
622 BIND_SYMBOL(vte_terminal_im_append_menuitems
);
623 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
624 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
625 * this symbol, we are probably on an older version and use the old API instead */
626 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
627 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
628 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
629 if (! BIND_SYMBOL(vte_terminal_get_adjustment
))
630 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
631 vf
->vte_terminal_get_adjustment
= default_vte_terminal_get_adjustment
;
633 #undef BIND_REQUIRED_SYMBOL_RGBA_WRAPPED
634 #undef BIND_REQUIRED_SYMBOL
635 #undef BIND_REQUIRED_SYMBOL_FULL
637 #undef BIND_SYMBOL_FULL
643 void vte_apply_user_settings(void)
645 VteConfig
*vc
= &vte_config
;
646 PangoFontDescription
*font_desc
;
648 if (! ui_prefs
.msgwindow_visible
)
651 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
652 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
653 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
654 font_desc
= pango_font_description_from_string(vc
->font
);
655 vf
->vte_terminal_set_font(VTE_TERMINAL(vc
->vte
), font_desc
);
656 pango_font_description_free(font_desc
);
657 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
658 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
659 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
660 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
661 vte_set_cursor_blink_mode();
667 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
669 switch (GPOINTER_TO_INT(user_data
))
673 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vte_config
.vte
)))
674 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vte_config
.vte
));
679 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vte_config
.vte
));
682 case POPUP_SELECTALL
:
687 case POPUP_CHANGEPATH
:
689 GeanyDocument
*doc
= document_get_current();
691 vte_cwd(doc
->file_name
, TRUE
);
694 case POPUP_RESTARTTERMINAL
:
696 vte_restart(vte_config
.vte
);
699 case POPUP_PREFERENCES
:
701 GtkWidget
*notebook
, *tab_page
;
705 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
706 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
708 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
709 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
717 static GtkWidget
*vte_create_popup_menu(void)
719 GtkWidget
*menu
, *item
;
720 GtkAccelGroup
*accel_group
;
721 gboolean show_im_menu
= TRUE
;
723 menu
= gtk_menu_new();
725 accel_group
= gtk_accel_group_new();
726 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
728 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
729 gtk_widget_add_accelerator(item
, "activate", accel_group
,
730 GDK_KEY_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
731 gtk_widget_show(item
);
732 gtk_container_add(GTK_CONTAINER(menu
), item
);
733 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
735 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
736 gtk_widget_add_accelerator(item
, "activate", accel_group
,
737 GDK_KEY_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
738 gtk_widget_show(item
);
739 gtk_container_add(GTK_CONTAINER(menu
), item
);
740 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
742 item
= gtk_separator_menu_item_new();
743 gtk_widget_show(item
);
744 gtk_container_add(GTK_CONTAINER(menu
), item
);
746 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
747 gtk_widget_show(item
);
748 gtk_container_add(GTK_CONTAINER(menu
), item
);
749 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
751 item
= gtk_separator_menu_item_new();
752 gtk_widget_show(item
);
753 gtk_container_add(GTK_CONTAINER(menu
), item
);
755 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
756 gtk_widget_show(item
);
757 gtk_container_add(GTK_CONTAINER(menu
), item
);
758 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
760 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
761 gtk_widget_show(item
);
762 gtk_container_add(GTK_CONTAINER(menu
), item
);
763 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
765 item
= gtk_separator_menu_item_new();
766 gtk_widget_show(item
);
767 gtk_container_add(GTK_CONTAINER(menu
), item
);
769 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
770 gtk_widget_show(item
);
771 gtk_container_add(GTK_CONTAINER(menu
), item
);
772 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
774 msgwin_menu_add_common_items(GTK_MENU(menu
));
776 /* VTE 2.91 doesn't have IM context items, and GTK >= 3.10 doesn't show them anyway */
777 if (! vf
->vte_terminal_im_append_menuitems
|| gtk_check_version(3, 10, 0) == NULL
)
778 show_im_menu
= FALSE
;
779 else /* otherwise, query the setting */
780 g_object_get(gtk_settings_get_default(), "gtk-show-input-method-menu", &show_im_menu
, NULL
);
783 vte_config
.im_submenu
= NULL
;
786 item
= gtk_separator_menu_item_new();
787 gtk_widget_show(item
);
788 gtk_container_add(GTK_CONTAINER(menu
), item
);
790 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
791 vte_config
.im_submenu
= gtk_menu_new();
793 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
794 gtk_widget_show(item
);
795 gtk_container_add(GTK_CONTAINER(menu
), item
);
797 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vte_config
.im_submenu
);
798 /* submenu populated after vte realized */
805 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
807 gboolean
vte_send_cmd(const gchar
*cmd
)
809 g_return_val_if_fail(cmd
!= NULL
, FALSE
);
813 vf
->vte_terminal_feed_child(VTE_TERMINAL(vte_config
.vte
), cmd
, strlen(cmd
));
814 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
822 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
823 * Determines the working directory using various OS-specific mechanisms and stores the determined
824 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
825 const gchar
*vte_get_working_directory(void)
829 gchar buffer
[4096 + 1];
830 gchar
*file
= g_strdup_printf("/proc/%d/cwd", pid
);
831 gint length
= readlink(file
, buffer
, sizeof(buffer
));
833 if (length
> 0 && *buffer
== '/')
835 buffer
[length
] = '\0';
836 g_free(vte_info
.dir
);
837 vte_info
.dir
= g_strdup(buffer
);
839 else if (length
== 0)
841 gchar
*cwd
= g_get_current_dir();
845 if (chdir(file
) == 0)
847 g_free(vte_info
.dir
);
848 vte_info
.dir
= g_get_current_dir();
850 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
862 /* Changes the current working directory of the VTE to the path of the given filename.
863 * filename is expected to be in UTF-8 encoding.
864 * filename can also be a path, then it is used directly.
865 * If force is set to TRUE, it will always change the cwd
867 void vte_cwd(const gchar
*filename
, gboolean force
)
869 if (vte_info
.have_vte
&& (vte_config
.follow_path
|| force
) &&
870 filename
!= NULL
&& g_path_is_absolute(filename
))
874 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
875 path
= g_strdup(filename
);
877 path
= g_path_get_dirname(filename
);
879 vte_get_working_directory(); /* refresh vte_info.dir */
880 if (! utils_str_equal(path
, vte_info
.dir
))
882 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
883 gchar
*quoted_path
= g_shell_quote(path
);
884 const gchar
*cmd_prefix
= vte_config
.send_cmd_prefix
? vte_config
.send_cmd_prefix
: "";
885 gchar
*cmd
= g_strconcat(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
);
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
));
912 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
914 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(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
, vte_config
.font
))
934 SETPTR(vte_config
.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
, &vte_config
.colour_fore
);
946 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
948 gtk_color_button_get_color(widget
, &vte_config
.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
, vte_config
.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(vte_config
.vte
));
994 void vte_send_selection_to_vte(void)
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
);
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
);
1015 if (vte_config
.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
));
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')
1033 vf
->vte_terminal_feed_child(VTE_TERMINAL(vte_config
.vte
), text
, len
);
1036 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
1037 gtk_widget_grab_focus(vte_config
.vte
);
1038 msgwin_show_hide(TRUE
);