main.cpp 12 KB

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