summaryrefslogtreecommitdiff
path: root/Applications/Terminal/main.cpp
blob: 6c0aa6c7c3a54a2d54fc3c4cc015628b48e35948 (plain)
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
#include <Kernel/KeyCode.h>
#include <LibCore/CArgsParser.h>
#include <LibCore/CUserInfo.h>
#include <LibDraw/PNGLoader.h>
#include <LibGUI/GAboutDialog.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GActionGroup.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GFontDatabase.h>
#include <LibGUI/GGroupBox.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GRadioButton.h>
#include <LibGUI/GSlider.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
#include <LibVT/TerminalWidget.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

static void run_command(int ptm_fd, String command)
{
    pid_t pid = fork();
    if (pid == 0) {
        const char* tty_name = ptsname(ptm_fd);
        if (!tty_name) {
            perror("ptsname");
            exit(1);
        }
        close(ptm_fd);
        int pts_fd = open(tty_name, O_RDWR);
        if (pts_fd < 0) {
            perror("open");
            exit(1);
        }

        // NOTE: It's okay if this fails.
        (void)ioctl(0, TIOCNOTTY);

        close(0);
        close(1);
        close(2);

        int rc = dup2(pts_fd, 0);
        if (rc < 0) {
            perror("dup2");
            exit(1);
        }
        rc = dup2(pts_fd, 1);
        if (rc < 0) {
            perror("dup2");
            exit(1);
        }
        rc = dup2(pts_fd, 2);
        if (rc < 0) {
            perror("dup2");
            exit(1);
        }
        rc = close(pts_fd);
        if (rc < 0) {
            perror("close");
            exit(1);
        }
        rc = ioctl(0, TIOCSCTTY);
        if (rc < 0) {
            perror("ioctl(TIOCSCTTY)");
            exit(1);
        }
        const char* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr };
        if (!command.is_empty()) {
            args[1] = "-c";
            args[2] = command.characters();
        }
        const char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
        rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs));
        if (rc < 0) {
            perror("execve");
            exit(1);
        }
        ASSERT_NOT_REACHED();
    }
}

RefPtr<GWindow> create_settings_window(TerminalWidget& terminal, RefPtr<CConfigFile> config)
{
    auto window = GWindow::construct();
    window->set_title("Terminal Settings");
    window->set_rect(50, 50, 200, 140);

    auto settings = GWidget::construct();
    window->set_main_widget(settings);
    settings->set_fill_with_background_color(true);
    settings->set_background_role(ColorRole::Button);
    settings->set_layout(make<GBoxLayout>(Orientation::Vertical));
    settings->layout()->set_margins({ 4, 4, 4, 4 });

    auto radio_container = GGroupBox::construct("Bell Mode", settings);
    radio_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
    radio_container->layout()->set_margins({ 6, 16, 6, 6 });
    radio_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
    radio_container->set_preferred_size(100, 70);

    auto sysbell_radio = GRadioButton::construct("Use (Audible) System Bell", radio_container);
    auto visbell_radio = GRadioButton::construct("Use (Visual) Terminal Bell", radio_container);
    sysbell_radio->set_checked(terminal.should_beep());
    visbell_radio->set_checked(!terminal.should_beep());
    sysbell_radio->on_checked = [&terminal](const bool checked) {
        terminal.set_should_beep(checked);
    };

    auto slider_container = GGroupBox::construct("Background Opacity", settings);
    slider_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
    slider_container->layout()->set_margins({ 6, 16, 6, 6 });
    slider_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
    slider_container->set_preferred_size(100, 50);
    auto slider = GSlider::construct(Orientation::Horizontal, slider_container);

    slider->on_value_changed = [&terminal, &config](int value) {
        terminal.set_opacity(value);
    };

    slider->set_range(0, 255);
    slider->set_value(terminal.opacity());

    return window;
}

int main(int argc, char** argv)
{
    if (pledge("stdio tty rpath cpath wpath shared_buffer proc exec unix fattr", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    struct sigaction act;
    memset(&act, 0, sizeof(act));
    act.sa_flags = SA_NOCLDWAIT;
    act.sa_handler = SIG_IGN;
    int rc = sigaction(SIGCHLD, &act, nullptr);
    if (rc < 0) {
        perror("sigaction");
        return 1;
    }

    GApplication app(argc, argv);

    if (pledge("stdio tty rpath cpath wpath shared_buffer proc exec", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    CArgsParser args_parser("Terminal");

    args_parser.add_arg("e", "execute", "Execute this command inside the terminal.");

    CArgsParserResult args = args_parser.parse(argc, argv);

    if (chdir(get_current_user_home_path().characters()) < 0)
        perror("chdir");

    int ptm_fd = open("/dev/ptmx", O_RDWR | O_CLOEXEC);
    if (ptm_fd < 0) {
        perror("open(ptmx)");
        return 1;
    }

    run_command(ptm_fd, args.get("e"));

    auto window = GWindow::construct();
    window->set_title("Terminal");
    window->set_background_color(Color::Black);
    window->set_double_buffering_enabled(false);

    RefPtr<CConfigFile> config = CConfigFile::get_for_app("Terminal");
    auto terminal = TerminalWidget::construct(ptm_fd, true, config);
    terminal->on_command_exit = [&] {
        app.quit(0);
    };
    terminal->on_title_change = [&](auto& title) {
        window->set_title(title);
    };
    window->set_main_widget(terminal);
    window->move_to(300, 300);
    terminal->apply_size_increments_to_window(*window);
    window->show();
    window->set_icon(load_png("/res/icons/16x16/app-terminal.png"));
    terminal->set_should_beep(config->read_bool_entry("Window", "AudibleBeep", false));

    RefPtr<GWindow> settings_window;

    auto new_opacity = config->read_num_entry("Window", "Opacity", 255);
    terminal->set_opacity(new_opacity);
    window->set_has_alpha_channel(new_opacity < 255);

    auto menubar = make<GMenuBar>();

    auto app_menu = GMenu::construct("Terminal");
    app_menu->add_action(GAction::create("Open new terminal", { Mod_Ctrl | Mod_Shift, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/app-terminal.png"), [&](auto&) {
        if (!fork()) {
            execl("/bin/Terminal", "Terminal", nullptr);
            exit(1);
        }
    }));
    app_menu->add_action(GAction::create("Settings...", load_png("/res/icons/gear16.png"),
        [&](const GAction&) {
            if (!settings_window) {
                settings_window = create_settings_window(*terminal, config);
                settings_window->on_close_request = [&] {
                    settings_window = nullptr;
                    return GWindow::CloseRequestDecision::Close;
                };
            }
            settings_window->show();
            settings_window->move_to_front();
        }));
    app_menu->add_separator();
    app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
        dbgprintf("Terminal: Quit menu activated!\n");
        GApplication::the().quit(0);
    }));
    menubar->add_menu(move(app_menu));

    auto edit_menu = GMenu::construct("Edit");
    edit_menu->add_action(terminal->copy_action());
    edit_menu->add_action(terminal->paste_action());
    menubar->add_menu(move(edit_menu));


    GActionGroup font_action_group;
    font_action_group.set_exclusive(true);
    auto font_menu = GMenu::construct("Font");
    GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
        auto action = GAction::create(font_name, [&](GAction& action) {
            action.set_checked(true);
            terminal->set_font(GFontDatabase::the().get_by_name(action.text()));
            auto metadata = GFontDatabase::the().get_metadata_by_name(action.text());
            ASSERT(metadata.has_value());
            config->write_entry("Text", "Font", metadata.value().path);
            config->sync();
            terminal->force_repaint();
        });
        font_action_group.add_action(*action);
        action->set_checkable(true);
        if (terminal->font().name() == font_name)
            action->set_checked(true);
        font_menu->add_action(*action);
    });
    menubar->add_menu(move(font_menu));

    auto help_menu = GMenu::construct("Help");
    help_menu->add_action(GAction::create("About", [&](const GAction&) {
        GAboutDialog::show("Terminal", load_png("/res/icons/32x32/app-terminal.png"), window);
    }));
    menubar->add_menu(move(help_menu));

    app.set_menubar(move(menubar));

    config->sync();
    return app.exec();
}