Serenity Operating System
at master 82 lines 3.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, James Puleo <james@jame.xyz> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <AK/DeprecatedString.h> 9#include <AK/Random.h> 10#include <AK/StringBuilder.h> 11#include <LibCore/ArgsParser.h> 12#include <LibCore/DirIterator.h> 13#include <LibCore/System.h> 14#include <LibGUI/Application.h> 15#include <LibGUI/Desktop.h> 16#include <LibMain/Main.h> 17 18ErrorOr<int> serenity_main(Main::Arguments arguments) 19{ 20 TRY(Core::System::pledge("stdio rpath unix sendfd recvfd")); 21 22 bool show_all = false; 23 bool show_current = false; 24 bool set_random = false; 25 DeprecatedString path; 26 27 Core::ArgsParser args_parser; 28 args_parser.add_option(show_all, "Show all wallpapers", "show-all", 'a'); 29 args_parser.add_option(show_current, "Show current wallpaper", "show-current", 'c'); 30 args_parser.add_option(set_random, "Set random wallpaper", "set-random", 'r'); 31 args_parser.add_positional_argument(path, "Wallpaper to set", "path", Core::ArgsParser::Required::No); 32 args_parser.parse(arguments); 33 34 auto app = TRY(GUI::Application::try_create(arguments)); 35 36 TRY(Core::System::pledge("stdio rpath unix sendfd")); 37 38 if (show_all) { 39 Core::DirIterator wallpapers_directory_iterator("/res/wallpapers", Core::DirIterator::SkipDots); 40 if (wallpapers_directory_iterator.has_error()) 41 return Error::from_string_literal("Unable to iterate /res/wallpapers directory"); 42 43 while (wallpapers_directory_iterator.has_next()) { 44 auto name = wallpapers_directory_iterator.next_path(); 45 outln("{}", name); 46 } 47 } else if (show_current) { 48 auto current_wallpaper_path = GUI::Desktop::the().wallpaper_path(); 49 outln("{}", current_wallpaper_path); 50 } else if (set_random) { 51 Core::DirIterator wallpapers_directory_iterator("/res/wallpapers", Core::DirIterator::SkipDots); 52 if (wallpapers_directory_iterator.has_error()) 53 return Error::from_string_literal("Unable to iterate /res/wallpapers directory"); 54 55 Vector<DeprecatedString> wallpaper_paths; 56 57 auto current_wallpaper_path = GUI::Desktop::the().wallpaper_path(); 58 while (wallpapers_directory_iterator.has_next()) { 59 auto next_full_path = wallpapers_directory_iterator.next_full_path(); 60 if (next_full_path != current_wallpaper_path) 61 wallpaper_paths.append(move(next_full_path)); 62 } 63 64 if (wallpaper_paths.is_empty()) 65 return Error::from_string_literal("No wallpapers found"); 66 67 auto& chosen_wallpaper_path = wallpaper_paths.at(get_random_uniform(wallpaper_paths.size())); 68 auto chosen_wallpaper_bitmap = TRY(Gfx::Bitmap::load_from_file(chosen_wallpaper_path)); 69 if (!GUI::Desktop::the().set_wallpaper(chosen_wallpaper_bitmap, chosen_wallpaper_path)) 70 return Error::from_string_literal("Failed to set wallpaper"); 71 72 outln("Set wallpaper to {}", chosen_wallpaper_path); 73 } else { 74 if (path.is_null()) 75 return Error::from_string_literal("Must provide a path to a wallpaper"); 76 77 auto wallpaper_bitmap = TRY(Gfx::Bitmap::load_from_file(path)); 78 if (!GUI::Desktop::the().set_wallpaper(wallpaper_bitmap, path)) 79 return Error::from_string_literal("Failed to set wallpaper"); 80 } 81 return 0; 82}