terminal-screen.c 74.9 KB
Newer Older
1
/*
Christian Persch's avatar
Christian Persch committed
2 3
 * Copyright © 2001 Havoc Pennington
 * Copyright © 2007, 2008 Christian Persch
4
 *
5 6 7 8 9 10
 * Gnome-terminal is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Gnome-terminal is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
15 16
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18
 */

Christian Persch's avatar
Christian Persch committed
19
#include <config.h>
20

Christian Persch's avatar
Christian Persch committed
21 22 23 24
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
25

Christian Persch's avatar
Christian Persch committed
26
#include <gtk/gtk.h>
27
#include <gdk/gdkkeysyms.h>
28

29
#ifdef GDK_WINDOWING_X11
30
#include <gdk/gdkx.h>
31
#endif
32

33
#include <gconf/gconf.h>
Christian Persch's avatar
Christian Persch committed
34

35
#include "terminal-accels.h"
Christian Persch's avatar
Christian Persch committed
36
#include "terminal-app.h"
37
#include "terminal-debug.h"
Christian Persch's avatar
Christian Persch committed
38
#include "terminal-intl.h"
39
#include "terminal-marshal.h"
40
#include "terminal-profile.h"
41
#include "terminal-screen-container.h"
42
#include "terminal-util.h"
Christian Persch's avatar
Christian Persch committed
43
#include "terminal-window.h"
44

Christian Persch's avatar
Rename.  
Christian Persch committed
45
#include "eggshell.h"
46

47 48 49
#define URL_MATCH_CURSOR  (GDK_HAND2)
#define SKEY_MATCH_CURSOR (GDK_HAND2)

50 51 52
typedef struct
{
  int tag;
53
  TerminalURLFlavour flavor;
54 55
} TagData;

56 57 58 59 60 61
struct _TerminalScreenPrivate
{
  TerminalWindow *window;
  TerminalProfile *profile; /* may be NULL at times */
  guint profile_changed_id;
  guint profile_forgotten_id;
62 63
  char *raw_title, *raw_icon_title;
  char *cooked_title, *cooked_icon_title;
64
  char *override_title;
65
  gboolean icon_title_set;
66
  char *initial_working_directory;
67
  char **initial_env;
68
  char **override_command;
69
  int child_pid;
70
  int pty_fd;
71
  double font_scale;
72
  gboolean user_title; /* title was manually set */
73
  GSList *match_tags;
74
  guint launch_child_source_id;
75 76
};

77 78
enum
{
79
  PROFILE_SET,
80
  SHOW_POPUP_MENU,
81
  MATCH_CLICKED,
82
  CLOSE_SCREEN,
83 84 85
  LAST_SIGNAL
};

86 87
enum {
  PROP_0,
88
  PROP_PROFILE,
Christian Persch's avatar
Christian Persch committed
89 90
  PROP_ICON_TITLE,
  PROP_ICON_TITLE_SET,
91
  PROP_OVERRIDE_COMMAND,
Christian Persch's avatar
Christian Persch committed
92
  PROP_TITLE,
93
  PROP_INITIAL_ENVIRONMENT
94 95
};

Christian Persch's avatar
Christian Persch committed
96 97 98 99 100 101 102 103 104 105
enum
{
  TARGET_COLOR,
  TARGET_BGIMAGE,
  TARGET_RESET_BG,
  TARGET_MOZ_URL,
  TARGET_NETSCAPE_URL,
  TARGET_TAB
};

106
static void terminal_screen_dispose     (GObject             *object);
107
static void terminal_screen_finalize    (GObject             *object);
Christian Persch's avatar
Christian Persch committed
108 109 110 111 112 113 114
static void terminal_screen_drag_data_received (GtkWidget        *widget,
                                                GdkDragContext   *context,
                                                gint              x,
                                                gint              y,
                                                GtkSelectionData *selection_data,
                                                guint             info,
                                                guint             time);
115 116 117
static void terminal_screen_system_font_notify_cb (TerminalApp *app,
                                                   GParamSpec *pspec,
                                                   TerminalScreen *screen);
118
static void terminal_screen_change_font (TerminalScreen *screen);
Christian Persch's avatar
Christian Persch committed
119 120 121
static gboolean terminal_screen_popup_menu (GtkWidget *widget);
static gboolean terminal_screen_button_press (GtkWidget *widget,
                                              GdkEventButton *event);
122
static void terminal_screen_launch_child_on_idle (TerminalScreen *screen);
Christian Persch's avatar
Christian Persch committed
123
static void terminal_screen_child_exited  (VteTerminal *terminal);
124

125
static void terminal_screen_window_title_changed      (VteTerminal *vte_terminal,
126
                                                       TerminalScreen *screen);
127
static void terminal_screen_icon_title_changed        (VteTerminal *vte_terminal,
128
                                                       TerminalScreen *screen);
129

130 131
static void update_color_scheme                      (TerminalScreen *screen);

132
static gboolean terminal_screen_format_title (TerminalScreen *screen, const char *raw_title, char **old_cooked_title);
133 134 135

static void terminal_screen_cook_title      (TerminalScreen *screen);
static void terminal_screen_cook_icon_title (TerminalScreen *screen);
136

137 138 139 140 141 142
static char* terminal_screen_check_match       (TerminalScreen            *screen,
                                                int                   column,
                                                int                   row,
                                                int                  *flavor);
static void  terminal_screen_skey_match_remove (TerminalScreen            *screen);

143
static guint signals[LAST_SIGNAL];
144

145 146 147 148 149 150
#define USERCHARS "-[:alnum:]"
#define USERCHARS_CLASS "[" USERCHARS "]"
#define PASSCHARS_CLASS "[-[:alnum:]\\Q,?;.:/!%$^*&~\"#'\\E]"
#define HOSTCHARS_CLASS "[-[:alnum:]]"
#define HOST HOSTCHARS_CLASS "+(\\." HOSTCHARS_CLASS "+)*"
#define PORT "(?:\\:[[:digit:]]{1,5})?"
151
#define PATHCHARS_CLASS "[-[:alnum:]\\Q_$.+!*,;@&=?/~#%\\E]"
152
#define PATHTERM_CLASS "[^\\Q]'.}>) \t\r\n,\"\\E]"
Christian Persch's avatar
Christian Persch committed
153
#define SCHEME "(?:news:|telnet:|nntp:|file:\\/|https?:|ftps?:|sftp:|webcal:)"
154
#define USERPASS USERCHARS_CLASS "+(?:" PASSCHARS_CLASS "+)?"
155
#define URLPATH   "(?:(/"PATHCHARS_CLASS"+(?:[(]"PATHCHARS_CLASS"*[)])*"PATHCHARS_CLASS"*)*"PATHTERM_CLASS")?"
156 157 158 159

typedef struct {
  const char *pattern;
  TerminalURLFlavour flavor;
160
  GRegexCompileFlags flags;
161 162 163
} TerminalRegexPattern;

static const TerminalRegexPattern url_regex_patterns[] = {
164 165
  { SCHEME "//(?:" USERPASS "\\@)?" HOST PORT URLPATH, FLAVOR_AS_IS, G_REGEX_CASELESS },
  { "(?:www|ftp)" HOSTCHARS_CLASS "*\\." HOST PORT URLPATH , FLAVOR_DEFAULT_TO_HTTP, G_REGEX_CASELESS  },
166
  { "(?:callto:|h323:|sip:)" USERCHARS_CLASS "[" USERCHARS ".]*(?:" PORT "/[a-z0-9]+)?\\@" HOST, FLAVOR_VOIP_CALL, G_REGEX_CASELESS  },
167
  { "(?:mailto:)?" USERCHARS_CLASS "[" USERCHARS ".]*\\@" HOSTCHARS_CLASS "+\\." HOST, FLAVOR_EMAIL, G_REGEX_CASELESS  },
168
  { "news:[[:alnum:]\\Q^_{|}~!\"#$%&'()*+,./;:=?`\\E]+", FLAVOR_AS_IS, G_REGEX_CASELESS  },
169 170 171 172 173 174 175 176 177 178 179 180 181
};

static const TerminalRegexPattern skey_regex_patterns[] = {
  { "s/key [[:digit:]]* [-[:alnum:]]*",         FLAVOR_AS_IS },
  { "otp-[a-z0-9]* [[:digit:]]* [-[:alnum:]]*", FLAVOR_AS_IS },
};

static GRegex **url_regexes;
static TerminalURLFlavour *url_regex_flavors;
static guint n_url_regexes;
static GRegex **skey_regexes;
static guint n_skey_regexes;

182
G_DEFINE_TYPE (TerminalScreen, terminal_screen, VTE_TYPE_TERMINAL)
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static char *
cwd_of_pid (int pid)
{
  static const char patterns[][18] = {
    "/proc/%d/cwd",         /* Linux */
    "/proc/%d/path/cwd",    /* Solaris >= 10 */
  };
  guint i;
  
  if (pid == -1)
    return NULL;

  /* Try to get the working directory using various OS-specific mechanisms */
  for (i = 0; i < G_N_ELEMENTS (patterns); ++i)
    {
      char cwd_file[64];
      char buf[PATH_MAX + 1];
      int len;

      g_snprintf (cwd_file, sizeof (cwd_file), patterns[i], pid);
      len = readlink (cwd_file, buf, sizeof (buf) - 1);

      if (len > 0 && buf[0] == '/')
        return g_strndup (buf, len);

      /* If that didn't do it, try this hack */
      if (len <= 0)
        {
          char *cwd, *working_dir = NULL;

          cwd = g_get_current_dir ();
          if (cwd != NULL)
            {
              /* On Solaris, readlink returns an empty string, but the
               * link can be used as a directory, including as a target
               * of chdir().
               */
              if (chdir (cwd_file) == 0)
                {
                  working_dir = g_get_current_dir ();
                  chdir (cwd);
                }
              g_free (cwd);
            }

          if (working_dir)
            return working_dir;
        }
    }

  return NULL;
}

237 238 239 240 241 242
static void
free_tag_data (TagData *tagdata)
{
  g_slice_free (TagData, tagdata);
}

243
#ifdef GNOME_ENABLE_DEBUG
244 245 246
static void
parent_size_request (GtkWidget *scrolled_window, GtkRequisition *req, GtkWidget *screen)
{
247 248 249
  _terminal_debug_print (TERMINAL_DEBUG_GEOMETRY,
                         "[screen %p] scrolled-window size req %d : %d\n",
                         screen, req->width, req->height);
250
}
251
#endif
252 253 254 255 256 257 258 259 260 261 262 263 264 265

static void
parent_parent_set_cb (GtkWidget *widget,
                      GtkWidget *old_parent,
                      TerminalScreen *screen)
{
  TerminalScreenPrivate *priv = screen->priv;
  GtkWidget *toplevel;

  if (widget->parent)
    {
      g_return_if_fail (GTK_IS_NOTEBOOK (widget->parent));

      toplevel = gtk_widget_get_toplevel (widget);
266 267 268
#if GTK_CHECK_VERSION (2, 19, 3)
      g_return_if_fail (gtk_widget_is_toplevel (toplevel));
#else
269
      g_return_if_fail (GTK_WIDGET_TOPLEVEL (toplevel));
270
#endif
271 272 273 274 275 276

      priv->window = TERMINAL_WINDOW (toplevel);
    }
  else
    priv->window = NULL;
}
277

278
static void
279 280
parent_set_callback (GtkWidget *widget,
                     GtkWidget *old_parent)
281
{
282 283 284 285 286 287
  if (old_parent)
    g_signal_handlers_disconnect_by_func (old_parent, G_CALLBACK (parent_parent_set_cb), widget);

  if (widget->parent)
    g_signal_connect (widget->parent, "parent-set", G_CALLBACK (parent_parent_set_cb), widget);

288
#ifdef GNOME_ENABLE_DEBUG
289
  _TERMINAL_DEBUG_IF (TERMINAL_DEBUG_GEOMETRY)
290 291 292 293 294 295
    {
      if (old_parent)
        g_signal_handlers_disconnect_by_func (old_parent, G_CALLBACK (parent_size_request), widget);
      if (widget->parent)
        g_signal_connect (widget->parent, "size-request", G_CALLBACK (parent_size_request), widget);
    }
296
#endif
297
}
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
static void
terminal_screen_class_enable_menu_bar_accel_notify_cb (TerminalApp *app,
                                                       GParamSpec *pspec,
                                                       TerminalScreenClass *klass)
{
  static gboolean is_enabled = TRUE; /* the binding is enabled by default since GtkWidgetClass installs it */
  gboolean enable;
  GtkBindingSet *binding_set;

  g_object_get (app, TERMINAL_APP_ENABLE_MENU_BAR_ACCEL, &enable, NULL);

  /* Only remove the 'skip' entry when we have added it previously! */
  if (enable == is_enabled)
    return;

  is_enabled = enable;

  binding_set = gtk_binding_set_by_class (klass);
  if (enable)
    gtk_binding_entry_remove (binding_set, GDK_F10, GDK_SHIFT_MASK);
  else
    gtk_binding_entry_skip (binding_set, GDK_F10, GDK_SHIFT_MASK);
}

Christian Persch's avatar
Christian Persch committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
static void
terminal_screen_realize (GtkWidget *widget)
{
  TerminalScreen *screen = TERMINAL_SCREEN (widget);
  TerminalScreenPrivate *priv = screen->priv;
  TerminalBackgroundType bg_type;

  GTK_WIDGET_CLASS (terminal_screen_parent_class)->realize (widget);

  g_assert (priv->window != NULL);

  /* FIXME: Don't enable this if we have a compmgr. */
  bg_type = terminal_profile_get_property_enum (priv->profile, TERMINAL_PROFILE_BACKGROUND_TYPE);
  vte_terminal_set_background_transparent (VTE_TERMINAL (screen),
                                           bg_type == TERMINAL_BACKGROUND_TRANSPARENT &&
                                           !terminal_window_uses_argb_visual (priv->window));
}

static void
terminal_screen_style_set (GtkWidget *widget,
                           GtkStyle *previous_style)
{
  TerminalScreen *screen = TERMINAL_SCREEN (widget);
  void (* style_set) (GtkWidget*, GtkStyle*) = GTK_WIDGET_CLASS (terminal_screen_parent_class)->style_set;

  if (style_set)
    style_set (widget, previous_style);

  update_color_scheme (screen);

  if (GTK_WIDGET_REALIZED (widget))
    terminal_screen_change_font (screen);
}

357
#ifdef GNOME_ENABLE_DEBUG
358 359 360
static void
size_request (GtkWidget *widget,
              GtkRequisition *req)
361
{
362 363 364
  _terminal_debug_print (TERMINAL_DEBUG_GEOMETRY,
                         "[screen %p] size-request %d : %d\n",
                         widget, req->width, req->height);
365
}
366

367 368 369
static void
size_allocate (GtkWidget *widget,
               GtkAllocation *allocation)
370
{
371 372 373
  _terminal_debug_print (TERMINAL_DEBUG_GEOMETRY,
                         "[screen %p] size-alloc   %d : %d at (%d, %d)\n",
                         widget, allocation->width, allocation->height, allocation->x, allocation->y);
374
}
375
#endif
376

377 378
static void
terminal_screen_init (TerminalScreen *screen)
379
{
380 381 382 383 384 385 386 387
  const GtkTargetEntry target_table[] = {
    { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, TARGET_TAB },
    { "application/x-color", 0, TARGET_COLOR },
    { "property/bgimage",    0, TARGET_BGIMAGE },
    { "x-special/gnome-reset-background", 0, TARGET_RESET_BG },
    { "text/x-moz-url",  0, TARGET_MOZ_URL },
    { "_NETSCAPE_URL", 0, TARGET_NETSCAPE_URL }
  };
388
  VteTerminal *terminal = VTE_TERMINAL (screen);
389
  TerminalScreenPrivate *priv;
390 391 392
  GtkTargetList *target_list;
  GtkTargetEntry *targets;
  int n_targets;
393
  guint i;
394

395
  priv = screen->priv = G_TYPE_INSTANCE_GET_PRIVATE (screen, TERMINAL_TYPE_SCREEN, TerminalScreenPrivate);
396

397 398
  vte_terminal_set_mouse_autohide (VTE_TERMINAL (screen), TRUE);

399
  priv->child_pid = -1;
400
  priv->pty_fd = -1;
401

402
  priv->font_scale = PANGO_SCALE_MEDIUM;
403

404 405 406 407 408 409 410 411 412
  for (i = 0; i < n_url_regexes; ++i)
    {
      TagData *tag_data;

      tag_data = g_slice_new (TagData);
      tag_data->flavor = url_regex_flavors[i];
      tag_data->tag = vte_terminal_match_add_gregex (terminal, url_regexes[i], 0);
      vte_terminal_match_set_cursor_type (terminal, tag_data->tag, URL_MATCH_CURSOR);

413
      priv->match_tags = g_slist_prepend (priv->match_tags, tag_data);
414
    }
415

416 417 418
  /* Setup DND */
  target_list = gtk_target_list_new (NULL, 0);
  gtk_target_list_add_uri_targets (target_list, 0);
419 420
  gtk_target_list_add_text_targets (target_list, 0);
  gtk_target_list_add_table (target_list, target_table, G_N_ELEMENTS (target_table));
421 422 423 424 425 426 427 428 429 430 431 432 433

  targets = gtk_target_table_new_from_list (target_list, &n_targets);

  gtk_drag_dest_set (GTK_WIDGET (screen),
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_DROP,
                     targets, n_targets,
                     GDK_ACTION_COPY | GDK_ACTION_MOVE);

  gtk_target_table_free (targets, n_targets);
  gtk_target_list_unref (target_list);

434
  priv->override_title = NULL;
435
  priv->user_title = FALSE;
436 437 438 439
  
  g_signal_connect (screen, "window-title-changed",
                    G_CALLBACK (terminal_screen_window_title_changed),
                    screen);
440
  g_signal_connect (screen, "icon-title-changed",
441
                    G_CALLBACK (terminal_screen_icon_title_changed),
442
                    screen);
443

444 445 446
  g_signal_connect (terminal_app_get (), "notify::system-font",
                    G_CALLBACK (terminal_screen_system_font_notify_cb), screen);

447 448
  g_signal_connect (screen, "parent-set", G_CALLBACK (parent_set_callback), NULL);

449
#ifdef GNOME_ENABLE_DEBUG
450
  _TERMINAL_DEBUG_IF (TERMINAL_DEBUG_GEOMETRY)
451 452 453 454
    {
      g_signal_connect_after (screen, "size-request", G_CALLBACK (size_request), NULL);
      g_signal_connect_after (screen, "size-allocate", G_CALLBACK (size_allocate), NULL);
    }
455
#endif
456 457
}

458 459 460 461 462 463 464 465 466 467
static void
terminal_screen_get_property (GObject *object,
                              guint prop_id,
                              GValue *value,
                              GParamSpec *pspec)
{
  TerminalScreen *screen = TERMINAL_SCREEN (object);

  switch (prop_id)
    {
468 469 470
      case PROP_PROFILE:
        g_value_set_object (value, terminal_screen_get_profile (screen));
        break;
Christian Persch's avatar
Christian Persch committed
471 472 473 474 475 476
      case PROP_ICON_TITLE:
        g_value_set_string (value, terminal_screen_get_icon_title (screen));
        break;
      case PROP_ICON_TITLE_SET:
        g_value_set_boolean (value, terminal_screen_get_icon_title_set (screen));
        break;
477 478 479
      case PROP_OVERRIDE_COMMAND:
        g_value_set_boxed (value, terminal_screen_get_override_command (screen));
        break;
480 481 482
      case PROP_INITIAL_ENVIRONMENT:
        g_value_set_boxed (value, terminal_screen_get_initial_environment (screen));
        break;
483 484 485 486 487 488 489 490 491
      case PROP_TITLE:
        g_value_set_string (value, terminal_screen_get_title (screen));
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

492 493 494 495 496 497 498 499 500 501
static void
terminal_screen_set_property (GObject *object,
                              guint prop_id,
                              const GValue *value,
                              GParamSpec *pspec)
{
  TerminalScreen *screen = TERMINAL_SCREEN (object);

  switch (prop_id)
    {
502 503 504 505 506 507 508 509
      case PROP_PROFILE: {
        TerminalProfile *profile;

        profile = g_value_get_object (value);
        g_assert (profile != NULL);
        terminal_screen_set_profile (screen, profile);
        break;
      }
510 511 512
      case PROP_OVERRIDE_COMMAND:
        terminal_screen_set_override_command (screen, g_value_get_boxed (value));
        break;
513 514 515
      case PROP_INITIAL_ENVIRONMENT:
        terminal_screen_set_initial_environment (screen, g_value_get_boxed (value));
        break;
516 517 518 519 520 521 522 523 524 525
      case PROP_ICON_TITLE:
      case PROP_ICON_TITLE_SET:
      case PROP_TITLE:
        /* not writable */
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

526 527 528 529
static void
terminal_screen_class_init (TerminalScreenClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Tony Tsui's avatar
Tony Tsui committed
530
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
Christian Persch's avatar
Christian Persch committed
531
  VteTerminalClass *terminal_class = VTE_TERMINAL_CLASS (klass);
532
  TerminalApp *app;
533
  guint i;
Tony Tsui's avatar
Tony Tsui committed
534

535
  object_class->dispose = terminal_screen_dispose;
536
  object_class->finalize = terminal_screen_finalize;
537
  object_class->get_property = terminal_screen_get_property;
538
  object_class->set_property = terminal_screen_set_property;
Tony Tsui's avatar
Tony Tsui committed
539

Christian Persch's avatar
Christian Persch committed
540 541 542 543 544
  widget_class->realize = terminal_screen_realize;
  widget_class->style_set = terminal_screen_style_set;
  widget_class->drag_data_received = terminal_screen_drag_data_received;
  widget_class->button_press_event = terminal_screen_button_press;
  widget_class->popup_menu = terminal_screen_popup_menu;
545

Christian Persch's avatar
Christian Persch committed
546 547
  terminal_class->child_exited = terminal_screen_child_exited;

548
  signals[PROFILE_SET] =
549
    g_signal_new (I_("profile-set"),
550 551 552 553
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (TerminalScreenClass, profile_set),
                  NULL, NULL,
554 555 556
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE,
                  1, TERMINAL_TYPE_PROFILE);
557
  
558
  signals[SHOW_POPUP_MENU] =
Christian Persch's avatar
Christian Persch committed
559
    g_signal_new (I_("show-popup-menu"),
560 561
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
562
                  G_STRUCT_OFFSET (TerminalScreenClass, show_popup_menu),
563 564 565 566 567 568
                  NULL, NULL,
                  g_cclosure_marshal_VOID__POINTER,
                  G_TYPE_NONE,
                  1,
                  G_TYPE_POINTER);

569 570
  signals[MATCH_CLICKED] =
    g_signal_new (I_("match-clicked"),
571 572
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
573 574 575 576 577
                  G_STRUCT_OFFSET (TerminalScreenClass, match_clicked),
                  g_signal_accumulator_true_handled, NULL,
                  _terminal_marshal_BOOLEAN__STRING_INT_UINT,
                  G_TYPE_BOOLEAN,
                  3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_UINT);
578
  
579 580 581 582 583 584 585 586 587
  signals[CLOSE_SCREEN] =
    g_signal_new (I_("close-screen"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (TerminalScreenClass, close_screen),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE,
                  0);
588 589 590 591 592 593 594 595

  g_object_class_install_property
    (object_class,
     PROP_PROFILE,
     g_param_spec_string ("profile", NULL, NULL,
                          NULL,
                          G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

Christian Persch's avatar
Christian Persch committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609
  g_object_class_install_property
    (object_class,
     PROP_ICON_TITLE,
     g_param_spec_string ("icon-title", NULL, NULL,
                          NULL,
                          G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

  g_object_class_install_property
    (object_class,
     PROP_ICON_TITLE_SET,
     g_param_spec_boolean ("icon-title-set", NULL, NULL,
                           FALSE,
                           G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

610 611
  g_object_class_install_property
    (object_class,
612
     PROP_OVERRIDE_COMMAND,
613 614 615 616
     g_param_spec_boxed ("override-command", NULL, NULL,
                         G_TYPE_STRV,
                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

Christian Persch's avatar
Christian Persch committed
617 618 619 620 621 622
  g_object_class_install_property
    (object_class,
     PROP_TITLE,
     g_param_spec_string ("title", NULL, NULL,
                          NULL,
                          G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
623

624 625 626 627 628 629 630
  g_object_class_install_property
    (object_class,
     PROP_INITIAL_ENVIRONMENT,
     g_param_spec_boxed ("initial-environment", NULL, NULL,
                         G_TYPE_STRV,
                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

631
  g_type_class_add_private (object_class, sizeof (TerminalScreenPrivate));
632 633 634 635 636 637 638 639 640 641 642 643

  /* Precompile the regexes */
  n_url_regexes = G_N_ELEMENTS (url_regex_patterns);
  url_regexes = g_new0 (GRegex*, n_url_regexes);
  url_regex_flavors = g_new0 (TerminalURLFlavour, n_url_regexes);
  n_skey_regexes = G_N_ELEMENTS (skey_regex_patterns);
  skey_regexes = g_new0 (GRegex*, n_skey_regexes);

  for (i = 0; i < n_url_regexes; ++i)
    {
      GError *error = NULL;

644 645 646
      url_regexes[i] = g_regex_new (url_regex_patterns[i].pattern,
                                    url_regex_patterns[i].flags | G_REGEX_OPTIMIZE,
                                    0, &error);
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
      if (error)
        {
          g_message ("%s", error->message);
          g_error_free (error);
        }

      url_regex_flavors[i] = url_regex_patterns[i].flavor;
    }

  for (i = 0; i < n_skey_regexes; ++i)
    {
      GError *error = NULL;

      skey_regexes[i] = g_regex_new (skey_regex_patterns[i].pattern, G_REGEX_OPTIMIZE, 0, &error);
      if (error)
        {
          g_message ("%s", error->message);
          g_error_free (error);
        }
    }
667 668 669 670 671 672

  /* This fixes bug #329827 */
  app = terminal_app_get ();
  terminal_screen_class_enable_menu_bar_accel_notify_cb (app, NULL, klass);
  g_signal_connect (app, "notify::" TERMINAL_APP_ENABLE_MENU_BAR_ACCEL,
                    G_CALLBACK (terminal_screen_class_enable_menu_bar_accel_notify_cb), klass);
673
}
674 675

static void
676
terminal_screen_dispose (GObject *object)
677
{
678
  TerminalScreen *screen = TERMINAL_SCREEN (object);
679
  TerminalScreenPrivate *priv = screen->priv;
680
  GtkSettings *settings;
681

682 683 684 685 686
  settings = gtk_widget_get_settings (GTK_WIDGET (screen));
  g_signal_handlers_disconnect_matched (settings, G_SIGNAL_MATCH_DATA,
                                        0, 0, NULL, NULL,
                                        screen);

687 688 689 690 691 692
  if (priv->launch_child_source_id != 0)
    {
      g_source_remove (priv->launch_child_source_id);
      priv->launch_child_source_id = 0;
    }

693 694 695 696 697 698
  G_OBJECT_CLASS (terminal_screen_parent_class)->dispose (object);
}

static void
terminal_screen_finalize (GObject *object)
{
Christian Persch's avatar
Christian Persch committed
699
  TerminalScreen *screen = TERMINAL_SCREEN (object);
700
  TerminalScreenPrivate *priv = screen->priv;
701

702 703 704
  g_signal_handlers_disconnect_by_func (terminal_app_get (),
                                        G_CALLBACK (terminal_screen_system_font_notify_cb),
                                        screen);
705

706 707
  terminal_screen_set_profile (screen, NULL);

708 709
  g_free (priv->raw_title);
  g_free (priv->cooked_title);
710
  g_free (priv->override_title);
711 712
  g_free (priv->raw_icon_title);
  g_free (priv->cooked_icon_title);
713
  g_free (priv->initial_working_directory);
714
  g_strfreev (priv->override_command);
Christian Persch's avatar
Christian Persch committed
715
  g_strfreev (priv->initial_env);
716

717 718
  g_slist_foreach (priv->match_tags, (GFunc) free_tag_data, NULL);
  g_slist_free (priv->match_tags);
719

720
  G_OBJECT_CLASS (terminal_screen_parent_class)->finalize (object);
721 722
}

723 724 725 726 727 728 729
TerminalScreen *
terminal_screen_new (TerminalProfile *profile,
                     char           **override_command,
                     const char      *title,
                     const char      *working_dir,
                     char           **child_env,
                     double           zoom)
730
{
731
  TerminalScreen *screen;
732
  TerminalScreenPrivate *priv;
733 734 735 736

  g_return_val_if_fail (TERMINAL_IS_PROFILE (profile), NULL);

  screen = g_object_new (TERMINAL_TYPE_SCREEN, NULL);
737
  priv = screen->priv;
738 739 740

  terminal_screen_set_profile (screen, profile);

741 742 743 744
  vte_terminal_set_size (VTE_TERMINAL (screen),
			 terminal_profile_get_property_int (profile, TERMINAL_PROFILE_DEFAULT_SIZE_COLUMNS),
			 terminal_profile_get_property_int (profile, TERMINAL_PROFILE_DEFAULT_SIZE_ROWS));

745 746 747
  if (title)
    terminal_screen_set_override_title (screen, title);

748
  priv->initial_working_directory = g_strdup (working_dir);
749 750 751 752 753 754 755 756 757 758

  if (override_command)
    terminal_screen_set_override_command (screen, override_command);

  if (child_env)
    terminal_screen_set_initial_environment (screen, child_env);

  terminal_screen_set_font_scale (screen, zoom);
  terminal_screen_set_font (screen);

759 760 761
  /* Launch the child on idle */
  terminal_screen_launch_child_on_idle (screen);

762
  return screen;
763 764
}

765 766 767 768 769 770 771 772 773 774 775
const char*
terminal_screen_get_raw_title (TerminalScreen *screen)
{
  TerminalScreenPrivate *priv = screen->priv;
  
  if (priv->raw_title)
    return priv->raw_title;

  return "";
}

776 777 778
const char*
terminal_screen_get_title (TerminalScreen *screen)
{
779 780 781
  TerminalScreenPrivate *priv = screen->priv;
  
  if (priv->cooked_title == NULL)
782
    terminal_screen_cook_title (screen);
783 784

  /* cooked_title may still be NULL */
785 786
  if (priv->cooked_title != NULL)
    return priv->cooked_title;
787 788 789 790
  else
    return "";
}

791 792 793
const char*
terminal_screen_get_icon_title (TerminalScreen *screen)
{
794 795 796
  TerminalScreenPrivate *priv = screen->priv;
  
  if (priv->cooked_icon_title == NULL)
797 798 799
    terminal_screen_cook_icon_title (screen);

  /* cooked_icon_title may still be NULL */
800 801
  if (priv->cooked_icon_title != NULL)
    return priv->cooked_icon_title;
802 803 804 805 806 807 808 809 810 811
  else
    return "";
}

gboolean
terminal_screen_get_icon_title_set (TerminalScreen *screen)
{
  return screen->priv->icon_title_set;
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
/* Supported format specifiers:
 * %S = static title
 * %D = dynamic title
 * %A = dynamic title, falling back to static title if empty
 * %- = separator, if not at start or end of string (excluding whitespace)
 */
static const char *
terminal_screen_get_title_format (TerminalScreen *screen)
{
  TerminalScreenPrivate *priv = screen->priv;
  static const char *formats[] = {
    "%A"      /* TERMINAL_TITLE_REPLACE */,
    "%D%-%S"  /* TERMINAL_TITLE_BEFORE  */,
    "%S%-%D"  /* TERMINAL_TITLE_AFTER   */,
    "%S"      /* TERMINAL_TITLE_IGNORE  */
  };

  return formats[terminal_profile_get_property_enum (priv->profile, TERMINAL_PROFILE_TITLE_MODE)];
}

/**
 * terminal_screen_format_title::
 * @screen:
 * @raw_title: main ingredient
 * @titleptr <inout>: pointer of the current title string
 * 
 * Format title according @format, and stores it in <literal>*titleptr</literal>.
 * Always ensures that *titleptr will be non-NULL.
 *
 * Returns: %TRUE iff the title changed
 */
static gboolean
terminal_screen_format_title (TerminalScreen *screen,
                              const char *raw_title,
                              char **titleptr)
{
  TerminalScreenPrivate *priv = screen->priv;
  const char *format, *arg;
  const char *static_title = NULL;
  GString *title;
Christian Persch's avatar
Christian Persch committed
852
  gboolean add_sep = FALSE;
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

  g_assert (titleptr);

  /* use --title argument if one was supplied, otherwise ask the profile */
  if (priv->override_title)
    static_title = priv->override_title;
  else
    static_title = terminal_profile_get_property_string (priv->profile, TERMINAL_PROFILE_TITLE);

  //title = g_string_sized_new (strlen (static_title) + strlen (raw_title) + 3 + 1);
  title = g_string_sized_new (128);

  format = terminal_screen_get_title_format (screen);
  for (arg = format; *arg; arg += 2)
    {
      const char *text_to_append = NULL;

      g_assert (arg[0] == '%');

      switch (arg[1])
        {
          case 'A':
            text_to_append = raw_title ? raw_title : static_title;
            break;
          case 'D':
            text_to_append = raw_title;
            break;
          case 'S':
            text_to_append = static_title;
            break;
          case '-':
            text_to_append = NULL;
            add_sep = TRUE;
            break;
          default:
            g_assert_not_reached ();
        }

      if (!text_to_append || !text_to_append[0])
        continue;

      if (add_sep && title->len > 0)
        g_string_append (title, " - ");

      g_string_append (title, text_to_append);
      add_sep = FALSE;
    }

  if (*titleptr == NULL || strcmp (title->str, *titleptr) != 0)
    {
      g_free (*titleptr);
      *titleptr = g_string_free (title, FALSE);
      return TRUE;
    }

  g_string_free (title, TRUE);
  return FALSE;
}

static void 
terminal_screen_cook_title (TerminalScreen *screen)
{
  TerminalScreenPrivate *priv = screen->priv;
  
  if (terminal_screen_format_title (screen, priv->raw_title, &priv->cooked_title))
    g_object_notify (G_OBJECT (screen), "title");
}

static void 
terminal_screen_cook_icon_title (TerminalScreen *screen)
{
  TerminalScreenPrivate *priv = screen->priv;

  if (terminal_screen_format_title (screen, priv->raw_icon_title, &priv->cooked_icon_title))
    g_object_notify (G_OBJECT (screen), "icon-title");
}

930 931 932 933
static void
terminal_screen_profile_notify_cb (TerminalProfile *profile,
                                   GParamSpec *pspec,
                                   TerminalScreen *screen)
934
{
935
  TerminalScreenPrivate *priv = screen->priv;
936
  GObject *object = G_OBJECT (screen);
937
  VteTerminal *vte_terminal = VTE_TERMINAL (screen);
938 939 940 941 942 943 944
  const char *prop_name;
  TerminalBackgroundType bg_type;

  if (pspec)
    prop_name = pspec->name;
  else
    prop_name = NULL;
945

946 947
  g_object_freeze_notify (object);

948
  if (priv->window)
949 950 951 952
    {
      /* We need these in line for the set_size in
       * update_on_realize
       */
953
      terminal_window_update_geometry (priv->window);
954
    }
955
  
956 957 958
  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_SCROLLBAR_POSITION))
    _terminal_screen_update_scrollbar (screen);

959 960 961
  if (!prop_name ||
      prop_name == I_(TERMINAL_PROFILE_TITLE_MODE) ||
      prop_name == I_(TERMINAL_PROFILE_TITLE))
Jonathan Blandford's avatar
Jonathan Blandford committed
962
    {
963 964
      terminal_screen_cook_title (screen);
      terminal_screen_cook_icon_title (screen);
Jonathan Blandford's avatar
Jonathan Blandford committed
965
    }
966

967 968 969
  if (GTK_WIDGET_REALIZED (screen) &&
      (!prop_name ||
       prop_name == I_(TERMINAL_PROFILE_USE_SYSTEM_FONT) ||
970
       prop_name == I_(TERMINAL_PROFILE_FONT)))
971
    terminal_screen_change_font (screen);
972

973 974 975 976
  if (!prop_name ||
      prop_name == I_(TERMINAL_PROFILE_USE_THEME_COLORS) ||
      prop_name == I_(TERMINAL_PROFILE_FOREGROUND_COLOR) ||
      prop_name == I_(TERMINAL_PROFILE_BACKGROUND_COLOR) ||
977
      prop_name == I_(TERMINAL_PROFILE_BOLD_COLOR_SAME_AS_FG) ||
978
      prop_name == I_(TERMINAL_PROFILE_BOLD_COLOR) ||
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
      prop_name == I_(TERMINAL_PROFILE_PALETTE))
    update_color_scheme (screen);

  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_SILENT_BELL))
      vte_terminal_set_audible_bell (vte_terminal, !terminal_profile_get_property_boolean (profile, TERMINAL_PROFILE_SILENT_BELL));

  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_WORD_CHARS))
    vte_terminal_set_word_chars (vte_terminal,
                                 terminal_profile_get_property_string (profile, TERMINAL_PROFILE_WORD_CHARS));
  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_SCROLL_ON_KEYSTROKE))
    vte_terminal_set_scroll_on_keystroke (vte_terminal,
                                          terminal_profile_get_property_boolean (profile, TERMINAL_PROFILE_SCROLL_ON_KEYSTROKE));
  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_SCROLL_ON_OUTPUT))
    vte_terminal_set_scroll_on_output (vte_terminal,
                                       terminal_profile_get_property_boolean (profile, TERMINAL_PROFILE_SCROLL_ON_OUTPUT));
994 995 996 997 998 999 1000 1001
  if (!prop_name ||
      prop_name == I_(TERMINAL_PROFILE_SCROLLBACK_LINES) ||
      prop_name == I_(TERMINAL_PROFILE_SCROLLBACK_UNLIMITED))
    {
      glong lines = terminal_profile_get_property_boolean (profile, TERMINAL_PROFILE_SCROLLBACK_UNLIMITED) ?
		    -1 : terminal_profile_get_property_int (profile, TERMINAL_PROFILE_SCROLLBACK_LINES);
      vte_terminal_set_scrollback_lines (vte_terminal, lines);
    }
1002 1003

  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_USE_SKEY))
1004
    {
1005 1006
      if (terminal_profile_get_property_boolean (profile, TERMINAL_PROFILE_USE_SKEY))
        {
1007 1008 1009 1010 1011 1012 1013
          guint i;

          for (i = 0; i < n_skey_regexes; ++i)
            {
              TagData *tag_data;

              tag_data = g_slice_new (TagData);
1014
              tag_data->flavor = FLAVOR_SKEY;
1015 1016 1017
              tag_data->tag = vte_terminal_match_add_gregex (vte_terminal, skey_regexes[i], 0);
              vte_terminal_match_set_cursor_type (vte_terminal, tag_data->tag, SKEY_MATCH_CURSOR);

1018
              priv->match_tags = g_slist_prepend (priv->match_tags, tag_data);
1019
            }
1020 1021 1022 1023 1024
        }
      else
        {
          terminal_screen_skey_match_remove (screen);
        }
1025
    }
1026 1027 1028

  if (!prop_name ||
      prop_name == I_(TERMINAL_PROFILE_BACKGROUND_TYPE) ||
1029
      prop_name == I_(TERMINAL_PROFILE_BACKGROUND_IMAGE) ||
1030 1031
      prop_name == I_(TERMINAL_PROFILE_BACKGROUND_DARKNESS) ||
      prop_name == I_(TERMINAL_PROFILE_SCROLL_BACKGROUND))
1032
    {
1033
      bg_type = terminal_profile_get_property_enum (profile, TERMINAL_PROFILE_BACKGROUND_TYPE);
1034

1035 1036
      if (bg_type == TERMINAL_BACKGROUND_IMAGE)
        {
1037 1038
          vte_terminal_set_background_image (vte_terminal,
                                             terminal_profile_get_property_object (profile, TERMINAL_PROFILE_BACKGROUND_IMAGE));
1039 1040 1041 1042 1043
          vte_terminal_set_scroll_background (vte_terminal,
                                              terminal_profile_get_property_boolean (profile, TERMINAL_PROFILE_SCROLL_BACKGROUND));
        }
      else
        {
1044
          vte_terminal_set_background_image (vte_terminal, NULL);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
          vte_terminal_set_scroll_background (vte_terminal, FALSE);
        }

      if (bg_type == TERMINAL_BACKGROUND_IMAGE ||
          bg_type == TERMINAL_BACKGROUND_TRANSPARENT)
        {
          vte_terminal_set_background_saturation (vte_terminal,
                                                  1.0 - terminal_profile_get_property_double (profile, TERMINAL_PROFILE_BACKGROUND_DARKNESS));
          vte_terminal_set_opacity (vte_terminal,
                                    0xffff * terminal_profile_get_property_double (profile, TERMINAL_PROFILE_BACKGROUND_DARKNESS));
        }
      else
        {
          vte_terminal_set_background_saturation (vte_terminal, 1.0); /* normal color */
          vte_terminal_set_opacity (vte_terminal, 0xffff);
        }
      
      /* FIXME: Don't enable this if we have a compmgr. */
      vte_terminal_set_background_transparent (vte_terminal,
Christian Persch's avatar
Christian Persch committed
1064 1065
                                               bg_type == TERMINAL_BACKGROUND_TRANSPARENT &&
                                               (!priv->window || !terminal_window_uses_argb_visual (priv->window)));
1066
    }
1067

1068
  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_BACKSPACE_BINDING))
1069
  vte_terminal_set_backspace_binding (vte_terminal,
1070
                                      terminal_profile_get_property_enum (profile, TERMINAL_PROFILE_BACKSPACE_BINDING));
1071
  
1072
  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_DELETE_BINDING))
1073
  vte_terminal_set_delete_binding (vte_terminal,
1074
                                   terminal_profile_get_property_enum (profile, TERMINAL_PROFILE_DELETE_BINDING));
1075 1076 1077 1078

  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_ALLOW_BOLD))
    vte_terminal_set_allow_bold (vte_terminal,
                                 terminal_profile_get_property_boolean (profile, TERMINAL_PROFILE_ALLOW_BOLD));
1079 1080

  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_CURSOR_BLINK_MODE))
1081 1082
    vte_terminal_set_cursor_blink_mode (vte_terminal,
                                        terminal_profile_get_property_enum (priv->profile, TERMINAL_PROFILE_CURSOR_BLINK_MODE));
1083

1084 1085 1086 1087
  if (!prop_name || prop_name == I_(TERMINAL_PROFILE_CURSOR_SHAPE))
    vte_terminal_set_cursor_shape (vte_terminal,
                                   terminal_profile_get_property_enum (priv->profile, TERMINAL_PROFILE_CURSOR_SHAPE));

1088
  g_object_thaw_notify (object);
1089 1090
}

1091 1092 1093
static void
update_color_scheme (TerminalScreen *screen)
{
1094
  TerminalScreenPrivate *priv = screen->priv;
Christian Persch's avatar
Christian Persch committed
1095
  TerminalProfile *profile = priv->profile;
1096
  GtkStyle *style;
1097
  GdkColor colors[TERMINAL_PALETTE_SIZE];
1098
  const GdkColor *fg_color, *bg_color, *bold_color;
1099 1100
  GdkColor fg, bg;
  guint n_colors;
1101 1102 1103

  style = gtk_widget_get_style (GTK_WIDGET (screen));
  if (!style)
1104
    return;
Havoc Pennington's avatar
Havoc Pennington committed
1105