main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "ChessWidget.h"
  9. #include <LibConfig/Client.h>
  10. #include <LibCore/System.h>
  11. #include <LibDesktop/Launcher.h>
  12. #include <LibFileSystemAccessClient/Client.h>
  13. #include <LibGUI/ActionGroup.h>
  14. #include <LibGUI/Application.h>
  15. #include <LibGUI/Clipboard.h>
  16. #include <LibGUI/Icon.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/Menubar.h>
  19. #include <LibGUI/MessageBox.h>
  20. #include <LibGUI/Process.h>
  21. #include <LibGUI/Window.h>
  22. #include <LibMain/Main.h>
  23. struct EngineDetails {
  24. StringView command;
  25. StringView name { command };
  26. String path {};
  27. };
  28. static Vector<EngineDetails> s_all_engines {
  29. { "ChessEngine"sv },
  30. { "stockfish"sv, "Stockfish"sv },
  31. };
  32. static ErrorOr<Vector<EngineDetails>> available_engines()
  33. {
  34. Vector<EngineDetails> available_engines;
  35. for (auto& engine : s_all_engines) {
  36. auto path_or_error = Core::System::resolve_executable_from_environment(engine.command);
  37. if (path_or_error.is_error())
  38. continue;
  39. engine.path = path_or_error.release_value();
  40. TRY(available_engines.try_append(engine));
  41. }
  42. return available_engines;
  43. }
  44. ErrorOr<int> serenity_main(Main::Arguments arguments)
  45. {
  46. TRY(Core::System::pledge("stdio rpath recvfd sendfd thread proc exec unix"));
  47. auto app = TRY(GUI::Application::create(arguments));
  48. Config::pledge_domain("Games");
  49. Config::monitor_domain("Games");
  50. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man6/Chess.md") }));
  51. TRY(Desktop::Launcher::seal_allowlist());
  52. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-chess"sv));
  53. auto window = GUI::Window::construct();
  54. auto widget = TRY(window->set_main_widget<ChessWidget>());
  55. auto engines = TRY(available_engines());
  56. for (auto const& engine : engines)
  57. TRY(Core::System::unveil(engine.path, "x"sv));
  58. TRY(Core::System::unveil("/etc/passwd", "r"));
  59. TRY(Core::System::unveil("/res", "r"));
  60. TRY(Core::System::unveil("/bin/GamesSettings", "x"));
  61. TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
  62. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  63. TRY(Core::System::unveil(nullptr, nullptr));
  64. window->set_title("Chess");
  65. window->set_base_size({ 4, 4 });
  66. window->set_size_increment({ 8, 8 });
  67. window->resize(508, 508);
  68. window->set_icon(app_icon.bitmap_for_size(16));
  69. widget->set_piece_set(Config::read_string("Games"sv, "Chess"sv, "PieceSet"sv, "Classic"sv));
  70. widget->set_board_theme(Config::read_string("Games"sv, "Chess"sv, "BoardTheme"sv, "Beige"sv));
  71. widget->set_coordinates(Config::read_bool("Games"sv, "Chess"sv, "ShowCoordinates"sv, true));
  72. widget->set_show_available_moves(Config::read_bool("Games"sv, "Chess"sv, "ShowAvailableMoves"sv, true));
  73. widget->set_highlight_checks(Config::read_bool("Games"sv, "Chess"sv, "HighlightChecks"sv, true));
  74. auto game_menu = window->add_menu("&Game"_string);
  75. game_menu->add_action(GUI::Action::create("&Resign", { Mod_None, Key_F3 }, [&](auto&) {
  76. widget->resign();
  77. }));
  78. game_menu->add_action(GUI::Action::create("&Flip Board", { Mod_Ctrl, Key_F }, [&](auto&) {
  79. widget->flip_board();
  80. }));
  81. game_menu->add_separator();
  82. game_menu->add_action(GUI::Action::create("&Import PGN...", { Mod_Ctrl, Key_O }, [&](auto&) {
  83. FileSystemAccessClient::OpenFileOptions options {
  84. .allowed_file_types = Vector {
  85. GUI::FileTypeFilter { "PGN Files", { { "pgn" } } },
  86. GUI::FileTypeFilter::all_files(),
  87. }
  88. };
  89. auto result = FileSystemAccessClient::Client::the().open_file(window, options);
  90. if (result.is_error())
  91. return;
  92. if (auto maybe_error = widget->import_pgn(*result.value().release_stream()); maybe_error.is_error())
  93. dbgln("Failed to import PGN: {}", maybe_error.release_error());
  94. else
  95. dbgln("Imported PGN file from {}", result.value().filename());
  96. }));
  97. game_menu->add_action(GUI::Action::create("&Export PGN...", { Mod_Ctrl, Key_S }, [&](auto&) {
  98. auto result = FileSystemAccessClient::Client::the().save_file(window, "Untitled", "pgn");
  99. if (result.is_error())
  100. return;
  101. if (auto maybe_error = widget->export_pgn(*result.value().release_stream()); maybe_error.is_error())
  102. dbgln("Failed to export PGN: {}", maybe_error.release_error());
  103. else
  104. dbgln("Exported PGN file to {}", result.value().filename());
  105. }));
  106. game_menu->add_action(GUI::Action::create("&Copy FEN", { Mod_Ctrl, Key_C }, [&](auto&) {
  107. GUI::Clipboard::the().set_data(widget->get_fen().release_value_but_fixme_should_propagate_errors().bytes());
  108. GUI::MessageBox::show(window, "Board state copied to clipboard as FEN."sv, "Copy FEN"sv, GUI::MessageBox::Type::Information);
  109. }));
  110. game_menu->add_separator();
  111. game_menu->add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"sv)), [&](auto&) {
  112. if (widget->board().game_result() == Chess::Board::Result::NotFinished) {
  113. if (widget->resign() < 0)
  114. return;
  115. }
  116. widget->reset();
  117. }));
  118. game_menu->add_separator();
  119. auto settings_action = GUI::Action::create(
  120. "Chess &Settings", {}, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/games.png"sv)), [window](auto&) {
  121. GUI::Process::spawn_or_show_error(window, "/bin/GamesSettings"sv, Array { "--open-tab", "chess" });
  122. },
  123. window);
  124. settings_action->set_status_tip("Open the Game Settings for Chess"_string);
  125. game_menu->add_action(settings_action);
  126. auto show_available_moves_action = GUI::Action::create_checkable("Show Available Moves", [&](auto& action) {
  127. widget->set_show_available_moves(action.is_checked());
  128. widget->update();
  129. Config::write_bool("Games"sv, "Chess"sv, "ShowAvailableMoves"sv, action.is_checked());
  130. });
  131. show_available_moves_action->set_checked(widget->show_available_moves());
  132. game_menu->add_action(show_available_moves_action);
  133. game_menu->add_separator();
  134. game_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  135. GUI::Application::the()->quit();
  136. }));
  137. auto engine_menu = window->add_menu("&Engine"_string);
  138. GUI::ActionGroup engines_action_group;
  139. engines_action_group.set_exclusive(true);
  140. auto engine_submenu = engine_menu->add_submenu("&Engine"_string);
  141. auto human_engine_checkbox = GUI::Action::create_checkable("Human", [&](auto&) {
  142. widget->set_engine(nullptr);
  143. });
  144. human_engine_checkbox->set_checked(true);
  145. engines_action_group.add_action(human_engine_checkbox);
  146. engine_submenu->add_action(human_engine_checkbox);
  147. for (auto const& engine : engines) {
  148. auto action = GUI::Action::create_checkable(engine.name, [&](auto&) {
  149. auto new_engine = Engine::construct(engine.path);
  150. new_engine->on_connection_lost = [&]() {
  151. if (!widget->want_engine_move())
  152. return;
  153. auto rc = GUI::MessageBox::show(window, "Connection to the chess engine was lost while waiting for a move. Do you want to try again?"sv, "Chess"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
  154. if (rc == GUI::Dialog::ExecResult::Yes)
  155. widget->input_engine_move();
  156. else
  157. human_engine_checkbox->activate();
  158. };
  159. widget->set_engine(move(new_engine));
  160. widget->input_engine_move();
  161. });
  162. engines_action_group.add_action(*action);
  163. engine_submenu->add_action(*action);
  164. }
  165. auto help_menu = window->add_menu("&Help"_string);
  166. help_menu->add_action(GUI::CommonActions::make_command_palette_action(window));
  167. help_menu->add_action(GUI::CommonActions::make_help_action([](auto&) {
  168. Desktop::Launcher::open(URL::create_with_file_scheme("/usr/share/man/man6/Chess.md"), "/bin/Help");
  169. }));
  170. help_menu->add_action(GUI::CommonActions::make_about_action("Chess"_string, app_icon, window));
  171. window->show();
  172. widget->reset();
  173. return app->exec();
  174. }