2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-13 18:00:57 +00:00
|
|
|
* Copyright (c) 2022, James Puleo <james@jame.xyz>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-07 20:24:45 +00:00
|
|
|
#include <AK/Random.h>
|
2020-01-27 17:25:36 +00:00
|
|
|
#include <AK/String.h>
|
2019-02-08 15:19:16 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/Desktop.h>
|
2022-01-30 19:29:39 +00:00
|
|
|
#include <LibMain/Main.h>
|
2019-02-08 15:19:16 +00:00
|
|
|
|
2022-01-30 19:29:39 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-05-17 13:06:06 +00:00
|
|
|
{
|
2020-01-27 17:25:36 +00:00
|
|
|
bool show_all = false;
|
|
|
|
bool show_current = false;
|
2021-11-07 20:24:45 +00:00
|
|
|
bool set_random = false;
|
2022-02-13 18:06:34 +00:00
|
|
|
String path;
|
2019-05-17 13:06:06 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(show_all, "Show all wallpapers", "show-all", 'a');
|
|
|
|
args_parser.add_option(show_current, "Show current wallpaper", "show-current", 'c');
|
2021-11-07 20:24:45 +00:00
|
|
|
args_parser.add_option(set_random, "Set random wallpaper", "set-random", 'r');
|
2022-02-13 18:06:34 +00:00
|
|
|
args_parser.add_positional_argument(path, "Wallpaper to set", "path", Core::ArgsParser::Required::No);
|
2022-01-30 19:29:39 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-05-17 13:06:06 +00:00
|
|
|
|
2022-04-16 16:17:39 +00:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2022-02-13 18:06:34 +00:00
|
|
|
|
|
|
|
if (show_all) {
|
|
|
|
Core::DirIterator wallpapers_directory_iterator("/res/wallpapers", Core::DirIterator::SkipDots);
|
|
|
|
if (wallpapers_directory_iterator.has_error())
|
|
|
|
return Error::from_string_literal("Unable to iterate /res/wallpapers directory");
|
2019-05-17 13:06:06 +00:00
|
|
|
|
2022-02-13 18:06:34 +00:00
|
|
|
while (wallpapers_directory_iterator.has_next()) {
|
|
|
|
auto name = wallpapers_directory_iterator.next_path();
|
|
|
|
outln("{}", name);
|
|
|
|
}
|
|
|
|
} else if (show_current) {
|
|
|
|
auto current_wallpaper_path = GUI::Desktop::the().wallpaper_path();
|
|
|
|
outln("{}", current_wallpaper_path);
|
|
|
|
} else if (set_random) {
|
|
|
|
Core::DirIterator wallpapers_directory_iterator("/res/wallpapers", Core::DirIterator::SkipDots);
|
|
|
|
if (wallpapers_directory_iterator.has_error())
|
|
|
|
return Error::from_string_literal("Unable to iterate /res/wallpapers directory");
|
2019-05-17 13:06:06 +00:00
|
|
|
|
2022-02-13 18:06:34 +00:00
|
|
|
Vector<String> wallpaper_paths;
|
|
|
|
|
|
|
|
auto current_wallpaper_path = GUI::Desktop::the().wallpaper_path();
|
|
|
|
while (wallpapers_directory_iterator.has_next()) {
|
|
|
|
auto next_full_path = wallpapers_directory_iterator.next_full_path();
|
|
|
|
if (next_full_path != current_wallpaper_path)
|
|
|
|
wallpaper_paths.append(move(next_full_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wallpaper_paths.is_empty())
|
|
|
|
return Error::from_string_literal("No wallpapers found");
|
|
|
|
|
|
|
|
auto& chosen_wallpaper_path = wallpaper_paths.at(get_random_uniform(wallpaper_paths.size()));
|
|
|
|
auto chosen_wallpaper_bitmap = TRY(Gfx::Bitmap::try_load_from_file(chosen_wallpaper_path));
|
|
|
|
if (!GUI::Desktop::the().set_wallpaper(chosen_wallpaper_bitmap, chosen_wallpaper_path))
|
|
|
|
return Error::from_string_literal("Failed to set wallpaper");
|
|
|
|
|
|
|
|
outln("Set wallpaper to {}", chosen_wallpaper_path);
|
|
|
|
} else {
|
|
|
|
if (path.is_null())
|
|
|
|
return Error::from_string_literal("Must provide a path to a wallpaper");
|
|
|
|
|
|
|
|
auto wallpaper_bitmap = TRY(Gfx::Bitmap::try_load_from_file(path));
|
|
|
|
if (!GUI::Desktop::the().set_wallpaper(wallpaper_bitmap, path))
|
|
|
|
return Error::from_string_literal("Failed to set wallpaper");
|
|
|
|
}
|
|
|
|
return 0;
|
2019-02-08 15:19:16 +00:00
|
|
|
}
|