main.cpp 6.9 KB

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