2019-01-15 01:30:55 -02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <assert.h>
|
2019-01-15 05:49:24 -02:00
|
|
|
#include <sys/ioctl.h>
|
2019-01-15 20:12:20 -02:00
|
|
|
#include <sys/select.h>
|
2019-01-15 01:30:55 -02:00
|
|
|
#include "Terminal.h"
|
2019-01-30 18:18:13 -02:00
|
|
|
#include <Kernel/KeyCode.h>
|
2019-02-11 11:56:23 -02:00
|
|
|
#include <LibGUI/GApplication.h>
|
2019-02-10 11:28:39 -02:00
|
|
|
#include <LibGUI/GWidget.h>
|
|
|
|
|
#include <LibGUI/GWindow.h>
|
2019-02-11 12:37:12 -02:00
|
|
|
#include <LibGUI/GMenuBar.h>
|
2019-02-12 11:09:48 -02:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-02-12 11:35:33 -02:00
|
|
|
#include <LibGUI/GFontDatabase.h>
|
2019-01-15 01:30:55 -02:00
|
|
|
|
2019-01-15 03:30:19 -02:00
|
|
|
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);
|
2019-02-12 08:25:25 -02:00
|
|
|
if (pts_fd < 0) {
|
|
|
|
|
perror("open");
|
2019-01-15 05:49:24 -02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2019-02-12 08:25:25 -02:00
|
|
|
|
|
|
|
|
// NOTE: It's okay if this fails.
|
|
|
|
|
(void) ioctl(0, TIOCNOTTY);
|
|
|
|
|
|
2019-01-15 03:30:19 -02:00
|
|
|
close(0);
|
|
|
|
|
close(1);
|
|
|
|
|
close(2);
|
2019-02-12 08:25:25 -02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2019-01-15 05:49:24 -02:00
|
|
|
rc = ioctl(0, TIOCSCTTY);
|
|
|
|
|
if (rc < 0) {
|
|
|
|
|
perror("ioctl(TIOCSCTTY)");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2019-02-26 18:40:35 -03:00
|
|
|
char* args[] = { "/bin/sh", nullptr };
|
2019-03-26 21:36:33 -03:00
|
|
|
char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin", nullptr };
|
2019-02-26 18:40:35 -03:00
|
|
|
rc = execve("/bin/sh", args, envs);
|
2019-01-15 03:30:19 -02:00
|
|
|
if (rc < 0) {
|
|
|
|
|
perror("execve");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-15 01:30:55 -02:00
|
|
|
|
2019-02-11 11:56:23 -02:00
|
|
|
int main(int argc, char** argv)
|
2019-01-16 09:57:07 -02:00
|
|
|
{
|
2019-02-11 11:56:23 -02:00
|
|
|
GApplication app(argc, argv);
|
|
|
|
|
|
2019-01-16 10:36:10 -02:00
|
|
|
int ptm_fd = open("/dev/ptmx", O_RDWR);
|
|
|
|
|
if (ptm_fd < 0) {
|
|
|
|
|
perror("open(ptmx)");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-01-15 03:30:19 -02:00
|
|
|
|
|
|
|
|
make_shell(ptm_fd);
|
|
|
|
|
|
2019-02-10 11:28:39 -02:00
|
|
|
auto* window = new GWindow;
|
2019-04-10 09:29:47 -03:00
|
|
|
window->set_background_color(Color::Black);
|
2019-03-17 00:23:54 -03:00
|
|
|
window->set_double_buffering_enabled(false);
|
2019-03-18 20:01:02 -03:00
|
|
|
window->set_should_exit_event_loop_on_close(true);
|
2019-01-15 01:30:55 -02:00
|
|
|
|
2019-02-10 11:28:39 -02:00
|
|
|
Terminal terminal(ptm_fd);
|
2019-03-19 23:27:07 -03:00
|
|
|
window->set_has_alpha_channel(false);
|
2019-02-10 11:28:39 -02:00
|
|
|
window->set_main_widget(&terminal);
|
2019-02-11 03:09:54 -02:00
|
|
|
window->move_to(300, 300);
|
2019-02-20 20:21:23 -03:00
|
|
|
terminal.apply_size_increments_to_window(*window);
|
2019-02-10 11:28:39 -02:00
|
|
|
window->show();
|
|
|
|
|
|
2019-02-11 12:37:12 -02:00
|
|
|
auto menubar = make<GMenuBar>();
|
|
|
|
|
|
|
|
|
|
auto app_menu = make<GMenu>("Terminal");
|
2019-03-02 22:52:22 -03:00
|
|
|
app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [] (const GAction&) {
|
2019-02-12 11:09:48 -02:00
|
|
|
dbgprintf("Terminal: Quit menu activated!\n");
|
2019-02-17 05:58:35 -03:00
|
|
|
GApplication::the().quit(0);
|
2019-02-12 11:09:48 -02:00
|
|
|
return;
|
|
|
|
|
}));
|
2019-02-11 12:37:12 -02:00
|
|
|
menubar->add_menu(move(app_menu));
|
|
|
|
|
|
2019-02-12 05:39:19 -02:00
|
|
|
auto font_menu = make<GMenu>("Font");
|
2019-03-06 10:06:40 -03:00
|
|
|
GFontDatabase::the().for_each_fixed_width_font([&] (const String& font_name) {
|
2019-02-19 22:39:46 -03:00
|
|
|
font_menu->add_action(GAction::create(font_name, [&terminal] (const GAction& action) {
|
2019-02-12 11:35:33 -02:00
|
|
|
terminal.set_font(GFontDatabase::the().get_by_name(action.text()));
|
|
|
|
|
terminal.force_repaint();
|
|
|
|
|
}));
|
|
|
|
|
});
|
2019-02-12 05:39:19 -02:00
|
|
|
menubar->add_menu(move(font_menu));
|
|
|
|
|
|
|
|
|
|
auto help_menu = make<GMenu>("Help");
|
2019-02-19 22:39:46 -03:00
|
|
|
help_menu->add_action(GAction::create("About", [] (const GAction&) {
|
2019-02-12 11:09:48 -02:00
|
|
|
dbgprintf("FIXME: Implement Help/About\n");
|
|
|
|
|
}));
|
2019-02-11 21:52:19 -02:00
|
|
|
menubar->add_menu(move(help_menu));
|
2019-02-11 12:37:12 -02:00
|
|
|
|
|
|
|
|
app.set_menubar(move(menubar));
|
|
|
|
|
|
2019-02-11 11:56:23 -02:00
|
|
|
return app.exec();
|
2019-01-15 01:30:55 -02:00
|
|
|
}
|