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
|
/*
* Copyright (C) 2007 OpenedHand Ltd
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include <gtk/gtk.h>
#include <vte/vte.h>
#include <glib.h>
static void
on_window_title_changed (VteTerminal *terminal, GtkWindow *window)
{
char *title;
title = g_strdup_printf ("%s - Terminal",
vte_terminal_get_window_title (terminal));
gtk_window_set_title (window, title);
g_free (title);
}
static void
on_eof (VteTerminal *terminal, gpointer user_data)
{
gtk_main_quit ();
}
int
main (int argc, char **argv)
{
GtkWidget *window, *terminal, *scrolled_win;
GError *err = NULL;
char *cmd = NULL;
char **cmd_argv = NULL;
int cmd_argc;
GOptionContext *context = NULL;
const GOptionEntry options[] = {
{
"command", 'e', 0,
G_OPTION_ARG_STRING, &cmd,
"Execute a command in the terminal", NULL},
{NULL}
};
context =
g_option_context_new
("Open matchbox terminal and parser the command line args");
g_option_context_add_main_entries (context, options, NULL);
g_option_context_add_group (context, gtk_get_option_group (TRUE));
g_option_context_parse (context, &argc, &argv, &err);
g_option_context_free (context);
if (err != NULL) {
g_printerr ("Failed to parse command line arguments: %s\n", err->message);
g_error_free (err);
return -1;
}
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "destroy", gtk_main_quit, NULL);
gtk_window_set_title (GTK_WINDOW (window), "Terminal");
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrolled_win),
GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
gtk_container_add (GTK_CONTAINER (window), scrolled_win);
terminal = g_object_connect
(vte_terminal_new (),
/* Child dying */
"signal::child-exited", on_eof, NULL,
"signal::eof", on_eof, NULL,
/* Child is trying to control the terminal */
"signal::window-title-changed", on_window_title_changed, window,
NULL);
gtk_container_add (GTK_CONTAINER (scrolled_win), terminal);
if (cmd == NULL) {
cmd = vte_get_user_shell ();
if (!cmd)
cmd = g_strdup (g_getenv ("SHELL"));
if (!cmd)
cmd = g_strdup ("/bin/sh");
}
if (!g_shell_parse_argv(cmd, &cmd_argc, &cmd_argv, &err)) {
g_printerr ("Failed to parse shell command line '%s'\n", cmd);
} else if (!vte_terminal_spawn_sync (VTE_TERMINAL (terminal),
VTE_PTY_DEFAULT,
NULL,
cmd_argv,
NULL,
G_SPAWN_SEARCH_PATH,
NULL,
NULL,
NULL,
NULL,
&err)) {
g_printerr ("Failed to spawn shell: %s\n", err->message);
g_error_free (err);
}
gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
gtk_widget_show_all (window);
gtk_main ();
g_free (cmd);
return 0;
}
|