Build with DEBUG defined
[tilda-gobject.git] / tilda-terminal.c
index 92e73b6..78a91bf 100644 (file)
@@ -2,6 +2,11 @@
 #include "tilda-terminal.h"
 #include "tilda-terminal-dbus-glue.h"
 
+// FIXME: temporary helpers for gettext
+// TODO:  remove these
+#define _(X) X
+#define N_(X) X
+
 static void
 tilda_terminal_dbus_register_object (TildaTerminal *tt)
 {
@@ -14,13 +19,196 @@ tilda_terminal_dbus_register_object (TildaTerminal *tt)
        g_free (object_path);
 }
 
-static GObjectClass *parent_class = NULL;
+/**
+ * Start the current tt->shell in the given TildaTerminal
+ * NOTE: this will kill whatever is running in the terminal,
+ * NOTE: and run the current tt->shell instead :)
+ * Return: TRUE if ok, FALSE otherwise
+ */
+static gboolean
+tilda_terminal_start_shell (TildaTerminal *tt)
+{
+       gint ret;
+       gint argc;
+       gchar **argv;
+       GError *error = NULL;
 
-/* API */
+       /* Launch a custom command if tt->shell is set (not NULL) */
+       if (tt->shell)
+       {
+               /* Try to parse the user's custom command */
+               ret = g_shell_parse_argv (tt->shell, &argc, &argv, &error);
+
+               if (ret == FALSE)
+               {
+                       g_printerr (_("Problem parsing custom command: %s\n"), error->message);
+                       g_printerr (_("Launching default shell instead\n"));
+
+                       g_error_free (error);
+                       goto launch_default_shell;
+               }
+
+               /* Try to start the user's custom command */
+               ret = vte_terminal_fork_command (VTE_TERMINAL(tt->vte_term),
+                                                                                argv[0], /* Command */
+                                                                                argv,    /* Arg Vector */
+                                                                                NULL,    /* Env Vector */
+                                                                                tt->working_directory, /* Start directory */
+                                                                                TRUE,    /* Add to lastlog */
+                                                                                TRUE,    /* Add to utmp */
+                                                                                TRUE);   /* Add to wtmp */
+
+               g_strfreev (argv);
+
+               /* Check for error */
+               if (ret == -1)
+               {
+                       g_printerr (_("Unable to launch custom command: %s\n"), tt->shell);
+                       g_printerr (_("Launching default shell instead\n"));
+
+                       goto launch_default_shell;
+               }
+
+               return TRUE; /* SUCCESS: the early way out */
+       }
 
-/*
- * All GObject stuff is below. You probably don't need to change this...
+launch_default_shell:
+
+    ret = vte_terminal_fork_command (VTE_TERMINAL(tt->vte_term),
+                                                                        NULL, /* Command -- VTE will figure it out */
+                                                                        NULL, /* Arg Vector */
+                                                                        NULL, /* Env Vector */
+                                                                        tt->working_directory, /* Start Directory */
+                                                                        TRUE, /* Add to lastlog */
+                                                                        TRUE, /* Add to utmp */
+                                                                        TRUE);/* Add to wtmp */
+
+       if (ret == -1)
+       {
+               g_printerr (_("Unable to launch default shell\n"));
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+/**
+ * Called when the child process running in the VteTerminal exits.
  */
+static void
+tilda_terminal_child_exited_cb (GtkWidget *widget, gpointer data)
+{
+       TildaTerminal *self = TILDA_TERMINAL(data);
+
+       /* These can stay here. They don't need to go into a header because
+        * they are only used at this point in the code. */
+       enum exit_actions { HOLD_TERMINAL_OPEN, RESTART_COMMAND, EXIT_TERMINAL };
+
+       /* Check the user's preference for what to do when the child terminal
+        * is closed. Take the appropriate action */
+       switch (self->exit_action)
+       {
+               case EXIT_TERMINAL:
+                       tilda_window_remove_term (TILDA_WINDOW(self->parent_window), self->number);
+                       break;
+               case RESTART_COMMAND:
+                       vte_terminal_feed (VTE_TERMINAL(self->vte_term), "\r\n\r\n", 4);
+                       tilda_terminal_start_shell (self);
+                       break;
+               case HOLD_TERMINAL_OPEN:
+                       break;
+               default:
+                       break;
+       }
+}
+
+/**
+ * Called when the child window title changes. Determines if a new
+ * title needs to be put into the notebook's tab label.
+ */
+static void
+tilda_terminal_window_title_changed_cb (GtkWidget *widget, gpointer data)
+{
+       TildaTerminal *self = TILDA_TERMINAL(data);
+       TildaWindow *parent_window = TILDA_WINDOW(self->parent_window);
+       GtkWidget *label;
+       const gchar *vte_title;
+       gchar *new_title;
+
+       enum dynamic_titles { NOT_DISPLAYED, AFTER_INITIAL, BEFORE_INITIAL, REPLACE_INITIAL };
+       label = gtk_notebook_get_tab_label (GTK_NOTEBOOK(parent_window->notebook), self->hbox);
+
+       /* If we aren't using a dynamic title -- NOT_DISPLAYED -- then just
+        * set it to the static title and exit */
+       if (!self->dynamic_title)
+       {
+               gtk_label_set_text (GTK_LABEL(label), self->title);
+               return;
+       }
+
+       /* Get the title from VTE */
+       vte_title = vte_terminal_get_window_title (VTE_TERMINAL (widget));
+
+       /* Take the appropriate action */
+       switch (self->dynamic_title)
+       {
+               case REPLACE_INITIAL:
+                       new_title = g_strdup (vte_title);
+                       break;
+
+               case BEFORE_INITIAL:
+                       new_title = g_strdup_printf ("%s - %s", vte_title, self->title);
+                       break;
+
+               case AFTER_INITIAL:
+                       new_title = g_strdup_printf ("%s - %s", self->title, vte_title);
+                       break;
+
+               case NOT_DISPLAYED:
+               default:
+                       g_printerr (_("FIXME: Bad value of self->dynamic_title\n"));
+                       new_title = g_strdup(self->title);
+                       break;
+       }
+
+       gtk_label_set_text (GTK_LABEL(label), new_title);
+       g_free (new_title);
+}
+
+/**
+ * Set the given TildaTerminal to the appropriate transparency level
+ * based on the self->transparency_percent member. */
+static void
+tilda_terminal_set_transparent (TildaTerminal *self)
+{
+       TildaWindow *parent_window = TILDA_WINDOW(self->parent_window);
+       gdouble temp;
+
+       /* Convert the transparency to VTE's format */
+       temp = ((gdouble) self->transparency_percent) / 100.0;
+
+       if (self->transparency_percent > 0)
+       {
+               vte_terminal_set_background_saturation (VTE_TERMINAL(self->vte_term), temp);
+               vte_terminal_set_opacity (VTE_TERMINAL(self->vte_term), (1.0 - temp) * 0xffff);
+
+               /* Use fake transparency if necessary */
+               vte_terminal_set_background_transparent (VTE_TERMINAL(self->vte_term),
+                                                                                                !parent_window->have_real_transparency);
+               return;
+       }
+
+       /* Turn off transparency */
+       vte_terminal_set_background_saturation (VTE_TERMINAL(self->vte_term), 0);
+       vte_terminal_set_opacity (VTE_TERMINAL(self->vte_term), 0xffff);
+       vte_terminal_set_background_transparent (VTE_TERMINAL(self->vte_term), FALSE);
+}
+
+/*******************************************************************************
+ * All GObject stuff is below. You probably don't need to change this...
+ ******************************************************************************/
+
+static GObjectClass *parent_class = NULL;
 
 enum tilda_terminal_properties {
        TILDA_TERMINAL_NUMBER = 1,
@@ -33,6 +221,7 @@ enum tilda_terminal_properties {
        TILDA_TERMINAL_FONT,
        TILDA_TERMINAL_TITLE,
        TILDA_TERMINAL_WORKING_DIRECTORY,
+       TILDA_TERMINAL_WEB_BROWSER,
 
        TILDA_TERMINAL_SCROLLBACK_LINES,
        TILDA_TERMINAL_TRANSPARENCY_PERCENT,
@@ -51,6 +240,7 @@ enum tilda_terminal_properties {
        TILDA_TERMINAL_AUDIBLE_BELL,
        TILDA_TERMINAL_VISIBLE_BELL,
        TILDA_TERMINAL_DOUBLE_BUFFERED,
+       TILDA_TERMINAL_MOUSE_AUTOHIDE,
 };
 
 static void
@@ -105,6 +295,7 @@ tilda_terminal_set_property (GObject      *object,
                case TILDA_TERMINAL_SHELL:
                        g_free (self->shell);
                        self->shell = g_value_dup_string (value);
+                       tilda_terminal_start_shell (self);
                        g_print ("terminal shell: %s\n", self->shell);
                        break;
 
@@ -129,6 +320,12 @@ tilda_terminal_set_property (GObject      *object,
                        g_print ("terminal wrk dir: %s\n", self->working_directory);
                        break;
 
+               case TILDA_TERMINAL_WEB_BROWSER:
+                       g_free (self->web_browser);
+                       self->web_browser = g_value_dup_string (value);
+                       g_print ("terminal web browser: %s\n", self->web_browser);
+                       break;
+
                case TILDA_TERMINAL_SCROLLBACK_LINES:
                        self->scrollback_lines = g_value_get_int (value);
                        vte_terminal_set_scrollback_lines (VTE_TERMINAL(self->vte_term), self->scrollback_lines);
@@ -137,6 +334,7 @@ tilda_terminal_set_property (GObject      *object,
 
                case TILDA_TERMINAL_TRANSPARENCY_PERCENT:
                        self->transparency_percent = g_value_get_int (value);
+                       tilda_terminal_set_transparent (self);
                        g_print ("terminal transp percent: %d\n", self->transparency_percent);
                        break;
 
@@ -218,6 +416,12 @@ tilda_terminal_set_property (GObject      *object,
                        g_print ("terminal double buffered: %d\n", self->double_buffered);
                        break;
 
+               case TILDA_TERMINAL_MOUSE_AUTOHIDE:
+                       self->mouse_autohide = g_value_get_boolean (value);
+                       vte_terminal_set_mouse_autohide (VTE_TERMINAL(self->vte_term), self->mouse_autohide);
+                       g_print ("terminal mouse autohide: %d\n", self->mouse_autohide);
+                       break;
+
                default:
                        /* We don't have this property... */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -267,6 +471,10 @@ tilda_terminal_get_property (GObject    *object,
                        g_value_set_string (value, self->working_directory);
                        break;
 
+               case TILDA_TERMINAL_WEB_BROWSER:
+                       g_value_set_string (value, self->web_browser);
+                       break;
+
                case TILDA_TERMINAL_SCROLLBACK_LINES:
                        g_value_set_int (value, self->scrollback_lines);
                        break;
@@ -327,6 +535,9 @@ tilda_terminal_get_property (GObject    *object,
                        g_value_set_boolean (value, self->double_buffered);
                        break;
 
+               case TILDA_TERMINAL_MOUSE_AUTOHIDE:
+                       g_value_set_boolean (value, self->mouse_autohide);
+
                default:
                        /* We don't have this property... */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -361,11 +572,15 @@ tilda_terminal_constructor (GType                  type,
        gtk_widget_show (self->scrollbar);
 
 
+       /* Connect Signals */
        g_signal_connect (G_OBJECT(self->vte_term), "child-exited",
-                                         G_CALLBACK(gtk_main_quit), self);
-
-       vte_terminal_fork_command (VTE_TERMINAL(self->vte_term), NULL, NULL, NULL, NULL, FALSE, FALSE, FALSE);
+                                         G_CALLBACK(tilda_terminal_child_exited_cb), self);
+       g_signal_connect (G_OBJECT(self->vte_term), "eof",
+                                         G_CALLBACK(tilda_terminal_child_exited_cb), self);
+       g_signal_connect (G_OBJECT(self->vte_term), "window-title-changed",
+                                         G_CALLBACK(tilda_terminal_window_title_changed_cb), self);
 
+       tilda_terminal_start_shell (self);
        tilda_terminal_dbus_register_object (self);
 
        return obj;
@@ -519,6 +734,16 @@ tilda_terminal_class_init (gpointer g_class,
                                                                         TILDA_TERMINAL_WORKING_DIRECTORY,
                                                                         pspec);
 
+       pspec = g_param_spec_string ("web-browser",
+                                                                "Terminal's web browser command",
+                                                                NULL,
+                                                                NULL,
+                                                                G_PARAM_READWRITE);
+
+       g_object_class_install_property (gobject_class,
+                                                                        TILDA_TERMINAL_WEB_BROWSER,
+                                                                        pspec);
+
        pspec = g_param_spec_int ("scrollback-lines",
                                                          "Terminal's scrollback amount (lines)",
                                                          "Get/Set terminal's scrollback amount",
@@ -662,6 +887,13 @@ tilda_terminal_class_init (gpointer g_class,
        g_object_class_install_property (gobject_class,
                                                                         TILDA_TERMINAL_DOUBLE_BUFFERED,
                                                                         pspec);
+
+       pspec = g_param_spec_boolean ("mouse-autohide",
+                                                                 NULL, NULL, FALSE, G_PARAM_READWRITE);
+
+       g_object_class_install_property (gobject_class,
+                                                                        TILDA_TERMINAL_MOUSE_AUTOHIDE,
+                                                                        pspec);
 }
 
 GType