main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/KeyCode.h>
  27. #include <LibCore/ArgsParser.h>
  28. #include <LibCore/UserInfo.h>
  29. #include <LibGUI/AboutDialog.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/ActionGroup.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/FontDatabase.h>
  35. #include <LibGUI/GroupBox.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/MenuBar.h>
  38. #include <LibGUI/RadioButton.h>
  39. #include <LibGUI/Slider.h>
  40. #include <LibGUI/Widget.h>
  41. #include <LibGUI/Window.h>
  42. #include <LibGfx/Font.h>
  43. #include <LibGfx/Palette.h>
  44. #include <LibVT/TerminalWidget.h>
  45. #include <assert.h>
  46. #include <errno.h>
  47. #include <fcntl.h>
  48. #include <pwd.h>
  49. #include <signal.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <sys/ioctl.h>
  54. #include <sys/select.h>
  55. #include <unistd.h>
  56. static void run_command(int ptm_fd, String command)
  57. {
  58. pid_t pid = fork();
  59. if (pid == 0) {
  60. const char* tty_name = ptsname(ptm_fd);
  61. if (!tty_name) {
  62. perror("ptsname");
  63. exit(1);
  64. }
  65. close(ptm_fd);
  66. int pts_fd = open(tty_name, O_RDWR);
  67. if (pts_fd < 0) {
  68. perror("open");
  69. exit(1);
  70. }
  71. if (setsid() < 0) {
  72. perror("setsid");
  73. }
  74. close(0);
  75. close(1);
  76. close(2);
  77. int rc = dup2(pts_fd, 0);
  78. if (rc < 0) {
  79. perror("dup2");
  80. exit(1);
  81. }
  82. rc = dup2(pts_fd, 1);
  83. if (rc < 0) {
  84. perror("dup2");
  85. exit(1);
  86. }
  87. rc = dup2(pts_fd, 2);
  88. if (rc < 0) {
  89. perror("dup2");
  90. exit(1);
  91. }
  92. rc = close(pts_fd);
  93. if (rc < 0) {
  94. perror("close");
  95. exit(1);
  96. }
  97. rc = ioctl(0, TIOCSCTTY);
  98. if (rc < 0) {
  99. perror("ioctl(TIOCSCTTY)");
  100. exit(1);
  101. }
  102. String shell = "/bin/Shell";
  103. auto* pw = getpwuid(getuid());
  104. if (pw && pw->pw_shell) {
  105. shell = pw->pw_shell;
  106. }
  107. endpwent();
  108. const char* args[4] = { shell.characters(), nullptr, nullptr, nullptr };
  109. if (!command.is_empty()) {
  110. args[1] = "-c";
  111. args[2] = command.characters();
  112. }
  113. const char* envs[] = { "PROMPT=\\X\\u@\\h:\\w\\a\\e[33;1m\\h\\e[0m \\e[32;1m\\w\\e[0m \\p ", "TERM=xterm", "PAGER=more", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
  114. rc = execve(shell.characters(), const_cast<char**>(args), const_cast<char**>(envs));
  115. if (rc < 0) {
  116. perror("execve");
  117. exit(1);
  118. }
  119. ASSERT_NOT_REACHED();
  120. }
  121. }
  122. RefPtr<GUI::Window> create_settings_window(TerminalWidget& terminal)
  123. {
  124. auto window = GUI::Window::construct();
  125. window->set_title("Terminal Settings");
  126. window->set_rect(50, 50, 200, 140);
  127. window->set_modal(true);
  128. auto& settings = window->set_main_widget<GUI::Widget>();
  129. settings.set_fill_with_background_color(true);
  130. settings.set_background_role(ColorRole::Button);
  131. settings.set_layout<GUI::VerticalBoxLayout>();
  132. settings.layout()->set_margins({ 4, 4, 4, 4 });
  133. auto radio_container = settings.add<GUI::GroupBox>("Bell Mode");
  134. radio_container->set_layout<GUI::VerticalBoxLayout>();
  135. radio_container->layout()->set_margins({ 6, 16, 6, 6 });
  136. radio_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  137. radio_container->set_preferred_size(100, 70);
  138. auto sysbell_radio = radio_container->add<GUI::RadioButton>("Use (Audible) System Bell");
  139. auto visbell_radio = radio_container->add<GUI::RadioButton>("Use (Visual) Terminal Bell");
  140. sysbell_radio->set_checked(terminal.should_beep());
  141. visbell_radio->set_checked(!terminal.should_beep());
  142. sysbell_radio->on_checked = [&terminal](const bool checked) {
  143. terminal.set_should_beep(checked);
  144. };
  145. auto slider_container = settings.add<GUI::GroupBox>("Background Opacity");
  146. slider_container->set_layout<GUI::VerticalBoxLayout>();
  147. slider_container->layout()->set_margins({ 6, 16, 6, 6 });
  148. slider_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  149. slider_container->set_preferred_size(100, 50);
  150. auto slider = slider_container->add<GUI::HorizontalSlider>();
  151. slider->on_value_changed = [&terminal](int value) {
  152. terminal.set_opacity(value);
  153. };
  154. slider->set_range(0, 255);
  155. slider->set_value(terminal.opacity());
  156. return window;
  157. }
  158. int main(int argc, char** argv)
  159. {
  160. if (pledge("stdio tty rpath accept cpath wpath shared_buffer proc exec unix fattr", nullptr) < 0) {
  161. perror("pledge");
  162. return 1;
  163. }
  164. struct sigaction act;
  165. memset(&act, 0, sizeof(act));
  166. act.sa_flags = SA_NOCLDWAIT;
  167. act.sa_handler = SIG_IGN;
  168. int rc = sigaction(SIGCHLD, &act, nullptr);
  169. if (rc < 0) {
  170. perror("sigaction");
  171. return 1;
  172. }
  173. GUI::Application app(argc, argv);
  174. if (pledge("stdio tty rpath accept cpath wpath shared_buffer proc exec", nullptr) < 0) {
  175. perror("pledge");
  176. return 1;
  177. }
  178. const char* command_to_execute = nullptr;
  179. Core::ArgsParser args_parser;
  180. args_parser.add_option(command_to_execute, "Execute this command inside the terminal", nullptr, 'e', "command");
  181. args_parser.parse(argc, argv);
  182. if (chdir(get_current_user_home_path().characters()) < 0)
  183. perror("chdir");
  184. int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
  185. if (ptm_fd < 0) {
  186. perror("posix_openpt");
  187. return 1;
  188. }
  189. if (grantpt(ptm_fd) < 0) {
  190. perror("grantpt");
  191. return 1;
  192. }
  193. if (unlockpt(ptm_fd) < 0) {
  194. perror("unlockpt");
  195. return 1;
  196. }
  197. run_command(ptm_fd, command_to_execute);
  198. auto window = GUI::Window::construct();
  199. window->set_title("Terminal");
  200. window->set_background_color(Color::Black);
  201. window->set_double_buffering_enabled(false);
  202. RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("Terminal");
  203. auto& terminal = window->set_main_widget<TerminalWidget>(ptm_fd, true, config);
  204. terminal.on_command_exit = [&] {
  205. app.quit(0);
  206. };
  207. terminal.on_title_change = [&](auto& title) {
  208. window->set_title(title);
  209. };
  210. window->move_to(300, 300);
  211. terminal.apply_size_increments_to_window(*window);
  212. window->show();
  213. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"));
  214. terminal.set_should_beep(config->read_bool_entry("Window", "AudibleBeep", false));
  215. RefPtr<GUI::Window> settings_window;
  216. auto new_opacity = config->read_num_entry("Window", "Opacity", 255);
  217. terminal.set_opacity(new_opacity);
  218. window->set_has_alpha_channel(new_opacity < 255);
  219. auto menubar = make<GUI::MenuBar>();
  220. auto app_menu = GUI::Menu::construct("Terminal");
  221. app_menu->add_action(GUI::Action::create("Open new terminal", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"), [&](auto&) {
  222. if (!fork()) {
  223. execl("/bin/Terminal", "Terminal", nullptr);
  224. exit(1);
  225. }
  226. }));
  227. app_menu->add_action(GUI::Action::create("Settings...", Gfx::Bitmap::load_from_file("/res/icons/gear16.png"),
  228. [&](const GUI::Action&) {
  229. if (!settings_window) {
  230. settings_window = create_settings_window(terminal);
  231. settings_window->on_close_request = [&] {
  232. settings_window = nullptr;
  233. return GUI::Window::CloseRequestDecision::Close;
  234. };
  235. }
  236. settings_window->show();
  237. settings_window->move_to_front();
  238. }));
  239. app_menu->add_separator();
  240. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  241. dbgprintf("Terminal: Quit menu activated!\n");
  242. GUI::Application::the().quit(0);
  243. }));
  244. menubar->add_menu(move(app_menu));
  245. auto edit_menu = GUI::Menu::construct("Edit");
  246. edit_menu->add_action(terminal.copy_action());
  247. edit_menu->add_action(terminal.paste_action());
  248. menubar->add_menu(move(edit_menu));
  249. GUI::ActionGroup font_action_group;
  250. font_action_group.set_exclusive(true);
  251. auto font_menu = GUI::Menu::construct("Font");
  252. GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  253. auto action = GUI::Action::create(font_name, [&](GUI::Action& action) {
  254. action.set_checked(true);
  255. terminal.set_font(GFontDatabase::the().get_by_name(action.text()));
  256. auto metadata = GFontDatabase::the().get_metadata_by_name(action.text());
  257. ASSERT(metadata.has_value());
  258. config->write_entry("Text", "Font", metadata.value().path);
  259. config->sync();
  260. terminal.force_repaint();
  261. });
  262. font_action_group.add_action(*action);
  263. action->set_checkable(true);
  264. if (terminal.font().name() == font_name)
  265. action->set_checked(true);
  266. font_menu->add_action(*action);
  267. });
  268. menubar->add_menu(move(font_menu));
  269. auto help_menu = GUI::Menu::construct("Help");
  270. help_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  271. GUI::AboutDialog::show("Terminal", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-terminal.png"), window);
  272. }));
  273. menubar->add_menu(move(help_menu));
  274. app.set_menubar(move(menubar));
  275. if (unveil("/res", "r") < 0) {
  276. perror("unveil");
  277. return 1;
  278. }
  279. if (unveil("/bin/Terminal", "x") < 0) {
  280. perror("unveil");
  281. return 1;
  282. }
  283. if (unveil(config->file_name().characters(), "rwc")) {
  284. perror("unveil");
  285. return 1;
  286. }
  287. unveil(nullptr, nullptr);
  288. config->sync();
  289. return app.exec();
  290. }