From adc83e5802faa9f239972040d7c340215e13340a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Nov 2021 14:31:53 +0100 Subject: [PATCH] Taskbar: Port to LibMain :^) This opens up using TRY() for syscalls, Core::Object creation, and even some Vector operations. --- Userland/Services/Taskbar/CMakeLists.txt | 2 +- Userland/Services/Taskbar/main.cpp | 38 +++++++++++------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Userland/Services/Taskbar/CMakeLists.txt b/Userland/Services/Taskbar/CMakeLists.txt index 5f3770d0f5..b8c6d2293b 100644 --- a/Userland/Services/Taskbar/CMakeLists.txt +++ b/Userland/Services/Taskbar/CMakeLists.txt @@ -14,5 +14,5 @@ set(SOURCES ) serenity_bin(Taskbar) -target_link_libraries(Taskbar LibGUI LibDesktop LibConfig) +target_link_libraries(Taskbar LibGUI LibDesktop LibConfig LibMain) serenity_install_headers(Services/Taskbar) diff --git a/Userland/Services/Taskbar/main.cpp b/Userland/Services/Taskbar/main.cpp index 5199d2586d..f093d6dec0 100644 --- a/Userland/Services/Taskbar/main.cpp +++ b/Userland/Services/Taskbar/main.cpp @@ -15,12 +15,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -29,16 +31,13 @@ #include #include -static Vector discover_apps_and_categories(); -static NonnullRefPtr build_system_menu(); +static ErrorOr> discover_apps_and_categories(); +static ErrorOr> build_system_menu(); -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio recvfd sendfd proc exec rpath unix sigaction", nullptr) < 0) { - perror("pledge"); - return 1; - } - auto app = GUI::Application::construct(argc, argv); + TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath unix sigaction", nullptr)); + auto app = TRY(GUI::Application::try_create(arguments)); Config::pledge_domains("Taskbar"); Config::monitor_domain("Taskbar"); app->event_loop().register_signal(SIGCHLD, [](int) { @@ -50,15 +49,12 @@ int main(int argc, char** argv) // We need to obtain the WM connection here as well before the pledge shortening. GUI::WindowManagerServerConnection::the(); - if (pledge("stdio recvfd sendfd proc exec rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio recvfd sendfd proc exec rpath", nullptr)); - auto menu = build_system_menu(); + auto menu = TRY(build_system_menu()); menu->realize_menu_if_needed(); - auto window = TaskbarWindow::construct(move(menu)); + auto window = TRY(TaskbarWindow::try_create(move(menu))); window->show(); window->make_window_manager( @@ -90,7 +86,7 @@ Vector g_themes; RefPtr g_themes_menu; GUI::ActionGroup g_themes_group; -Vector discover_apps_and_categories() +ErrorOr> discover_apps_and_categories() { HashTable seen_app_categories; Desktop::AppFile::for_each([&](auto af) { @@ -102,18 +98,18 @@ Vector discover_apps_and_categories() quick_sort(g_apps, [](auto& a, auto& b) { return a.name < b.name; }); Vector sorted_app_categories; - for (auto& category : seen_app_categories) { - sorted_app_categories.append(category); - } + TRY(sorted_app_categories.try_ensure_capacity(seen_app_categories.size())); + for (auto const& category : seen_app_categories) + sorted_app_categories.unchecked_append(category); quick_sort(sorted_app_categories); return sorted_app_categories; } -NonnullRefPtr build_system_menu() +ErrorOr> build_system_menu() { - const Vector sorted_app_categories = discover_apps_and_categories(); - auto system_menu = GUI::Menu::construct("\xE2\x9A\xA1"); // HIGH VOLTAGE SIGN + Vector const sorted_app_categories = TRY(discover_apps_and_categories()); + auto system_menu = TRY(GUI::Menu::try_create("\xE2\x9A\xA1")); // HIGH VOLTAGE SIGN system_menu->add_action(GUI::Action::create("&About SerenityOS", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/ladyball.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { Core::Process::spawn("/bin/About"sv);