main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <assert.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/select.h>
  10. #include "Terminal.h"
  11. #include <Kernel/KeyCode.h>
  12. #include <LibGUI/GApplication.h>
  13. #include <LibGUI/GWidget.h>
  14. #include <LibGUI/GWindow.h>
  15. #include <LibGUI/GMenuBar.h>
  16. #include <LibGUI/GAction.h>
  17. #include <LibGUI/GFontDatabase.h>
  18. #include <LibGUI/GSlider.h>
  19. static void make_shell(int ptm_fd)
  20. {
  21. pid_t pid = fork();
  22. if (pid == 0) {
  23. const char* tty_name = ptsname(ptm_fd);
  24. if (!tty_name) {
  25. perror("ptsname");
  26. exit(1);
  27. }
  28. close(ptm_fd);
  29. int pts_fd = open(tty_name, O_RDWR);
  30. if (pts_fd < 0) {
  31. perror("open");
  32. exit(1);
  33. }
  34. // NOTE: It's okay if this fails.
  35. (void) ioctl(0, TIOCNOTTY);
  36. close(0);
  37. close(1);
  38. close(2);
  39. int rc = dup2(pts_fd, 0);
  40. if (rc < 0) {
  41. perror("dup2");
  42. exit(1);
  43. }
  44. rc = dup2(pts_fd, 1);
  45. if (rc < 0) {
  46. perror("dup2");
  47. exit(1);
  48. }
  49. rc = dup2(pts_fd, 2);
  50. if (rc < 0) {
  51. perror("dup2");
  52. exit(1);
  53. }
  54. rc = close(pts_fd);
  55. if (rc < 0) {
  56. perror("close");
  57. exit(1);
  58. }
  59. rc = ioctl(0, TIOCSCTTY);
  60. if (rc < 0) {
  61. perror("ioctl(TIOCSCTTY)");
  62. exit(1);
  63. }
  64. char* args[] = { "/bin/Shell", nullptr };
  65. char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin", nullptr };
  66. rc = execve("/bin/Shell", args, envs);
  67. if (rc < 0) {
  68. perror("execve");
  69. exit(1);
  70. }
  71. ASSERT_NOT_REACHED();
  72. }
  73. }
  74. int main(int argc, char** argv)
  75. {
  76. GApplication app(argc, argv);
  77. int ptm_fd = open("/dev/ptmx", O_RDWR);
  78. if (ptm_fd < 0) {
  79. perror("open(ptmx)");
  80. return 1;
  81. }
  82. make_shell(ptm_fd);
  83. auto* window = new GWindow;
  84. window->set_background_color(Color::Black);
  85. window->set_double_buffering_enabled(false);
  86. window->set_should_exit_event_loop_on_close(true);
  87. Terminal terminal(ptm_fd);
  88. window->set_has_alpha_channel(true);
  89. window->set_main_widget(&terminal);
  90. window->move_to(300, 300);
  91. terminal.apply_size_increments_to_window(*window);
  92. window->show();
  93. window->set_icon_path("/res/icons/16x16/app-terminal.png");
  94. auto* opacity_adjustment_window = new GWindow;
  95. opacity_adjustment_window->set_title("Adjust opacity");
  96. opacity_adjustment_window->set_rect(50, 50, 200, 100);
  97. auto* slider = new GSlider(nullptr);
  98. opacity_adjustment_window->set_main_widget(slider);
  99. slider->set_fill_with_background_color(true);
  100. slider->set_background_color(Color::LightGray);
  101. slider->on_value_changed = [&terminal] (int value) {
  102. float opacity = value / 100.0;
  103. terminal.set_opacity(opacity);
  104. };
  105. slider->set_range(0, 100);
  106. slider->set_value(100);
  107. auto menubar = make<GMenuBar>();
  108. auto app_menu = make<GMenu>("Terminal");
  109. app_menu->add_action(GAction::create("Adjust opacity...", [opacity_adjustment_window] (const GAction&) {
  110. opacity_adjustment_window->show();
  111. }));
  112. app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [] (const GAction&) {
  113. dbgprintf("Terminal: Quit menu activated!\n");
  114. GApplication::the().quit(0);
  115. return;
  116. }));
  117. menubar->add_menu(move(app_menu));
  118. auto font_menu = make<GMenu>("Font");
  119. GFontDatabase::the().for_each_fixed_width_font([&] (const String& font_name) {
  120. font_menu->add_action(GAction::create(font_name, [&terminal] (const GAction& action) {
  121. terminal.set_font(GFontDatabase::the().get_by_name(action.text()));
  122. terminal.force_repaint();
  123. }));
  124. });
  125. menubar->add_menu(move(font_menu));
  126. auto help_menu = make<GMenu>("Help");
  127. help_menu->add_action(GAction::create("About", [] (const GAction&) {
  128. dbgprintf("FIXME: Implement Help/About\n");
  129. }));
  130. menubar->add_menu(move(help_menu));
  131. app.set_menubar(move(menubar));
  132. return app.exec();
  133. }