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 gtk_menu_popup_at_pointer(GTK_MENU(vte_config
.menu
), (GdkEvent
*) event
);
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
;
722 menu
= gtk_menu_new();
724 accel_group
= gtk_accel_group_new();
725 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
727 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
728 gtk_widget_add_accelerator(item
, "activate", accel_group
,
729 GDK_KEY_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
730 gtk_widget_show(item
);
731 gtk_container_add(GTK_CONTAINER(menu
), item
);
732 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
734 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
735 gtk_widget_add_accelerator(item
, "activate", accel_group
,
736 GDK_KEY_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
737 gtk_widget_show(item
);
738 gtk_container_add(GTK_CONTAINER(menu
), item
);
739 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
741 item
= gtk_separator_menu_item_new();
742 gtk_widget_show(item
);
743 gtk_container_add(GTK_CONTAINER(menu
), item
);
745 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
746 gtk_widget_show(item
);
747 gtk_container_add(GTK_CONTAINER(menu
), item
);
748 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
750 item
= gtk_separator_menu_item_new();
751 gtk_widget_show(item
);
752 gtk_container_add(GTK_CONTAINER(menu
), item
);
754 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
755 gtk_widget_show(item
);
756 gtk_container_add(GTK_CONTAINER(menu
), item
);
757 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
759 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
760 gtk_widget_show(item
);
761 gtk_container_add(GTK_CONTAINER(menu
), item
);
762 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
764 item
= gtk_separator_menu_item_new();
765 gtk_widget_show(item
);
766 gtk_container_add(GTK_CONTAINER(menu
), item
);
768 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
769 gtk_widget_show(item
);
770 gtk_container_add(GTK_CONTAINER(menu
), item
);
771 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
773 msgwin_menu_add_common_items(GTK_MENU(menu
));
775 vte_config
.im_submenu
= NULL
;
781 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
783 gboolean
vte_send_cmd(const gchar
*cmd
)
785 g_return_val_if_fail(cmd
!= NULL
, FALSE
);
789 vf
->vte_terminal_feed_child(VTE_TERMINAL(vte_config
.vte
), cmd
, strlen(cmd
));
790 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
798 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
799 * Determines the working directory using various OS-specific mechanisms and stores the determined
800 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
801 const gchar
*vte_get_working_directory(void)
805 gchar buffer
[4096 + 1];
806 gchar
*file
= g_strdup_printf("/proc/%d/cwd", pid
);
807 gint length
= readlink(file
, buffer
, sizeof(buffer
));
809 if (length
> 0 && *buffer
== '/')
811 buffer
[length
] = '\0';
812 g_free(vte_info
.dir
);
813 vte_info
.dir
= g_strdup(buffer
);
815 else if (length
== 0)
817 gchar
*cwd
= g_get_current_dir();
821 if (chdir(file
) == 0)
823 g_free(vte_info
.dir
);
824 vte_info
.dir
= g_get_current_dir();
826 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
838 /* Changes the current working directory of the VTE to the path of the given filename.
839 * filename is expected to be in UTF-8 encoding.
840 * filename can also be a path, then it is used directly.
841 * If force is set to TRUE, it will always change the cwd
843 void vte_cwd(const gchar
*filename
, gboolean force
)
845 if (vte_info
.have_vte
&& (vte_config
.follow_path
|| force
) &&
846 filename
!= NULL
&& g_path_is_absolute(filename
))
850 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
851 path
= g_strdup(filename
);
853 path
= g_path_get_dirname(filename
);
855 vte_get_working_directory(); /* refresh vte_info.dir */
856 if (! utils_str_equal(path
, vte_info
.dir
))
858 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
859 gchar
*quoted_path
= g_shell_quote(path
);
860 const gchar
*cmd_prefix
= vte_config
.send_cmd_prefix
? vte_config
.send_cmd_prefix
: "";
861 gchar
*cmd
= g_strconcat(cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
862 if (! vte_send_cmd(cmd
))
864 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
865 ui_set_statusbar(FALSE
, "%s", msg
);
866 geany_debug("%s", msg
);
876 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
877 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
879 if (info
== TARGET_TEXT_PLAIN
)
881 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
882 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
883 (const gchar
*) gtk_selection_data_get_data(data
),
884 gtk_selection_data_get_length(data
));
888 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
890 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
893 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
897 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
899 g_return_if_fail(GTK_IS_WIDGET(user_data
));
900 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
904 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
906 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
908 if (! utils_str_equal(fontbtn
, vte_config
.font
))
910 SETPTR(vte_config
.font
, g_strdup(gtk_font_button_get_font_name(widget
)));
911 vte_apply_user_settings();
916 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
918 gtk_color_button_get_color(widget
, &vte_config
.colour_fore
);
922 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
924 gtk_color_button_get_color(widget
, &vte_config
.colour_back
);
928 void vte_append_preferences_tab(void)
930 if (vte_info
.have_vte
)
932 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
933 GtkWidget
*check_run_in_vte
, *check_skip_script
;
934 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
936 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
937 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
938 ui_setup_open_button_callback(button_shell
, NULL
,
939 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
941 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
942 gtk_widget_set_sensitive(check_skip_script
, vte_config
.run_in_vte
);
944 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
945 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
946 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
948 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
949 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
951 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
952 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
954 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
955 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
957 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
958 gtk_widget_show_all(frame_term
);
963 void vte_select_all(void)
965 if (vf
->vte_terminal_select_all
!= NULL
)
966 vf
->vte_terminal_select_all(VTE_TERMINAL(vte_config
.vte
));
970 void vte_send_selection_to_vte(void)
976 doc
= document_get_current();
977 g_return_if_fail(doc
!= NULL
);
979 if (sci_has_selection(doc
->editor
->sci
))
981 text
= sci_get_selection_contents(doc
->editor
->sci
);
984 { /* Get the current line */
985 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
986 text
= sci_get_line(doc
->editor
->sci
, line_num
);
991 if (vte_config
.send_selection_unsafe
)
992 { /* Explicitly append a trailing newline character to get the command executed,
993 this is disabled by default as it could cause all sorts of damage. */
994 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
996 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
1001 { /* Make sure there is no newline character at the end to prevent unwanted execution */
1002 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
1009 vf
->vte_terminal_feed_child(VTE_TERMINAL(vte_config
.vte
), text
, len
);
1012 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
1013 gtk_widget_grab_focus(vte_config
.vte
);
1014 msgwin_show_hide(TRUE
);