main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <sys/ioctl.h>
  53. #include <sys/select.h>
  54. #include <unistd.h>
  55. static void run_command(int ptm_fd, String command)
  56. {
  57. pid_t pid = fork();
  58. if (pid == 0) {
  59. const char* tty_name = ptsname(ptm_fd);
  60. if (!tty_name) {
  61. perror("ptsname");
  62. exit(1);
  63. }
  64. close(ptm_fd);
  65. int pts_fd = open(tty_name, O_RDWR);
  66. if (pts_fd < 0) {
  67. perror("open");
  68. exit(1);
  69. }
  70. if (setsid() < 0) {
  71. perror("setsid");
  72. }
  73. close(0);
  74. close(1);
  75. close(2);
  76. int rc = dup2(pts_fd, 0);
  77. if (rc < 0) {
  78. perror("dup2");
  79. exit(1);
  80. }
  81. rc = dup2(pts_fd, 1);
  82. if (rc < 0) {
  83. perror("dup2");
  84. exit(1);
  85. }
  86. rc = dup2(pts_fd, 2);
  87. if (rc < 0) {
  88. perror("dup2");
  89. exit(1);
  90. }
  91. rc = close(pts_fd);
  92. if (rc < 0) {
  93. perror("close");
  94. exit(1);
  95. }
  96. rc = ioctl(0, TIOCSCTTY);
  97. if (rc < 0) {
  98. perror("ioctl(TIOCSCTTY)");
  99. exit(1);
  100. }
  101. String shell = "/bin/Shell";
  102. auto* pw = getpwuid(getuid());
  103. if (pw && pw->pw_shell) {
  104. shell = pw->pw_shell;
  105. }
  106. endpwent();
  107. const char* args[4] = { shell.characters(), nullptr, nullptr, nullptr };
  108. if (!command.is_empty()) {
  109. args[1] = "-c";
  110. args[2] = command.characters();
  111. }
  112. 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 };
  113. rc = execve(shell.characters(), const_cast<char**>(args), const_cast<char**>(envs));
  114. if (rc < 0) {
  115. perror("execve");
  116. exit(1);
  117. }
  118. ASSERT_NOT_REACHED();
  119. }
  120. }
  121. RefPtr<GUI::Window> create_settings_window(TerminalWidget& terminal)
  122. {
  123. auto window = GUI::Window::construct();
  124. window->set_title("Terminal Settings");
  125. window->set_resizable(false);
  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 unix", 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. int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
  183. if (ptm_fd < 0) {
  184. perror("posix_openpt");
  185. return 1;
  186. }
  187. if (grantpt(ptm_fd) < 0) {
  188. perror("grantpt");
  189. return 1;
  190. }
  191. if (unlockpt(ptm_fd) < 0) {
  192. perror("unlockpt");
  193. return 1;
  194. }
  195. RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("Terminal");
  196. if (command_to_execute)
  197. run_command(ptm_fd, command_to_execute);
  198. else
  199. run_command(ptm_fd, config->read_entry("Startup", "Command", ""));
  200. auto window = GUI::Window::construct();
  201. window->set_title("Terminal");
  202. window->set_background_color(Color::Black);
  203. window->set_double_buffering_enabled(false);
  204. auto& terminal = window->set_main_widget<TerminalWidget>(ptm_fd, true, config);
  205. terminal.on_command_exit = [&] {
  206. app.quit(0);
  207. };
  208. terminal.on_title_change = [&](auto& title) {
  209. window->set_title(title);
  210. };
  211. window->move_to(300, 300);
  212. terminal.apply_size_increments_to_window(*window);
  213. window->show();
  214. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"));
  215. terminal.set_should_beep(config->read_bool_entry("Window", "AudibleBeep", false));
  216. RefPtr<GUI::Window> settings_window;
  217. auto new_opacity = config->read_num_entry("Window", "Opacity", 255);
  218. terminal.set_opacity(new_opacity);
  219. window->set_has_alpha_channel(new_opacity < 255);
  220. auto menubar = GUI::MenuBar::construct();
  221. auto& app_menu = menubar->add_menu("Terminal");
  222. 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&) {
  223. if (!fork()) {
  224. execl("/bin/Terminal", "Terminal", nullptr);
  225. exit(1);
  226. }
  227. }));
  228. app_menu.add_action(GUI::Action::create("Settings...", Gfx::Bitmap::load_from_file("/res/icons/gear16.png"),
  229. [&](const GUI::Action&) {
  230. if (!settings_window) {
  231. settings_window = create_settings_window(terminal);
  232. settings_window->on_close_request = [&] {
  233. settings_window = nullptr;
  234. return GUI::Window::CloseRequestDecision::Close;
  235. };
  236. }
  237. settings_window->show();
  238. settings_window->move_to_front();
  239. }));
  240. app_menu.add_separator();
  241. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  242. dbgprintf("Terminal: Quit menu activated!\n");
  243. GUI::Application::the().quit(0);
  244. }));
  245. auto& edit_menu = menubar->add_menu("Edit");
  246. edit_menu.add_action(terminal.copy_action());
  247. edit_menu.add_action(terminal.paste_action());
  248. GUI::ActionGroup font_action_group;
  249. font_action_group.set_exclusive(true);
  250. auto& font_menu = menubar->add_menu("Font");
  251. GUI::FontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  252. auto action = GUI::Action::create_checkable(font_name, [&](auto& action) {
  253. terminal.set_font(GUI::FontDatabase::the().get_by_name(action.text()));
  254. auto metadata = GUI::FontDatabase::the().get_metadata_by_name(action.text());
  255. ASSERT(metadata.has_value());
  256. config->write_entry("Text", "Font", metadata.value().path);
  257. config->sync();
  258. terminal.force_repaint();
  259. });
  260. font_action_group.add_action(*action);
  261. if (terminal.font().name() == font_name)
  262. action->set_checked(true);
  263. font_menu.add_action(*action);
  264. });
  265. auto& help_menu = menubar->add_menu("Help");
  266. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  267. GUI::AboutDialog::show("Terminal", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-terminal.png"), window);
  268. }));
  269. app.set_menubar(move(menubar));
  270. if (unveil("/res", "r") < 0) {
  271. perror("unveil");
  272. return 1;
  273. }
  274. if (unveil("/bin/Terminal", "x") < 0) {
  275. perror("unveil");
  276. return 1;
  277. }
  278. if (unveil("/tmp/portal/launch", "rw") < 0) {
  279. perror("unveil");
  280. return 1;
  281. }
  282. if (unveil(config->file_name().characters(), "rwc")) {
  283. perror("unveil");
  284. return 1;
  285. }
  286. unveil(nullptr, nullptr);
  287. config->sync();
  288. return app.exec();
  289. }