main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ShutdownDialog.h"
  8. #include "TaskbarWindow.h"
  9. #include <AK/Debug.h>
  10. #include <AK/QuickSort.h>
  11. #include <AK/Try.h>
  12. #include <LibConfig/Client.h>
  13. #include <LibCore/ConfigFile.h>
  14. #include <LibCore/EventLoop.h>
  15. #include <LibCore/StandardPaths.h>
  16. #include <LibCore/System.h>
  17. #include <LibDesktop/AppFile.h>
  18. #include <LibDesktop/Launcher.h>
  19. #include <LibGUI/ActionGroup.h>
  20. #include <LibGUI/Application.h>
  21. #include <LibGUI/ConnectionToWindowManagerServer.h>
  22. #include <LibGUI/ConnectionToWindowServer.h>
  23. #include <LibGUI/Menu.h>
  24. #include <LibGUI/MenuItem.h>
  25. #include <LibGUI/Process.h>
  26. #include <LibGUI/Window.h>
  27. #include <LibGfx/Palette.h>
  28. #include <LibGfx/SystemTheme.h>
  29. #include <LibMain/Main.h>
  30. #include <WindowServer/Window.h>
  31. #include <serenity.h>
  32. #include <signal.h>
  33. #include <spawn.h>
  34. #include <stdio.h>
  35. #include <sys/wait.h>
  36. #include <unistd.h>
  37. static ErrorOr<Vector<DeprecatedString>> discover_apps_and_categories();
  38. static ErrorOr<NonnullRefPtr<GUI::Menu>> build_system_menu(GUI::Window&);
  39. ErrorOr<int> serenity_main(Main::Arguments arguments)
  40. {
  41. TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath unix sigaction"));
  42. auto app = TRY(GUI::Application::try_create(arguments));
  43. Config::pledge_domains({ "Taskbar", "Calendar" });
  44. Config::monitor_domain("Taskbar");
  45. Config::monitor_domain("Calendar");
  46. app->event_loop().register_signal(SIGCHLD, [](int) {
  47. // Wait all available children
  48. while (waitpid(-1, nullptr, WNOHANG) > 0)
  49. ;
  50. });
  51. TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath unix"));
  52. GUI::ConnectionToWindowManagerServer::the();
  53. Desktop::Launcher::ensure_connection();
  54. TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath"));
  55. auto window = TRY(TaskbarWindow::create());
  56. auto menu = TRY(build_system_menu(*window));
  57. menu->realize_menu_if_needed();
  58. window->add_system_menu(menu);
  59. window->show();
  60. window->make_window_manager(
  61. WindowServer::WMEventMask::WindowStateChanges
  62. | WindowServer::WMEventMask::WindowRemovals
  63. | WindowServer::WMEventMask::WindowIconChanges
  64. | WindowServer::WMEventMask::WorkspaceChanges);
  65. return app->exec();
  66. }
  67. struct AppMetadata {
  68. DeprecatedString executable;
  69. DeprecatedString name;
  70. DeprecatedString category;
  71. DeprecatedString working_directory;
  72. GUI::Icon icon;
  73. bool run_in_terminal;
  74. bool requires_root;
  75. };
  76. Vector<AppMetadata> g_apps;
  77. Color g_menu_selection_color;
  78. Vector<Gfx::SystemThemeMetaData> g_themes;
  79. RefPtr<GUI::Menu> g_themes_menu;
  80. GUI::ActionGroup g_themes_group;
  81. ErrorOr<Vector<DeprecatedString>> discover_apps_and_categories()
  82. {
  83. HashTable<DeprecatedString> seen_app_categories;
  84. Desktop::AppFile::for_each([&](auto af) {
  85. if (af->exclude_from_system_menu())
  86. return;
  87. if (access(af->executable().characters(), X_OK) == 0) {
  88. g_apps.append({ af->executable(), af->name(), af->category(), af->working_directory(), af->icon(), af->run_in_terminal(), af->requires_root() });
  89. seen_app_categories.set(af->category());
  90. }
  91. });
  92. quick_sort(g_apps, [](auto& a, auto& b) { return a.name < b.name; });
  93. Vector<DeprecatedString> sorted_app_categories;
  94. TRY(sorted_app_categories.try_ensure_capacity(seen_app_categories.size()));
  95. for (auto const& category : seen_app_categories)
  96. sorted_app_categories.unchecked_append(category);
  97. quick_sort(sorted_app_categories);
  98. return sorted_app_categories;
  99. }
  100. ErrorOr<NonnullRefPtr<GUI::Menu>> build_system_menu(GUI::Window& window)
  101. {
  102. Vector<DeprecatedString> const sorted_app_categories = TRY(discover_apps_and_categories());
  103. auto system_menu = TRY(GUI::Menu::try_create("\xE2\x9A\xA1")); // HIGH VOLTAGE SIGN
  104. system_menu->add_action(GUI::Action::create("&About SerenityOS", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/ladyball.png"sv)), [&](auto&) {
  105. GUI::Process::spawn_or_show_error(&window, "/bin/About"sv);
  106. }));
  107. system_menu->add_separator();
  108. // First we construct all the necessary app category submenus.
  109. auto category_icons = TRY(Core::ConfigFile::open("/res/icons/SystemMenu.ini"));
  110. HashMap<DeprecatedString, NonnullRefPtr<GUI::Menu>> app_category_menus;
  111. Function<void(DeprecatedString const&)> create_category_menu;
  112. create_category_menu = [&](DeprecatedString const& category) {
  113. if (app_category_menus.contains(category))
  114. return;
  115. DeprecatedString parent_category, child_category = category;
  116. for (ssize_t i = category.length() - 1; i >= 0; i--) {
  117. if (category[i] == '/') {
  118. parent_category = category.substring(0, i);
  119. child_category = category.substring(i + 1);
  120. }
  121. }
  122. GUI::Menu* parent_menu;
  123. if (parent_category.is_empty()) {
  124. parent_menu = system_menu;
  125. } else {
  126. parent_menu = app_category_menus.get(parent_category).value();
  127. if (!parent_menu) {
  128. create_category_menu(parent_category);
  129. parent_menu = app_category_menus.get(parent_category).value();
  130. VERIFY(parent_menu);
  131. }
  132. }
  133. auto& category_menu = parent_menu->add_submenu(child_category);
  134. auto category_icon_path = category_icons->read_entry("16x16", category);
  135. if (!category_icon_path.is_empty()) {
  136. auto icon_or_error = Gfx::Bitmap::load_from_file(category_icon_path);
  137. if (!icon_or_error.is_error())
  138. category_menu.set_icon(icon_or_error.release_value());
  139. }
  140. app_category_menus.set(category, category_menu);
  141. };
  142. for (auto const& category : sorted_app_categories)
  143. create_category_menu(category);
  144. // Then we create and insert all the app menu items into the right place.
  145. int app_identifier = 0;
  146. for (auto const& app : g_apps) {
  147. auto icon = app.icon.bitmap_for_size(16);
  148. if constexpr (SYSTEM_MENU_DEBUG) {
  149. if (icon)
  150. dbgln("App {} has icon with size {}", app.name, icon->size());
  151. }
  152. auto parent_menu = app_category_menus.get(app.category).value_or(system_menu.ptr());
  153. parent_menu->add_action(GUI::Action::create(app.name, icon, [app_identifier](auto&) {
  154. dbgln("Activated app with ID {}", app_identifier);
  155. auto& app = g_apps[app_identifier];
  156. char const* argv[4] { nullptr, nullptr, nullptr, nullptr };
  157. auto pls_with_executable = DeprecatedString::formatted("/bin/pls {}", app.executable);
  158. if (app.run_in_terminal && !app.requires_root) {
  159. argv[0] = "/bin/Terminal";
  160. argv[1] = "-e";
  161. argv[2] = app.executable.characters();
  162. } else if (!app.run_in_terminal && app.requires_root) {
  163. argv[0] = "/bin/Escalator";
  164. argv[1] = app.executable.characters();
  165. } else if (app.run_in_terminal && app.requires_root) {
  166. argv[0] = "/bin/Terminal";
  167. argv[1] = "-e";
  168. argv[2] = pls_with_executable.characters();
  169. } else {
  170. argv[0] = app.executable.characters();
  171. }
  172. posix_spawn_file_actions_t spawn_actions;
  173. posix_spawn_file_actions_init(&spawn_actions);
  174. auto home_directory = Core::StandardPaths::home_directory();
  175. if (app.working_directory.is_empty())
  176. posix_spawn_file_actions_addchdir(&spawn_actions, home_directory.characters());
  177. else
  178. posix_spawn_file_actions_addchdir(&spawn_actions, app.working_directory.characters());
  179. pid_t child_pid;
  180. if ((errno = posix_spawn(&child_pid, argv[0], &spawn_actions, nullptr, const_cast<char**>(argv), environ))) {
  181. perror("posix_spawn");
  182. } else {
  183. if (disown(child_pid) < 0)
  184. perror("disown");
  185. }
  186. posix_spawn_file_actions_destroy(&spawn_actions);
  187. }));
  188. ++app_identifier;
  189. }
  190. system_menu->add_separator();
  191. g_themes_group.set_exclusive(true);
  192. g_themes_group.set_unchecking_allowed(false);
  193. g_themes_menu = &system_menu->add_submenu("&Themes");
  194. g_themes_menu->set_icon(TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/themes.png"sv)));
  195. g_themes = TRY(Gfx::list_installed_system_themes());
  196. auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme();
  197. {
  198. int theme_identifier = 0;
  199. for (auto& theme : g_themes) {
  200. auto action = GUI::Action::create_checkable(theme.name, [theme_identifier, &window](auto&) {
  201. auto& theme = g_themes[theme_identifier];
  202. dbgln("Theme switched to {} at path {}", theme.name, theme.path);
  203. if (window.main_widget()->palette().color_scheme_path() != ""sv)
  204. VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(theme.path, theme.name, false, GUI::ConnectionToWindowServer::the().get_preferred_color_scheme()));
  205. else
  206. VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(theme.path, theme.name, false, "Custom"sv));
  207. });
  208. if (theme.name == current_theme_name)
  209. action->set_checked(true);
  210. g_themes_group.add_action(action);
  211. g_themes_menu->add_action(action);
  212. ++theme_identifier;
  213. }
  214. }
  215. GUI::Application::the()->on_theme_change = [&]() {
  216. if (g_themes_menu->is_visible())
  217. return;
  218. auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme();
  219. auto theme_overridden = GUI::ConnectionToWindowServer::the().is_system_theme_overridden();
  220. for (size_t index = 0; index < g_themes.size(); ++index) {
  221. auto* action = g_themes_menu->action_at(index);
  222. action->set_checked(!theme_overridden && action->text() == current_theme_name);
  223. }
  224. };
  225. system_menu->add_action(GUI::Action::create("&Settings", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-settings.png"sv)), [&](auto&) {
  226. GUI::Process::spawn_or_show_error(&window, "/bin/Settings"sv);
  227. }));
  228. system_menu->add_separator();
  229. system_menu->add_action(GUI::Action::create("&Help", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-help.png"sv)), [&](auto&) {
  230. GUI::Process::spawn_or_show_error(&window, "/bin/Help"sv);
  231. }));
  232. system_menu->add_action(GUI::Action::create("&Run...", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-run.png"sv)), [](auto&) {
  233. posix_spawn_file_actions_t spawn_actions;
  234. posix_spawn_file_actions_init(&spawn_actions);
  235. auto home_directory = Core::StandardPaths::home_directory();
  236. posix_spawn_file_actions_addchdir(&spawn_actions, home_directory.characters());
  237. pid_t child_pid;
  238. const char* argv[] = { "/bin/Run", nullptr };
  239. if ((errno = posix_spawn(&child_pid, "/bin/Run", &spawn_actions, nullptr, const_cast<char**>(argv), environ))) {
  240. perror("posix_spawn");
  241. } else {
  242. if (disown(child_pid) < 0)
  243. perror("disown");
  244. }
  245. posix_spawn_file_actions_destroy(&spawn_actions);
  246. }));
  247. system_menu->add_separator();
  248. system_menu->add_action(GUI::Action::create("E&xit...", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"sv)), [](auto&) {
  249. auto command = ShutdownDialog::show();
  250. if (command.size() == 0)
  251. return;
  252. pid_t child_pid;
  253. if ((errno = posix_spawn(&child_pid, command[0], nullptr, nullptr, const_cast<char**>(command.data()), environ))) {
  254. perror("posix_spawn");
  255. } else {
  256. if (disown(child_pid) < 0)
  257. perror("disown");
  258. }
  259. }));
  260. return system_menu;
  261. }