2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-04-19 20:54:10 -03:00
|
|
|
#include "SnakeGame.h"
|
2022-01-09 16:32:44 -03:00
|
|
|
#include <AK/URL.h>
|
2021-08-26 21:19:41 -03:00
|
|
|
#include <LibConfig/Client.h>
|
2021-11-23 07:41:55 -03:00
|
|
|
#include <LibCore/System.h>
|
2022-01-09 16:32:44 -03:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Button.h>
|
2020-10-31 18:58:52 -03:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Menu.h>
|
2021-04-13 11:18:20 -03:00
|
|
|
#include <LibGUI/Menubar.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Window.h>
|
2021-11-23 07:41:55 -03:00
|
|
|
#include <LibMain/Main.h>
|
2020-01-12 08:29:40 -03:00
|
|
|
#include <stdio.h>
|
2019-04-19 20:54:10 -03:00
|
|
|
|
2021-11-23 07:41:55 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-04-19 20:54:10 -03:00
|
|
|
{
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio rpath recvfd sendfd unix"));
|
2020-01-12 08:29:40 -03:00
|
|
|
|
2021-11-23 21:39:17 -03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2020-01-12 08:29:40 -03:00
|
|
|
|
2022-02-11 13:33:09 -03:00
|
|
|
Config::pledge_domain("Snake");
|
2021-08-26 21:19:41 -03:00
|
|
|
|
2022-01-09 16:32:44 -03:00
|
|
|
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man6/Snake.md") }));
|
|
|
|
|
TRY(Desktop::Launcher::seal_allowlist());
|
|
|
|
|
|
2021-11-27 19:26:34 -03:00
|
|
|
TRY(Core::System::pledge("stdio rpath recvfd sendfd"));
|
2019-04-19 20:54:10 -03:00
|
|
|
|
2021-11-23 07:41:55 -03:00
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
2022-08-07 13:19:42 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/launch", "rw"));
|
2021-11-23 07:41:55 -03:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-11-01 17:16:45 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-snake"sv));
|
2020-10-31 18:58:52 -03:00
|
|
|
|
2021-11-23 21:39:17 -03:00
|
|
|
auto window = TRY(GUI::Window::try_create());
|
2019-07-23 13:20:00 -03:00
|
|
|
|
2019-04-20 14:51:10 -03:00
|
|
|
window->set_double_buffering_enabled(false);
|
2019-04-19 20:54:10 -03:00
|
|
|
window->set_title("Snake");
|
2021-05-04 12:02:36 -03:00
|
|
|
window->resize(324, 344);
|
2019-04-19 20:54:10 -03:00
|
|
|
|
2021-11-27 07:18:48 -03:00
|
|
|
auto game = TRY(window->try_set_main_widget<SnakeGame>());
|
2019-04-19 20:54:10 -03:00
|
|
|
|
2021-11-27 07:18:48 -03:00
|
|
|
auto game_menu = TRY(window->try_add_menu("&Game"));
|
2019-08-27 08:25:08 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
TRY(game_menu->try_add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/reload.png"sv)), [&](auto&) {
|
2021-11-27 07:18:48 -03:00
|
|
|
game->reset();
|
|
|
|
|
})));
|
|
|
|
|
TRY(game_menu->try_add_separator());
|
|
|
|
|
TRY(game_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
2020-07-04 11:52:01 -03:00
|
|
|
GUI::Application::the()->quit();
|
2021-11-27 07:18:48 -03:00
|
|
|
})));
|
2019-04-19 20:54:10 -03:00
|
|
|
|
2021-11-27 07:18:48 -03:00
|
|
|
auto help_menu = TRY(window->try_add_menu("&Help"));
|
2022-01-09 16:32:44 -03:00
|
|
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_help_action([](auto&) {
|
|
|
|
|
Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man6/Snake.md"), "/bin/Help");
|
|
|
|
|
})));
|
2021-11-27 07:18:48 -03:00
|
|
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Snake", app_icon, window)));
|
2019-04-19 20:54:10 -03:00
|
|
|
|
|
|
|
|
window->show();
|
|
|
|
|
|
2020-10-31 18:58:52 -03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-04-19 20:54:10 -03:00
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-04-19 20:54:10 -03:00
|
|
|
}
|