main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Editor.h"
  7. #include "HackStudio.h"
  8. #include "HackStudioWidget.h"
  9. #include "Project.h"
  10. #include <AK/StringBuilder.h>
  11. #include <LibConfig/Client.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/System.h>
  15. #include <LibGUI/Application.h>
  16. #include <LibGUI/Menubar.h>
  17. #include <LibGUI/MessageBox.h>
  18. #include <LibGUI/Notification.h>
  19. #include <LibGUI/Window.h>
  20. #include <LibMain/Main.h>
  21. #include <fcntl.h>
  22. #include <spawn.h>
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <unistd.h>
  27. using namespace HackStudio;
  28. static WeakPtr<HackStudioWidget> s_hack_studio_widget;
  29. static bool make_is_available();
  30. static ErrorOr<void> notify_make_not_available();
  31. static void update_path_environment_variable();
  32. static Optional<DeprecatedString> last_opened_project_path();
  33. ErrorOr<int> serenity_main(Main::Arguments arguments)
  34. {
  35. TRY(Core::System::pledge("stdio recvfd sendfd tty rpath cpath wpath proc exec unix fattr thread ptrace"));
  36. auto app = TRY(GUI::Application::try_create(arguments));
  37. Config::pledge_domains({ "HackStudio", "Terminal" });
  38. auto window = GUI::Window::construct();
  39. window->resize(840, 600);
  40. auto icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png"sv));
  41. window->set_icon(icon);
  42. update_path_environment_variable();
  43. if (!make_is_available()) {
  44. TRY(notify_make_not_available());
  45. }
  46. char const* path_argument = nullptr;
  47. bool mode_coredump = false;
  48. Core::ArgsParser args_parser;
  49. args_parser.add_positional_argument(path_argument, "Path to a workspace or a file", "path", Core::ArgsParser::Required::No);
  50. args_parser.add_option(mode_coredump, "Debug a coredump in HackStudio", "coredump", 'c');
  51. args_parser.parse(arguments);
  52. auto argument_absolute_path = Core::File::real_path_for(path_argument);
  53. auto project_path = Core::File::real_path_for(".");
  54. if (!mode_coredump) {
  55. if (!argument_absolute_path.is_null())
  56. project_path = argument_absolute_path;
  57. else if (auto path = last_opened_project_path(); path.has_value())
  58. project_path = path.release_value();
  59. }
  60. auto hack_studio_widget = TRY(HackStudioWidget::create(project_path));
  61. window->set_main_widget(hack_studio_widget);
  62. s_hack_studio_widget = hack_studio_widget;
  63. window->set_title(DeprecatedString::formatted("{} - Hack Studio", hack_studio_widget->project().name()));
  64. TRY(hack_studio_widget->initialize_menubar(*window));
  65. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  66. hack_studio_widget->locator().close();
  67. if (hack_studio_widget->warn_unsaved_changes("There are unsaved changes, do you want to save before exiting?") == HackStudioWidget::ContinueDecision::Yes)
  68. return GUI::Window::CloseRequestDecision::Close;
  69. return GUI::Window::CloseRequestDecision::StayOpen;
  70. };
  71. window->show();
  72. hack_studio_widget->update_actions();
  73. if (mode_coredump)
  74. hack_studio_widget->open_coredump(argument_absolute_path);
  75. return app->exec();
  76. }
  77. static bool make_is_available()
  78. {
  79. pid_t pid;
  80. char const* argv[] = { "make", "--version", nullptr };
  81. posix_spawn_file_actions_t action;
  82. posix_spawn_file_actions_init(&action);
  83. posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
  84. if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
  85. perror("posix_spawn");
  86. return false;
  87. }
  88. int wstatus;
  89. waitpid(pid, &wstatus, 0);
  90. posix_spawn_file_actions_destroy(&action);
  91. return WEXITSTATUS(wstatus) == 0;
  92. }
  93. static ErrorOr<void> notify_make_not_available()
  94. {
  95. auto notification = GUI::Notification::construct();
  96. auto icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/app-hack-studio.png"sv));
  97. notification->set_icon(icon);
  98. notification->set_title("'make' Not Available");
  99. notification->set_text("You probably want to install the binutils, gcc, and make ports from the root of the Serenity repository");
  100. notification->show();
  101. return {};
  102. }
  103. static void update_path_environment_variable()
  104. {
  105. StringBuilder path;
  106. auto const* path_env_ptr = getenv("PATH");
  107. if (path_env_ptr != NULL)
  108. path.append({ path_env_ptr, strlen(path_env_ptr) });
  109. if (path.length())
  110. path.append(':');
  111. path.append(DEFAULT_PATH_SV);
  112. setenv("PATH", path.to_deprecated_string().characters(), true);
  113. }
  114. static Optional<DeprecatedString> last_opened_project_path()
  115. {
  116. auto projects = HackStudioWidget::read_recent_projects();
  117. if (projects.size() == 0)
  118. return {};
  119. if (!Core::File::exists(projects[0]))
  120. return {};
  121. return { projects[0] };
  122. }
  123. namespace HackStudio {
  124. GUI::TextEditor& current_editor()
  125. {
  126. return s_hack_studio_widget->current_editor();
  127. }
  128. void open_file(DeprecatedString const& filename)
  129. {
  130. s_hack_studio_widget->open_file(filename);
  131. }
  132. void open_file(DeprecatedString const& filename, size_t line, size_t column)
  133. {
  134. s_hack_studio_widget->open_file(filename, line, column);
  135. }
  136. RefPtr<EditorWrapper> current_editor_wrapper()
  137. {
  138. if (!s_hack_studio_widget)
  139. return nullptr;
  140. return s_hack_studio_widget->current_editor_wrapper();
  141. }
  142. Project& project()
  143. {
  144. return s_hack_studio_widget->project();
  145. }
  146. DeprecatedString currently_open_file()
  147. {
  148. if (!s_hack_studio_widget)
  149. return {};
  150. return s_hack_studio_widget->active_file();
  151. }
  152. void set_current_editor_wrapper(RefPtr<EditorWrapper> wrapper)
  153. {
  154. s_hack_studio_widget->set_current_editor_wrapper(wrapper);
  155. }
  156. void update_editor_window_title()
  157. {
  158. s_hack_studio_widget->update_current_editor_title();
  159. s_hack_studio_widget->update_window_title();
  160. }
  161. Locator& locator()
  162. {
  163. return s_hack_studio_widget->locator();
  164. }
  165. void for_each_open_file(Function<void(ProjectFile const&)> func)
  166. {
  167. s_hack_studio_widget->for_each_open_file(move(func));
  168. }
  169. bool semantic_syntax_highlighting_is_enabled()
  170. {
  171. return s_hack_studio_widget->semantic_syntax_highlighting_is_enabled();
  172. }
  173. }