Remove invalid python-specific code
[geany-mirror.git] / src / vte.c
blob916e2c264e715db9f7c54df7485d5f3543efa0cf
1 /*
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.
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #ifdef HAVE_VTE
31 #include "vte.h"
33 #include "callbacks.h"
34 #include "document.h"
35 #include "geanyobject.h"
36 #include "msgwindow.h"
37 #include "prefs.h"
38 #include "sciwrappers.h"
39 #include "support.h"
40 #include "ui_utils.h"
41 #include "utils.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 */
46 #include <stdlib.h>
47 #include <sys/types.h>
48 #include <unistd.h>
50 #include <gtk/gtk.h>
51 #include <gdk/gdkkeysyms.h>
52 #include <signal.h>
53 #include <string.h>
54 #include <errno.h>
57 VteInfo vte_info = { FALSE, FALSE, FALSE, NULL, NULL };
58 VteConfig *vc;
60 static GPid pid = 0;
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;
75 struct _VteTerminal
77 GtkWidget widget;
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())
84 typedef enum {
85 VTE_CURSOR_BLINK_SYSTEM,
86 VTE_CURSOR_BLINK_ON,
87 VTE_CURSOR_BLINK_OFF
88 } VteTerminalCursorBlinkMode;
90 typedef enum {
91 /* we don't care for the other possible values */
92 VTE_PTY_DEFAULT = 0
93 } VtePtyFlags;
96 /* Holds function pointers we need to access the VTE API. */
97 struct VteFunctions
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);
156 enum
158 POPUP_COPY,
159 POPUP_PASTE,
160 POPUP_SELECTALL,
161 POPUP_CHANGEPATH,
162 POPUP_RESTARTTERMINAL,
163 POPUP_PREFERENCES,
164 TARGET_UTF8_STRING = 0,
165 TARGET_TEXT,
166 TARGET_COMPOUND_TEXT,
167 TARGET_STRING,
168 TARGET_TEXT_PLAIN
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;
198 rgba->alpha = 1.0;
201 #define WRAP_RGBA_SETTER(name) \
202 static void wrap_##name(VteTerminal *terminal, const GdkColor *color) \
204 GdkRGBA rgba; \
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", &gtk_menu_key_accel, NULL);
230 if (vc->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");
233 else
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 if (doc)
244 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
248 void vte_init(void)
250 if (vte_info.have_vte == FALSE)
251 { /* vte_info.have_vte can be false even if VTE is compiled in, think of command line option */
252 geany_debug("Disabling terminal support");
253 return;
256 if (!EMPTY(vte_info.lib_vte))
258 module = g_module_open(vte_info.lib_vte, G_MODULE_BIND_LAZY);
260 #ifdef VTE_MODULE_PATH
261 else
263 module = g_module_open(VTE_MODULE_PATH, G_MODULE_BIND_LAZY);
265 #endif
267 if (module == NULL)
269 gint i;
270 const gchar *sonames[] = {
271 #ifdef __APPLE__
272 "libvte-2.91.0.dylib", "libvte-2.91.dylib",
273 "libvte2_90.9.dylib", "libvte2_90.dylib",
274 #endif
275 "libvte-2.91.so", "libvte-2.91.so.0",
276 "libvte2_90.so", "libvte2_90.so.9",
277 NULL
280 for (i = 0; sonames[i] != NULL && module == NULL; i++)
282 module = g_module_open(sonames[i], G_MODULE_BIND_LAZY);
286 if (module == NULL)
288 vte_info.have_vte = FALSE;
289 geany_debug("Could not load libvte.so, embedded terminal support disabled");
290 return;
292 else
294 geany_debug("Loaded libvte from %s", g_module_name(module));
295 vf = g_new0(struct VteFunctions, 1);
296 if (vte_register_symbols(module))
297 vte_info.have_vte = TRUE;
298 else
300 vte_info.have_vte = FALSE;
301 g_free(vf);
302 /* FIXME: is closing the module safe? see vte_close() and test on FreeBSD */
303 /*g_module_close(module);*/
304 module = NULL;
305 return;
309 create_vte();
311 /* setup the F10 menu override (so it works before the widget is first realised). */
312 override_menu_key();
314 g_signal_connect(geany_object, "geany-startup-complete", G_CALLBACK(on_startup_complete), NULL);
318 static void on_vte_realize(void)
320 /* the vte widget has to be realised before color changes take effect */
321 vte_apply_user_settings();
323 if (vf->vte_terminal_im_append_menuitems && vc->im_submenu)
324 vf->vte_terminal_im_append_menuitems(VTE_TERMINAL(vc->vte), GTK_MENU_SHELL(vc->im_submenu));
328 static gboolean vte_start_idle(G_GNUC_UNUSED gpointer user_data)
330 vte_start(vc->vte);
331 return FALSE;
335 static void create_vte(void)
337 GtkWidget *vte, *scrollbar, *hbox;
339 vc->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 vc->menu = vte_create_popup_menu();
345 g_object_ref_sink(vc->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);
384 void vte_close(void)
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(vc->vte, G_CALLBACK(vte_start), NULL);
389 gtk_widget_destroy(vc->vte);
390 gtk_widget_destroy(vc->menu);
391 g_object_unref(vc->menu);
392 g_free(vc->shell);
393 g_free(vc->font);
394 g_free(vc->send_cmd_prefix);
395 g_free(vc);
396 g_free(vf);
397 g_free(gtk_menu_key_accel);
398 /* Don't unload the module explicitly because it causes a segfault on FreeBSD. The segfault
399 * happens when the app really exits, not directly on g_module_close(). This still needs to
400 * be investigated. */
401 /*g_module_close(module); */
405 static gboolean set_dirty_idle(gpointer user_data)
407 gtk_widget_set_name(terminal_label, "geany-terminal-dirty");
408 terminal_label_update_source = 0;
409 return FALSE;
413 static void set_clean(gboolean value)
415 if (clean != value)
417 if (terminal_label)
419 if (terminal_label_update_source > 0)
421 g_source_remove(terminal_label_update_source);
422 terminal_label_update_source = 0;
424 if (value)
425 gtk_widget_set_name(terminal_label, NULL);
426 else
427 terminal_label_update_source = g_timeout_add(150, set_dirty_idle, NULL);
429 clean = value;
434 static gboolean vte_keyrelease_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
436 if (ui_is_keyval_enter_or_return(event->keyval) ||
437 ((event->keyval == GDK_KEY_c) && (event->state & GDK_CONTROL_MASK)))
439 /* assume any text on the prompt has been executed when pressing Enter/Return */
440 set_clean(TRUE);
442 return FALSE;
446 static gboolean vte_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
448 if (vc->enable_bash_keys)
449 return FALSE; /* Ctrl-[CD] will be handled by the VTE itself */
451 if (event->type != GDK_KEY_RELEASE)
452 return FALSE;
454 if ((event->keyval == GDK_KEY_c ||
455 event->keyval == GDK_KEY_d ||
456 event->keyval == GDK_KEY_C ||
457 event->keyval == GDK_KEY_D) &&
458 event->state & GDK_CONTROL_MASK &&
459 ! (event->state & GDK_SHIFT_MASK) && ! (event->state & GDK_MOD1_MASK))
461 vte_restart(widget);
462 return TRUE;
464 return FALSE;
468 static void vte_commit_cb(VteTerminal *vte, gchar *arg1, guint arg2, gpointer user_data)
470 set_clean(FALSE);
474 static void vte_start(GtkWidget *widget)
476 /* split the shell command line, so arguments will work too */
477 gchar **argv = g_strsplit(vc->shell, " ", -1);
479 if (argv != NULL)
481 gchar **env = vte_get_child_environment();
483 if (vf->vte_terminal_spawn_sync)
485 if (! vf->vte_terminal_spawn_sync(VTE_TERMINAL(widget), VTE_PTY_DEFAULT,
486 vte_info.dir, argv, env, 0, NULL, NULL,
487 &pid, NULL, NULL))
489 pid = -1;
492 else
494 pid = vf->vte_terminal_fork_command(VTE_TERMINAL(widget), argv[0], argv, env,
495 vte_info.dir, TRUE, TRUE, TRUE);
497 g_strfreev(env);
498 g_strfreev(argv);
500 else
501 pid = 0; /* use 0 as invalid pid */
503 set_clean(TRUE);
507 static void vte_restart(GtkWidget *widget)
509 vte_get_working_directory(); /* try to keep the working directory when restarting the VTE */
510 if (pid > 0)
512 kill(pid, SIGINT);
513 pid = 0;
515 vf->vte_terminal_reset(VTE_TERMINAL(widget), TRUE, TRUE);
516 vte_start(widget);
517 set_clean(TRUE);
521 static gboolean vte_button_pressed(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
523 if (event->button == 3)
525 gtk_widget_grab_focus(vc->vte);
526 gtk_menu_popup(GTK_MENU(vc->menu), NULL, NULL, NULL, NULL, event->button, event->time);
527 return TRUE;
529 else if (event->button == 2)
531 gtk_widget_grab_focus(widget);
533 return FALSE;
537 static void vte_set_cursor_blink_mode(void)
539 if (vf->vte_terminal_set_cursor_blink_mode != NULL)
540 /* vte >= 0.17.1 */
541 vf->vte_terminal_set_cursor_blink_mode(VTE_TERMINAL(vc->vte),
542 (vc->cursor_blinks) ? VTE_CURSOR_BLINK_ON : VTE_CURSOR_BLINK_OFF);
543 else
544 /* vte < 0.17.1 */
545 vf->vte_terminal_set_cursor_blinks(VTE_TERMINAL(vc->vte), vc->cursor_blinks);
549 static gboolean vte_is_2_91(void)
551 guint major = vf->vte_get_major_version ? vf->vte_get_major_version() : 0;
552 guint minor = vf->vte_get_minor_version ? vf->vte_get_minor_version() : 0;
554 /* 2.91 API started at 0.38 */
555 return ((major > 0 || (major == 0 && minor >= 38)) ||
556 /* 0.38 doesn't have runtime version checks, so check a symbol that didn't exist before */
557 vf->vte_terminal_spawn_sync != NULL);
561 static gboolean vte_register_symbols(GModule *mod)
563 #define BIND_SYMBOL_FULL(name, dest) \
564 g_module_symbol(mod, name, (void*)(dest))
565 #define BIND_SYMBOL(field) \
566 BIND_SYMBOL_FULL(#field, &vf->field)
567 #define BIND_REQUIRED_SYMBOL_FULL(name, dest) \
568 G_STMT_START { \
569 if (! BIND_SYMBOL_FULL(name, dest)) \
571 g_critical(_("invalid VTE library \"%s\": missing symbol \"%s\""), \
572 g_module_name(mod), name); \
573 return FALSE; \
575 } G_STMT_END
576 #define BIND_REQUIRED_SYMBOL(field) \
577 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field)
578 #define BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(field) \
579 G_STMT_START { \
580 BIND_REQUIRED_SYMBOL_FULL(#field, &vf->field##_rgba); \
581 vf->field = wrap_##field; \
582 } G_STMT_END
584 BIND_SYMBOL(vte_get_major_version);
585 BIND_SYMBOL(vte_get_minor_version);
586 BIND_REQUIRED_SYMBOL(vte_terminal_new);
587 BIND_REQUIRED_SYMBOL(vte_terminal_set_size);
588 if (! BIND_SYMBOL(vte_terminal_spawn_sync))
589 /* vte_terminal_spawn_sync() is available only in 0.38 */
590 BIND_REQUIRED_SYMBOL(vte_terminal_fork_command);
591 /* 0.38 removed vte_terminal_set_word_chars() */
592 BIND_SYMBOL(vte_terminal_set_word_chars);
593 /* 0.40 introduced it under a different API */
594 BIND_SYMBOL(vte_terminal_set_word_char_exceptions);
595 BIND_REQUIRED_SYMBOL(vte_terminal_set_mouse_autohide);
596 BIND_REQUIRED_SYMBOL(vte_terminal_reset);
597 BIND_REQUIRED_SYMBOL(vte_terminal_get_type);
598 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_output);
599 BIND_REQUIRED_SYMBOL(vte_terminal_set_scroll_on_keystroke);
600 BIND_REQUIRED_SYMBOL(vte_terminal_set_font);
601 BIND_REQUIRED_SYMBOL(vte_terminal_set_scrollback_lines);
602 BIND_REQUIRED_SYMBOL(vte_terminal_get_has_selection);
603 BIND_REQUIRED_SYMBOL(vte_terminal_copy_clipboard);
604 BIND_REQUIRED_SYMBOL(vte_terminal_paste_clipboard);
606 if (vte_is_2_91())
608 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_foreground);
609 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_bold);
610 BIND_REQUIRED_SYMBOL_RGBA_WRAPPED(vte_terminal_set_color_background);
612 else
614 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_foreground);
615 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_bold);
616 BIND_REQUIRED_SYMBOL(vte_terminal_set_color_background);
618 BIND_REQUIRED_SYMBOL(vte_terminal_feed_child);
619 BIND_SYMBOL(vte_terminal_im_append_menuitems);
620 if (! BIND_SYMBOL(vte_terminal_set_cursor_blink_mode))
621 /* vte_terminal_set_cursor_blink_mode() is only available since 0.17.1, so if we don't find
622 * this symbol, we are probably on an older version and use the old API instead */
623 BIND_REQUIRED_SYMBOL(vte_terminal_set_cursor_blinks);
624 BIND_REQUIRED_SYMBOL(vte_terminal_select_all);
625 BIND_REQUIRED_SYMBOL(vte_terminal_set_audible_bell);
626 if (! BIND_SYMBOL(vte_terminal_get_adjustment))
627 /* vte_terminal_get_adjustment() is available since 0.9 and removed in 0.38 */
628 vf->vte_terminal_get_adjustment = default_vte_terminal_get_adjustment;
630 #undef BIND_REQUIRED_SYMBOL_RGBA_WRAPPED
631 #undef BIND_REQUIRED_SYMBOL
632 #undef BIND_REQUIRED_SYMBOL_FULL
633 #undef BIND_SYMBOL
634 #undef BIND_SYMBOL_FULL
636 return TRUE;
640 void vte_apply_user_settings(void)
642 PangoFontDescription *font_desc;
644 if (! ui_prefs.msgwindow_visible)
645 return;
647 vf->vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte), vc->scrollback_lines);
648 vf->vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(vc->vte), vc->scroll_on_key);
649 vf->vte_terminal_set_scroll_on_output(VTE_TERMINAL(vc->vte), vc->scroll_on_out);
650 font_desc = pango_font_description_from_string(vc->font);
651 vf->vte_terminal_set_font(VTE_TERMINAL(vc->vte), font_desc);
652 pango_font_description_free(font_desc);
653 vf->vte_terminal_set_color_foreground(VTE_TERMINAL(vc->vte), &vc->colour_fore);
654 vf->vte_terminal_set_color_bold(VTE_TERMINAL(vc->vte), &vc->colour_fore);
655 vf->vte_terminal_set_color_background(VTE_TERMINAL(vc->vte), &vc->colour_back);
656 vf->vte_terminal_set_audible_bell(VTE_TERMINAL(vc->vte), prefs.beep_on_errors);
657 vte_set_cursor_blink_mode();
659 override_menu_key();
663 static void vte_popup_menu_clicked(GtkMenuItem *menuitem, gpointer user_data)
665 switch (GPOINTER_TO_INT(user_data))
667 case POPUP_COPY:
669 if (vf->vte_terminal_get_has_selection(VTE_TERMINAL(vc->vte)))
670 vf->vte_terminal_copy_clipboard(VTE_TERMINAL(vc->vte));
671 break;
673 case POPUP_PASTE:
675 vf->vte_terminal_paste_clipboard(VTE_TERMINAL(vc->vte));
676 break;
678 case POPUP_SELECTALL:
680 vte_select_all();
681 break;
683 case POPUP_CHANGEPATH:
685 GeanyDocument *doc = document_get_current();
686 if (doc != NULL)
687 vte_cwd(doc->file_name, TRUE);
688 break;
690 case POPUP_RESTARTTERMINAL:
692 vte_restart(vc->vte);
693 break;
695 case POPUP_PREFERENCES:
697 GtkWidget *notebook, *tab_page;
699 prefs_show_dialog();
701 notebook = ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2");
702 tab_page = ui_lookup_widget(ui_widgets.prefs_dialog, "frame_term");
704 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook),
705 gtk_notebook_page_num(GTK_NOTEBOOK(notebook), GTK_WIDGET(tab_page)));
707 break;
713 static GtkWidget *vte_create_popup_menu(void)
715 GtkWidget *menu, *item;
716 GtkAccelGroup *accel_group;
717 gboolean show_im_menu = TRUE;
719 menu = gtk_menu_new();
721 accel_group = gtk_accel_group_new();
722 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), accel_group);
724 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY, NULL);
725 gtk_widget_add_accelerator(item, "activate", accel_group,
726 GDK_KEY_c, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
727 gtk_widget_show(item);
728 gtk_container_add(GTK_CONTAINER(menu), item);
729 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_COPY));
731 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE, NULL);
732 gtk_widget_add_accelerator(item, "activate", accel_group,
733 GDK_KEY_v, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
734 gtk_widget_show(item);
735 gtk_container_add(GTK_CONTAINER(menu), item);
736 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_PASTE));
738 item = gtk_separator_menu_item_new();
739 gtk_widget_show(item);
740 gtk_container_add(GTK_CONTAINER(menu), item);
742 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_SELECT_ALL, NULL);
743 gtk_widget_show(item);
744 gtk_container_add(GTK_CONTAINER(menu), item);
745 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_SELECTALL));
747 item = gtk_separator_menu_item_new();
748 gtk_widget_show(item);
749 gtk_container_add(GTK_CONTAINER(menu), item);
751 item = gtk_image_menu_item_new_with_mnemonic(_("_Set Path From Document"));
752 gtk_widget_show(item);
753 gtk_container_add(GTK_CONTAINER(menu), item);
754 g_signal_connect(item, "activate", G_CALLBACK(vte_popup_menu_clicked), GINT_TO_POINTER(POPUP_CHANGEPATH));
756 item = gtk_image_menu_item_new_with_mnemonic(_("_Restart Terminal"));
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_RESTARTTERMINAL));
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_from_stock(GTK_STOCK_PREFERENCES, NULL);
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_PREFERENCES));
770 msgwin_menu_add_common_items(GTK_MENU(menu));
772 /* VTE 2.91 doesn't have IM context items, and GTK >= 3.10 doesn't show them anyway */
773 if (! vf->vte_terminal_im_append_menuitems || gtk_check_version(3, 10, 0) == NULL)
774 show_im_menu = FALSE;
775 else /* otherwise, query the setting */
776 g_object_get(gtk_settings_get_default(), "gtk-show-input-method-menu", &show_im_menu, NULL);
778 if (! show_im_menu)
779 vc->im_submenu = NULL;
780 else
782 item = gtk_separator_menu_item_new();
783 gtk_widget_show(item);
784 gtk_container_add(GTK_CONTAINER(menu), item);
786 /* the IM submenu should always be the last item to be consistent with other GTK popup menus */
787 vc->im_submenu = gtk_menu_new();
789 item = gtk_image_menu_item_new_with_mnemonic(_("_Input Methods"));
790 gtk_widget_show(item);
791 gtk_container_add(GTK_CONTAINER(menu), item);
793 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), vc->im_submenu);
794 /* submenu populated after vte realized */
797 return menu;
801 /* If the command could be executed, TRUE is returned, FALSE otherwise (i.e. there was some text
802 * on the prompt). */
803 gboolean vte_send_cmd(const gchar *cmd)
805 if (clean)
807 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), cmd, strlen(cmd));
808 set_clean(TRUE); /* vte_terminal_feed_child() also marks the vte as not clean */
809 return TRUE;
811 else
812 return FALSE;
816 /* Taken from Terminal by os-cillation: terminal_screen_get_working_directory, thanks.
817 * Determines the working directory using various OS-specific mechanisms and stores the determined
818 * directory in vte_info.dir. Note: vte_info.dir contains the real path. */
819 const gchar *vte_get_working_directory(void)
821 if (pid > 0)
823 gchar buffer[4096 + 1];
824 gchar *file = g_strdup_printf("/proc/%d/cwd", pid);
825 gint length = readlink(file, buffer, sizeof(buffer));
827 if (length > 0 && *buffer == '/')
829 buffer[length] = '\0';
830 g_free(vte_info.dir);
831 vte_info.dir = g_strdup(buffer);
833 else if (length == 0)
835 gchar *cwd = g_get_current_dir();
837 if (cwd != NULL)
839 if (chdir(file) == 0)
841 g_free(vte_info.dir);
842 vte_info.dir = g_get_current_dir();
843 if (chdir(cwd) != 0)
844 geany_debug("%s: %s", G_STRFUNC, g_strerror(errno));
846 g_free(cwd);
849 g_free(file);
852 return vte_info.dir;
856 /* Changes the current working directory of the VTE to the path of the given filename.
857 * filename is expected to be in UTF-8 encoding.
858 * filename can also be a path, then it is used directly.
859 * If force is set to TRUE, it will always change the cwd
861 void vte_cwd(const gchar *filename, gboolean force)
863 if (vte_info.have_vte && (vc->follow_path || force) &&
864 filename != NULL && g_path_is_absolute(filename))
866 gchar *path;
868 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
869 path = g_strdup(filename);
870 else
871 path = g_path_get_dirname(filename);
873 vte_get_working_directory(); /* refresh vte_info.dir */
874 if (! utils_str_equal(path, vte_info.dir))
876 /* use g_shell_quote to avoid problems with spaces, '!' or something else in path */
877 gchar *quoted_path = g_shell_quote(path);
878 gchar *cmd = g_strconcat(vc->send_cmd_prefix, "cd ", quoted_path, "\n", NULL);
879 if (! vte_send_cmd(cmd))
881 const gchar *msg = _("Directory not changed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
882 ui_set_statusbar(FALSE, "%s", msg);
883 geany_debug("%s", msg);
885 g_free(quoted_path);
886 g_free(cmd);
888 g_free(path);
893 static void vte_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
894 gint x, gint y, GtkSelectionData *data, guint info, guint ltime)
896 if (info == TARGET_TEXT_PLAIN)
898 if (gtk_selection_data_get_format(data) == 8 && gtk_selection_data_get_length(data) > 0)
899 vf->vte_terminal_feed_child(VTE_TERMINAL(widget),
900 (const gchar*) gtk_selection_data_get_data(data),
901 gtk_selection_data_get_length(data));
903 else
905 gchar *text = (gchar*) gtk_selection_data_get_text(data);
906 if (!EMPTY(text))
907 vf->vte_terminal_feed_child(VTE_TERMINAL(widget), text, strlen(text));
908 g_free(text);
910 gtk_drag_finish(drag_context, TRUE, FALSE, ltime);
914 static void on_check_run_in_vte_toggled(GtkToggleButton *togglebutton, GtkWidget *user_data)
916 g_return_if_fail(GTK_IS_WIDGET(user_data));
917 gtk_widget_set_sensitive(user_data, gtk_toggle_button_get_active(togglebutton));
921 static void on_term_font_set(GtkFontButton *widget, gpointer user_data)
923 const gchar *fontbtn = gtk_font_button_get_font_name(widget);
925 if (! utils_str_equal(fontbtn, vc->font))
927 SETPTR(vc->font, g_strdup(gtk_font_button_get_font_name(widget)));
928 vte_apply_user_settings();
933 static void on_term_fg_color_set(GtkColorButton *widget, gpointer user_data)
935 gtk_color_button_get_color(widget, &vc->colour_fore);
939 static void on_term_bg_color_set(GtkColorButton *widget, gpointer user_data)
941 gtk_color_button_get_color(widget, &vc->colour_back);
945 void vte_append_preferences_tab(void)
947 if (vte_info.have_vte)
949 GtkWidget *frame_term, *button_shell, *entry_shell;
950 GtkWidget *check_run_in_vte, *check_skip_script;
951 GtkWidget *font_button, *fg_color_button, *bg_color_button;
953 button_shell = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "button_term_shell"));
954 entry_shell = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "entry_shell"));
955 ui_setup_open_button_callback(button_shell, NULL,
956 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_ENTRY(entry_shell));
958 check_skip_script = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "check_skip_script"));
959 gtk_widget_set_sensitive(check_skip_script, vc->run_in_vte);
961 check_run_in_vte = GTK_WIDGET(ui_lookup_widget(ui_widgets.prefs_dialog, "check_run_in_vte"));
962 g_signal_connect(G_OBJECT(check_run_in_vte), "toggled",
963 G_CALLBACK(on_check_run_in_vte_toggled), check_skip_script);
965 font_button = ui_lookup_widget(ui_widgets.prefs_dialog, "font_term");
966 g_signal_connect(font_button, "font-set", G_CALLBACK(on_term_font_set), NULL);
968 fg_color_button = ui_lookup_widget(ui_widgets.prefs_dialog, "color_fore");
969 g_signal_connect(fg_color_button, "color-set", G_CALLBACK(on_term_fg_color_set), NULL);
971 bg_color_button = ui_lookup_widget(ui_widgets.prefs_dialog, "color_back");
972 g_signal_connect(bg_color_button, "color-set", G_CALLBACK(on_term_bg_color_set), NULL);
974 frame_term = ui_lookup_widget(ui_widgets.prefs_dialog, "frame_term");
975 gtk_widget_show_all(frame_term);
980 void vte_select_all(void)
982 if (vf->vte_terminal_select_all != NULL)
983 vf->vte_terminal_select_all(VTE_TERMINAL(vc->vte));
987 void vte_send_selection_to_vte(void)
989 GeanyDocument *doc;
990 gchar *text;
991 gsize len;
993 doc = document_get_current();
994 g_return_if_fail(doc != NULL);
996 if (sci_has_selection(doc->editor->sci))
998 text = sci_get_selection_contents(doc->editor->sci);
1000 else
1001 { /* Get the current line */
1002 gint line_num = sci_get_current_line(doc->editor->sci);
1003 text = sci_get_line(doc->editor->sci, line_num);
1006 len = strlen(text);
1008 if (vc->send_selection_unsafe)
1009 { /* Explicitly append a trailing newline character to get the command executed,
1010 this is disabled by default as it could cause all sorts of damage. */
1011 if (text[len-1] != '\n' && text[len-1] != '\r')
1013 SETPTR(text, g_strconcat(text, "\n", NULL));
1014 len++;
1017 else
1018 { /* Make sure there is no newline character at the end to prevent unwanted execution */
1019 while (text[len-1] == '\n' || text[len-1] == '\r')
1021 text[len-1] = '\0';
1022 len--;
1026 vf->vte_terminal_feed_child(VTE_TERMINAL(vc->vte), text, len);
1028 /* show the VTE */
1029 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
1030 gtk_widget_grab_focus(vc->vte);
1031 msgwin_show_hide(TRUE);
1033 g_free(text);
1037 #endif