File: main.vala

package info (click to toggle)
deepin-terminal 3.2.1.1+ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,764 kB
  • sloc: ansic: 128; exp: 25; sh: 18; makefile: 15
file content (271 lines) | stat: -rw-r--r-- 10,053 bytes parent folder | download
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
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2011 ~ 2017 Deepin, Inc.
 *               2011 ~ 2017 Wang Yong
 *
 * Author:     Wang Yong <wangyong@deepin.com>
 * Maintainer: Wang Yong <wangyong@deepin.com>
 *
 * 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
 * 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/>.
 */

using Gdk;
using Gee;
using Gtk;
using Keymap;
using Vte;
using Widgets;

[DBus (name = "com.deepin.terminal")]
public class TerminalApp : Application {
    public static void on_bus_acquired(DBusConnection conn, TerminalApp app) {
        try {
            conn.register_object("/com/deepin/terminal", app);
        } catch (Error e) {
            stderr.printf("Could not register service\n");
        }
    }

    public void exit() {
        quit();
        window.quit();
    }

    public signal void quit();
}

[DBus (name = "com.deepin.quake_terminal")]
public class QuakeTerminalApp : Application {
    public static void on_bus_acquired(DBusConnection conn, QuakeTerminalApp app) {
        try {
            conn.register_object("/com/deepin/quake_terminal", app);
        } catch (Error e) {
            stderr.printf("Could not register service\n");
        }
    }

    public void show_or_hide() {
        this.quake_window.toggle_quake_window();
    }
}

[DBus (name = "com.deepin.quake_terminal")]
interface QuakeDaemon : Object {
    public abstract void show_or_hide() throws Error;
}


const string GETTEXT_PACKAGE = "deepin-terminal";

public class Application : Object {

    private static bool quake_mode = false;
    private static string? window_mode = null;
    private static string? work_directory = null;
    private static string? load_theme = null;

    // pass_options just for print help information, we need parse -e or -x commands myself.
    [CCode (array_length = false, array_null_terminated = true)]
    public static string[]? pass_options = null;

    public Widgets.QuakeWindow quake_window;
    public Widgets.Window window;
    public WorkspaceManager workspace_manager;
    public static ArrayList<string> commands;
    public static int64 start_time;

    private bool inited = false;
    private static bool version = false;

    public static void main(string[] args) {
        start_time = GLib.get_real_time() / 1000;

        // NOTE: set IBUS_NO_SNOOPER_APPS variable to avoid Ctrl + 5 eat by input method (such as fcitx.);
        Environment.set_variable("IBUS_DISABLE_SNOOPER", "1", true);

        // Set 'NO_AT_BRIDGE' environment variable with 1 to dislable accessibility dbus warning.
        Environment.set_variable("NO_AT_BRIDGE", "1", true);

        Intl.setlocale();
        Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "utf-8");
        Intl.bindtextdomain(GETTEXT_PACKAGE, "/usr/share/locale");

        // Need parse -e or -x commands my self, OptionEntry just will got first argument after -e or -x.
        commands = new ArrayList<string>();
        bool find_command_flag = false;
        foreach (string arg in args) {
            if (find_command_flag) {
                commands.add(arg);
            }

            if (arg == "-e" || arg == "-x") {
                find_command_flag = true;
            }
        }

        try {
            string window_mode_description = "%s (normal, maximize, fullscreen)".printf(_("Set the terminal window mode"));

            GLib.OptionEntry[] pass_options = {
                OptionEntry() {
                    long_name="version",
                    short_name=0,
                    flags=0,
                    arg=OptionArg.NONE,
                    arg_data=&version,
                    description=_("Display version"),
                    arg_description=null
                },
                OptionEntry() {
                    long_name="work-directory",
                    short_name='w',
                    flags=0,
                    arg=OptionArg.FILENAME,
                    arg_data=&work_directory,
                    description=_("Set the terminal startup directory"),
                    arg_description=null
                },
                OptionEntry() {
                    long_name="window-mode",
                    short_name='m',
                    flags=0,
                    arg=OptionArg.STRING,
                    arg_data=&window_mode,
                    description=window_mode_description,
                    arg_description=null
                },
                OptionEntry() {
                    long_name="execute",
                    short_name='e',
                    flags=0,
                    arg=OptionArg.STRING_ARRAY,
                    arg_data=&pass_options,
                    description=_("Run a program in the terminal"),
                    arg_description=null
                },
                OptionEntry() {
                    long_name="execute",
                    short_name='x',
                    flags=0,
                    arg=OptionArg.STRING_ARRAY,
                    arg_data=&pass_options,
                    description=_("Run a program in the terminal"),
                    arg_description=null
                },
                OptionEntry() {
                    long_name="quake-mode",
                    short_name='q',
                    flags=0,
                    arg=OptionArg.NONE,
                    arg_data=&quake_mode,
                    description=_("Quake mode"),
                    arg_description=null
                },
                OptionEntry() {
                    long_name="load-theme",
                    short_name='l',
                    flags=0,
                    arg=OptionArg.STRING,
                    arg_data=&load_theme,
                    description=_("Load theme"),
                    arg_description=null
                },
                OptionEntry()
            };

            var opt_context = new OptionContext(_("Deepin Terminal"));
            opt_context.set_summary(_("Deepin Terminal is an advanced terminal emulator with workspace, multiple windows, remote management, quake mode and other features.\n\nIt sharpens your focus in the world of command line!"));
            opt_context.set_help_enabled(true);
            opt_context.add_main_entries(pass_options, null);
            opt_context.parse(ref args);
        } catch (OptionError e) {
            // Don't print option error, avoid confuse user.
        }

        if (version) {
            stdout.printf("Deepin Terminal %s\n".printf(Constant.VERSION));
            stdout.printf ("Copyright 2011-2017 Deepin, Inc.\n");
        } else {
            Gtk.init(ref args);

            // Just for debug perfermance.
            // Gdk.Window.set_debug_updates(true);

            if (quake_mode) {
                QuakeTerminalApp app = new QuakeTerminalApp();
                Bus.own_name(BusType.SESSION,
                             "com.deepin.quake_terminal",
                             BusNameOwnerFlags.NONE,
                             ((con) => {QuakeTerminalApp.on_bus_acquired(con, app);}),
                             () => {app.run(false);},
                             () => {app.run(true);});
            } else {
                TerminalApp app = new TerminalApp();
                Bus.own_name(BusType.SESSION,
                             "com.deepin.terminal",
                             BusNameOwnerFlags.NONE,
                             ((con) => {TerminalApp.on_bus_acquired(con, app);}),
                             () => {app.run(false);},
                             () => {app.run(true);});
            }

            Gtk.main();
        }
    }

    public void run(bool has_start) {
        // Bus.own_name is callback, when application exit will execute `run` function.
        // Use inited variable to avoid application run by Bus.own_name release.
        if (inited) {
            return;
        }
        inited = true;

        if (has_start && quake_mode) {
            try {
                QuakeDaemon daemon = Bus.get_proxy_sync(BusType.SESSION, "com.deepin.quake_terminal", "/com/deepin/quake_terminal");
                daemon.show_or_hide();
            } catch (Error e) {
                stderr.printf("%s\n", e.message);
            }

            Gtk.main_quit();
        } else {
            Utils.load_css_theme(Utils.get_root_path("style.css"));

            Tabbar tabbar = new Tabbar();
            workspace_manager = new WorkspaceManager(tabbar, work_directory);

            if (quake_mode) {
                quake_window = new Widgets.QuakeWindow();
                quake_window.show_window(workspace_manager, tabbar);
                Utils.write_log("Deepin quake terminal start in: %s\n".printf((GLib.get_real_time() / 1000 - Application.start_time).to_string()));
                tabbar.init(workspace_manager, quake_window);
            } else {
                window = new Widgets.Window(window_mode);

                // Change theme temporary if 'load_theme' option is valid.
                if (load_theme != null) {
                    window.config.load_temp_theme(load_theme);
                }

                window.show_window((TerminalApp) this, workspace_manager, tabbar, has_start);
                Utils.write_log("Deepin terminal start in: %s\n".printf((GLib.get_real_time() / 1000 - Application.start_time).to_string()));
                tabbar.init(workspace_manager, window);
            }

        }
    }
}