main.cpp 11 KB

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