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_set_cursor_blink_mode
) (VteTerminal
*terminal
,
128 VteTerminalCursorBlinkMode mode
);
129 void (*vte_terminal_set_cursor_blinks
) (VteTerminal
*terminal
, gboolean blink
);
130 void (*vte_terminal_select_all
) (VteTerminal
*terminal
);
131 void (*vte_terminal_set_audible_bell
) (VteTerminal
*terminal
, gboolean is_audible
);
132 GtkAdjustment
* (*vte_terminal_get_adjustment
) (VteTerminal
*terminal
);
134 /* hack for the VTE 2.91 API using GdkRGBA: we wrap the API to keep using GdkColor on our side */
135 void (*vte_terminal_set_color_foreground_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
136 void (*vte_terminal_set_color_bold_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*foreground
);
137 void (*vte_terminal_set_color_background_rgba
) (VteTerminal
*terminal
, const GdkRGBA
*background
);
141 static void create_vte(void);
142 static void vte_start(GtkWidget
*widget
);
143 static void vte_restart(GtkWidget
*widget
);
144 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
);
145 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
146 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
);
147 static gboolean
vte_register_symbols(GModule
*module
);
148 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
);
149 static GtkWidget
*vte_create_popup_menu(void);
150 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
);
151 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
152 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
);
161 POPUP_RESTARTTERMINAL
,
163 TARGET_UTF8_STRING
= 0,
165 TARGET_COMPOUND_TEXT
,
170 static const GtkTargetEntry dnd_targets
[] =
172 { (gchar
*) "UTF8_STRING", 0, TARGET_UTF8_STRING
},
173 { (gchar
*) "TEXT", 0, TARGET_TEXT
},
174 { (gchar
*) "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
175 { (gchar
*) "STRING", 0, TARGET_STRING
},
176 { (gchar
*) "text/plain", 0, TARGET_TEXT_PLAIN
},
180 /* replacement for vte_terminal_get_adjustment() when it's not available */
181 static GtkAdjustment
*default_vte_terminal_get_adjustment(VteTerminal
*vte
)
183 if (GTK_IS_SCROLLABLE(vte
))
184 return gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vte
));
185 /* this is only valid in < 0.38, 0.38 broke ABI */
186 return vte
->adjustment
;
190 /* Wrap VTE 2.91 API using GdkRGBA with GdkColor so we use a single API on our side */
192 static void rgba_from_color(GdkRGBA
*rgba
, const GdkColor
*color
)
194 rgba
->red
= color
->red
/ 65535.0;
195 rgba
->green
= color
->green
/ 65535.0;
196 rgba
->blue
= color
->blue
/ 65535.0;
200 #define WRAP_RGBA_SETTER(name) \
201 static void wrap_##name(VteTerminal *terminal, const GdkColor *color) \
204 rgba_from_color(&rgba, color); \
205 vf->name##_rgba(terminal, &rgba); \
208 WRAP_RGBA_SETTER(vte_terminal_set_color_background
)
209 WRAP_RGBA_SETTER(vte_terminal_set_color_bold
)
210 WRAP_RGBA_SETTER(vte_terminal_set_color_foreground
)
212 #undef WRAP_RGBA_SETTER
215 static gchar
**vte_get_child_environment(void)
217 const gchar
*exclude_vars
[] = {"COLUMNS", "LINES", "TERM", "TERM_PROGRAM", NULL
};
219 return utils_copy_environment(exclude_vars
, "TERM", "xterm", NULL
);
223 static void override_menu_key(void)
225 if (gtk_menu_key_accel
== NULL
) /* for restoring the default value */
226 g_object_get(G_OBJECT(gtk_settings_get_default()),
227 "gtk-menu-bar-accel", >k_menu_key_accel
, NULL
);
229 if (vte_config
.ignore_menu_bar_accel
)
230 gtk_settings_set_string_property(gtk_settings_get_default(),
231 "gtk-menu-bar-accel", "<Shift><Control><Mod1><Mod2><Mod3><Mod4><Mod5>F10", "Geany");
233 gtk_settings_set_string_property(gtk_settings_get_default(),
234 "gtk-menu-bar-accel", gtk_menu_key_accel
, "Geany");
238 static void on_startup_complete(G_GNUC_UNUSED GObject
*dummy
)
240 GeanyDocument
*doc
= document_get_current();
242 /* ensure the widget is mapped and fully initialized, so actions like pasting text work
243 * (see https://github.com/geany/geany/issues/2813 for details) */
244 gtk_widget_realize(vte_config
.vte
);
247 vte_cwd((doc
->real_path
!= NULL
) ? doc
->real_path
: doc
->file_name
, FALSE
);
253 if (vte_info
.have_vte
== FALSE
)
254 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
255 geany_debug("Disabling terminal support");
259 if (!EMPTY(vte_info
.lib_vte
))
261 module
= g_module_open(vte_info
.lib_vte
, G_MODULE_BIND_LAZY
);
263 #ifdef VTE_MODULE_PATH
266 module
= g_module_open(VTE_MODULE_PATH
, G_MODULE_BIND_LAZY
);
273 const gchar
*sonames
[] = {
275 "libvte-2.91.0.dylib", "libvte-2.91.dylib",
276 "libvte2_90.9.dylib", "libvte2_90.dylib",
278 "libvte-2.91.so", "libvte-2.91.so.0",
279 "libvte2_90.so", "libvte2_90.so.9",
283 for (i
= 0; sonames
[i
] != NULL
&& module
== NULL
; i
++)
285 module
= g_module_open(sonames
[i
], G_MODULE_BIND_LAZY
);
291 vte_info
.have_vte
= FALSE
;
292 geany_debug("Could not load libvte.so, embedded terminal support disabled");
297 geany_debug("Loaded libvte from %s", g_module_name(module
));
298 vf
= g_new0(struct VteFunctions
, 1);
299 if (vte_register_symbols(module
))
300 vte_info
.have_vte
= TRUE
;
303 vte_info
.have_vte
= FALSE
;
305 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
306 /*g_module_close(module);*/
314 /* setup the F10 menu override (so it works before the widget is first realised). */
317 g_signal_connect(geany_object
, "geany-startup-complete", G_CALLBACK(on_startup_complete
), NULL
);
321 static void on_vte_realize(void)
323 /* the vte widget has to be realised before color changes take effect */
324 vte_apply_user_settings();
328 static gboolean
vte_start_idle(G_GNUC_UNUSED gpointer user_data
)
330 vte_start(vte_config
.vte
);
335 static void create_vte(void)
337 GtkWidget
*vte
, *scrollbar
, *hbox
;
339 vte_config
.vte
= vte
= vf
->vte_terminal_new();
340 scrollbar
= gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL
, vf
->vte_terminal_get_adjustment(VTE_TERMINAL(vte
)));
341 gtk_widget_set_can_focus(scrollbar
, FALSE
);
343 /* create menu now so copy/paste shortcuts work */
344 vte_config
.menu
= vte_create_popup_menu();
345 g_object_ref_sink(vte_config
.menu
);
347 hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
348 gtk_box_pack_start(GTK_BOX(hbox
), vte
, TRUE
, TRUE
, 0);
349 gtk_box_pack_start(GTK_BOX(hbox
), scrollbar
, FALSE
, FALSE
, 0);
351 /* set the default widget size first to prevent VTE expanding too much,
352 * sometimes causing the hscrollbar to be too big or out of view. */
353 gtk_widget_set_size_request(GTK_WIDGET(vte
), 10, 10);
354 vf
->vte_terminal_set_size(VTE_TERMINAL(vte
), 30, 1);
356 vf
->vte_terminal_set_mouse_autohide(VTE_TERMINAL(vte
), TRUE
);
357 if (vf
->vte_terminal_set_word_chars
)
358 vf
->vte_terminal_set_word_chars(VTE_TERMINAL(vte
), VTE_WORDCHARS
);
359 else if (vf
->vte_terminal_set_word_char_exceptions
)
360 vf
->vte_terminal_set_word_char_exceptions(VTE_TERMINAL(vte
), VTE_ADDITIONAL_WORDCHARS
);
362 gtk_drag_dest_set(vte
, GTK_DEST_DEFAULT_ALL
,
363 dnd_targets
, G_N_ELEMENTS(dnd_targets
), GDK_ACTION_COPY
);
365 g_signal_connect(vte
, "child-exited", G_CALLBACK(vte_start
), NULL
);
366 g_signal_connect(vte
, "button-press-event", G_CALLBACK(vte_button_pressed
), NULL
);
367 g_signal_connect(vte
, "event", G_CALLBACK(vte_keypress_cb
), NULL
);
368 g_signal_connect(vte
, "key-release-event", G_CALLBACK(vte_keyrelease_cb
), NULL
);
369 g_signal_connect(vte
, "commit", G_CALLBACK(vte_commit_cb
), NULL
);
370 g_signal_connect(vte
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
371 g_signal_connect(vte
, "drag-data-received", G_CALLBACK(vte_drag_data_received
), NULL
);
373 /* start shell on idle otherwise the initial prompt can get corrupted */
374 g_idle_add(vte_start_idle
, NULL
);
376 gtk_widget_show_all(hbox
);
377 terminal_label
= gtk_label_new(_("Terminal"));
378 gtk_notebook_insert_page(GTK_NOTEBOOK(msgwindow
.notebook
), hbox
, terminal_label
, MSG_VTE
);
380 g_signal_connect_after(vte
, "realize", G_CALLBACK(on_vte_realize
), NULL
);
386 /* free the vte widget before unloading vte module
387 * this prevents a segfault on X close window if the message window is hidden */
388 g_signal_handlers_disconnect_by_func(vte_config
.vte
, G_CALLBACK(vte_start
), NULL
);
389 gtk_widget_destroy(vte_config
.vte
);
390 gtk_widget_destroy(vte_config
.menu
);
391 g_object_unref(vte_config
.menu
);
392 g_free(vte_config
.shell
);
393 g_free(vte_config
.font
);
394 g_free(vte_config
.send_cmd_prefix
);
396 g_free(gtk_menu_key_accel
);
397 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
398 * happens when the app really exits, not directly on g_module_close(). This still needs to
399 * be investigated. */
400 /*g_module_close(module); */
404 static gboolean
set_dirty_idle(gpointer user_data
)
406 gtk_widget_set_name(terminal_label
, "geany-terminal-dirty");
407 terminal_label_update_source
= 0;
412 static void set_clean(gboolean value
)
418 if (terminal_label_update_source
> 0)
420 g_source_remove(terminal_label_update_source
);
421 terminal_label_update_source
= 0;
424 gtk_widget_set_name(terminal_label
, NULL
);
426 terminal_label_update_source
= g_timeout_add(150, set_dirty_idle
, NULL
);
433 static gboolean
vte_keyrelease_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
435 if (ui_is_keyval_enter_or_return(event
->keyval
) ||
436 ((event
->keyval
== GDK_KEY_c
) && (event
->state
& GDK_CONTROL_MASK
)))
438 /* assume any text on the prompt has been executed when pressing Enter/Return */
445 static gboolean
vte_keypress_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
447 if (vte_config
.enable_bash_keys
)
448 return FALSE
; /* Ctrl-[CD] will be handled by the VTE itself */
450 if (event
->type
!= GDK_KEY_RELEASE
)
453 if ((event
->keyval
== GDK_KEY_c
||
454 event
->keyval
== GDK_KEY_d
||
455 event
->keyval
== GDK_KEY_C
||
456 event
->keyval
== GDK_KEY_D
) &&
457 event
->state
& GDK_CONTROL_MASK
&&
458 ! (event
->state
& GDK_SHIFT_MASK
) && ! (event
->state
& GDK_MOD1_MASK
))
467 static void vte_commit_cb(VteTerminal
*vte
, gchar
*arg1
, guint arg2
, gpointer user_data
)
473 static void vte_start(GtkWidget
*widget
)
475 /* split the shell command line, so arguments will work too */
476 gchar
**argv
= g_strsplit(vte_config
.shell
, " ", -1);
480 gchar
**env
= vte_get_child_environment();
482 if (vf
->vte_terminal_spawn_sync
)
484 if (! vf
->vte_terminal_spawn_sync(VTE_TERMINAL(widget
), VTE_PTY_DEFAULT
,
485 vte_info
.dir
, argv
, env
, 0, NULL
, NULL
,
493 pid
= vf
->vte_terminal_fork_command(VTE_TERMINAL(widget
), argv
[0], argv
, env
,
494 vte_info
.dir
, TRUE
, TRUE
, TRUE
);
500 pid
= 0; /* use 0 as invalid pid */
506 static void vte_restart(GtkWidget
*widget
)
508 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
514 vf
->vte_terminal_reset(VTE_TERMINAL(widget
), TRUE
, TRUE
);
520 static gboolean
vte_button_pressed(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
522 if (event
->button
== 3)
524 gtk_widget_grab_focus(vte_config
.vte
);
525 gtk_menu_popup_at_pointer(GTK_MENU(vte_config
.menu
), (GdkEvent
*) event
);
528 else if (event
->button
== 2)
530 gtk_widget_grab_focus(widget
);
536 static void vte_set_cursor_blink_mode(void)
538 if (vf
->vte_terminal_set_cursor_blink_mode
!= NULL
)
540 vf
->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vte_config
.vte
),
541 (vte_config
.cursor_blinks
) ? VTE_CURSOR_BLINK_ON
: VTE_CURSOR_BLINK_OFF
);
544 vf
->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vte_config
.vte
), vte_config
.cursor_blinks
);
548 static gboolean
vte_is_2_91(void)
550 guint major
= vf
->vte_get_major_version
? vf
->vte_get_major_version() : 0;
551 guint minor
= vf
->vte_get_minor_version
? vf
->vte_get_minor_version() : 0;
553 /* 2.91 API started at 0.38 */
554 return ((major
> 0 || (major
== 0 && minor
>= 38)) ||
555 /* 0.38 doesn't have runtime version checks, so check a symbol that didn't exist before */
556 vf
->vte_terminal_spawn_sync
!= NULL
);
560 static gboolean
vte_register_symbols(GModule
*mod
)
562 #define BIND_SYMBOL_FULL(name, dest) \
563 g_module_symbol(mod, name, (void*)(dest))
564 #define BIND_SYMBOL(field) \
565 BIND_SYMBOL_FULL(#field, &vf->field)
566 #define BIND_REQUIRED_SYMBOL_FULL(name, dest) \
568 if (! BIND_SYMBOL_FULL(name, dest)) \
570 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
571 g_module_name(mod), name); \
575 #define BIND_REQUIRED_SYMBOL(field) \
576 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field)
577 #define BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(field) \
579 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field##_rgba); \
580 vf->field = wrap_##field; \
583 BIND_SYMBOL(vte_get_major_version
);
584 BIND_SYMBOL(vte_get_minor_version
);
585 BIND_REQUIRED_SYMBOL(vte_terminal_new
);
586 BIND_REQUIRED_SYMBOL(vte_terminal_set_size
);
587 if (! BIND_SYMBOL(vte_terminal_spawn_sync
))
588 /* vte_terminal_spawn_sync() is available only in 0.38 */
589 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command
);
590 /* 0.38 removed vte_terminal_set_word_chars() */
591 BIND_SYMBOL(vte_terminal_set_word_chars
);
592 /* 0.40 introduced it under a different API */
593 BIND_SYMBOL(vte_terminal_set_word_char_exceptions
);
594 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide
);
595 BIND_REQUIRED_SYMBOL(vte_terminal_reset
);
596 BIND_REQUIRED_SYMBOL(vte_terminal_get_type
);
597 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output
);
598 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke
);
599 BIND_REQUIRED_SYMBOL(vte_terminal_set_font
);
600 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines
);
601 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection
);
602 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard
);
603 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard
);
607 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_foreground
);
608 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_bold
);
609 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_background
);
613 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground
);
614 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold
);
615 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background
);
617 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child
);
618 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode
))
619 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
620 * this symbol, we are probably on an older version and use the old API instead */
621 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks
);
622 BIND_REQUIRED_SYMBOL(vte_terminal_select_all
);
623 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell
);
624 if (! BIND_SYMBOL(vte_terminal_get_adjustment
))
625 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
626 vf
->vte_terminal_get_adjustment
= default_vte_terminal_get_adjustment
;
628 #undef BIND_REQUIRED_SYMBOL_RGBA_WRAPPED
629 #undef BIND_REQUIRED_SYMBOL
630 #undef BIND_REQUIRED_SYMBOL_FULL
632 #undef BIND_SYMBOL_FULL
638 void vte_apply_user_settings(void)
640 VteConfig
*vc
= &vte_config
;
641 PangoFontDescription
*font_desc
;
643 if (! ui_prefs
.msgwindow_visible
)
646 vf
->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc
->vte
), vc
->scrollback_lines
);
647 vf
->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_key
);
648 vf
->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc
->vte
), vc
->scroll_on_out
);
649 font_desc
= pango_font_description_from_string(vc
->font
);
650 vf
->vte_terminal_set_font(VTE_TERMINAL(vc
->vte
), font_desc
);
651 pango_font_description_free(font_desc
);
652 vf
->vte_terminal_set_color_foreground(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
653 vf
->vte_terminal_set_color_bold(VTE_TERMINAL(vc
->vte
), &vc
->colour_fore
);
654 vf
->vte_terminal_set_color_background(VTE_TERMINAL(vc
->vte
), &vc
->colour_back
);
655 vf
->vte_terminal_set_audible_bell(VTE_TERMINAL(vc
->vte
), prefs
.beep_on_errors
);
656 vte_set_cursor_blink_mode();
662 static void vte_popup_menu_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
664 switch (GPOINTER_TO_INT(user_data
))
668 if (vf
->vte_terminal_get_has_selection(VTE_TERMINAL(vte_config
.vte
)))
669 vf
->vte_terminal_copy_clipboard(VTE_TERMINAL(vte_config
.vte
));
674 vf
->vte_terminal_paste_clipboard(VTE_TERMINAL(vte_config
.vte
));
677 case POPUP_SELECTALL
:
682 case POPUP_CHANGEPATH
:
684 GeanyDocument
*doc
= document_get_current();
686 vte_cwd(doc
->file_name
, TRUE
);
689 case POPUP_RESTARTTERMINAL
:
691 vte_restart(vte_config
.vte
);
694 case POPUP_PREFERENCES
:
696 GtkWidget
*notebook
, *tab_page
;
700 notebook
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2");
701 tab_page
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
703 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook
),
704 gtk_notebook_page_num(GTK_NOTEBOOK(notebook
), GTK_WIDGET(tab_page
)));
712 static GtkWidget
*vte_create_popup_menu(void)
714 GtkWidget
*menu
, *item
;
715 GtkAccelGroup
*accel_group
;
717 menu
= gtk_menu_new();
719 accel_group
= gtk_accel_group_new();
720 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), accel_group
);
722 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, NULL
);
723 gtk_widget_add_accelerator(item
, "activate", accel_group
,
724 GDK_KEY_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
725 gtk_widget_show(item
);
726 gtk_container_add(GTK_CONTAINER(menu
), item
);
727 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_COPY
));
729 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, NULL
);
730 gtk_widget_add_accelerator(item
, "activate", accel_group
,
731 GDK_KEY_v
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
732 gtk_widget_show(item
);
733 gtk_container_add(GTK_CONTAINER(menu
), item
);
734 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PASTE
));
736 item
= gtk_separator_menu_item_new();
737 gtk_widget_show(item
);
738 gtk_container_add(GTK_CONTAINER(menu
), item
);
740 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL
, NULL
);
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_SELECTALL
));
745 item
= gtk_separator_menu_item_new();
746 gtk_widget_show(item
);
747 gtk_container_add(GTK_CONTAINER(menu
), item
);
749 item
= gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
750 gtk_widget_show(item
);
751 gtk_container_add(GTK_CONTAINER(menu
), item
);
752 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_CHANGEPATH
));
754 item
= gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
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_RESTARTTERMINAL
));
759 item
= gtk_separator_menu_item_new();
760 gtk_widget_show(item
);
761 gtk_container_add(GTK_CONTAINER(menu
), item
);
763 item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
764 gtk_widget_show(item
);
765 gtk_container_add(GTK_CONTAINER(menu
), item
);
766 g_signal_connect(item
, "activate", G_CALLBACK(vte_popup_menu_clicked
), GINT_TO_POINTER(POPUP_PREFERENCES
));
768 msgwin_menu_add_common_items(GTK_MENU(menu
));
774 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
776 gboolean
vte_send_cmd(const gchar
*cmd
)
778 g_return_val_if_fail(cmd
!= NULL
, FALSE
);
782 vf
->vte_terminal_feed_child(VTE_TERMINAL(vte_config
.vte
), cmd
, strlen(cmd
));
783 set_clean(TRUE
); /* vte_terminal_feed_child() also marks the vte as not clean */
791 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
792 * Determines the working directory using various OS-specific mechanisms and stores the determined
793 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
794 const gchar
*vte_get_working_directory(void)
798 gchar buffer
[4096 + 1];
799 gchar
*file
= g_strdup_printf("/proc/%d/cwd", pid
);
800 gint length
= readlink(file
, buffer
, sizeof(buffer
));
802 if (length
> 0 && *buffer
== '/')
804 buffer
[length
] = '\0';
805 g_free(vte_info
.dir
);
806 vte_info
.dir
= g_strdup(buffer
);
808 else if (length
== 0)
810 gchar
*cwd
= g_get_current_dir();
814 if (chdir(file
) == 0)
816 g_free(vte_info
.dir
);
817 vte_info
.dir
= g_get_current_dir();
819 geany_debug("%s: %s", G_STRFUNC
, g_strerror(errno
));
831 /* Changes the current working directory of the VTE to the path of the given filename.
832 * filename is expected to be in UTF-8 encoding.
833 * filename can also be a path, then it is used directly.
834 * If force is set to TRUE, it will always change the cwd
836 void vte_cwd(const gchar
*filename
, gboolean force
)
838 if (vte_info
.have_vte
&& (vte_config
.follow_path
|| force
) &&
839 filename
!= NULL
&& g_path_is_absolute(filename
))
843 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
844 path
= g_strdup(filename
);
846 path
= g_path_get_dirname(filename
);
848 vte_get_working_directory(); /* refresh vte_info.dir */
849 if (! utils_str_equal(path
, vte_info
.dir
))
851 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
852 gchar
*quoted_path
= g_shell_quote(path
);
853 const gchar
*cmd_prefix
= vte_config
.send_cmd_prefix
? vte_config
.send_cmd_prefix
: "";
854 gchar
*cmd
= g_strconcat(cmd_prefix
, "cd ", quoted_path
, "\n", NULL
);
855 if (! vte_send_cmd(cmd
))
857 const gchar
*msg
= _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
858 ui_set_statusbar(FALSE
, "%s", msg
);
859 geany_debug("%s", msg
);
869 static void vte_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
870 gint x
, gint y
, GtkSelectionData
*data
, guint info
, guint ltime
)
872 if (info
== TARGET_TEXT_PLAIN
)
874 if (gtk_selection_data_get_format(data
) == 8 && gtk_selection_data_get_length(data
) > 0)
875 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
),
876 (const gchar
*) gtk_selection_data_get_data(data
),
877 gtk_selection_data_get_length(data
));
881 gchar
*text
= (gchar
*) gtk_selection_data_get_text(data
);
883 vf
->vte_terminal_feed_child(VTE_TERMINAL(widget
), text
, strlen(text
));
886 gtk_drag_finish(drag_context
, TRUE
, FALSE
, ltime
);
890 static void on_check_run_in_vte_toggled(GtkToggleButton
*togglebutton
, GtkWidget
*user_data
)
892 g_return_if_fail(GTK_IS_WIDGET(user_data
));
893 gtk_widget_set_sensitive(user_data
, gtk_toggle_button_get_active(togglebutton
));
897 static void on_term_font_set(GtkFontButton
*widget
, gpointer user_data
)
899 const gchar
*fontbtn
= gtk_font_button_get_font_name(widget
);
901 if (! utils_str_equal(fontbtn
, vte_config
.font
))
903 SETPTR(vte_config
.font
, g_strdup(gtk_font_button_get_font_name(widget
)));
904 vte_apply_user_settings();
909 static void on_term_fg_color_set(GtkColorButton
*widget
, gpointer user_data
)
911 gtk_color_button_get_color(widget
, &vte_config
.colour_fore
);
915 static void on_term_bg_color_set(GtkColorButton
*widget
, gpointer user_data
)
917 gtk_color_button_get_color(widget
, &vte_config
.colour_back
);
921 void vte_append_preferences_tab(void)
923 if (vte_info
.have_vte
)
925 GtkWidget
*frame_term
, *button_shell
, *entry_shell
;
926 GtkWidget
*check_run_in_vte
, *check_skip_script
;
927 GtkWidget
*font_button
, *fg_color_button
, *bg_color_button
;
929 button_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "button_term_shell"));
930 entry_shell
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "entry_shell"));
931 ui_setup_open_button_callback(button_shell
, NULL
,
932 GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_ENTRY(entry_shell
));
934 check_skip_script
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_skip_script"));
935 gtk_widget_set_sensitive(check_skip_script
, vte_config
.run_in_vte
);
937 check_run_in_vte
= GTK_WIDGET(ui_lookup_widget(ui_widgets
.prefs_dialog
, "check_run_in_vte"));
938 g_signal_connect(G_OBJECT(check_run_in_vte
), "toggled",
939 G_CALLBACK(on_check_run_in_vte_toggled
), check_skip_script
);
941 font_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "font_term");
942 g_signal_connect(font_button
, "font-set", G_CALLBACK(on_term_font_set
), NULL
);
944 fg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_fore");
945 g_signal_connect(fg_color_button
, "color-set", G_CALLBACK(on_term_fg_color_set
), NULL
);
947 bg_color_button
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "color_back");
948 g_signal_connect(bg_color_button
, "color-set", G_CALLBACK(on_term_bg_color_set
), NULL
);
950 frame_term
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame_term");
951 gtk_widget_show_all(frame_term
);
956 void vte_select_all(void)
958 if (vf
->vte_terminal_select_all
!= NULL
)
959 vf
->vte_terminal_select_all(VTE_TERMINAL(vte_config
.vte
));
963 void vte_send_selection_to_vte(void)
969 doc
= document_get_current();
970 g_return_if_fail(doc
!= NULL
);
972 if (sci_has_selection(doc
->editor
->sci
))
974 text
= sci_get_selection_contents(doc
->editor
->sci
);
977 { /* Get the current line */
978 gint line_num
= sci_get_current_line(doc
->editor
->sci
);
979 text
= sci_get_line(doc
->editor
->sci
, line_num
);
984 if (vte_config
.send_selection_unsafe
)
985 { /* Explicitly append a trailing newline character to get the command executed,
986 this is disabled by default as it could cause all sorts of damage. */
987 if (text
[len
-1] != '\n' && text
[len
-1] != '\r')
989 SETPTR(text
, g_strconcat(text
, "\n", NULL
));
994 { /* Make sure there is no newline character at the end to prevent unwanted execution */
995 while (text
[len
-1] == '\n' || text
[len
-1] == '\r')
1002 vf
->vte_terminal_feed_child(VTE_TERMINAL(vte_config
.vte
), text
, len
);
1005 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
1006 gtk_widget_grab_focus(vte_config
.vte
);
1007 msgwin_show_hide(TRUE
);