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
|
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <pwd.h>
#include "Terminal.h"
#include <Kernel/KeyCode.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GRadioButton.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GFontDatabase.h>
#include <LibGUI/GSlider.h>
#include <LibCore/CUserInfo.h>
static void make_shell(int ptm_fd)
{
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);
}
char* args[] = { "/bin/Shell", nullptr };
char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
rc = execve("/bin/Shell", args, envs);
if (rc < 0) {
perror("execve");
exit(1);
}
ASSERT_NOT_REACHED();
}
}
GWindow* create_opacity_settings_window(Terminal& terminal, RetainPtr<CConfigFile> config)
{
auto* opacity_adjustment_window = new GWindow;
opacity_adjustment_window->set_title("Adjust opacity");
opacity_adjustment_window->set_rect(50, 50, 200, 100);
auto* slider = new GSlider(nullptr);
opacity_adjustment_window->set_main_widget(slider);
slider->set_fill_with_background_color(true);
slider->set_background_color(Color::LightGray);
slider->on_value_changed = [&terminal, &config] (int value) {
float opacity = value / 100.0;
terminal.set_opacity(opacity);
};
slider->set_range(0, 100);
slider->set_value(terminal.opacity() * 100.0);
return opacity_adjustment_window;
}
GWindow* create_beep_choice_window(Terminal& terminal, RetainPtr<CConfigFile> config)
{
auto* beep_choice_window = new GWindow;
beep_choice_window->set_title("Terminal beep settings");
beep_choice_window->set_rect(50, 50, 200, 100);
auto* radio_buttons = new GWidget;
beep_choice_window->set_main_widget(radio_buttons);
radio_buttons->set_fill_with_background_color(true);
radio_buttons->set_layout(make<GBoxLayout>(Orientation::Vertical));
radio_buttons->layout()->set_margins({ 4, 4, 4, 4 });
auto* sysbell_radio = new GRadioButton("Use (Audible) System Bell", radio_buttons);
auto* visbell_radio = new GRadioButton("Use (Visual) Terminal Bell", radio_buttons);
sysbell_radio->set_checked(terminal.should_beep());
visbell_radio->set_checked(!terminal.should_beep());
sysbell_radio->on_checked = [&terminal] (const bool res) {
terminal.set_should_beep(res);
};
return beep_choice_window;
}
int main(int argc, char** argv)
{
GApplication app(argc, argv);
chdir(get_current_user_home_path());
int ptm_fd = open("/dev/ptmx", O_RDWR);
if (ptm_fd < 0) {
perror("open(ptmx)");
return 1;
}
make_shell(ptm_fd);
auto* window = new GWindow;
window->set_title("Terminal");
window->set_background_color(Color::Black);
window->set_double_buffering_enabled(false);
window->set_should_exit_event_loop_on_close(true);
RetainPtr<CConfigFile> config = CConfigFile::get_for_app("Terminal");
Terminal terminal(ptm_fd, config);
window->set_has_alpha_channel(true);
window->set_main_widget(&terminal);
window->move_to(300, 300);
terminal.apply_size_increments_to_window(*window);
window->show();
window->set_icon_path("/res/icons/16x16/app-terminal.png");
terminal.set_should_beep(config->read_num_entry("Window", "AudibleBeep", 1) == 1);
WeakPtr<GWindow> opacity_adjustment_window =
create_opacity_settings_window(terminal, config)->make_weak_ptr();
WeakPtr<GWindow> beep_choice_window =
create_beep_choice_window(terminal, config)->make_weak_ptr();
auto new_opacity = config->read_num_entry("Window", "Opacity", 255);
terminal.set_opacity((float)new_opacity / 255.0);
auto menubar = make<GMenuBar>();
auto app_menu = make<GMenu>("Terminal");
app_menu->add_action(GAction::create("Adjust opacity...",
[&opacity_adjustment_window, &terminal, &config] (const GAction&) {
if (!opacity_adjustment_window)
opacity_adjustment_window =
create_opacity_settings_window(terminal, config)->make_weak_ptr();
opacity_adjustment_window->show();
}));
app_menu->add_action(GAction::create("Change audio output...",
[&beep_choice_window, &terminal, &config] (const GAction&) {
if (!beep_choice_window)
beep_choice_window =
create_beep_choice_window(terminal, config)->make_weak_ptr();
beep_choice_window->show();
}));
app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [] (const GAction&) {
dbgprintf("Terminal: Quit menu activated!\n");
GApplication::the().quit(0);
return;
}));
menubar->add_menu(move(app_menu));
auto font_menu = make<GMenu>("Font");
GFontDatabase::the().for_each_fixed_width_font([&] (const String& font_name) {
font_menu->add_action(GAction::create(font_name, [&terminal, &config] (const GAction& action) {
terminal.set_font(GFontDatabase::the().get_by_name(action.text()));
auto metadata = GFontDatabase::the().get_metadata_by_name(action.text());
config->write_entry("Text", "Font", metadata.path);
config->sync();
terminal.force_repaint();
}));
});
menubar->add_menu(move(font_menu));
auto help_menu = make<GMenu>("Help");
help_menu->add_action(GAction::create("About", [] (const GAction&) {
dbgprintf("FIXME: Implement Help/About\n");
}));
menubar->add_menu(move(help_menu));
app.set_menubar(move(menubar));
config->sync();
return app.exec();
}
|