main.cpp 13 KB

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