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.
34 #include "callbacks.h"
36 #include "geanyobject.h"
37 #include "msgwindow.h"
39 #include "sciwrappers.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 */
50 #include <sys/types.h>
53 #include <gdk/gdkkeysyms.h>
59 VteInfo vte_info
= { FALSE
, FALSE
, FALSE
, NULL
, NULL
};
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
;
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())
87 VTE_CURSOR_BLINK_SYSTEM
,
90 } VteTerminalCursorBlinkMode
;
93 /* we don't care for the other possible values */
98 /* Holds function pointers we need to access the VTE API. */
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
);
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
);
165 POPUP_RESTARTTERMINAL
,
167 TARGET_UTF8_STRING
= 0,
169 TARGET_COMPOUND_TEXT
,
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
));
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;
207 # define WRAP_RGBA_SETTER(name) \
208 static void wrap_##name(VteTerminal *terminal, const GdkColor *color) \
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
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", >k_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");
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();
251 vte_cwd((doc
->real_path
!= NULL
) ? doc
->real_path
: doc
->file_name
, FALSE
);
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");
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
270 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
277 const gchar
*sonames
[] = {
278 #if GTK_CHECK_VERSION(3, 0, 0)
280 "libvte-2.91.0.dylib", "libvte-2.91.dylib",
281 "libvte2_90.9.dylib", "libvte2_90.dylib",
283 "libvte-2.91.so", "libvte-2.91.so.0",
284 "libvte2_90.so", "libvte2_90.so.9",
287 "libvte.9.dylib", "libvte.dylib",
289 "libvte.so", "libvte.so.9", "libvte.so.8", "libvte.so.4",
294 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
296 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
302 vte_info
.have_vte
= FALSE
;
303 geany_debug("Could not load libvte.so, embedded terminal support disabled");
308 geany_debug("Loaded libvte from %s", g_module_name(module
));
309 vf
= g_new0(struct VteFunctions
, 1);
310 if (vte_register_symbols(module
))
311 vte_info
.have_vte
= TRUE
;
314 vte_info
.have_vte
= FALSE
;
316 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
317 /*g_module_close(module);*/
325 /* setup the F10 menu override (so it works before the widget is first realised). */
328 g_signal_connect(geany_object
, "geany-startup-complete", G_CALLBACK(on_startup_complete
), NULL
);
332 static void on_vte_realize(void)
334 /* the vte widget has to be realised before color changes take effect */
335 vte_apply_user_settings();
337 if (vf
->vte_terminal_im_append_menuitems
&& vc
->im_submenu
)
338 vf
->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc
->vte
), GTK_MENU_SHELL(vc
->im_submenu
));
342 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
349 static void create_vte(void)
351 GtkWidget
*vte
, *scrollbar
, *hbox
;
353 vc
->vte
= vte
= vf
->vte_terminal_new();
354 scrollbar
= gtk_vscrollbar_new(vf
->vte_terminal_get_adjustment(VTE_TERMINAL(vte
)));
355 gtk_widget_set_can_focus(scrollbar
, FALSE
);
357 /* create menu now so copy/paste shortcuts work */
358 vc
->menu
= vte_create_popup_menu();
359 g_object_ref_sink(vc
->menu
);
361 hbox
= gtk_hbox_new(FALSE
, 0);
362 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
363 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
365 /* set the default widget size first to prevent VTE expanding too much,
366 * sometimes causing the hscrollbar to be too big or out of view. */
367 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
368 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
370 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
371 if (vf
->vte_terminal_set_word_chars
)
372 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
373 else if (vf
->vte_terminal_set_word_char_exceptions
)
374 vf
->vte_terminal_set_word_char_exceptions(VTE_TERMINAL(vte
), VTE_ADDITIONAL_WORDCHARS
);
376 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
377 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
379 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
380 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
381 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
382 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
383 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
384 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
385 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
387 /* start shell on idle otherwise the initial prompt can get corrupted */
388 g_idle_add(vte_start_idle
, NULL
);
390 gtk_widget_show_all(hbox
);
391 terminal_label
= gtk_label_new(_("Terminal"));
392 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
394 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
401 /* free the vte widget before unloading vte module
402 * this prevents a segfault on X close window if the message window is hidden */
403 gtk_widget_destroy(vc
->vte
);
404 gtk_widget_destroy(vc
->menu
);
405 g_object_unref(vc
->menu
);
408 g_free(vc
->send_cmd_prefix
);
410 g_free(gtk_menu_key_accel
);
411 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
412 * happens when the app really exits, not directly on g_module_close(). This still needs to
413 * be investigated. */
414 /*g_module_close(module); */
418 static gboolean
set_dirty_idle(gpointer user_data
)
420 gtk_widget_set_name(terminal_label
, "geany-terminal-dirty");
421 terminal_label_update_source
= 0;
426 static void set_clean(gboolean value
)
432 if (terminal_label_update_source
> 0)
434 g_source_remove(terminal_label_update_source
);
435 terminal_label_update_source
= 0;
438 gtk_widget_set_name(terminal_label
, NULL
);
440 terminal_label_update_source
= g_timeout_add(150, set_dirty_idle
, NULL
);
447 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
449 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
450 ((event
->keyval
== GDK_c
) && (event
->state
& GDK_CONTROL_MASK
)))
452 /* assume any text on the prompt has been executed when pressing Enter/Return */
459 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
461 if (vc
->enable_bash_keys
)
462 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
464 if (event
->type
!= GDK_KEY_RELEASE
)
467 if ((event
->keyval
== GDK_c
||
468 event
->keyval
== GDK_d
||
469 event
->keyval
== GDK_C
||
470 event
->keyval
== GDK_D
) &&
471 event
->state
& GDK_CONTROL_MASK
&&
472 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
481 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
487 static void vte_start(GtkWidget
*widget
)
489 /* split the shell command line, so arguments will work too */
490 gchar
**argv
= g_strsplit(vc
->shell
, " ", -1);
494 gchar
**env
= vte_get_child_environment();
496 if (vf
->vte_terminal_spawn_sync
)
498 if (! vf
->vte_terminal_spawn_sync(VTE_TERMINAL(widget
), VTE_PTY_DEFAULT
,
499 vte_info
.dir
, argv
, env
, 0, NULL
, NULL
,
507 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
508 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
514 pid
= 0; /* use 0 as invalid pid */
520 static void vte_restart(GtkWidget
*widget
)
522 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
528 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
533 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
535 if (event
->button
== 3)
537 gtk_widget_grab_focus(vc
->vte
);
538 gtk_menu_popup(GTK_MENU(vc
->menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
540 else if (event
->button
== 2)
542 gtk_widget_grab_focus(widget
);
548 static void vte_set_cursor_blink_mode(void)
550 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
552 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc
->vte
),
553 (vc
->cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
556 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc
->vte
), vc
->cursor_blinks
);
560 #if GTK_CHECK_VERSION(3, 0, 0)
561 static gboolean
vte_is_2_91(void)
563 guint major
= vf
->vte_get_major_version
? vf
->vte_get_major_version() : 0;
564 guint minor
= vf
->vte_get_minor_version
? vf
->vte_get_minor_version() : 0;
566 /* 2.91 API started at 0.38 */
567 return ((major
> 0 || (major
== 0 && minor
>= 38)) ||
568 /* 0.38 doesn't have runtime version checks, so check a symbol that didn't exist before */
569 vf
->vte_terminal_spawn_sync
!= NULL
);
574 static gboolean
vte_register_symbols(GModule
*mod
)
576 #define BIND_SYMBOL_FULL(name, dest) \
577 g_module_symbol(mod, name, (void*)(dest))
578 #define BIND_SYMBOL(field) \
579 BIND_SYMBOL_FULL(#field, &vf->field)
580 #define BIND_REQUIRED_SYMBOL_FULL(name, dest) \
582 if (! BIND_SYMBOL_FULL(name, dest)) \
584 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
585 g_module_name(mod), name); \
589 #define BIND_REQUIRED_SYMBOL(field) \
590 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field)
591 #define BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(field) \
593 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field##_rgba); \
594 vf->field = wrap_##field; \
597 BIND_SYMBOL(vte_get_major_version
);
598 BIND_SYMBOL(vte_get_minor_version
);
599 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
600 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
601 if (! BIND_SYMBOL(vte_terminal_spawn_sync
))
602 /* vte_terminal_spawn_sync() is available only in 0.38 */
603 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
604 /* 0.38 removed vte_terminal_set_word_chars() */
605 BIND_SYMBOL(vte_terminal_set_word_chars
);
606 /* 0.40 introduced it under a different API */
607 BIND_SYMBOL(vte_terminal_set_word_char_exceptions
);
608 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
609 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
610 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
611 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
612 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
613 BIND_REQUIRED_SYMBOL(vte_terminal_set_font
);
614 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
615 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
616 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
617 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
618 #if GTK_CHECK_VERSION(3, 0, 0)
621 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_foreground
);
622 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_bold
);
623 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_background
);
628 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
629 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
630 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
632 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
633 BIND_SYMBOL(vte_terminal_im_append_menuitems
);
634 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
635 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
636 * this symbol, we are probably on an older version and use the old API instead */
637 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
638 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
639 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
640 if (! BIND_SYMBOL(vte_terminal_get_adjustment
))
641 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
642 vf
->vte_terminal_get_adjustment
= default_vte_terminal_get_adjustment
;
644 #undef BIND_REQUIRED_SYMBOL_RGBA_WRAPPED
645 #undef BIND_REQUIRED_SYMBOL
646 #undef BIND_REQUIRED_SYMBOL_FULL
648 #undef BIND_SYMBOL_FULL
654 void vte_apply_user_settings(void)
656 PangoFontDescription
*font_desc
;
658 if (! ui_prefs
.msgwindow_visible
)
661 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
662 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
663 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
664 font_desc
= pango_font_description_from_string(vc
->font
);
665 vf
->vte_terminal_set_font(VTE_TERMINAL(vc
->vte
), font_desc
);
666 pango_font_description_free(font_desc
);
667 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
668 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
669 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
670 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
671 vte_set_cursor_blink_mode();
677 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
679 switch (GPOINTER_TO_INT(user_data
))
683 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vc
->vte
)))
684 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vc
->vte
));
689 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vc
->vte
));
692 case POPUP_SELECTALL
:
697 case POPUP_CHANGEPATH
:
699 GeanyDocument
*doc
= document_get_current();
701 vte_cwd(doc
->file_name
, TRUE
);
704 case POPUP_RESTARTTERMINAL
:
706 vte_restart(vc
->vte
);
709 case POPUP_PREFERENCES
:
711 GtkWidget
*notebook
, *tab_page
;
715 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
716 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
718 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
719 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
727 static GtkWidget
*vte_create_popup_menu(void)
729 GtkWidget
*menu
, *item
;
730 GtkAccelGroup
*accel_group
;
731 gboolean show_im_menu
= TRUE
;
733 menu
= gtk_menu_new();
735 accel_group
= gtk_accel_group_new();
736 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
738 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
739 gtk_widget_add_accelerator(item
, "activate", accel_group
,
740 GDK_c
, 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_COPY
));
745 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
746 gtk_widget_add_accelerator(item
, "activate", accel_group
,
747 GDK_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
748 gtk_widget_show(item
);
749 gtk_container_add(GTK_CONTAINER(menu
), item
);
750 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
752 item
= gtk_separator_menu_item_new();
753 gtk_widget_show(item
);
754 gtk_container_add(GTK_CONTAINER(menu
), item
);
756 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
757 gtk_widget_show(item
);
758 gtk_container_add(GTK_CONTAINER(menu
), item
);
759 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_SELECTALL
));
761 item
= gtk_separator_menu_item_new();
762 gtk_widget_show(item
);
763 gtk_container_add(GTK_CONTAINER(menu
), item
);
765 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
766 gtk_widget_show(item
);
767 gtk_container_add(GTK_CONTAINER(menu
), item
);
768 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
770 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
771 gtk_widget_show(item
);
772 gtk_container_add(GTK_CONTAINER(menu
), item
);
773 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_RESTARTTERMINAL
));
775 item
= gtk_separator_menu_item_new();
776 gtk_widget_show(item
);
777 gtk_container_add(GTK_CONTAINER(menu
), item
);
779 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
780 gtk_widget_show(item
);
781 gtk_container_add(GTK_CONTAINER(menu
), item
);
782 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
784 msgwin_menu_add_common_items(GTK_MENU(menu
));
786 /* VTE 2.91 doesn't have IM context items, and GTK >= 3.10 doesn't show them anyway */
787 if (! vf
->vte_terminal_im_append_menuitems
|| gtk_check_version(3, 10, 0) == NULL
)
788 show_im_menu
= FALSE
;
789 else /* otherwise, query the setting */
790 g_object_get(gtk_settings_get_default(), "gtk-show-input-method-menu", &show_im_menu
, NULL
);
793 vc
->im_submenu
= NULL
;
796 item
= gtk_separator_menu_item_new();
797 gtk_widget_show(item
);
798 gtk_container_add(GTK_CONTAINER(menu
), item
);
800 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
801 vc
->im_submenu
= gtk_menu_new();
803 item
= gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
804 gtk_widget_show(item
);
805 gtk_container_add(GTK_CONTAINER(menu
), item
);
807 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), vc
->im_submenu
);
808 /* submenu populated after vte realized */
815 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
817 gboolean
vte_send_cmd(const gchar
*cmd
)
821 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), cmd
, strlen(cmd
));
822 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
830 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
831 * Determines the working directory using various OS-specific mechanisms and stores the determined
832 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
833 const gchar
*vte_get_working_directory(void)
837 gchar buffer
[4096 + 1];
838 gchar
*file
= g_strdup_printf("/proc/%d/cwd", pid
);
839 gint length
= readlink(file
, buffer
, sizeof(buffer
));
841 if (length
> 0 && *buffer
== '/')
843 buffer
[length
] = '\0';
844 g_free(vte_info
.dir
);
845 vte_info
.dir
= g_strdup(buffer
);
847 else if (length
== 0)
849 gchar
*cwd
= g_get_current_dir();
853 if (chdir(file
) == 0)
855 g_free(vte_info
.dir
);
856 vte_info
.dir
= g_get_current_dir();
858 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
870 /* Changes the current working directory of the VTE to the path of the given filename.
871 * filename is expected to be in UTF-8 encoding.
872 * filename can also be a path, then it is used directly.
873 * If force is set to TRUE, it will always change the cwd
875 void vte_cwd(const gchar
*filename
, gboolean force
)
877 if (vte_info
.have_vte
&& (vc
->follow_path
|| force
) &&
878 filename
!= NULL
&& g_path_is_absolute(filename
))
882 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
883 path
= g_strdup(filename
);
885 path
= g_path_get_dirname(filename
);
887 vte_get_working_directory(); /* refresh vte_info.dir */
888 if (! utils_str_equal(path
, vte_info
.dir
))
890 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
891 gchar
*quoted_path
= g_shell_quote(path
);
892 gchar
*cmd
= g_strconcat(vc
->send_cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
893 if (! vte_send_cmd(cmd
))
895 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
896 ui_set_statusbar(FALSE
, "%s", msg
);
897 geany_debug("%s", msg
);
907 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
908 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
910 if (info
== TARGET_TEXT_PLAIN
)
912 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
913 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
914 (const gchar
*) gtk_selection_data_get_data(data
),
915 gtk_selection_data_get_length(data
));
919 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
921 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
924 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
928 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
930 g_return_if_fail(GTK_IS_WIDGET(user_data
));
931 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
935 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
937 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
939 if (! utils_str_equal(fontbtn
, vc
->font
))
941 SETPTR(vc
->font
, g_strdup(gtk_font_button_get_font_name(widget
)));
942 vte_apply_user_settings();
947 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
949 gtk_color_button_get_color(widget
, &vc
->colour_fore
);
953 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
955 gtk_color_button_get_color(widget
, &vc
->colour_back
);
959 void vte_append_preferences_tab(void)
961 if (vte_info
.have_vte
)
963 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
964 GtkWidget
*check_run_in_vte
, *check_skip_script
;
965 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
967 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
968 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
969 ui_setup_open_button_callback(button_shell
, NULL
,
970 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
972 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
973 gtk_widget_set_sensitive(check_skip_script
, vc
->run_in_vte
);
975 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
976 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
977 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
979 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
980 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
982 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
983 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
985 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
986 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
988 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
989 gtk_widget_show_all(frame_term
);
994 void vte_select_all(void)
996 if (vf
->vte_terminal_select_all
!= NULL
)
997 vf
->vte_terminal_select_all(VTE_TERMINAL(vc
->vte
));
1001 void vte_send_selection_to_vte(void)
1007 doc
= document_get_current();
1008 g_return_if_fail(doc
!= NULL
);
1010 if (sci_has_selection(doc
->editor
->sci
))
1012 text
= sci_get_selection_contents(doc
->editor
->sci
);
1015 { /* Get the current line */
1016 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
1017 text
= sci_get_line(doc
->editor
->sci
, line_num
);
1022 if (vc
->send_selection_unsafe
)
1023 { /* Explicitly append a trailing newline character to get the command executed,
1024 this is disabled by default as it could cause all sorts of damage. */
1025 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
1027 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
1032 { /* Make sure there is no newline character at the end to prevent unwanted execution */
1033 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
1040 vf
->vte_terminal_feed_child(VTE_TERMINAL(vc
->vte
), text
, len
);
1043 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
1044 gtk_widget_grab_focus(vc
->vte
);
1045 msgwin_show_hide(TRUE
);