main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/CArgsParser.h>
  28. #include <LibCore/CUserInfo.h>
  29. #include <LibDraw/PNGLoader.h>
  30. #include <LibGUI/GAboutDialog.h>
  31. #include <LibGUI/GAction.h>
  32. #include <LibGUI/GActionGroup.h>
  33. #include <LibGUI/GApplication.h>
  34. #include <LibGUI/GBoxLayout.h>
  35. #include <LibGUI/GFontDatabase.h>
  36. #include <LibGUI/GGroupBox.h>
  37. #include <LibGUI/GMenuBar.h>
  38. #include <LibGUI/GRadioButton.h>
  39. #include <LibGUI/GSlider.h>
  40. #include <LibGUI/GWidget.h>
  41. #include <LibGUI/GWindow.h>
  42. #include <LibVT/TerminalWidget.h>
  43. #include <assert.h>
  44. #include <errno.h>
  45. #include <fcntl.h>
  46. #include <pwd.h>
  47. #include <signal.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <sys/ioctl.h>
  52. #include <sys/select.h>
  53. #include <unistd.h>
  54. static void run_command(int ptm_fd, String command)
  55. {
  56. pid_t pid = fork();
  57. if (pid == 0) {
  58. const char* tty_name = ptsname(ptm_fd);
  59. if (!tty_name) {
  60. perror("ptsname");
  61. exit(1);
  62. }
  63. close(ptm_fd);
  64. int pts_fd = open(tty_name, O_RDWR);
  65. if (pts_fd < 0) {
  66. perror("open");
  67. exit(1);
  68. }
  69. if (setsid() < 0) {
  70. perror("setsid");
  71. }
  72. close(0);
  73. close(1);
  74. close(2);
  75. int rc = dup2(pts_fd, 0);
  76. if (rc < 0) {
  77. perror("dup2");
  78. exit(1);
  79. }
  80. rc = dup2(pts_fd, 1);
  81. if (rc < 0) {
  82. perror("dup2");
  83. exit(1);
  84. }
  85. rc = dup2(pts_fd, 2);
  86. if (rc < 0) {
  87. perror("dup2");
  88. exit(1);
  89. }
  90. rc = close(pts_fd);
  91. if (rc < 0) {
  92. perror("close");
  93. exit(1);
  94. }
  95. rc = ioctl(0, TIOCSCTTY);
  96. if (rc < 0) {
  97. perror("ioctl(TIOCSCTTY)");
  98. exit(1);
  99. }
  100. String shell = "/bin/Shell";
  101. auto* pw = getpwuid(getuid());
  102. if (pw && pw->pw_shell) {
  103. shell = pw->pw_shell;
  104. }
  105. endpwent();
  106. const char* args[4] = { shell.characters(), nullptr, nullptr, nullptr };
  107. if (!command.is_empty()) {
  108. args[1] = "-c";
  109. args[2] = command.characters();
  110. }
  111. const char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
  112. rc = execve(shell.characters(), const_cast<char**>(args), const_cast<char**>(envs));
  113. if (rc < 0) {
  114. perror("execve");
  115. exit(1);
  116. }
  117. ASSERT_NOT_REACHED();
  118. }
  119. }
  120. RefPtr<GUI::Window> create_settings_window(TerminalWidget& terminal)
  121. {
  122. auto window = GUI::Window::construct();
  123. window->set_title("Terminal Settings");
  124. window->set_rect(50, 50, 200, 140);
  125. auto settings = GUI::Widget::construct();
  126. window->set_main_widget(settings);
  127. settings->set_fill_with_background_color(true);
  128. settings->set_background_role(ColorRole::Button);
  129. settings->set_layout(make<GUI::VBoxLayout>());
  130. settings->layout()->set_margins({ 4, 4, 4, 4 });
  131. auto radio_container = GUI::GroupBox::construct("Bell Mode", settings);
  132. radio_container->set_layout(make<GUI::VBoxLayout>());
  133. radio_container->layout()->set_margins({ 6, 16, 6, 6 });
  134. radio_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  135. radio_container->set_preferred_size(100, 70);
  136. auto sysbell_radio = GUI::RadioButton::construct("Use (Audible) System Bell", radio_container);
  137. auto visbell_radio = GUI::RadioButton::construct("Use (Visual) Terminal Bell", radio_container);
  138. sysbell_radio->set_checked(terminal.should_beep());
  139. visbell_radio->set_checked(!terminal.should_beep());
  140. sysbell_radio->on_checked = [&terminal](const bool checked) {
  141. terminal.set_should_beep(checked);
  142. };
  143. auto slider_container = GUI::GroupBox::construct("Background Opacity", settings);
  144. slider_container->set_layout(make<GUI::VBoxLayout>());
  145. slider_container->layout()->set_margins({ 6, 16, 6, 6 });
  146. slider_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  147. slider_container->set_preferred_size(100, 50);
  148. auto slider = GUI::Slider::construct(Orientation::Horizontal, slider_container);
  149. slider->on_value_changed = [&terminal](int value) {
  150. terminal.set_opacity(value);
  151. };
  152. slider->set_range(0, 255);
  153. slider->set_value(terminal.opacity());
  154. return window;
  155. }
  156. int main(int argc, char** argv)
  157. {
  158. if (pledge("stdio tty rpath accept cpath wpath shared_buffer proc exec unix fattr", nullptr) < 0) {
  159. perror("pledge");
  160. return 1;
  161. }
  162. struct sigaction act;
  163. memset(&act, 0, sizeof(act));
  164. act.sa_flags = SA_NOCLDWAIT;
  165. act.sa_handler = SIG_IGN;
  166. int rc = sigaction(SIGCHLD, &act, nullptr);
  167. if (rc < 0) {
  168. perror("sigaction");
  169. return 1;
  170. }
  171. GUI::Application app(argc, argv);
  172. if (pledge("stdio tty rpath accept cpath wpath shared_buffer proc exec", nullptr) < 0) {
  173. perror("pledge");
  174. return 1;
  175. }
  176. const char* command_to_execute = "/bin/Shell";
  177. Core::ArgsParser args_parser;
  178. args_parser.add_option(command_to_execute, "Execute this command inside the terminal", nullptr, 'e', "command");
  179. args_parser.parse(argc, argv);
  180. if (chdir(get_current_user_home_path().characters()) < 0)
  181. perror("chdir");
  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. run_command(ptm_fd, command_to_execute);
  196. auto window = GUI::Window::construct();
  197. window->set_title("Terminal");
  198. window->set_background_color(Color::Black);
  199. window->set_double_buffering_enabled(false);
  200. RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("Terminal");
  201. auto terminal = TerminalWidget::construct(ptm_fd, true, config);
  202. terminal->on_command_exit = [&] {
  203. app.quit(0);
  204. };
  205. terminal->on_title_change = [&](auto& title) {
  206. window->set_title(title);
  207. };
  208. window->set_main_widget(terminal);
  209. window->move_to(300, 300);
  210. terminal->apply_size_increments_to_window(*window);
  211. window->show();
  212. window->set_icon(Gfx::load_png("/res/icons/16x16/app-terminal.png"));
  213. terminal->set_should_beep(config->read_bool_entry("Window", "AudibleBeep", false));
  214. RefPtr<GUI::Window> settings_window;
  215. auto new_opacity = config->read_num_entry("Window", "Opacity", 255);
  216. terminal->set_opacity(new_opacity);
  217. window->set_has_alpha_channel(new_opacity < 255);
  218. auto menubar = make<GUI::MenuBar>();
  219. auto app_menu = GUI::Menu::construct("Terminal");
  220. 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&) {
  221. if (!fork()) {
  222. execl("/bin/Terminal", "Terminal", nullptr);
  223. exit(1);
  224. }
  225. }));
  226. app_menu->add_action(GUI::Action::create("Settings...", Gfx::load_png("/res/icons/gear16.png"),
  227. [&](const GUI::Action&) {
  228. if (!settings_window) {
  229. settings_window = create_settings_window(*terminal);
  230. settings_window->on_close_request = [&] {
  231. settings_window = nullptr;
  232. return GUI::Window::CloseRequestDecision::Close;
  233. };
  234. }
  235. settings_window->show();
  236. settings_window->move_to_front();
  237. }));
  238. app_menu->add_separator();
  239. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  240. dbgprintf("Terminal: Quit menu activated!\n");
  241. GUI::Application::the().quit(0);
  242. }));
  243. menubar->add_menu(move(app_menu));
  244. auto edit_menu = GUI::Menu::construct("Edit");
  245. edit_menu->add_action(terminal->copy_action());
  246. edit_menu->add_action(terminal->paste_action());
  247. menubar->add_menu(move(edit_menu));
  248. GUI::ActionGroup font_action_group;
  249. font_action_group.set_exclusive(true);
  250. auto font_menu = GUI::Menu::construct("Font");
  251. GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  252. auto action = GUI::Action::create(font_name, [&](GUI::Action& action) {
  253. action.set_checked(true);
  254. terminal->set_font(GFontDatabase::the().get_by_name(action.text()));
  255. auto metadata = GFontDatabase::the().get_metadata_by_name(action.text());
  256. ASSERT(metadata.has_value());
  257. config->write_entry("Text", "Font", metadata.value().path);
  258. config->sync();
  259. terminal->force_repaint();
  260. });
  261. font_action_group.add_action(*action);
  262. action->set_checkable(true);
  263. if (terminal->font().name() == font_name)
  264. action->set_checked(true);
  265. font_menu->add_action(*action);
  266. });
  267. menubar->add_menu(move(font_menu));
  268. auto help_menu = GUI::Menu::construct("Help");
  269. help_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  270. GUI::AboutDialog::show("Terminal", Gfx::load_png("/res/icons/32x32/app-terminal.png"), window);
  271. }));
  272. menubar->add_menu(move(help_menu));
  273. app.set_menubar(move(menubar));
  274. if (unveil("/res", "r") < 0) {
  275. perror("unveil");
  276. return 1;
  277. }
  278. if (unveil("/bin/Terminal", "x") < 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. }