main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <LibCore/ArgsParser.h>
  27. #include <LibGUI/AboutDialog.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/ActionGroup.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/GroupBox.h>
  33. #include <LibGUI/Icon.h>
  34. #include <LibGUI/Menu.h>
  35. #include <LibGUI/MenuBar.h>
  36. #include <LibGUI/RadioButton.h>
  37. #include <LibGUI/Slider.h>
  38. #include <LibGUI/SpinBox.h>
  39. #include <LibGUI/Widget.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGfx/Font.h>
  42. #include <LibGfx/FontDatabase.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 <serenity.h>
  50. #include <signal.h>
  51. #include <spawn.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <sys/ioctl.h>
  56. #include <sys/select.h>
  57. #include <unistd.h>
  58. static void utmp_update(const char* tty, pid_t pid, bool create)
  59. {
  60. if (!tty)
  61. return;
  62. int utmpupdate_pid = fork();
  63. if (utmpupdate_pid < 0) {
  64. perror("fork");
  65. return;
  66. }
  67. if (utmpupdate_pid)
  68. return;
  69. const auto shell_pid_string = String::formatted("{}", pid);
  70. execl("/bin/utmpupdate", "/bin/utmpupdate", "-f", "Terminal", "-p", shell_pid_string.characters(), (create ? "-c" : "-d"), tty, nullptr);
  71. }
  72. static pid_t run_command(int ptm_fd, String command)
  73. {
  74. pid_t pid = fork();
  75. if (pid < 0) {
  76. perror("fork");
  77. dbgln("run_command: could not fork to run '{}'", command);
  78. return pid;
  79. }
  80. if (pid == 0) {
  81. const char* tty_name = ptsname(ptm_fd);
  82. if (!tty_name) {
  83. perror("ptsname");
  84. exit(1);
  85. }
  86. close(ptm_fd);
  87. int pts_fd = open(tty_name, O_RDWR);
  88. if (pts_fd < 0) {
  89. perror("open");
  90. exit(1);
  91. }
  92. if (setsid() < 0) {
  93. perror("setsid");
  94. }
  95. close(0);
  96. close(1);
  97. close(2);
  98. int rc = dup2(pts_fd, 0);
  99. if (rc < 0) {
  100. perror("dup2");
  101. exit(1);
  102. }
  103. rc = dup2(pts_fd, 1);
  104. if (rc < 0) {
  105. perror("dup2");
  106. exit(1);
  107. }
  108. rc = dup2(pts_fd, 2);
  109. if (rc < 0) {
  110. perror("dup2");
  111. exit(1);
  112. }
  113. rc = close(pts_fd);
  114. if (rc < 0) {
  115. perror("close");
  116. exit(1);
  117. }
  118. rc = ioctl(0, TIOCSCTTY);
  119. if (rc < 0) {
  120. perror("ioctl(TIOCSCTTY)");
  121. exit(1);
  122. }
  123. String shell = "/bin/Shell";
  124. auto* pw = getpwuid(getuid());
  125. if (pw && pw->pw_shell) {
  126. shell = pw->pw_shell;
  127. }
  128. endpwent();
  129. const char* args[4] = { shell.characters(), nullptr, nullptr, nullptr };
  130. if (!command.is_empty()) {
  131. args[1] = "-c";
  132. args[2] = command.characters();
  133. }
  134. const char* envs[] = { "PROMPT=\\X\\u@\\h:\\w\\a\\e[33;1m\\h\\e[0m \\e[34;1m\\w\\e[0m \\p ", "TERM=xterm", "PAGER=more", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
  135. rc = execve(shell.characters(), const_cast<char**>(args), const_cast<char**>(envs));
  136. if (rc < 0) {
  137. perror("execve");
  138. exit(1);
  139. }
  140. ASSERT_NOT_REACHED();
  141. }
  142. return pid;
  143. }
  144. static RefPtr<GUI::Window> create_settings_window(TerminalWidget& terminal)
  145. {
  146. auto window = GUI::Window::construct();
  147. window->set_title("Terminal Settings");
  148. window->set_resizable(false);
  149. window->resize(200, 230);
  150. window->set_modal(true);
  151. auto& settings = window->set_main_widget<GUI::Widget>();
  152. settings.set_fill_with_background_color(true);
  153. settings.set_background_role(ColorRole::Button);
  154. settings.set_layout<GUI::VerticalBoxLayout>();
  155. settings.layout()->set_margins({ 4, 4, 4, 4 });
  156. auto& radio_container = settings.add<GUI::GroupBox>("Bell Mode");
  157. radio_container.set_layout<GUI::VerticalBoxLayout>();
  158. radio_container.layout()->set_margins({ 6, 16, 6, 6 });
  159. radio_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  160. radio_container.set_preferred_size(100, 70);
  161. auto& sysbell_radio = radio_container.add<GUI::RadioButton>("Use (Audible) System Bell");
  162. auto& visbell_radio = radio_container.add<GUI::RadioButton>("Use (Visual) Terminal Bell");
  163. sysbell_radio.set_checked(terminal.should_beep());
  164. visbell_radio.set_checked(!terminal.should_beep());
  165. sysbell_radio.on_checked = [&terminal](const bool checked) {
  166. terminal.set_should_beep(checked);
  167. };
  168. auto& slider_container = settings.add<GUI::GroupBox>("Background Opacity");
  169. slider_container.set_layout<GUI::VerticalBoxLayout>();
  170. slider_container.layout()->set_margins({ 6, 16, 6, 6 });
  171. slider_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  172. slider_container.set_preferred_size(100, 50);
  173. auto& slider = slider_container.add<GUI::HorizontalSlider>();
  174. slider.on_value_changed = [&terminal](int value) {
  175. terminal.set_opacity(value);
  176. };
  177. slider.set_range(0, 255);
  178. slider.set_value(terminal.opacity());
  179. auto& spinbox_container = settings.add<GUI::GroupBox>("Scroll Length");
  180. spinbox_container.set_layout<GUI::VerticalBoxLayout>();
  181. spinbox_container.layout()->set_margins({ 6, 16, 6, 6 });
  182. spinbox_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  183. spinbox_container.set_preferred_size(100, 46);
  184. auto& spinbox = spinbox_container.add<GUI::SpinBox>();
  185. spinbox.set_min(1);
  186. spinbox.set_value(terminal.scroll_length());
  187. spinbox.on_change = [&terminal](int value) {
  188. terminal.set_scroll_length(value);
  189. };
  190. auto& history_size_spinbox_container = settings.add<GUI::GroupBox>("Maximum scrollback history lines");
  191. history_size_spinbox_container.set_layout<GUI::VerticalBoxLayout>();
  192. history_size_spinbox_container.layout()->set_margins({ 6, 16, 6, 6 });
  193. history_size_spinbox_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  194. history_size_spinbox_container.set_preferred_size(100, 46);
  195. auto& history_size_spinbox = history_size_spinbox_container.add<GUI::SpinBox>();
  196. history_size_spinbox.set_range(0, 40960);
  197. history_size_spinbox.set_value(terminal.max_history_size());
  198. history_size_spinbox.on_change = [&terminal](int value) {
  199. terminal.set_max_history_size(value);
  200. };
  201. return window;
  202. }
  203. int main(int argc, char** argv)
  204. {
  205. if (pledge("stdio tty rpath accept cpath wpath shared_buffer proc exec unix fattr sigaction", nullptr) < 0) {
  206. perror("pledge");
  207. return 1;
  208. }
  209. struct sigaction act;
  210. memset(&act, 0, sizeof(act));
  211. act.sa_flags = SA_NOCLDWAIT;
  212. act.sa_handler = SIG_IGN;
  213. int rc = sigaction(SIGCHLD, &act, nullptr);
  214. if (rc < 0) {
  215. perror("sigaction");
  216. return 1;
  217. }
  218. auto app = GUI::Application::construct(argc, argv);
  219. if (pledge("stdio tty rpath accept cpath wpath shared_buffer proc exec unix", nullptr) < 0) {
  220. perror("pledge");
  221. return 1;
  222. }
  223. const char* command_to_execute = nullptr;
  224. Core::ArgsParser args_parser;
  225. args_parser.add_option(command_to_execute, "Execute this command inside the terminal", nullptr, 'e', "command");
  226. args_parser.parse(argc, argv);
  227. int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
  228. if (ptm_fd < 0) {
  229. perror("posix_openpt");
  230. return 1;
  231. }
  232. if (grantpt(ptm_fd) < 0) {
  233. perror("grantpt");
  234. return 1;
  235. }
  236. if (unlockpt(ptm_fd) < 0) {
  237. perror("unlockpt");
  238. return 1;
  239. }
  240. RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("Terminal");
  241. pid_t shell_pid = 0;
  242. if (command_to_execute)
  243. shell_pid = run_command(ptm_fd, command_to_execute);
  244. else
  245. shell_pid = run_command(ptm_fd, config->read_entry("Startup", "Command", ""));
  246. auto* pts_name = ptsname(ptm_fd);
  247. utmp_update(pts_name, shell_pid, true);
  248. auto app_icon = GUI::Icon::default_icon("app-terminal");
  249. auto window = GUI::Window::construct();
  250. window->set_title("Terminal");
  251. window->set_background_color(Color::Black);
  252. window->set_double_buffering_enabled(false);
  253. auto& terminal = window->set_main_widget<TerminalWidget>(ptm_fd, true, config);
  254. terminal.on_command_exit = [&] {
  255. app->quit(0);
  256. };
  257. terminal.on_title_change = [&](auto& title) {
  258. window->set_title(title);
  259. };
  260. terminal.apply_size_increments_to_window(*window);
  261. window->show();
  262. window->set_icon(app_icon.bitmap_for_size(16));
  263. terminal.set_should_beep(config->read_bool_entry("Window", "AudibleBeep", false));
  264. RefPtr<GUI::Window> settings_window;
  265. auto new_opacity = config->read_num_entry("Window", "Opacity", 255);
  266. terminal.set_opacity(new_opacity);
  267. window->set_has_alpha_channel(new_opacity < 255);
  268. auto new_scrollback_size = config->read_num_entry("Terminal", "MaxHistorySize", terminal.max_history_size());
  269. terminal.set_max_history_size(new_scrollback_size);
  270. auto menubar = GUI::MenuBar::construct();
  271. auto& app_menu = menubar->add_menu("Terminal");
  272. 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&) {
  273. pid_t child;
  274. const char* argv[] = { "Terminal", nullptr };
  275. if ((errno = posix_spawn(&child, "/bin/Terminal", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  276. perror("posix_spawn");
  277. } else {
  278. if (disown(child) < 0)
  279. perror("disown");
  280. }
  281. }));
  282. app_menu.add_action(GUI::Action::create("Settings...", Gfx::Bitmap::load_from_file("/res/icons/16x16/gear.png"),
  283. [&](const GUI::Action&) {
  284. if (!settings_window) {
  285. settings_window = create_settings_window(terminal);
  286. settings_window->on_close_request = [&] {
  287. settings_window = nullptr;
  288. return GUI::Window::CloseRequestDecision::Close;
  289. };
  290. }
  291. settings_window->show();
  292. settings_window->move_to_front();
  293. }));
  294. app_menu.add_separator();
  295. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  296. dbgln("Terminal: Quit menu activated!");
  297. GUI::Application::the()->quit();
  298. }));
  299. auto& edit_menu = menubar->add_menu("Edit");
  300. edit_menu.add_action(terminal.copy_action());
  301. edit_menu.add_action(terminal.paste_action());
  302. auto& view_menu = menubar->add_menu("View");
  303. view_menu.add_action(terminal.clear_including_history_action());
  304. GUI::ActionGroup font_action_group;
  305. font_action_group.set_exclusive(true);
  306. auto& font_menu = menubar->add_menu("Font");
  307. Gfx::FontDatabase::the().for_each_fixed_width_font([&](const Gfx::Font& font) {
  308. auto action = GUI::Action::create_checkable(font.qualified_name(), [&](auto&) {
  309. terminal.set_font(font);
  310. config->write_entry("Text", "Font", font.qualified_name());
  311. config->sync();
  312. terminal.force_repaint();
  313. });
  314. font_action_group.add_action(*action);
  315. if (terminal.font().qualified_name() == font.qualified_name())
  316. action->set_checked(true);
  317. font_menu.add_action(*action);
  318. });
  319. auto& help_menu = menubar->add_menu("Help");
  320. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  321. GUI::AboutDialog::show("Terminal", app_icon.bitmap_for_size(32), window);
  322. }));
  323. app->set_menubar(move(menubar));
  324. if (unveil("/res", "r") < 0) {
  325. perror("unveil");
  326. return 1;
  327. }
  328. if (unveil("/bin/Terminal", "x") < 0) {
  329. perror("unveil");
  330. return 1;
  331. }
  332. if (unveil("/bin/utmpupdate", "x") < 0) {
  333. perror("unveil");
  334. return 1;
  335. }
  336. if (unveil("/tmp/portal/launch", "rw") < 0) {
  337. perror("unveil");
  338. return 1;
  339. }
  340. if (unveil(config->file_name().characters(), "rwc")) {
  341. perror("unveil");
  342. return 1;
  343. }
  344. unveil(nullptr, nullptr);
  345. config->sync();
  346. int result = app->exec();
  347. dbgln("Exiting terminal, updating utmp");
  348. utmp_update(pts_name, 0, false);
  349. return result;
  350. }