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