main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ChessWidget.h"
  27. #include <LibCore/ConfigFile.h>
  28. #include <LibCore/DirIterator.h>
  29. #include <LibGUI/AboutDialog.h>
  30. #include <LibGUI/ActionGroup.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/Icon.h>
  33. #include <LibGUI/Menu.h>
  34. #include <LibGUI/MenuBar.h>
  35. #include <LibGUI/Window.h>
  36. int main(int argc, char** argv)
  37. {
  38. auto app = GUI::Application::construct(argc, argv);
  39. auto app_icon = GUI::Icon::default_icon("app-chess");
  40. auto window = GUI::Window::construct();
  41. auto& widget = window->set_main_widget<ChessWidget>();
  42. RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("Chess");
  43. if (pledge("stdio rpath accept wpath cpath shared_buffer proc exec", nullptr) < 0) {
  44. perror("pledge");
  45. return 1;
  46. }
  47. if (unveil("/res", "r") < 0) {
  48. perror("unveil");
  49. return 1;
  50. }
  51. if (unveil(config->file_name().characters(), "crw") < 0) {
  52. perror("unveil");
  53. return 1;
  54. }
  55. if (unveil("/bin/ChessEngine", "x") < 0) {
  56. perror("unveil");
  57. return 1;
  58. }
  59. if (unveil(nullptr, nullptr) < 0) {
  60. perror("unveil");
  61. return 1;
  62. }
  63. auto size = config->read_num_entry("Display", "size", 512);
  64. window->set_title("Chess");
  65. window->resize(size, size);
  66. window->set_size_increment({ 8, 8 });
  67. window->set_resize_aspect_ratio(1, 1);
  68. window->set_icon(app_icon.bitmap_for_size(16));
  69. widget.set_piece_set(config->read_entry("Style", "PieceSet", "stelar7"));
  70. widget.set_board_theme(config->read_entry("Style", "BoardTheme", "Beige"));
  71. widget.set_coordinates(config->read_bool_entry("Style", "Coordinates", true));
  72. auto menubar = GUI::MenuBar::construct();
  73. auto& app_menu = menubar->add_menu("Chess");
  74. app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) {
  75. widget.reset();
  76. }));
  77. app_menu.add_separator();
  78. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  79. GUI::Application::the()->quit();
  80. }));
  81. auto& style_menu = menubar->add_menu("Style");
  82. GUI::ActionGroup piece_set_action_group;
  83. piece_set_action_group.set_exclusive(true);
  84. auto& piece_set_menu = style_menu.add_submenu("Piece Set");
  85. piece_set_menu.set_icon(app_icon.bitmap_for_size(16));
  86. Core::DirIterator di("/res/icons/chess/sets/", Core::DirIterator::SkipParentAndBaseDir);
  87. while (di.has_next()) {
  88. auto set = di.next_path();
  89. auto action = GUI::Action::create_checkable(set, [&](auto& action) {
  90. widget.set_piece_set(action.text());
  91. widget.update();
  92. config->write_entry("Style", "PieceSet", action.text());
  93. config->sync();
  94. });
  95. piece_set_action_group.add_action(*action);
  96. if (widget.piece_set() == set)
  97. action->set_checked(true);
  98. piece_set_menu.add_action(*action);
  99. }
  100. GUI::ActionGroup board_theme_action_group;
  101. board_theme_action_group.set_exclusive(true);
  102. auto& board_theme_menu = style_menu.add_submenu("Board Theme");
  103. board_theme_menu.set_icon(Gfx::Bitmap::load_from_file("/res/icons/chess/mini-board.png"));
  104. for (auto& theme : Vector({ "Beige", "Green", "Blue" })) {
  105. auto action = GUI::Action::create_checkable(theme, [&](auto& action) {
  106. widget.set_board_theme(action.text());
  107. widget.update();
  108. config->write_entry("Style", "BoardTheme", action.text());
  109. config->sync();
  110. });
  111. board_theme_action_group.add_action(*action);
  112. if (widget.board_theme().name == theme)
  113. action->set_checked(true);
  114. board_theme_menu.add_action(*action);
  115. }
  116. auto coordinates_action = GUI::Action::create_checkable("Coordinates", [&](auto& action) {
  117. widget.set_coordinates(action.is_checked());
  118. widget.update();
  119. config->write_bool_entry("Style", "Coordinates", action.is_checked());
  120. config->sync();
  121. });
  122. coordinates_action->set_checked(widget.coordinates());
  123. style_menu.add_action(coordinates_action);
  124. auto& engine_menu = menubar->add_menu("Engine");
  125. GUI::ActionGroup engines_action_group;
  126. engines_action_group.set_exclusive(true);
  127. auto& engine_submenu = engine_menu.add_submenu("Engine");
  128. for (auto& engine : Vector({ "Human", "ChessEngine" })) {
  129. auto action = GUI::Action::create_checkable(engine, [&](auto& action) {
  130. if (action.text() == "Human") {
  131. widget.set_engine(nullptr);
  132. } else {
  133. widget.set_engine(Engine::construct(action.text()));
  134. widget.maybe_input_engine_move();
  135. }
  136. });
  137. engines_action_group.add_action(*action);
  138. if (engine == String("Human"))
  139. action->set_checked(true);
  140. engine_submenu.add_action(*action);
  141. }
  142. auto& help_menu = menubar->add_menu("Help");
  143. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  144. GUI::AboutDialog::show("Chess", app_icon.bitmap_for_size(32), window);
  145. }));
  146. app->set_menubar(move(menubar));
  147. window->show();
  148. widget.reset();
  149. return app->exec();
  150. }