-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal-screen.cc
2567 lines (2134 loc) · 82.1 KB
/
terminal-screen.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
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
852
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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2001 Havoc Pennington
* Copyright © 2007, 2008, 2010, 2011 Christian Persch
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "terminal-pcre2.hh"
#include "terminal-regex.hh"
#include "terminal-screen.hh"
#include "terminal-client-utils.hh"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <uuid.h>
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
#include <sys/sysctl.h>
#endif
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include <gtk/gtk.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
#include "terminal-accels.hh"
#include "terminal-app.hh"
#include "terminal-debug.hh"
#include "terminal-defines.hh"
#include "terminal-enums.hh"
#include "terminal-intl.hh"
#include "terminal-marshal.h"
#include "terminal-schemas.hh"
#include "terminal-screen-container.hh"
#include "terminal-util.hh"
#include "terminal-window.hh"
#include "terminal-info-bar.hh"
#include "terminal-libgsystem.hh"
#include "eggshell.hh"
#define URL_MATCH_CURSOR (GDK_HAND2)
typedef struct {
volatile int refcount;
char **argv; /* as passed */
char **exec_argv; /* as processed */
char **envv;
char *cwd;
gboolean as_shell;
VtePtyFlags pty_flags;
GSpawnFlags spawn_flags;
/* FD passing */
GUnixFDList *fd_list;
int n_fd_map;
int* fd_map;
/* async exec callback */
TerminalScreenExecCallback callback;
gpointer callback_data;
GDestroyNotify callback_data_destroy_notify;
/* Cancellable */
GCancellable *cancellable;
} ExecData;
typedef struct
{
int tag;
TerminalURLFlavor flavor;
} TagData;
struct _TerminalScreenPrivate
{
char *uuid;
gboolean registered; /* D-Bus interface is registered */
GSettings *profile; /* never nullptr */
guint profile_changed_id;
guint profile_forgotten_id;
int child_pid;
GSList *match_tags;
gboolean exec_on_realize;
guint idle_exec_source;
ExecData *exec_data;
GdkRGBA bg_color;
GdkRGBA fg_color;
};
enum
{
PROFILE_SET,
SHOW_POPUP_MENU,
MATCH_CLICKED,
CLOSE_SCREEN,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_PROFILE,
PROP_TITLE,
PROP_BG_COLOR,
PROP_FG_COLOR
};
enum
{
TARGET_COLOR,
TARGET_BGIMAGE,
TARGET_RESET_BG,
TARGET_MOZ_URL,
TARGET_NETSCAPE_URL,
TARGET_TAB
};
#define TERMINAL_SCREEN_STYLE_CLASS "terminal-screen"
static void terminal_screen_constructed (GObject *object);
static void terminal_screen_dispose (GObject *object);
static void terminal_screen_finalize (GObject *object);
static void terminal_screen_drag_data_received (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time);
static void terminal_screen_set_font (TerminalScreen *screen);
static void terminal_screen_system_font_changed_cb (GSettings *,
const char*,
TerminalScreen *screen);
static gboolean terminal_screen_popup_menu (GtkWidget *widget);
static gboolean terminal_screen_button_press (GtkWidget *widget,
GdkEventButton *event);
//增加按压释放事件
static gboolean terminal_screen_button_release (GtkWidget *widget,
GdkEventButton *event);
static void terminal_screen_hierarchy_changed (GtkWidget *widget,
GtkWidget *previous_toplevel);
static void terminal_screen_child_exited (VteTerminal *terminal,
int status);
static void terminal_screen_window_title_changed (VteTerminal *vte_terminal,
TerminalScreen *screen);
static void update_color_scheme (TerminalScreen *screen);
static char* terminal_screen_check_hyperlink (TerminalScreen *screen,
GdkEvent *event);
static void terminal_screen_check_extra (TerminalScreen *screen,
GdkEvent *event,
char **number_info,
char **timestamp_info);
static char* terminal_screen_check_match (TerminalScreen *screen,
GdkEvent *event,
int *flavor);
static void terminal_screen_show_info_bar (TerminalScreen *screen,
GError *error,
gboolean show_relaunch);
static char**terminal_screen_get_child_environment (TerminalScreen *screen,
char **initial_envv,
char **path,
char **shell);
static gboolean terminal_screen_get_child_command (TerminalScreen *screen,
char **exec_argv,
const char *path_env,
const char *shell_env,
gboolean shell,
gboolean *preserve_cwd_p,
GSpawnFlags *spawn_flags_p,
char ***argv_p,
GError **err);
static void terminal_screen_queue_idle_exec (TerminalScreen *screen);
static guint signals[LAST_SIGNAL];
typedef struct {
const char *pattern;
TerminalURLFlavor flavor;
} TerminalRegexPattern;
static const TerminalRegexPattern url_regex_patterns[] = {
{ REGEX_URL_AS_IS, FLAVOR_AS_IS },
{ REGEX_URL_HTTP, FLAVOR_DEFAULT_TO_HTTP },
{ REGEX_URL_FILE, FLAVOR_AS_IS },
{ REGEX_URL_VOIP, FLAVOR_VOIP_CALL },
{ REGEX_EMAIL, FLAVOR_EMAIL },
{ REGEX_NEWS_MAN, FLAVOR_AS_IS },
{ REGEX_LP, FLAVOR_LP },
};
static const TerminalRegexPattern extra_regex_patterns[] = {
{ "(0[Xx][[:xdigit:]]+|[[:digit:]]+)", FLAVOR_NUMBER },
};
static VteRegex **url_regexes;
static VteRegex **extra_regexes;
static TerminalURLFlavor *url_regex_flavors;
static TerminalURLFlavor *extra_regex_flavors;
static guint n_url_regexes;
static guint n_extra_regexes;
/* See bug #697024 */
#ifndef __linux__
#undef dup3
#define dup3 fake_dup3
static int
fake_dup3 (int fd, int fd2, int flags)
{
if (dup2 (fd, fd2) == -1)
return -1;
return fcntl (fd2, F_SETFD, flags);
}
#endif /* !__linux__ */
static char*
strv_to_string (char **strv)
{
return strv ? g_strjoinv (" ", strv) : g_strdup ("(null)");
}
static char*
exec_data_to_string (ExecData *data)
{
gs_free char *str1 = nullptr;
gs_free char *str2 = nullptr;
return data ? g_strdup_printf ("data %p argv:[%s] exec-argv:[%s] envv:%p(%u) as-shell:%s cwd:%s",
data,
(str1 = strv_to_string (data->argv)),
(str2 = strv_to_string (data->exec_argv)),
data->envv, data->envv ? g_strv_length (data->envv) : 0,
data->as_shell ? "true" : "false",
data->cwd)
: g_strdup ("(null)");
}
static ExecData*
exec_data_new (void)
{
ExecData *data = g_new0 (ExecData, 1);
data->refcount = 1;
return data;
}
static ExecData *
exec_data_clone (ExecData *data)
{
if (data == nullptr)
return nullptr;
ExecData *clone = exec_data_new ();
clone->envv = g_strdupv (data->envv);
clone->cwd = g_strdup (data->cwd);
/* If FDs were passed, cannot repeat argv. Return data only for env and cwd */
if (data->fd_list != nullptr) {
clone->as_shell = TRUE;
return clone;
}
clone->argv = g_strdupv (data->argv);
clone->as_shell = data->as_shell;
return clone;
}
static void
exec_data_callback (ExecData *data,
GError *error,
TerminalScreen *screen)
{
if (data->callback)
data->callback (screen, error, data->callback_data);
}
static ExecData*
exec_data_ref (ExecData *data)
{
data->refcount++;
return data;
}
static void
exec_data_unref (ExecData *data)
{
if (data == nullptr)
return;
if (--data->refcount > 0)
return;
g_strfreev (data->argv);
g_strfreev (data->exec_argv);
g_strfreev (data->envv);
g_free (data->cwd);
g_clear_object (&data->fd_list);
g_free (data->fd_map);
if (data->callback_data_destroy_notify && data->callback_data)
data->callback_data_destroy_notify (data->callback_data);
g_clear_object (&data->cancellable);
g_free (data);
}
GS_DEFINE_CLEANUP_FUNCTION0(ExecData*, _terminal_local_unref_exec_data, exec_data_unref)
#define terminal_unref_exec_data __attribute__((__cleanup__(_terminal_local_unref_exec_data)))
static void
terminal_screen_clear_exec_data (TerminalScreen *screen,
gboolean cancelled)
{
TerminalScreenPrivate *priv = screen->priv;
if (priv->exec_data == nullptr)
return;
if (cancelled) {
gs_free_error GError *err = nullptr;
g_set_error_literal (&err, G_IO_ERROR, G_IO_ERROR_CANCELLED,
"Spawning was cancelled");
exec_data_callback (priv->exec_data, err, screen);
}
exec_data_unref (priv->exec_data);
priv->exec_data = nullptr;
}
G_DEFINE_TYPE (TerminalScreen, terminal_screen, VTE_TYPE_TERMINAL)
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 nullptr;
/* 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;
/* disable "format not a string literal" error, we know what we are doing */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
g_snprintf (cwd_file, sizeof (cwd_file), patterns[i], pid);
#pragma GCC diagnostic pop
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 ();
(void) chdir (cwd);
}
g_free (cwd);
}
if (working_dir)
return working_dir;
}
}
return nullptr;
}
static void
free_tag_data (TagData *tagdata)
{
g_slice_free (TagData, tagdata);
}
static void
precompile_regexes (const TerminalRegexPattern *regex_patterns,
guint n_regexes,
VteRegex ***regexes,
TerminalURLFlavor **regex_flavors)
{
guint i;
*regexes = g_new0 (VteRegex*, n_regexes);
*regex_flavors = g_new0 (TerminalURLFlavor, n_regexes);
for (i = 0; i < n_regexes; ++i)
{
GError *error = nullptr;
(*regexes)[i] = vte_regex_new_for_match (regex_patterns[i].pattern, -1,
PCRE2_UTF | PCRE2_NO_UTF_CHECK | PCRE2_UCP | PCRE2_MULTILINE,
&error);
g_assert_no_error (error);
if (!vte_regex_jit ((*regexes)[i], PCRE2_JIT_COMPLETE, &error) ||
!vte_regex_jit ((*regexes)[i], PCRE2_JIT_PARTIAL_SOFT, &error)) {
g_printerr ("Failed to JIT regex '%s': %s\n", regex_patterns[i].pattern, error->message);
g_clear_error (&error);
}
(*regex_flavors)[i] = regex_patterns[i].flavor;
}
}
static void
terminal_screen_class_enable_menu_bar_accel_notify_cb (GSettings *settings,
const char *key,
TerminalScreenClass *klass)
{
static gboolean is_enabled = TRUE; /* the binding is enabled by default since GtkWidgetClass installs it */
gboolean enable;
GtkBindingSet *binding_set;
enable = g_settings_get_boolean (settings, key);
/* 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_KEY_F10, GDK_SHIFT_MASK);
else
gtk_binding_entry_skip (binding_set, GDK_KEY_F10, GDK_SHIFT_MASK);
}
static TerminalWindow *
terminal_screen_get_window (TerminalScreen *screen)
{
GtkWidget *widget = GTK_WIDGET (screen);
GtkWidget *toplevel;
toplevel = gtk_widget_get_toplevel (widget);
if (!gtk_widget_is_toplevel (toplevel))
return nullptr;
return TERMINAL_WINDOW (toplevel);
}
static void
terminal_screen_realize (GtkWidget *widget)
{
TerminalScreen *screen = TERMINAL_SCREEN (widget);
GTK_WIDGET_CLASS (terminal_screen_parent_class)->realize (widget);
terminal_screen_set_font (screen);
TerminalScreenPrivate *priv = screen->priv;
if (priv->exec_on_realize)
terminal_screen_queue_idle_exec (screen);
priv->exec_on_realize = FALSE;
}
static void
terminal_screen_update_style (TerminalScreen *screen)
{
update_color_scheme (screen);
terminal_screen_set_font (screen);
}
static void
terminal_screen_style_updated (GtkWidget *widget)
{
TerminalScreen *screen = TERMINAL_SCREEN (widget);
GTK_WIDGET_CLASS (terminal_screen_parent_class)->style_updated (widget);
terminal_screen_update_style (screen);
}
static void
terminal_screen_init (TerminalScreen *screen)
{
const GtkTargetEntry target_table[] = {
{ (char *) "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, TARGET_TAB },
{ (char *) "application/x-color", 0, TARGET_COLOR },
{ (char *) "x-special/gnome-reset-background", 0, TARGET_RESET_BG },
{ (char *) "text/x-moz-url", 0, TARGET_MOZ_URL },
{ (char *) "_NETSCAPE_URL", 0, TARGET_NETSCAPE_URL }
};
VteTerminal *terminal = VTE_TERMINAL (screen);
TerminalScreenPrivate *priv;
TerminalApp *app;
GtkTargetList *target_list;
GtkTargetEntry *targets;
int n_targets;
guint i;
uuid_t u;
char uuidstr[37];
GtkStyleContext *context;
priv = screen->priv = G_TYPE_INSTANCE_GET_PRIVATE (screen, TERMINAL_TYPE_SCREEN, TerminalScreenPrivate);
context = gtk_widget_get_style_context (GTK_WIDGET (screen));
gtk_style_context_add_class (context, TERMINAL_SCREEN_STYLE_CLASS);
uuid_generate (u);
uuid_unparse (u, uuidstr);
priv->uuid = g_strdup (uuidstr);
vte_terminal_set_mouse_autohide (terminal, TRUE);
priv->child_pid = -1;
vte_terminal_set_allow_hyperlink (terminal, TRUE);
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_regex (terminal, url_regexes[i], 0);
vte_terminal_match_set_cursor_type (terminal, tag_data->tag, URL_MATCH_CURSOR);
priv->match_tags = g_slist_prepend (priv->match_tags, tag_data);
}
/* Setup DND */
target_list = gtk_target_list_new (nullptr, 0);
gtk_target_list_add_uri_targets (target_list, 0);
gtk_target_list_add_text_targets (target_list, 0);
gtk_target_list_add_table (target_list, target_table, G_N_ELEMENTS (target_table));
targets = gtk_target_table_new_from_list (target_list, &n_targets);
gtk_drag_dest_set (GTK_WIDGET (screen),
GtkDestDefaults(GTK_DEST_DEFAULT_MOTION |
GTK_DEST_DEFAULT_HIGHLIGHT |
GTK_DEST_DEFAULT_DROP),
targets, n_targets,
GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE));
gtk_target_table_free (targets, n_targets);
gtk_target_list_unref (target_list);
g_signal_connect (screen, "window-title-changed",
G_CALLBACK (terminal_screen_window_title_changed),
screen);
app = terminal_app_get ();
g_signal_connect (terminal_app_get_desktop_interface_settings (app), "changed::" MONOSPACE_FONT_KEY_NAME,
G_CALLBACK (terminal_screen_system_font_changed_cb), screen);
}
static void
terminal_screen_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TerminalScreen *screen = TERMINAL_SCREEN (object);
switch (prop_id)
{
case PROP_PROFILE:
g_value_set_object (value, terminal_screen_get_profile (screen));
break;
case PROP_TITLE:
g_value_set_string (value, terminal_screen_get_title (screen));
break;
case PROP_BG_COLOR:
g_value_set_boxed (value, terminal_screen_get_bg_color (screen));
break;
case PROP_FG_COLOR:
g_value_set_boxed (value, terminal_screen_get_bg_color (screen));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
terminal_screen_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TerminalScreen *screen = TERMINAL_SCREEN (object);
switch (prop_id)
{
case PROP_PROFILE:
terminal_screen_set_profile (screen, (GSettings*)g_value_get_object (value));
break;
case PROP_TITLE:
case PROP_FG_COLOR:
case PROP_BG_COLOR:
/* not writable */
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
terminal_screen_class_init (TerminalScreenClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
VteTerminalClass *terminal_class = VTE_TERMINAL_CLASS (klass);
GSettings *settings;
object_class->constructed = terminal_screen_constructed;
object_class->dispose = terminal_screen_dispose;
object_class->finalize = terminal_screen_finalize;
object_class->get_property = terminal_screen_get_property;
object_class->set_property = terminal_screen_set_property;
widget_class->realize = terminal_screen_realize;
widget_class->style_updated = terminal_screen_style_updated;
widget_class->drag_data_received = terminal_screen_drag_data_received;
widget_class->button_press_event = terminal_screen_button_press;
widget_class->button_release_event = terminal_screen_button_release;
widget_class->popup_menu = terminal_screen_popup_menu;
widget_class->hierarchy_changed = terminal_screen_hierarchy_changed;
terminal_class->child_exited = terminal_screen_child_exited;
signals[PROFILE_SET] =
g_signal_new (I_("profile-set"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TerminalScreenClass, profile_set),
nullptr, nullptr,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE,
1, G_TYPE_SETTINGS);
signals[SHOW_POPUP_MENU] =
g_signal_new (I_("show-popup-menu"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TerminalScreenClass, show_popup_menu),
nullptr, nullptr,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE,
1,
G_TYPE_POINTER);
signals[MATCH_CLICKED] =
g_signal_new (I_("match-clicked"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TerminalScreenClass, match_clicked),
g_signal_accumulator_true_handled, nullptr,
_terminal_marshal_BOOLEAN__STRING_INT_UINT,
G_TYPE_BOOLEAN,
3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_UINT);
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),
nullptr, nullptr,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
g_object_class_install_property
(object_class,
PROP_PROFILE,
g_param_spec_object ("profile", nullptr, nullptr,
G_TYPE_SETTINGS,
GParamFlags(G_PARAM_READWRITE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB)));
g_object_class_install_property
(object_class,
PROP_TITLE,
g_param_spec_string ("title", nullptr, nullptr,
nullptr,
GParamFlags(G_PARAM_READABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB)));
g_object_class_install_property
(object_class,
PROP_BG_COLOR,
g_param_spec_boxed ("bg-color", NULL, NULL,
GDK_TYPE_RGBA,
GParamFlags(G_PARAM_READABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB)));
g_object_class_install_property
(object_class,
PROP_FG_COLOR,
g_param_spec_boxed ("fg-color", NULL, NULL,
GDK_TYPE_RGBA,
GParamFlags(G_PARAM_READABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB)));
g_type_class_add_private (object_class, sizeof (TerminalScreenPrivate));
gtk_widget_class_install_style_property (widget_class,
g_param_spec_float ("background-darkness", NULL, NULL, -1, 1, -1,
GParamFlags(G_PARAM_READABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB)));
n_url_regexes = G_N_ELEMENTS (url_regex_patterns);
precompile_regexes (url_regex_patterns, n_url_regexes, &url_regexes, &url_regex_flavors);
n_extra_regexes = G_N_ELEMENTS (extra_regex_patterns);
precompile_regexes (extra_regex_patterns, n_extra_regexes, &extra_regexes, &extra_regex_flavors);
/* This fixes bug #329827 */
settings = terminal_app_get_global_settings (terminal_app_get ());
terminal_screen_class_enable_menu_bar_accel_notify_cb (settings, TERMINAL_SETTING_ENABLE_MENU_BAR_ACCEL_KEY, klass);
g_signal_connect (settings, "changed::" TERMINAL_SETTING_ENABLE_MENU_BAR_ACCEL_KEY,
G_CALLBACK (terminal_screen_class_enable_menu_bar_accel_notify_cb), klass);
}
static void
terminal_screen_constructed (GObject *object)
{
TerminalScreen *screen = TERMINAL_SCREEN (object);
TerminalScreenPrivate *priv = screen->priv;
G_OBJECT_CLASS (terminal_screen_parent_class)->constructed (object);
terminal_app_register_screen (terminal_app_get (), screen);
priv->registered = TRUE;
}
static void
terminal_screen_dispose (GObject *object)
{
TerminalScreen *screen = TERMINAL_SCREEN (object);
TerminalScreenPrivate *priv = screen->priv;
GtkSettings *settings;
/* Unset child PID so that when an eventual child-exited signal arrives,
* we don't emit "close".
*/
priv->child_pid = -1;
settings = gtk_widget_get_settings (GTK_WIDGET (screen));
g_signal_handlers_disconnect_matched (settings, G_SIGNAL_MATCH_DATA,
0, 0, nullptr, nullptr,
screen);
if (priv->idle_exec_source != 0)
{
g_source_remove (priv->idle_exec_source);
priv->idle_exec_source = 0;
}
terminal_screen_clear_exec_data (screen, TRUE);
G_OBJECT_CLASS (terminal_screen_parent_class)->dispose (object);
/* Unregister *after* chaining up to the parent's dispose,
* since that will terminate the child process if there still
* is any, and we need to get the dbus signal out
* from the TerminalReceiver.
*/
if (priv->registered) {
terminal_app_unregister_screen (terminal_app_get (), screen);
priv->registered = FALSE;
}
}
static void
terminal_screen_finalize (GObject *object)
{
TerminalScreen *screen = TERMINAL_SCREEN (object);
TerminalScreenPrivate *priv = screen->priv;
g_signal_handlers_disconnect_by_func (terminal_app_get_desktop_interface_settings (terminal_app_get ()),
(void*)terminal_screen_system_font_changed_cb,
screen);
terminal_screen_set_profile (screen, nullptr);
g_slist_free_full (priv->match_tags, (GDestroyNotify) free_tag_data);
g_free (priv->uuid);
G_OBJECT_CLASS (terminal_screen_parent_class)->finalize (object);
}
TerminalScreen *
terminal_screen_new (GSettings *profile,
const char *title,
double zoom)
{
g_return_val_if_fail (G_IS_SETTINGS (profile), nullptr);
TerminalScreen *screen = (TerminalScreen*)g_object_new (TERMINAL_TYPE_SCREEN, nullptr);
terminal_screen_set_profile (screen, profile);
vte_terminal_set_size (VTE_TERMINAL (screen),
g_settings_get_int (profile, TERMINAL_PROFILE_DEFAULT_SIZE_COLUMNS_KEY),
g_settings_get_int (profile, TERMINAL_PROFILE_DEFAULT_SIZE_ROWS_KEY));
/* If given an initial title, strip it of control characters and
* feed it to the terminal.
*/
if (title != nullptr) {
GString *seq;
const char *p;
seq = g_string_new ("\033]0;");
for (p = title; *p; p = g_utf8_next_char (p)) {
gunichar c = g_utf8_get_char (p);
if (c < 0x20 || (c >= 0x7f && c <= 0x9f))
continue;
else if (c == ';')
break;
g_string_append_unichar (seq, c);
}
g_string_append (seq, "\033\\");
vte_terminal_feed (VTE_TERMINAL (screen), seq->str, seq->len);
g_string_free (seq, TRUE);
}
vte_terminal_set_font_scale (VTE_TERMINAL (screen), zoom);
terminal_screen_set_font (screen);
return screen;
}
static gboolean
terminal_screen_reexec_from_exec_data (TerminalScreen *screen,
ExecData *data,
char **envv,
const char *cwd,
GCancellable *cancellable,
GError **error)
{
_TERMINAL_DEBUG_IF (TERMINAL_DEBUG_PROCESSES) {
gs_free char *str = exec_data_to_string (data);
_terminal_debug_print (TERMINAL_DEBUG_PROCESSES,
"[screen %p] reexec_from_data: envv:%p(%u) cwd:%s data:[%s]\n",
screen,
envv, envv ? g_strv_length (envv) : 0,
cwd,
str);
}
return terminal_screen_exec (screen,
data ? data->argv : nullptr,
envv ? envv : data ? data->envv : nullptr,
data ? data->as_shell : TRUE,
/* If we have command line args, must always pass the cwd from the command line, too */
data && data->argv ? data->cwd : cwd ? cwd : data ? data->cwd : nullptr,
nullptr /* fd list */, nullptr /* fd array */,
nullptr, nullptr, nullptr, /* callback + data + destroy notify */
cancellable,
error);
}
gboolean
terminal_screen_reexec_from_screen (TerminalScreen *screen,
TerminalScreen *parent_screen,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (TERMINAL_IS_SCREEN (screen), FALSE);
if (parent_screen == nullptr)
return TRUE;
g_return_val_if_fail (TERMINAL_IS_SCREEN (parent_screen), FALSE);
gs_free char* cwd = terminal_screen_get_current_dir (parent_screen);
_terminal_debug_print (TERMINAL_DEBUG_PROCESSES,
"[screen %p] reexec_from_screen: parent:%p cwd:%s\n",
screen,
parent_screen,
cwd);
return terminal_screen_reexec_from_exec_data (screen,
nullptr /* exec data */,
nullptr /* envv */,
cwd,
cancellable,
error);
}
gboolean
terminal_screen_reexec (TerminalScreen *screen,
char **envv,
const char *cwd,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (TERMINAL_IS_SCREEN (screen), FALSE);
_terminal_debug_print (TERMINAL_DEBUG_PROCESSES,
"[screen %p] reexec: envv:%p(%u) cwd:%s\n",
screen,
envv, envv ? g_strv_length (envv) : 0,
cwd);
return terminal_screen_reexec_from_exec_data (screen,
screen->priv->exec_data,
envv,
cwd,
cancellable,
error);
}
gboolean
terminal_screen_exec (TerminalScreen *screen,
char **argv,
char **initial_envv,
gboolean as_shell,
const char *cwd,
GUnixFDList *fd_list,
GVariant *fd_array,
TerminalScreenExecCallback callback,
gpointer user_data,
GDestroyNotify destroy_notify,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (TERMINAL_IS_SCREEN (screen), FALSE);
g_return_val_if_fail (cancellable == nullptr || G_IS_CANCELLABLE (cancellable), FALSE);
g_return_val_if_fail (error == nullptr || *error == nullptr, FALSE);
g_return_val_if_fail (gtk_widget_get_parent (GTK_WIDGET (screen)) != nullptr, FALSE);